Merge pull request #20115 from larssandergreen/fix-internal-anchor-URLs-in-mailings
[civicrm-core.git] / tests / phpunit / CRM / Logging / LoggingTest.php
index cd1d64c7505471fa861e297d4db787763ae419b3..050fce542a3d83edddb02a9c563186f94488c4b6 100644 (file)
@@ -6,68 +6,97 @@
  */
 class CRM_Logging_LoggingTest extends CiviUnitTestCase {
 
+  use CRMTraits_Custom_CustomDataTrait;
+
+  /**
+   * Has the db been set to multilingual.
+   *
+   * @var bool
+   */
+  protected $isDBMultilingual = FALSE;
+
   public function tearDown(): void {
-    CRM_Core_I18n_Schema::makeSinglelingual('en_US');
+    Civi::settings()->set('logging', FALSE);
+    if ($this->isDBMultilingual) {
+      CRM_Core_I18n_Schema::makeSinglelingual('en_US');
+    }
     $logging = new CRM_Logging_Schema();
     $logging->dropAllLogTables();
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    $this->cleanupCustomGroups();
     parent::tearDown();
   }
 
+  /**
+   * Check that log tables are created even for non standard custom fields
+   * tables.
+   *
+   * @throws \API_Exception
+   */
+  public function testLoggingNonStandardCustomTableName(): void {
+    $this->createCustomGroupWithFieldOfType(['table_name' => 'abcd']);
+    Civi::settings()->set('logging', TRUE);
+    $this->assertNotEmpty(CRM_Core_DAO::singleValueQuery("SHOW tables LIKE 'log_abcd'"));
+  }
+
   /**
    * Test creating logging schema when database is in multilingual mode.
    */
-  public function testMultilingualLogging() {
-    CRM_Core_I18n_Schema::makeMultilingual('en_US');
-    $logging = new CRM_Logging_Schema();
-    $logging->enableLogging();
-    $value = CRM_Core_DAO::singleValueQuery("SELECT id FROM log_civicrm_contact LIMIT 1", [], FALSE, FALSE);
+  public function testMultilingualLogging(): void {
+    $this->makeMultilingual();
+    Civi::settings()->set('logging', TRUE);
+    $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
     $this->assertNotNull($value, 'Logging not enabled successfully');
-    $logging->disableLogging();
   }
 
   /**
    * Test creating logging schema when database is in multilingual mode.
    * Also test altering a multilingual table.
    */
-  public function testMultilingualAlterSchemaLogging() {
-    CRM_Core_I18n_Schema::makeMultilingual('en_US');
+  public function testMultilingualAlterSchemaLogging(): void {
+    $this->makeMultilingual();
+    Civi::settings()->set('logging', TRUE);
     $logging = new CRM_Logging_Schema();
-    $logging->enableLogging();
-    $value = CRM_Core_DAO::singleValueQuery("SELECT id FROM log_civicrm_contact LIMIT 1", [], FALSE, FALSE);
+    $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
     $this->assertNotNull($value, 'Logging not enabled successfully');
     CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT '0'", [], FALSE, NULL, FALSE, FALSE);
     CRM_Core_I18n_Schema::rebuildMultilingualSchema(['en_US']);
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
     $logging->fixSchemaDifferencesFor('civicrm_option_value');
     Civi::service('sql_triggers')->rebuild('civicrm_option_value');
-    $query = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE `log_civicrm_option_value`", [], TRUE, NULL, FALSE, FALSE);
+    $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
     $query->fetch();
     $create = explode("\n", $query->Create_Table);
     // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version
     $this->assertTrue(
-      in_array("  `logging_test` int(11) DEFAULT '0'", $create)
-      || in_array("  `logging_test` int(11) DEFAULT 0", $create)
-      || in_array("  `logging_test` int DEFAULT '0'", $create)
-      || in_array("  `logging_test` int DEFAULT 0", $create)
+      in_array("  `logging_test` int(11) DEFAULT '0'", $create, TRUE)
+      || in_array('  `logging_test` int(11) DEFAULT 0', $create, TRUE)
+      || in_array("  `logging_test` int DEFAULT '0'", $create, TRUE)
+      || in_array('  `logging_test` int DEFAULT 0', $create, TRUE)
     );
-    CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`", [], FALSE, NULL, FALSE, FALSE);
-    $query = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE `log_civicrm_option_value`", [], TRUE, NULL, FALSE, FALSE);
+    CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`', [], FALSE, NULL, FALSE, FALSE);
+    $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
     $query->fetch();
     $domain = new CRM_Core_DAO_Domain();
     $domain->find(TRUE);
     $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
-    \Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
+    Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
     CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales);
     $logging->fixSchemaDifferencesFor('civicrm_option_value');
     Civi::service('sql_triggers')->rebuild('civicrm_option_value');
     $this->assertTrue(
-      in_array("  `logging_test` int(11) DEFAULT '0'", $create)
-      || in_array("  `logging_test` int(11) DEFAULT 0", $create)
-      || in_array("  `logging_test` int DEFAULT '0'", $create)
-      || in_array("  `logging_test` int DEFAULT 0", $create)
+      in_array("  `logging_test` int(11) DEFAULT '0'", $create, TRUE)
+      || in_array('  `logging_test` int(11) DEFAULT 0', $create, TRUE)
+      || in_array("  `logging_test` int DEFAULT '0'", $create, TRUE)
+      || in_array('  `logging_test` int DEFAULT 0', $create, TRUE)
     );
-    $logging->disableLogging();
+  }
+
+  /**
+   * Convert the database to multilingual mode.
+   */
+  protected function makeMultilingual(): void {
+    CRM_Core_I18n_Schema::makeMultilingual('en_US');
+    $this->isDBMultilingual = TRUE;
   }
 
 }