Commit | Line | Data |
---|---|---|
8a0b1cfd SL |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
8a0b1cfd | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
8a0b1cfd SL |
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 | /** | |
434e2e0e | 28 | * Class CRM_Core_I18n_SchemaTest |
8a0b1cfd SL |
29 | * @group headless |
30 | */ | |
434e2e0e | 31 | class CRM_Core_I18n_SchemaTest extends CiviUnitTestCase { |
8a0b1cfd SL |
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(); | |
434e2e0e | 52 | } |
8a0b1cfd SL |
53 | |
54 | /** | |
55 | * @param string $table | |
56 | * @param string $expectedRewrite | |
57 | * | |
58 | * @dataProvider translateTables | |
59 | */ | |
434e2e0e SL |
60 | public function testI18nSchemaRewrite($table, $expectedRewrite) { |
61 | CRM_Core_I18n_Schema::makeMultilingual('en_US'); | |
9bb55983 SL |
62 | $skip_tests = FALSE; |
63 | if (in_array($table, array('civicrm_option_group', 'civicrm_event'))) { | |
64 | $skip_tests = TRUE; | |
65 | } | |
434e2e0e SL |
66 | global $dbLocale; |
67 | $dbLocale = '_en_US'; | |
069b13f6 | 68 | // Test problematic queriy as per CRM-20427 |
434e2e0e SL |
69 | $query = "Select * FROM {$table}"; |
70 | $new_query = CRM_Core_I18n_Schema::rewriteQuery($query); | |
71 | $this->assertEquals("Select * FROM {$expectedRewrite}", $new_query); | |
069b13f6 | 72 | // Test query where table is not at the end |
434e2e0e SL |
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); | |
069b13f6 | 76 | // Test query where there is a 2nd table that shouldn't be re-wrten |
434e2e0e SL |
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); | |
069b13f6 SL |
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 | |
9bb55983 SL |
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) { | |
39b959db | 93 | $query6 = "SELECT " . '"' . "Fixed the the {$table} ticket" . '"'; |
9bb55983 SL |
94 | $new_query6 = CRM_Core_I18n_Schema::rewriteQuery($query6); |
95 | $this->assertEquals($query6, $new_query6); | |
96 | } | |
069b13f6 SL |
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); | |
9bb55983 SL |
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 | } | |
434e2e0e | 112 | } |
8a0b1cfd SL |
113 | |
114 | } |