Merge pull request #19943 from civicrm/5.36
[civicrm-core.git] / tests / phpunit / CRM / Core / ErrorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Tests for linking to resource files
14 * @group headless
15 */
16 class CRM_Core_ErrorTest extends CiviUnitTestCase {
17
18 public function setUp(): void {
19 parent::setUp();
20 $config = CRM_Core_Config::singleton();
21 $this->oldConfigAndLogDir = $config->configAndLogDir;
22 $config->configAndLogDir = $this->createTempDir('test-log-');
23 }
24
25 public function tearDown(): void {
26 $config = CRM_Core_Config::singleton();
27 $config->configAndLogDir = $this->oldConfigAndLogDir;
28 parent::tearDown();
29 }
30
31 /**
32 * Make sure that formatBacktrace() accepts values from debug_backtrace()
33 */
34 public function testFormatBacktrace_debug() {
35 $bt = debug_backtrace();
36 $msg = CRM_Core_Error::formatBacktrace($bt);
37 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_debug/', $msg);
38 }
39
40 /**
41 * Make sure that formatBacktrace() accepts values from Exception::getTrace()
42 */
43 public function testFormatBacktrace_exception() {
44 $e = new Exception('foo');
45 $msg = CRM_Core_Error::formatBacktrace($e->getTrace());
46 $this->assertRegexp('/CRM_Core_ErrorTest->testFormatBacktrace_exception/', $msg);
47 }
48
49 public function testExceptionLogging() {
50 $e = new \Exception("the exception");
51 Civi::log()->notice('There was an exception!', [
52 'exception' => $e,
53 ]);
54
55 $e = new Error('the error');
56 Civi::log()->notice('There was an error!', [
57 'exception' => $e,
58 ]);
59 }
60
61 /**
62 * We have two coding conventions for writing to log. Make sure that they work together.
63 *
64 * This tests a theory about what caused CRM-10766.
65 */
66 public function testMixLog() {
67 CRM_Core_Error::debug_log_message("static-1");
68 $logger = CRM_Core_Error::createDebugLogger();
69 CRM_Core_Error::debug_log_message("static-2");
70 $logger->info('obj-1');
71 CRM_Core_Error::debug_log_message("static-3");
72 $logger->info('obj-2');
73 CRM_Core_Error::debug_log_message("static-4");
74 $logger2 = CRM_Core_Error::createDebugLogger();
75 $logger2->info('obj-3');
76 CRM_Core_Error::debug_log_message("static-5");
77 $this->assertLogRegexp('/static-1.*static-2.*obj-1.*static-3.*obj-2.*static-4.*obj-3.*static-5/s');
78 }
79
80 /**
81 * @param $pattern
82 */
83 public function assertLogRegexp($pattern) {
84 $config = CRM_Core_Config::singleton();
85 $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log');
86 $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/');
87 foreach ($logFiles as $logFile) {
88 $this->assertRegexp($pattern, file_get_contents($logFile));
89 }
90 }
91
92 /**
93 * Check that a debugger is created and there is no error when passing in a prefix.
94 *
95 * Do some basic content checks.
96 */
97 public function testDebugLoggerFormat() {
98 $log = CRM_Core_Error::createDebugLogger('my-test');
99 $log->log('Mary had a little lamb');
100 $log->log('Little lamb');
101 $config = CRM_Core_Config::singleton();
102 $fileContents = file_get_contents($log->_filename);
103 $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . CRM_Core_Error::generateLogFileHash($config) . '.log', $log->_filename);
104 // 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
105 // there are chars for the date at the start.
106 $this->assertTrue(strpos($fileContents, '[info] Mary had a little lamb') > 10);
107 $this->assertContains('[info] Little lamb', $fileContents);
108 }
109
110 }