3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Tests for linking to resource files
16 class CRM_Core_ErrorTest
extends CiviUnitTestCase
{
18 public function setUp(): void
{
20 $config = CRM_Core_Config
::singleton();
21 $this->oldConfigAndLogDir
= $config->configAndLogDir
;
22 $config->configAndLogDir
= $this->createTempDir('test-log-');
25 public function tearDown(): void
{
26 $config = CRM_Core_Config
::singleton();
27 $config->configAndLogDir
= $this->oldConfigAndLogDir
;
32 * Make sure that formatBacktrace() accepts values from debug_backtrace()
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);
41 * Make sure that formatBacktrace() accepts values from Exception::getTrace()
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);
49 public function testExceptionLogging() {
50 $e = new \
Exception("the exception");
51 Civi
::log()->notice('There was an exception!', [
55 $e = new Error('the error');
56 Civi
::log()->notice('There was an error!', [
62 * We have two coding conventions for writing to log. Make sure that they work together.
64 * This tests a theory about what caused CRM-10766.
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');
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));
93 * Check that a debugger is created and there is no error when passing in a prefix.
95 * Do some basic content checks.
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);