Commit | Line | Data |
---|---|---|
15a68d41 SL |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class CRM_Core_DAOTest | |
5 | * @group headless | |
6 | */ | |
7 | class CRM_Logging_LoggingTest extends CiviUnitTestCase { | |
8 | ||
8a475c3d EM |
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 | ||
dd09ee0c | 18 | public function tearDown(): void { |
4b208509 | 19 | Civi::settings()->set('logging', FALSE); |
8a475c3d EM |
20 | if ($this->isDBMultilingual) { |
21 | CRM_Core_I18n_Schema::makeSinglelingual('en_US'); | |
22 | } | |
15a68d41 SL |
23 | $logging = new CRM_Logging_Schema(); |
24 | $logging->dropAllLogTables(); | |
8a475c3d | 25 | $this->cleanupCustomGroups(); |
15a68d41 SL |
26 | parent::tearDown(); |
27 | } | |
28 | ||
8a475c3d EM |
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 | ||
78b7cb89 EM |
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 | ||
15a68d41 SL |
63 | /** |
64 | * Test creating logging schema when database is in multilingual mode. | |
65 | */ | |
4b208509 | 66 | public function testMultilingualLogging(): void { |
8a475c3d | 67 | $this->makeMultilingual(); |
4b208509 EM |
68 | Civi::settings()->set('logging', TRUE); |
69 | $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE); | |
15a68d41 | 70 | $this->assertNotNull($value, 'Logging not enabled successfully'); |
15a68d41 SL |
71 | } |
72 | ||
15a68d41 SL |
73 | /** |
74 | * Test creating logging schema when database is in multilingual mode. | |
75 | * Also test altering a multilingual table. | |
76 | */ | |
4b208509 | 77 | public function testMultilingualAlterSchemaLogging(): void { |
8a475c3d | 78 | $this->makeMultilingual(); |
4b208509 | 79 | Civi::settings()->set('logging', TRUE); |
15a68d41 | 80 | $logging = new CRM_Logging_Schema(); |
4b208509 | 81 | $value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE); |
15a68d41 | 82 | $this->assertNotNull($value, 'Logging not enabled successfully'); |
9099cab3 CW |
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']); | |
4b208509 | 85 | Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = []; |
fcc20cec | 86 | $logging->fixSchemaDifferencesFor('civicrm_option_value'); |
87 | Civi::service('sql_triggers')->rebuild('civicrm_option_value'); | |
4b208509 | 88 | $query = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE `log_civicrm_option_value`', [], TRUE, NULL, FALSE, FALSE); |
15a68d41 | 89 | $query->fetch(); |
14e47fc1 | 90 | $create = explode("\n", $query->Create_Table); |
204aa6fb PF |
91 | // MySQL may return "DEFAULT 0" or "DEFAULT '0'" depending on version |
92 | $this->assertTrue( | |
4b208509 EM |
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) | |
204aa6fb | 97 | ); |
4b208509 EM |
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); | |
14e47fc1 SL |
100 | $query->fetch(); |
101 | $domain = new CRM_Core_DAO_Domain(); | |
102 | $domain->find(TRUE); | |
103 | $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); | |
4b208509 | 104 | Civi::$statics['CRM_Logging_Schema']['columnSpecs'] = []; |
14e47fc1 | 105 | CRM_Core_I18n_Schema::rebuildMultilingualSchema($locales); |
fcc20cec | 106 | $logging->fixSchemaDifferencesFor('civicrm_option_value'); |
107 | Civi::service('sql_triggers')->rebuild('civicrm_option_value'); | |
204aa6fb | 108 | $this->assertTrue( |
4b208509 EM |
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) | |
4f4ed4cf | 113 | ); |
15a68d41 SL |
114 | } |
115 | ||
8a475c3d EM |
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 | ||
15a68d41 | 124 | } |