Merge pull request #21577 from jmcclelland/display-backend-recur-options
[civicrm-core.git] / tests / phpunit / CRM / Logging / LoggingTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_DAOTest
5 * @group headless
6 */
7 class CRM_Logging_LoggingTest extends CiviUnitTestCase {
8
9 use CRMTraits_Custom_CustomDataTrait;
10
11 /**
12 * Has the db been set to multilingual.
13 *
14 * @var bool
15 */
16 protected $isDBMultilingual = FALSE;
17
18 public function tearDown(): void {
19 Civi::settings()->set('logging', FALSE);
20 if ($this->isDBMultilingual) {
21 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
22 }
23 $logging = new CRM_Logging_Schema();
24 $logging->dropAllLogTables();
25 $this->cleanupCustomGroups();
26 parent::tearDown();
27 }
28
29 /**
30 * Check that log tables are created even for non standard custom fields
31 * tables.
32 *
33 * @throws \API_Exception
34 */
35 public function testLoggingNonStandardCustomTableName(): void {
36 $this->createCustomGroupWithFieldOfType(['table_name' => 'abcd']);
37 Civi::settings()->set('logging', TRUE);
38 $this->assertNotEmpty(CRM_Core_DAO::singleValueQuery("SHOW tables LIKE 'log_abcd'"));
39 }
40
41 /**
42 * Test that hooks removing tables from logging are respected during custom field add.
43 *
44 * During custom field save logging is only handled for the affected table.
45 * We need to make sure this respects hooks to remove from the logging set.
46 */
47 public function testLoggingHookIgnore(): void {
48 $this->hookClass->setHook('civicrm_alterLogTables', [$this, 'ignoreSillyName']);
49 Civi::settings()->set('logging', TRUE);
50 $this->createCustomGroupWithFieldOfType(['table_name' => 'silly_name']);
51 $this->assertEmpty(CRM_Core_DAO::singleValueQuery("SHOW tables LIKE 'log_silly_name'"));
52 }
53
54 /**
55 * Implement hook to cause our log table to be ignored.
56 *
57 * @param array $logTableSpec
58 */
59 public function ignoreSillyName(array &$logTableSpec): void {
60 unset($logTableSpec['silly_name']);
61 }
62
63 /**
64 * Test creating logging schema when database is in multilingual mode.
65 */
66 public function testMultilingualLogging(): void {
67 $this->makeMultilingual();
68 Civi::settings()->set('logging', TRUE);
69 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
70 $this->assertNotNull($value, 'Logging not enabled successfully');
71 }
72
73 /**
74 * Test creating logging schema when database is in multilingual mode.
75 * Also test altering a multilingual table.
76 */
77 public function testMultilingualAlterSchemaLogging(): void {
78 $this->makeMultilingual();
79 Civi::settings()->set('logging', TRUE);
80 $logging = new CRM_Logging_Schema();
81 $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
82 $this->assertNotNull($value, 'Logging not enabled successfully');
83 CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_option_value` ADD COLUMN `logging_test` INT DEFAULT '0'", [], FALSE, NULL, FALSE, FALSE);
84 CRM_Core_I18n_Schema::rebuildMultilingualSchema(['en_US']);
85 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
86 $logging->fixSchemaDifferencesFor('civicrm_option_value');
87 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
88 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
89 $query->fetch();
90 $create = explode("\n", $query->Create_Table);
91 // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version
92 $this->assertTrue(
93 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
94 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
95 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
96 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
97 );
98 CRM_Core_DAO::executeQuery('ALTER TABLE `civicrm_option_value` DROP COLUMN `logging_test`', [], FALSE, NULL, FALSE, FALSE);
99 $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE);
100 $query->fetch();
101 $domain = new CRM_Core_DAO_Domain();
102 $domain->find(TRUE);
103 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
104 Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = [];
105 CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales);
106 $logging->fixSchemaDifferencesFor('civicrm_option_value');
107 Civi::service('sql_triggers')->rebuild('civicrm_option_value');
108 $this->assertTrue(
109 in_array(" `logging_test` int(11) DEFAULT '0'", $create, TRUE)
110 || in_array(' `logging_test` int(11) DEFAULT 0', $create, TRUE)
111 || in_array(" `logging_test` int DEFAULT '0'", $create, TRUE)
112 || in_array(' `logging_test` int DEFAULT 0', $create, TRUE)
113 );
114 }
115
116 /**
117 * Convert the database to multilingual mode.
118 */
119 protected function makeMultilingual(): void {
120 CRM_Core_I18n_Schema::makeMultilingual('en_US');
121 $this->isDBMultilingual = TRUE;
122 }
123
124 }