Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Tests for linking to resource files | |
acb109b7 | 14 | * @group headless |
6a488035 TO |
15 | */ |
16 | class CRM_Core_ErrorTest extends CiviUnitTestCase { | |
6a488035 | 17 | |
00be9182 | 18 | public function setUp() { |
6a488035 TO |
19 | parent::setUp(); |
20 | $config = CRM_Core_Config::singleton(); | |
21 | $this->oldConfigAndLogDir = $config->configAndLogDir; | |
22 | $config->configAndLogDir = $this->createTempDir('test-log-'); | |
23 | } | |
24 | ||
00be9182 | 25 | public function tearDown() { |
6a488035 | 26 | $config = CRM_Core_Config::singleton(); |
6c6e6187 | 27 | $config->configAndLogDir = $this->oldConfigAndLogDir; |
6a488035 TO |
28 | parent::tearDown(); |
29 | } | |
30 | ||
31 | /** | |
32 | * Make sure that formatBacktrace() accepts values from debug_backtrace() | |
33 | */ | |
00be9182 | 34 | public function testFormatBacktrace_debug() { |
6a488035 TO |
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 | */ | |
00be9182 | 43 | public function testFormatBacktrace_exception() { |
6a488035 TO |
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 | /** | |
50 | * We have two coding conventions for writing to log. Make sure that they work together. | |
51 | * | |
52 | * This tests a theory about what caused CRM-10766. | |
53 | */ | |
00be9182 | 54 | public function testMixLog() { |
6a488035 TO |
55 | CRM_Core_Error::debug_log_message("static-1"); |
56 | $logger = CRM_Core_Error::createDebugLogger(); | |
57 | CRM_Core_Error::debug_log_message("static-2"); | |
58 | $logger->info('obj-1'); | |
59 | CRM_Core_Error::debug_log_message("static-3"); | |
60 | $logger->info('obj-2'); | |
61 | CRM_Core_Error::debug_log_message("static-4"); | |
62 | $logger2 = CRM_Core_Error::createDebugLogger(); | |
63 | $logger2->info('obj-3'); | |
64 | CRM_Core_Error::debug_log_message("static-5"); | |
65 | $this->assertLogRegexp('/static-1.*static-2.*obj-1.*static-3.*obj-2.*static-4.*obj-3.*static-5/s'); | |
6c6e6187 | 66 | } |
6a488035 | 67 | |
4cbe18b8 EM |
68 | /** |
69 | * @param $pattern | |
70 | */ | |
00be9182 | 71 | public function assertLogRegexp($pattern) { |
6a488035 | 72 | $config = CRM_Core_Config::singleton(); |
92fcb95f TO |
73 | $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log'); |
74 | $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/'); | |
6a488035 TO |
75 | foreach ($logFiles as $logFile) { |
76 | $this->assertRegexp($pattern, file_get_contents($logFile)); | |
77 | } | |
78 | } | |
96025800 | 79 | |
edc8adfc | 80 | /** |
81 | * Check that a debugger is created and there is no error when passing in a prefix. | |
82 | * | |
83 | * Do some basic content checks. | |
84 | */ | |
85 | public function testDebugLoggerFormat() { | |
86 | $log = CRM_Core_Error::createDebugLogger('my-test'); | |
87 | $log->log('Mary had a little lamb'); | |
88 | $log->log('Little lamb'); | |
89 | $config = CRM_Core_Config::singleton(); | |
90 | $fileContents = file_get_contents($log->_filename); | |
0592d44f | 91 | $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . CRM_Core_Error::generateLogFileHash($config) . '.log', $log->_filename); |
edc8adfc | 92 | // 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 |
93 | // there are chars for the date at the start. | |
94 | $this->assertTrue(strpos($fileContents, '[info] Mary had a little lamb') > 10); | |
95 | $this->assertContains('[info] Little lamb', $fileContents); | |
96 | } | |
97 | ||
6a488035 | 98 | } |