Commit | Line | Data |
---|---|---|
fd7dc3f3 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
fd7dc3f3 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
fd7dc3f3 TO |
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 | +--------------------------------------------------------------------+ | |
cbcb7579 | 26 | */ |
fd7dc3f3 | 27 | |
fd7dc3f3 TO |
28 | /** |
29 | * Tests for parsing translatable strings in HTML content. | |
acb109b7 | 30 | * @group headless |
fd7dc3f3 TO |
31 | */ |
32 | class CRM_Utils_HTMLTest extends CiviUnitTestCase { | |
39b959db | 33 | |
fd7dc3f3 TO |
34 | /** |
35 | * @return array | |
36 | */ | |
37 | public function translateExamples() { | |
9099cab3 CW |
38 | $cases = []; |
39 | $cases[] = [ | |
fd7dc3f3 | 40 | '', |
9099cab3 CW |
41 | [], |
42 | ]; | |
39b959db | 43 | // missing ts |
9099cab3 | 44 | $cases[] = [ |
fd7dc3f3 | 45 | '<div>Hello world</div>', |
9099cab3 CW |
46 | [], |
47 | ]; | |
39b959db | 48 | // text, no arg |
9099cab3 | 49 | $cases[] = [ |
fd7dc3f3 | 50 | '<div>{{ts("Hello world")}}</div>', |
9099cab3 CW |
51 | ['Hello world'], |
52 | ]; | |
39b959db | 53 | // text, no arg, alternate text |
9099cab3 | 54 | $cases[] = [ |
fd7dc3f3 | 55 | '<div>{{ts("Good morning, Dave")}}</div>', |
9099cab3 CW |
56 | ['Good morning, Dave'], |
57 | ]; | |
39b959db | 58 | // text, with arg |
9099cab3 | 59 | $cases[] = [ |
fd7dc3f3 | 60 | '<div>{{ts("Hello world", {1: "whiz"})}}</div>', |
9099cab3 CW |
61 | ['Hello world'], |
62 | ]; | |
39b959db | 63 | // text, not really ts(), no arg |
9099cab3 | 64 | $cases[] = [ |
fd7dc3f3 | 65 | '<div>{{clients("Hello world")}}</div>', |
9099cab3 CW |
66 | [], |
67 | ]; | |
39b959db | 68 | // text, not really ts(), with arg |
9099cab3 | 69 | $cases[] = [ |
fd7dc3f3 | 70 | '<div>{{clients("Hello world", {1: "whiz"})}}</div>', |
9099cab3 CW |
71 | [], |
72 | ]; | |
39b959db | 73 | // two strings, duplicate |
9099cab3 | 74 | $cases[] = [ |
fd7dc3f3 | 75 | '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>', |
9099cab3 CW |
76 | ['Hello world'], |
77 | ]; | |
39b959db | 78 | // two strings, addition |
9099cab3 | 79 | $cases[] = [ |
fd7dc3f3 | 80 | '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>', |
9099cab3 CW |
81 | ['Hello world', 'How do you do?'], |
82 | ]; | |
39b959db | 83 | // two strings, separate calls |
9099cab3 | 84 | $cases[] = [ |
fd7dc3f3 | 85 | '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>', |
9099cab3 CW |
86 | ['Hello world', 'How do you do?'], |
87 | ]; | |
39b959db | 88 | // single quoted |
9099cab3 | 89 | $cases[] = [ |
fd7dc3f3 | 90 | '<div>{{ts(\'Hello world\')}}</div>', |
9099cab3 CW |
91 | ['Hello world'], |
92 | ]; | |
39b959db | 93 | // unclear string |
9099cab3 | 94 | $cases[] = [ |
fd7dc3f3 | 95 | '<div>{{ts(message)}}</div>', |
9099cab3 CW |
96 | [], |
97 | ]; | |
39b959db | 98 | // ts() within a string |
9099cab3 | 99 | $cases[] = [ |
fd7dc3f3 | 100 | '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>', |
9099cab3 CW |
101 | ['Does the ts(\'example\') notation work?'], |
102 | ]; | |
39b959db | 103 | // attribute, no arg |
9099cab3 | 104 | $cases[] = [ |
fd7dc3f3 | 105 | '<div crm-title="ts("Hello world")"></div>', |
9099cab3 CW |
106 | ['Hello world'], |
107 | ]; | |
39b959db | 108 | // attribute, with arg |
9099cab3 | 109 | $cases[] = [ |
fd7dc3f3 | 110 | '<div crm-title="ts("Hello world", {1: "whiz"})"></div>', |
9099cab3 CW |
111 | ['Hello world'], |
112 | ]; | |
39b959db | 113 | // attribute, two strings, with arg |
9099cab3 | 114 | $cases[] = [ |
fd7dc3f3 | 115 | '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>', |
9099cab3 CW |
116 | ['Hello world', 'How do you do, %1?'], |
117 | ]; | |
39b959db | 118 | // trick question! Not used on Smarty templates. |
9099cab3 | 119 | $cases[] = [ |
fd7dc3f3 | 120 | '<div>{ts}Hello world{/ts}</div>', |
9099cab3 CW |
121 | [], |
122 | ]; | |
fd7dc3f3 TO |
123 | |
124 | return $cases; | |
125 | } | |
126 | ||
127 | /** | |
128 | * @param string $html | |
129 | * Example HTML input. | |
130 | * @param array $expectedStrings | |
131 | * List of expected strings. | |
132 | * @dataProvider translateExamples | |
133 | */ | |
134 | public function testParseStrings($html, $expectedStrings) { | |
135 | // Magic! The JS parser works with HTML! | |
136 | $actualStrings = CRM_Utils_JS::parseStrings($html); | |
137 | sort($expectedStrings); | |
138 | sort($actualStrings); | |
139 | $this->assertEquals($expectedStrings, $actualStrings); | |
140 | } | |
cbcb7579 | 141 | |
fd7dc3f3 | 142 | } |