Merge pull request #20115 from larssandergreen/fix-internal-anchor-URLs-in-mailings
[civicrm-core.git] / tests / phpunit / CRM / Logging / SchemaTest.php
index e27089bfd60367073a2dd54e41261d563b8f8151..70e014bd9fd89224cead3ecdc5d9385932b2e301 100644 (file)
@@ -16,19 +16,21 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
   /**
    * Clean up after test.
    *
+   * @throws \API_Exception
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function tearDown(): void {
     $schema = new CRM_Logging_Schema();
-    $schema->disableLogging();
+    $schema->dropAllLogTables();
+    Civi::settings()->set('logging', FALSE);
     $this->databaseVersion = NULL;
     parent::tearDown();
     $this->quickCleanup(['civicrm_contact'], TRUE);
-    $schema->dropAllLogTables();
-    CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS civicrm_test_table");
-    CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS civicrm_test_column_info");
-    CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS civicrm_test_length_change");
-    CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS civicrm_test_enum_change");
+    CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS civicrm_test_table');
+    CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS civicrm_test_column_info');
+    CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS civicrm_test_length_change');
+    CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS civicrm_test_enum_change');
   }
 
   /**
@@ -36,7 +38,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
    *
    * @return array
    */
-  public function queryExamples() {
+  public function queryExamples(): array {
     $examples = [];
     $examples[] = ["`modified_date` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'", "`modified_date` timestamp NULL  COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'"];
     $examples[] = ["`modified_date` timestamp NULL DEFAULT current_timestamp ON UPDATE current_timestamp COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'", "`modified_date` timestamp NULL  COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'"];
@@ -55,10 +57,9 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
   /**
    * Test log tables are created as InnoDB by default
    */
-  public function testLogEngine() {
-    $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
-    $log_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_acl");
+  public function testLogEngine(): void {
+    Civi::settings()->set('logging', TRUE);
+    $log_table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE log_civicrm_acl');
     while ($log_table->fetch()) {
       $this->assertRegexp('/ENGINE=InnoDB/', $log_table->Create_Table);
     }
@@ -67,11 +68,10 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
   /**
    * Test that the log table engine can be changed via hook to e.g. MyISAM
    */
-  public function testHookLogEngine() {
+  public function testHookLogEngine(): void {
     $this->hookClass->setHook('civicrm_alterLogTables', [$this, 'alterLogTables']);
-    $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
-    $log_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_acl");
+    Civi::settings()->set('logging', TRUE);
+    $log_table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE log_civicrm_acl');
     while ($log_table->fetch()) {
       $this->assertRegexp('/ENGINE=MyISAM/', $log_table->Create_Table);
     }
@@ -80,7 +80,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
   /**
    * Tests that choosing to ignore a custom table does not result in e-notices.
    */
-  public function testIgnoreCustomTableByHook() {
+  public function testIgnoreCustomTableByHook(): void {
     $group = $this->customGroupCreate();
     Civi::settings()->set('logging', TRUE);
     $this->hookClass->setHook('civicrm_alterLogTables', [$this, 'noCustomTables']);
@@ -92,7 +92,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
    *
    * @param array $logTableSpec
    */
-  public function noCustomTables(&$logTableSpec) {
+  public function noCustomTables(&$logTableSpec): void {
     foreach (array_keys($logTableSpec) as $index) {
       if (substr($index, 0, 14) === 'civicrm_value_') {
         unset($logTableSpec[$index]);
@@ -105,12 +105,12 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
    *
    * @throws \Exception
    */
-  public function testArchiveEngineConversion() {
+  public function testArchiveEngineConversion(): void {
+    Civi::settings()->set('logging', TRUE);
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
     // change table to ARCHIVE
-    CRM_Core_DAO::executeQuery("ALTER TABLE log_civicrm_acl ENGINE ARCHIVE");
-    $log_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_acl");
+    CRM_Core_DAO::executeQuery('ALTER TABLE log_civicrm_acl ENGINE ARCHIVE');
+    $log_table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE log_civicrm_acl');
     while ($log_table->fetch()) {
       $this->assertRegexp('/ENGINE=ARCHIVE/', $log_table->Create_Table);
     }
@@ -153,8 +153,8 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     $log_table = CRM_Core_DAO::executeQuery("SHOW TRIGGERS WHERE `Trigger` LIKE 'civicrm_value_contact_{$customGroup['custom_group_id']}_after_insert%'");
 
     while ($log_table->fetch()) {
-      $this->assertContains('UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.entity_id;', $log_table->Statement, "Contact modification update should be in the trigger :\n" . $log_table->Statement);
-      $this->assertNotContains('civicrm_mailing', $log_table->Statement, 'Contact field should not update mailing table');
+      $this->assertStringContainsString('UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.entity_id;', $log_table->Statement, "Contact modification update should be in the trigger :\n" . $log_table->Statement);
+      $this->assertStringNotContainsString('civicrm_mailing', $log_table->Statement, 'Contact field should not update mailing table');
       $this->assertEquals(1, substr_count($log_table->Statement, 'SET modified_date'), 'Modified date should only be updated on one table (here it is contact)');
     }
   }
@@ -167,8 +167,9 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
       test_id  int(10) unsigned NOT NULL AUTO_INCREMENT,
       PRIMARY KEY (`test_id`)
     )  ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
+
+    Civi::settings()->set('logging', TRUE);
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
     $diffs = $schema->columnsWithDiffSpecs("civicrm_test_table", "log_civicrm_test_table");
     // Test that just having a non id named column with Auto Increment doesn't create diffs
     $this->assertTrue(empty($diffs['MODIFY']));
@@ -176,34 +177,35 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     $this->assertTrue(empty($diffs['OBSOLETE']));
 
     // Check we can add a primary key to the log table and it will not be treated as obsolete.
-    CRM_Core_DAO::executeQuery("
+    CRM_Core_DAO::executeQuery('
       ALTER TABLE log_civicrm_test_table ADD COLUMN `log_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
       ADD PRIMARY KEY (`log_id`)
-   ");
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+   ');
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
     $diffs = $schema->columnsWithDiffSpecs('civicrm_test_table', "log_civicrm_test_table");
-    $this->assertTrue(empty($diffs['OBSOLETE']));
+    $this->assertEmpty($diffs['OBSOLETE']);
 
     CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_test_table ADD COLUMN test_varchar varchar(255) DEFAULT NULL");
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
     // Check that it still picks up new columns added.
     $diffs = $schema->columnsWithDiffSpecs("civicrm_test_table", "log_civicrm_test_table");
     $this->assertTrue(!empty($diffs['ADD']));
     $this->assertTrue(empty($diffs['MODIFY']));
     $this->assertTrue(empty($diffs['OBSOLETE']));
+    unset(Civi::$statics['CRM_Logging_Schema']);
     $schema->fixSchemaDifferences();
-    CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_test_table CHANGE COLUMN test_varchar test_varchar varchar(400) DEFAULT NULL");
+    CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_test_table CHANGE COLUMN test_varchar test_varchar varchar(400) DEFAULT NULL');
     // Check that it properly picks up modifications to columns.
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    unset(Civi::$statics['CRM_Logging_Schema']);
     $diffs = $schema->columnsWithDiffSpecs("civicrm_test_table", "log_civicrm_test_table");
-    $this->assertTrue(!empty($diffs['MODIFY']));
-    $this->assertTrue(empty($diffs['ADD']));
-    $this->assertTrue(empty($diffs['OBSOLETE']));
+    $this->assertNotEmpty($diffs['MODIFY']);
+    $this->assertEmpty($diffs['ADD']);
+    $this->assertEmpty($diffs['OBSOLETE']);
     $schema->fixSchemaDifferences();
-    CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_test_table CHANGE COLUMN test_varchar test_varchar varchar(300) DEFAULT NULL");
+    CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_test_table CHANGE COLUMN test_varchar test_varchar varchar(300) DEFAULT NULL');
     // Check that when we reduce the size of column that the log table doesn't shrink as well.
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
-    $diffs = $schema->columnsWithDiffSpecs("civicrm_test_table", "log_civicrm_test_table");
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    $diffs = $schema->columnsWithDiffSpecs('civicrm_test_table', "log_civicrm_test_table");
     $this->assertTrue(empty($diffs['MODIFY']));
     $this->assertTrue(empty($diffs['ADD']));
     $this->assertTrue(empty($diffs['OBSOLETE']));
@@ -215,7 +217,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
   public function testTriggerInfo() {
     $info = [];
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
+    Civi::settings()->set('logging', TRUE);
     $schema->triggerInfo($info, 'civicrm_group');
     // should have 3 triggers (insert/update/delete)
     $this->assertCount(3, $info);
@@ -224,7 +226,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
       $this->assertEquals('civicrm_group', $trigger['table'][0]);
       if ($trigger['event'][0] == 'UPDATE') {
         // civicrm_group.cache_date should be an exception, i.e. not logged
-        $this->assertNotContains(
+        $this->assertStringNotContainsString(
           "IFNULL(OLD.`cache_date`,'') <> IFNULL(NEW.`cache_date`,'')",
           $trigger['sql']
         );
@@ -232,7 +234,10 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     }
   }
 
-  public function testColumnInfo() {
+  /**
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testColumnInfo(): void {
     CRM_Core_DAO::executeQuery("CREATE TABLE `civicrm_test_column_info` (
       test_id  int(10) unsigned NOT NULL AUTO_INCREMENT,
       test_varchar varchar(42) NOT NULL,
@@ -243,10 +248,11 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
       test_date date DEFAULT NULL,
       PRIMARY KEY (`test_id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
+    Civi::settings()->set('logging', TRUE);
+
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
     $schema->updateLogTableSchema(['updateChangedEngineConfig' => FALSE, 'forceEngineMigration' => FALSE]);
-    $ci = \Civi::$statics['CRM_Logging_Schema']['columnSpecs']['civicrm_test_column_info'];
+    $ci = Civi::$statics['CRM_Logging_Schema']['columnSpecs']['civicrm_test_column_info'];
 
     $this->assertEquals('test_id', $ci['test_id']['COLUMN_NAME']);
     $this->assertEquals('int', $ci['test_id']['DATA_TYPE']);
@@ -293,15 +299,15 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
       test_decimal decimal(20,2) NULL,
       PRIMARY KEY (`test_id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
+    Civi::settings()->set('logging', TRUE);
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
     CRM_Core_DAO::executeQuery(
       "ALTER TABLE civicrm_test_length_change
       CHANGE COLUMN test_integer test_integer int(6) NULL,
       CHANGE COLUMN test_decimal test_decimal decimal(22,2) NULL"
     );
     $schema->fixSchemaDifferences();
-    $ci = \Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
+    $ci = Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
     // length should increase
     if (!$this->isMySQL8()) {
       $this->assertEquals(6, $ci['log_civicrm_test_length_change']['test_integer']['LENGTH']);
@@ -313,7 +319,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
       CHANGE COLUMN test_decimal test_decimal decimal(20,2) NULL"
     );
     $schema->fixSchemaDifferences();
-    $ci = \Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
+    $ci = Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
     // length should not decrease
     if (!$this->isMySQL8()) {
       $this->assertEquals(6, $ci['log_civicrm_test_length_change']['test_integer']['LENGTH']);
@@ -321,28 +327,32 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     $this->assertEquals('22,2', $ci['log_civicrm_test_length_change']['test_decimal']['LENGTH']);
   }
 
-  public function testEnumChange() {
+  /**
+   * Test changing the enum.
+   */
+  public function testEnumChange(): void {
     CRM_Core_DAO::executeQuery("CREATE TABLE `civicrm_test_enum_change` (
       test_id int(10) unsigned NOT NULL AUTO_INCREMENT,
       test_enum enum('A','B','C') NULL,
       PRIMARY KEY (`test_id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
+    Civi::settings()->set('logging', TRUE);
     $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
     CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_test_enum_change CHANGE COLUMN test_enum test_enum enum('A','B','C','D') NULL");
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
     $schema->fixSchemaDifferences();
-    $ci = \Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
+    $ci = Civi::$statics['CRM_Logging_Schema']['columnSpecs'];
     // new enum value should be included
     $this->assertEquals("'A','B','C','D'", $ci['civicrm_test_enum_change']['test_enum']['ENUM_VALUES']);
   }
 
   /**
    * Test editing a custom field
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testCustomFieldEdit() {
-    $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
+  public function testCustomFieldEdit(): void {
+    Civi::settings()->set('logging', TRUE);
     $customGroup = $this->entityCustomGroupWithSingleFieldCreate('Contact', 'ContactTest.php');
 
     // get the custom group table name
@@ -367,6 +377,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     $this->callAPISuccess('custom_field', 'create', $params);
 
     // update logging schema
+    $schema = new CRM_Logging_Schema();
     $schema->fixSchemaDifferences();
 
     // verify
@@ -379,9 +390,8 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
    * Test creating a table with SchemaHandler::createTable when logging
    * is enabled.
    */
-  public function testCreateTableWithLogging() {
-    $schema = new CRM_Logging_Schema();
-    $schema->enableLogging();
+  public function testCreateTableWithLogging(): void {
+    Civi::settings()->set('logging', TRUE);
 
     CRM_Core_BAO_SchemaHandler::createTable([
       'name' => 'civicrm_test_table',
@@ -422,7 +432,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
     $this->assertStringContainsString('FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE', $dao->Create_Table);
 
     // Check log table.
-    $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_test_table");
+    $dao = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE log_civicrm_test_table');
     $dao->fetch();
     $this->assertStringNotContainsString('AUTO_INCREMENT', $dao->Create_Table);
     // This seems debatable whether `id` should lose its NOT NULL status
@@ -438,7 +448,7 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase {
    *
    * @return bool
    */
-  protected function isMySQL8() {
+  protected function isMySQL8(): bool {
     return (bool) (version_compare($this->databaseVersion, '8.0.19', '>=') && stripos($this->databaseVersion, 'mariadb') === FALSE);
   }