Merge pull request #15326 from totten/master-headfoot-2
[civicrm-core.git] / tests / phpunit / CRM / Core / I18n / SchemaTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11 /**
12 * Class CRM_Core_I18n_SchemaTest
13 * @group headless
14 */
15 class CRM_Core_I18n_SchemaTest extends CiviUnitTestCase {
16
17 /**
18 * Test tables to translate
19 * @return array
20 */
21 public static function translateTables() {
22 $tables = [];
23 $tables[] = ['civicrm_option_group', 'civicrm_option_group_en_US'];
24 $tables[] = ['civicrm_events_in_carts', 'civicrm_events_in_carts'];
25 $tables[] = ['civicrm_event', 'civicrm_event_en_US'];
26 return $tables;
27 }
28
29 public function setUp() {
30 parent::setUp();
31 }
32
33 public function tearDown() {
34 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
35 parent::tearDown();
36 }
37
38 /**
39 * @param string $table
40 * @param string $expectedRewrite
41 *
42 * @dataProvider translateTables
43 */
44 public function testI18nSchemaRewrite($table, $expectedRewrite) {
45 CRM_Core_I18n_Schema::makeMultilingual('en_US');
46 $skip_tests = FALSE;
47 if (in_array($table, ['civicrm_option_group', 'civicrm_event'])) {
48 $skip_tests = TRUE;
49 }
50 global $dbLocale;
51 $dbLocale = '_en_US';
52 // Test problematic queriy as per CRM-20427
53 $query = "Select * FROM {$table}";
54 $new_query = CRM_Core_I18n_Schema::rewriteQuery($query);
55 $this->assertEquals("Select * FROM {$expectedRewrite}", $new_query);
56 // Test query where table is not at the end
57 $query2 = "Select * FROM {$table} LIMIT 1";
58 $new_query2 = CRM_Core_I18n_Schema::rewriteQuery($query2);
59 $this->assertEquals("Select * FROM {$expectedRewrite} LIMIT 1", $new_query2);
60 // Test query where there is a 2nd table that shouldn't be re-wrten
61 $query3 = "SELECT * FROM {$table} JOIN civicrm_contact LIMIT 1";
62 $new_query3 = CRM_Core_I18n_Schema::rewriteQuery($query3);
63 $this->assertEquals("SELECT * FROM {$expectedRewrite} JOIN civicrm_contact LIMIT 1", $new_query3);
64 // Test table when name is escaped
65 $query4 = "SELECT * FROM `{$table}` WHERE id = 123";
66 $new_query4 = CRM_Core_I18n_Schema::rewriteQuery($query4);
67 $this->assertEquals("SELECT * FROM `{$expectedRewrite}` WHERE id = 123", $new_query4);
68 // Test where translatable table is quoted
69 // The `$table` appears in a string -- it should not be rewritten.
70 $query5 = 'SELECT id FROM civicrm_activity WHERE subject = "civicrm_option_group"';
71 $new_query5 = CRM_Core_I18n_Schema::rewriteQuery($query5);
72 $this->assertEquals($query5, $new_query5);
73 // Test where table is not the last thing to be in a quoted string
74 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
75 // Agreed as not a blocker for CRM-20427 as an issue previously.
76 if (!$skip_tests) {
77 $query6 = "SELECT " . '"' . "Fixed the the {$table} ticket" . '"';
78 $new_query6 = CRM_Core_I18n_Schema::rewriteQuery($query6);
79 $this->assertEquals($query6, $new_query6);
80 }
81 // Test where table is part of a sub query
82 $query7 = "SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$table})";
83 $new_query7 = CRM_Core_I18n_Schema::rewriteQuery($query7);
84 $this->assertEquals("SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$expectedRewrite})", $new_query7);
85 // Test differern verbs
86 $query8 = "DELETE FROM {$table}";
87 $new_query8 = CRM_Core_I18n_Schema::rewriteQuery($query8);
88 $this->assertEquals("DELETE FROM {$expectedRewrite}", $new_query8);
89 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
90 // Agreed as not a blocker for CRM-20427 as an issue previously
91 if (!$skip_tests) {
92 $query9 = 'INSERT INTO ' . "{$table}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")';
93 $new_query9 = CRM_Core_I18n_Schema::rewriteQuery($query9);
94 $this->assertEquals('INSERT INTO ' . "{$expectedRewrite}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")', $new_query9);
95 }
96 }
97
98 public function testSchemaBuild() {
99 CRM_Core_I18n_Schema::makeMultilingual('en_US');
100 $testCreateTable = CRM_Core_DAO::executeQuery("show create table civicrm_price_set", [], TRUE, NULL, FALSE, FALSE);
101 while ($testCreateTable->fetch()) {
102 $this->assertContains("`title_en_US` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Displayed title for the Price Set.'", $testCreateTable->Create_Table);
103 $this->assertContains("`help_pre_en_US` text COLLATE utf8_unicode_ci COMMENT 'Description and/or help text to display before fields in form.'", $testCreateTable->Create_Table);
104 }
105 }
106
107 }