Get contact fields from the schema
[civicrm-core.git] / tests / phpunit / CRM / Core / ErrorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Tests for linking to resource files
32 */
33 class CRM_Core_ErrorTest extends CiviUnitTestCase {
34 function get_info() {
35 return array(
36 'name' => 'Errors',
37 'description' => 'Tests for error handling',
38 'group' => 'Core',
39 );
40 }
41
42 function setUp() {
43 parent::setUp();
44 $config = CRM_Core_Config::singleton();
45 $this->oldConfigAndLogDir = $config->configAndLogDir;
46 $config->configAndLogDir = $this->createTempDir('test-log-');
47 }
48
49 function tearDown() {
50 $config = CRM_Core_Config::singleton();
51 $config->configAndLogDir= $this->oldConfigAndLogDir;
52 parent::tearDown();
53 }
54
55 /**
56 * Make sure that formatBacktrace() accepts values from debug_backtrace()
57 */
58 function testFormatBacktrace_debug() {
59 $bt = debug_backtrace();
60 $msg = CRM_Core_Error::formatBacktrace($bt);
61 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_debug/', $msg);
62 }
63
64 /**
65 * Make sure that formatBacktrace() accepts values from Exception::getTrace()
66 */
67 function testFormatBacktrace_exception() {
68 $e = new Exception('foo');
69 $msg = CRM_Core_Error::formatBacktrace($e->getTrace());
70 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_exception/', $msg);
71 }
72
73 /**
74 * We have two coding conventions for writing to log. Make sure that they work together.
75 *
76 * This tests a theory about what caused CRM-10766.
77 */
78 function testMixLog() {
79 CRM_Core_Error::debug_log_message("static-1");
80 $logger = CRM_Core_Error::createDebugLogger();
81 CRM_Core_Error::debug_log_message("static-2");
82 $logger->info('obj-1');
83 CRM_Core_Error::debug_log_message("static-3");
84 $logger->info('obj-2');
85 CRM_Core_Error::debug_log_message("static-4");
86 $logger2 = CRM_Core_Error::createDebugLogger();
87 $logger2->info('obj-3');
88 CRM_Core_Error::debug_log_message("static-5");
89 $this->assertLogRegexp('/static-1.*static-2.*obj-1.*static-3.*obj-2.*static-4.*obj-3.*static-5/s');
90 }
91
92 function assertLogRegexp($pattern) {
93 $config = CRM_Core_Config::singleton();
94 $logFiles = glob($config->configAndLogDir.'/CiviCRM*log*/');
95 $this->assertEquals(1, count($logFiles));
96 foreach ($logFiles as $logFile) {
97 $this->assertRegexp($pattern, file_get_contents($logFile));
98 }
99 }
100 }