Merge pull request #14557 from civicrm/5.15
[civicrm-core.git] / tests / phpunit / CRM / Core / I18n / SchemaTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27 /**
28 * Class CRM_Core_I18n_SchemaTest
29 * @group headless
30 */
31 class CRM_Core_I18n_SchemaTest extends CiviUnitTestCase {
32
33 /**
34 * Test tables to translate
35 * @return array
36 */
37 public static function translateTables() {
38 $tables = array();
39 $tables[] = array('civicrm_option_group', 'civicrm_option_group_en_US');
40 $tables[] = array('civicrm_events_in_carts', 'civicrm_events_in_carts');
41 $tables[] = array('civicrm_event', 'civicrm_event_en_US');
42 return $tables;
43 }
44
45 public function setUp() {
46 parent::setUp();
47 }
48
49 public function tearDown() {
50 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
51 parent::tearDown();
52 }
53
54 /**
55 * @param string $table
56 * @param string $expectedRewrite
57 *
58 * @dataProvider translateTables
59 */
60 public function testI18nSchemaRewrite($table, $expectedRewrite) {
61 CRM_Core_I18n_Schema::makeMultilingual('en_US');
62 $skip_tests = FALSE;
63 if (in_array($table, array('civicrm_option_group', 'civicrm_event'))) {
64 $skip_tests = TRUE;
65 }
66 global $dbLocale;
67 $dbLocale = '_en_US';
68 // Test problematic queriy as per CRM-20427
69 $query = "Select * FROM {$table}";
70 $new_query = CRM_Core_I18n_Schema::rewriteQuery($query);
71 $this->assertEquals("Select * FROM {$expectedRewrite}", $new_query);
72 // Test query where table is not at the end
73 $query2 = "Select * FROM {$table} LIMIT 1";
74 $new_query2 = CRM_Core_I18n_Schema::rewriteQuery($query2);
75 $this->assertEquals("Select * FROM {$expectedRewrite} LIMIT 1", $new_query2);
76 // Test query where there is a 2nd table that shouldn't be re-wrten
77 $query3 = "SELECT * FROM {$table} JOIN civicrm_contact LIMIT 1";
78 $new_query3 = CRM_Core_I18n_Schema::rewriteQuery($query3);
79 $this->assertEquals("SELECT * FROM {$expectedRewrite} JOIN civicrm_contact LIMIT 1", $new_query3);
80 // Test table when name is escaped
81 $query4 = "SELECT * FROM `{$table}` WHERE id = 123";
82 $new_query4 = CRM_Core_I18n_Schema::rewriteQuery($query4);
83 $this->assertEquals("SELECT * FROM `{$expectedRewrite}` WHERE id = 123", $new_query4);
84 // Test where translatable table is quoted
85 // The `$table` appears in a string -- it should not be rewritten.
86 $query5 = 'SELECT id FROM civicrm_activity WHERE subject = "civicrm_option_group"';
87 $new_query5 = CRM_Core_I18n_Schema::rewriteQuery($query5);
88 $this->assertEquals($query5, $new_query5);
89 // Test where table is not the last thing to be in a quoted string
90 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
91 // Agreed as not a blocker for CRM-20427 as an issue previously.
92 if (!$skip_tests) {
93 $query6 = "SELECT " . '"' . "Fixed the the {$table} ticket" . '"';
94 $new_query6 = CRM_Core_I18n_Schema::rewriteQuery($query6);
95 $this->assertEquals($query6, $new_query6);
96 }
97 // Test where table is part of a sub query
98 $query7 = "SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$table})";
99 $new_query7 = CRM_Core_I18n_Schema::rewriteQuery($query7);
100 $this->assertEquals("SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$expectedRewrite})", $new_query7);
101 // Test differern verbs
102 $query8 = "DELETE FROM {$table}";
103 $new_query8 = CRM_Core_I18n_Schema::rewriteQuery($query8);
104 $this->assertEquals("DELETE FROM {$expectedRewrite}", $new_query8);
105 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
106 // Agreed as not a blocker for CRM-20427 as an issue previously
107 if (!$skip_tests) {
108 $query9 = 'INSERT INTO ' . "{$table}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")';
109 $new_query9 = CRM_Core_I18n_Schema::rewriteQuery($query9);
110 $this->assertEquals('INSERT INTO ' . "{$expectedRewrite}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")', $new_query9);
111 }
112 }
113
114 public function testSchemaBuild() {
115 CRM_Core_I18n_Schema::makeMultilingual('en_US');
116 $testCreateTable = CRM_Core_DAO::executeQuery("show create table civicrm_price_set", [], TRUE, NULL, FALSE, FALSE);
117 while ($testCreateTable->fetch()) {
118 $this->assertContains("`title_en_US` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Displayed title for the Price Set.'", $testCreateTable->Create_Table);
119 $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);
120 }
121 }
122
123 }