3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 * Class CRM_Core_I18n_SchemaTest
15 class CRM_Core_I18n_SchemaTest
extends CiviUnitTestCase
{
18 * Test tables to translate
21 public static function translateTables() {
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'];
29 public function setUp() {
33 public function tearDown() {
34 CRM_Core_I18n_Schema
::makeSinglelingual('en_US');
39 * @param string $table
40 * @param string $expectedRewrite
42 * @dataProvider translateTables
44 public function testI18nSchemaRewrite($table, $expectedRewrite) {
45 CRM_Core_I18n_Schema
::makeMultilingual('en_US');
47 if (in_array($table, ['civicrm_option_group', 'civicrm_event'])) {
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.
77 $query6 = "SELECT " . '"' . "Fixed the the {$table} ticket" . '"';
78 $new_query6 = CRM_Core_I18n_Schema
::rewriteQuery($query6);
79 $this->assertEquals($query6, $new_query6);
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
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);
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
);