Fix token subscriber to format the display of the custom tokens
[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 * @throws \CRM_Core_Exception
44 */
45 public function testI18nSchemaRewrite($table, $expectedRewrite) {
46 CRM_Core_I18n_Schema::makeMultilingual('en_US');
47 $domains = $this->callAPISuccess('Domain', 'get')['values'];
48 $this->assertGreaterThan(1, count($domains));
49 foreach ($domains as $domain) {
50 // If the DB is multilingual the locales value must be not-null for all domains
51 // to ensure the db can be accessed (I suspect it must be the same for all locales but
52 // if null then the database layer attempts to access non-existent fields 'label' not label_en_us'.
53 $this->assertEquals('en_US', $domain['locales']);
54 }
55 $skip_tests = FALSE;
56 if (in_array($table, ['civicrm_option_group', 'civicrm_event'])) {
57 $skip_tests = TRUE;
58 }
59 global $dbLocale;
60 $dbLocale = '_en_US';
61 // Test problematic queriy as per CRM-20427
62 $query = "Select * FROM {$table}";
63 $new_query = CRM_Core_I18n_Schema::rewriteQuery($query);
64 $this->assertEquals("Select * FROM {$expectedRewrite}", $new_query);
65 // Test query where table is not at the end
66 $query2 = "Select * FROM {$table} LIMIT 1";
67 $new_query2 = CRM_Core_I18n_Schema::rewriteQuery($query2);
68 $this->assertEquals("Select * FROM {$expectedRewrite} LIMIT 1", $new_query2);
69 // Test query where there is a 2nd table that shouldn't be re-wrten
70 $query3 = "SELECT * FROM {$table} JOIN civicrm_contact LIMIT 1";
71 $new_query3 = CRM_Core_I18n_Schema::rewriteQuery($query3);
72 $this->assertEquals("SELECT * FROM {$expectedRewrite} JOIN civicrm_contact LIMIT 1", $new_query3);
73 // Test table when name is escaped
74 $query4 = "SELECT * FROM `{$table}` WHERE id = 123";
75 $new_query4 = CRM_Core_I18n_Schema::rewriteQuery($query4);
76 $this->assertEquals("SELECT * FROM `{$expectedRewrite}` WHERE id = 123", $new_query4);
77 // Test where translatable table is quoted
78 // The `$table` appears in a string -- it should not be rewritten.
79 $query5 = 'SELECT id FROM civicrm_activity WHERE subject = "civicrm_option_group"';
80 $new_query5 = CRM_Core_I18n_Schema::rewriteQuery($query5);
81 $this->assertEquals($query5, $new_query5);
82 // Test where table is not the last thing to be in a quoted string
83 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
84 // Agreed as not a blocker for CRM-20427 as an issue previously.
85 if (!$skip_tests) {
86 $query6 = "SELECT " . '"' . "Fixed the the {$table} ticket" . '"';
87 $new_query6 = CRM_Core_I18n_Schema::rewriteQuery($query6);
88 $this->assertEquals($query6, $new_query6);
89 }
90 // Test where table is part of a sub query
91 $query7 = "SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$table})";
92 $new_query7 = CRM_Core_I18n_Schema::rewriteQuery($query7);
93 $this->assertEquals("SELECT * FROM civicrm_foo WHERE foo_id = (SELECT value FROM {$expectedRewrite})", $new_query7);
94 // Test differern verbs
95 $query8 = "DELETE FROM {$table}";
96 $new_query8 = CRM_Core_I18n_Schema::rewriteQuery($query8);
97 $this->assertEquals("DELETE FROM {$expectedRewrite}", $new_query8);
98 // Test Currently skipped for civicrm_option_group and civicrm_event due to issues with the regex.
99 // Agreed as not a blocker for CRM-20427 as an issue previously
100 if (!$skip_tests) {
101 $query9 = 'INSERT INTO ' . "{$table}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")';
102 $new_query9 = CRM_Core_I18n_Schema::rewriteQuery($query9);
103 $this->assertEquals('INSERT INTO ' . "{$expectedRewrite}" . ' (foo, bar) VALUES (123, "' . "Just a {$table} string" . '")', $new_query9);
104 }
105 }
106
107 public function testSchemaBuild() {
108 CRM_Core_I18n_Schema::makeMultilingual('en_US');
109 $inUseCollation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
110 $testCreateTable = CRM_Core_DAO::executeQuery("show create table civicrm_price_set", [], TRUE, NULL, FALSE, FALSE);
111 while ($testCreateTable->fetch()) {
112 $this->assertContains("`title_en_US` varchar(255) COLLATE {$inUseCollation} NOT NULL COMMENT 'Displayed title for the Price Set.'", $testCreateTable->Create_Table);
113 $this->assertContains("`help_pre_en_US` text COLLATE {$inUseCollation} COMMENT 'Description and/or help text to display before fields in form.'", $testCreateTable->Create_Table);
114 }
115 }
116
117 }