Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Tests for linking to resource files | |
acb109b7 | 30 | * @group headless |
6a488035 TO |
31 | */ |
32 | class CRM_Core_ErrorTest extends CiviUnitTestCase { | |
6a488035 | 33 | |
00be9182 | 34 | public function setUp() { |
6a488035 TO |
35 | parent::setUp(); |
36 | $config = CRM_Core_Config::singleton(); | |
37 | $this->oldConfigAndLogDir = $config->configAndLogDir; | |
38 | $config->configAndLogDir = $this->createTempDir('test-log-'); | |
39 | } | |
40 | ||
00be9182 | 41 | public function tearDown() { |
6a488035 | 42 | $config = CRM_Core_Config::singleton(); |
6c6e6187 | 43 | $config->configAndLogDir = $this->oldConfigAndLogDir; |
6a488035 TO |
44 | parent::tearDown(); |
45 | } | |
46 | ||
47 | /** | |
48 | * Make sure that formatBacktrace() accepts values from debug_backtrace() | |
49 | */ | |
00be9182 | 50 | public function testFormatBacktrace_debug() { |
6a488035 TO |
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 | */ | |
00be9182 | 59 | public function testFormatBacktrace_exception() { |
6a488035 TO |
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 | */ | |
00be9182 | 70 | public function testMixLog() { |
6a488035 TO |
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'); | |
6c6e6187 | 82 | } |
6a488035 | 83 | |
4cbe18b8 EM |
84 | /** |
85 | * @param $pattern | |
86 | */ | |
00be9182 | 87 | public function assertLogRegexp($pattern) { |
6a488035 | 88 | $config = CRM_Core_Config::singleton(); |
92fcb95f TO |
89 | $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log'); |
90 | $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/'); | |
6a488035 TO |
91 | foreach ($logFiles as $logFile) { |
92 | $this->assertRegexp($pattern, file_get_contents($logFile)); | |
93 | } | |
94 | } | |
96025800 | 95 | |
edc8adfc | 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); | |
0592d44f | 107 | $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . CRM_Core_Error::generateLogFileHash($config) . '.log', $log->_filename); |
edc8adfc | 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 | ||
6a488035 | 114 | } |