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