Merge pull request #13051 from mlutfy/customvalue-checkbox-display
[civicrm-core.git] / tests / phpunit / CRM / Core / ErrorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 /**
29 * Tests for linking to resource files
30 * @group headless
31 */
32 class CRM_Core_ErrorTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 $config = CRM_Core_Config::singleton();
37 $this->oldConfigAndLogDir = $config->configAndLogDir;
38 $config->configAndLogDir = $this->createTempDir('test-log-');
39 }
40
41 public function tearDown() {
42 $config = CRM_Core_Config::singleton();
43 $config->configAndLogDir = $this->oldConfigAndLogDir;
44 parent::tearDown();
45 }
46
47 /**
48 * Make sure that formatBacktrace() accepts values from debug_backtrace()
49 */
50 public function testFormatBacktrace_debug() {
51 $bt = debug_backtrace();
52 $msg = CRM_Core_Error::formatBacktrace($bt);
53 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_debug/', $msg);
54 }
55
56 /**
57 * Make sure that formatBacktrace() accepts values from Exception::getTrace()
58 */
59 public function testFormatBacktrace_exception() {
60 $e = new Exception('foo');
61 $msg = CRM_Core_Error::formatBacktrace($e->getTrace());
62 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_exception/', $msg);
63 }
64
65 /**
66 * We have two coding conventions for writing to log. Make sure that they work together.
67 *
68 * This tests a theory about what caused CRM-10766.
69 */
70 public function testMixLog() {
71 CRM_Core_Error::debug_log_message("static-1");
72 $logger = CRM_Core_Error::createDebugLogger();
73 CRM_Core_Error::debug_log_message("static-2");
74 $logger->info('obj-1');
75 CRM_Core_Error::debug_log_message("static-3");
76 $logger->info('obj-2');
77 CRM_Core_Error::debug_log_message("static-4");
78 $logger2 = CRM_Core_Error::createDebugLogger();
79 $logger2->info('obj-3');
80 CRM_Core_Error::debug_log_message("static-5");
81 $this->assertLogRegexp('/static-1.*static-2.*obj-1.*static-3.*obj-2.*static-4.*obj-3.*static-5/s');
82 }
83
84 /**
85 * @param $pattern
86 */
87 public function assertLogRegexp($pattern) {
88 $config = CRM_Core_Config::singleton();
89 $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log');
90 $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/');
91 foreach ($logFiles as $logFile) {
92 $this->assertRegexp($pattern, file_get_contents($logFile));
93 }
94 }
95
96 /**
97 * Check that a debugger is created and there is no error when passing in a prefix.
98 *
99 * Do some basic content checks.
100 */
101 public function testDebugLoggerFormat() {
102 $log = CRM_Core_Error::createDebugLogger('my-test');
103 $log->log('Mary had a little lamb');
104 $log->log('Little lamb');
105 $config = CRM_Core_Config::singleton();
106 $fileContents = file_get_contents($log->_filename);
107 $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . CRM_Core_Error::generateLogFileHash($config) . '.log', $log->_filename);
108 // The 5 here is a bit arbitrary - on my local the date part is 15 chars (Mar 29 05:29:16) - but we are just checking that
109 // there are chars for the date at the start.
110 $this->assertTrue(strpos($fileContents, '[info] Mary had a little lamb') > 10);
111 $this->assertContains('[info] Little lamb', $fileContents);
112 }
113
114 }