From 87a52027c6b293d3c2ed5bc0f25f16856eca5d05 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 13 Apr 2016 19:52:35 +1200 Subject: [PATCH] fixes for tests when they run in suite, cache clearing etc --- CRM/Logging/Schema.php | 20 +++++++----- tests/phpunit/api/v3/LoggingTest.php | 47 +++++++++++++++++++++------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 436aa8df73..2c17f7b42b 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -556,9 +556,11 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) * @return array */ private function columnSpecsOf($table) { - static $columnSpecs = array(), $civiDB = NULL; - - if (empty($columnSpecs) || !isset($columnSpecs[$table])) { + static $civiDB = NULL; + if (empty(\Civi::$statics[__CLASS__]['columnSpecs'])) { + \Civi::$statics[__CLASS__]['columnSpecs'] = array(); + } + if (empty(\Civi::$statics[__CLASS__]['columnSpecs']) || !isset(\Civi::$statics[__CLASS__]['columnSpecs'][$table])) { if (!$civiDB) { $dao = new CRM_Contact_DAO_Contact(); $civiDB = $dao->_database; @@ -575,21 +577,23 @@ WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; return array(); } while ($dao->fetch()) { - if (!array_key_exists($dao->TABLE_NAME, $columnSpecs)) { - $columnSpecs[$dao->TABLE_NAME] = array(); + if (!array_key_exists($dao->TABLE_NAME, \Civi::$statics[__CLASS__]['columnSpecs'])) { + \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME] = array(); } - $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME] = array( + \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME][$dao->COLUMN_NAME] = array( 'COLUMN_NAME' => $dao->COLUMN_NAME, 'DATA_TYPE' => $dao->DATA_TYPE, 'IS_NULLABLE' => $dao->IS_NULLABLE, 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT, ); if (($first = strpos($dao->COLUMN_TYPE, '(')) != 0) { - $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME]['LENGTH'] = substr($dao->COLUMN_TYPE, $first, strpos($dao->COLUMN_TYPE, ')')); + \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME][$dao->COLUMN_NAME]['LENGTH'] = substr( + $dao->COLUMN_TYPE, $first, strpos($dao->COLUMN_TYPE, ')') + ); } } } - return $columnSpecs[$table]; + return \Civi::$statics[__CLASS__]['columnSpecs'][$table]; } /** diff --git a/tests/phpunit/api/v3/LoggingTest.php b/tests/phpunit/api/v3/LoggingTest.php index 2f470bd8f3..c563e3f7a9 100644 --- a/tests/phpunit/api/v3/LoggingTest.php +++ b/tests/phpunit/api/v3/LoggingTest.php @@ -39,6 +39,7 @@ class api_v3_LoggingTest extends CiviUnitTestCase { * This method is called before a test is executed. */ protected function setUp() { + $this->ensureTempColIsCleanedUp(); parent::setUp(); } @@ -58,7 +59,7 @@ class api_v3_LoggingTest extends CiviUnitTestCase { * Test that logging is successfully enabled and disabled. */ public function testEnableDisableLogging() { - $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging'))); $this->assertLoggingEnabled(FALSE); $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); @@ -67,15 +68,15 @@ class api_v3_LoggingTest extends CiviUnitTestCase { $this->checkTriggersCreated(TRUE); // Create a contact to make sure they aren't borked. $this->individualCreate(); - $this->assertTrue($this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); - $this->assertEquals(1, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid', 'group' => 'core'))); + $this->assertTrue($this->callAPISuccessGetValue('Setting', array('name' => 'logging'))); + $this->assertEquals(1, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid'))); $this->assertEquals( date('Y-m-d'), - date('Y-m-d', strtotime($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date', 'group' => 'core')))) + date('Y-m-d', strtotime($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date')))) ); $this->callAPISuccess('Setting', 'create', array('logging' => FALSE)); - $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging', 'group' => 'core'))); + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging'))); $this->assertLoggingEnabled(FALSE); } @@ -106,8 +107,8 @@ class api_v3_LoggingTest extends CiviUnitTestCase { $this->createLegacyStyleContactLogTable(); $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); $this->checkTriggersCreated(FALSE); - $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid', 'group' => 'core'))); - $this->assertEmpty($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date', 'group' => 'core'))); + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid'))); + $this->assertEmpty($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date'))); } /** @@ -119,10 +120,10 @@ class api_v3_LoggingTest extends CiviUnitTestCase { $this->callAPISuccess('System', 'updatelogtables', array()); $this->checkLogTableCreated(); $this->checkTriggersCreated(TRUE); - $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid', 'group' => 'core'))); + $this->assertEquals(0, $this->callAPISuccessGetValue('Setting', array('name' => 'logging_all_tables_uniquid'))); $this->assertEquals( date('Y-m-d'), - date('Y-m-d', strtotime($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date', 'group' => 'core')))) + date('Y-m-d', strtotime($this->callAPISuccessGetValue('Setting', array('name' => 'logging_uniqueid_date')))) ); } @@ -160,9 +161,7 @@ class api_v3_LoggingTest extends CiviUnitTestCase { $schema->fixSchemaDifferencesForAll(TRUE); foreach ($tables as $table) { - $dao = CRM_Core_DAO::executeQuery("SHOW columns FROM log_{$table} WHERE Field = 'temp_col'"); - $dao->fetch(TRUE); - $this->assertEquals(1, $dao->N, "Column temp_col not in $table"); + $this->assertTrue($this->checkColumnExistsInTable('log_' . $table, 'temp_col'), 'log_' . $table . ' has temp_col'); $dao = CRM_Core_DAO::executeQuery("SHOW TRIGGERS LIKE '{$table}'"); while ($dao->fetch()) { $this->assertContains('temp_col', $dao->Statement); @@ -374,4 +373,28 @@ class api_v3_LoggingTest extends CiviUnitTestCase { throw new CRM_Core_Exception("No match found for key : $expectKey with value : $expectValue"); } + /** + * Check if the column exists in the table. + * + * @param string $table + * @param string $column + * + * @return \CRM_Core_DAO|object + */ + protected function checkColumnExistsInTable($table, $column) { + $dao = CRM_Core_DAO::executeQuery("SHOW columns FROM {$table} WHERE Field = '{$column}'"); + $dao->fetch(TRUE); + return ($dao->N == 1); + } + + /** + * Helper for when it crashes and clean up needs to be done. + */ + protected function ensureTempColIsCleanedUp() { + if ($this->checkColumnExistsInTable('civicrm_acl', 'temp_col')) { + CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_acl DROP Column temp_col"); + CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_website DROP Column temp_col"); + } + } + } -- 2.25.1