From: eileen Date: Wed, 9 Mar 2016 03:37:34 +0000 (+1300) Subject: Add tests for logging functionality to ensure the tables are still creating well X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1f43f9e243aa2a96595dde042fcb30e42c73b232;p=civicrm-core.git Add tests for logging functionality to ensure the tables are still creating well Change-Id: I6ef4d48d16e25d232b0f46e7ce3cf5d569e7053c --- diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index cd3f512164..b64452a270 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -662,9 +662,7 @@ COLS; * Predicate whether logging is enabled. */ public function isEnabled() { - $config = CRM_Core_Config::singleton(); - - if ($config->logging) { + if (CRM_Core_Config::singleton()->logging) { return $this->tablesExist() and $this->triggersExist(); } return FALSE; @@ -677,6 +675,19 @@ COLS; return !empty($this->logs); } + /** + * Drop all log tables. + * + * This does not currently have a usage outside the tests. + */ + public function dropAllLogTables() { + if ($this->tablesExist()) { + foreach ($this->logs as $log_table) { + CRM_Core_DAO::executeQuery("DROP TABLE $log_table"); + } + } + } + /** * Get an sql clause to find the names of any log tables that do not match the normal pattern. * diff --git a/tests/phpunit/api/v3/LoggingTest.php b/tests/phpunit/api/v3/LoggingTest.php new file mode 100644 index 0000000000..7e885b0cce --- /dev/null +++ b/tests/phpunit/api/v3/LoggingTest.php @@ -0,0 +1,149 @@ +callAPISuccess('Setting', 'create', array('logging' => FALSE)); + $schema = new CRM_Logging_Schema(); + $schema->dropAllLogTables(); + CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name LIKE 'logg%'"); + } + + /** + * Test that logging is successfully enabled and disabled. + */ + public function testEnableDisableLogging() { + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); + $this->assertLoggingEnabled(FALSE); + + $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); + $this->assertLoggingEnabled(TRUE); + $this->checkLogTableCreated(); + $this->checkTriggersCreated(); + // Create a contact to make sure they aren't borked. + $this->individualCreate(); + $this->assertTrue($this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); + + $this->callAPISuccess('Setting', 'create', array('logging' => FALSE)); + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); + $this->assertLoggingEnabled(FALSE); + } + + /** + * Test that logging is successfully enabled and disabled. + */ + public function testEnableDisableLoggingWithTriggerHook() { + $this->hookClass->setHook('civicrm_alterLogTables', array($this, 'innodbLogTableSpec')); + $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); + $this->checkINNODBLogTableCreated(); + $this->checkTriggersCreated(); + // Create a contact to make sure they aren't borked. + $this->individualCreate(); + $this->callAPISuccess('Setting', 'create', array('logging' => FALSE)); + } + + /** + * Use a hook to declare an INNODB engine for the contact log table. + * + * @param array $logTableSpec + */ + public function innodbLogTableSpec(&$logTableSpec) { + $logTableSpec['civicrm_contact'] = array( + 'engine' => 'INNODB', + 'indexes' => array( + 'index_id' => 'id', + 'index_log_conn_id' => 'log_conn_id', + 'index_log_date' => 'log_date', + ), + ); + } + + /** + * Check the log tables were created and look OK. + */ + protected function checkLogTableCreated() { + $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_contact"); + $dao->fetch(); + $this->assertEquals('log_civicrm_contact', $dao->Table); + $tableField = 'Create_Table'; + $this->assertContains('`log_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,', $dao->$tableField); + return $dao->$tableField; + } + + /** + * Check the log tables were created and reflect the INNODB hook. + */ + protected function checkINNODBLogTableCreated() { + $createTableString = $this->checkLogTableCreated(); + $this->assertContains('ENGINE=InnoDB', $createTableString); + $this->assertContains('KEY `index_id` (`id`),', $createTableString); + } + + /** + * Check the triggers were created and look OK. + */ + protected function checkTriggersCreated() { + $dao = CRM_Core_DAO::executeQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); + while ($dao->fetch()) { + if ($dao->Timing == 'After') { + $this->assertContains('@uniqueID', $dao->Statement); + } + } + } + + /** + * Assert logging is enabled or disabled as per input parameter. + * + * @param bool $expected + * Do we expect it to be enabled. + */ + protected function assertLoggingEnabled($expected) { + $schema = new CRM_Logging_Schema(); + $this->assertTrue($schema->isEnabled() === $expected); + } + +}