| 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() { |
| 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() { |
| 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 | /** |
| 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 | */ |
| 54 | public function testMixLog() { |
| 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'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param $pattern |
| 70 | */ |
| 71 | public function assertLogRegexp($pattern) { |
| 72 | $config = CRM_Core_Config::singleton(); |
| 73 | $logFiles = glob($config->configAndLogDir . '/CiviCRM*.log'); |
| 74 | $this->assertEquals(1, count($logFiles), 'Expect to find 1 file matching: ' . $config->configAndLogDir . '/CiviCRM*log*/'); |
| 75 | foreach ($logFiles as $logFile) { |
| 76 | $this->assertRegexp($pattern, file_get_contents($logFile)); |
| 77 | } |
| 78 | } |
| 79 | |
| 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); |
| 91 | $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . CRM_Core_Error::generateLogFileHash($config) . '.log', $log->_filename); |
| 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 | |
| 98 | } |