Merge pull request #15815 from artfulrobot/issue-1108-fix-unsubscribe
[civicrm-core.git] / tests / phpunit / CRM / Utils / HTMLTest.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 /**
13 * Tests for parsing translatable strings in HTML content.
14 * @group headless
15 */
16 class CRM_Utils_HTMLTest extends CiviUnitTestCase {
17
18 /**
19 * @return array
20 */
21 public function translateExamples() {
22 $cases = [];
23 $cases[] = [
24 '',
25 [],
26 ];
27 // missing ts
28 $cases[] = [
29 '<div>Hello world</div>',
30 [],
31 ];
32 // text, no arg
33 $cases[] = [
34 '<div>{{ts("Hello world")}}</div>',
35 ['Hello world'],
36 ];
37 // text, no arg, alternate text
38 $cases[] = [
39 '<div>{{ts("Good morning, Dave")}}</div>',
40 ['Good morning, Dave'],
41 ];
42 // text, with arg
43 $cases[] = [
44 '<div>{{ts("Hello world", {1: "whiz"})}}</div>',
45 ['Hello world'],
46 ];
47 // text, not really ts(), no arg
48 $cases[] = [
49 '<div>{{clients("Hello world")}}</div>',
50 [],
51 ];
52 // text, not really ts(), with arg
53 $cases[] = [
54 '<div>{{clients("Hello world", {1: "whiz"})}}</div>',
55 [],
56 ];
57 // two strings, duplicate
58 $cases[] = [
59 '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>',
60 ['Hello world'],
61 ];
62 // two strings, addition
63 $cases[] = [
64 '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>',
65 ['Hello world', 'How do you do?'],
66 ];
67 // two strings, separate calls
68 $cases[] = [
69 '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>',
70 ['Hello world', 'How do you do?'],
71 ];
72 // single quoted
73 $cases[] = [
74 '<div>{{ts(\'Hello world\')}}</div>',
75 ['Hello world'],
76 ];
77 // unclear string
78 $cases[] = [
79 '<div>{{ts(message)}}</div>',
80 [],
81 ];
82 // ts() within a string
83 $cases[] = [
84 '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>',
85 ['Does the ts(\'example\') notation work?'],
86 ];
87 // attribute, no arg
88 $cases[] = [
89 '<div crm-title="ts("Hello world")"></div>',
90 ['Hello world'],
91 ];
92 // attribute, with arg
93 $cases[] = [
94 '<div crm-title="ts("Hello world", {1: "whiz"})"></div>',
95 ['Hello world'],
96 ];
97 // attribute, two strings, with arg
98 $cases[] = [
99 '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>',
100 ['Hello world', 'How do you do, %1?'],
101 ];
102 // trick question! Not used on Smarty templates.
103 $cases[] = [
104 '<div>{ts}Hello world{/ts}</div>',
105 [],
106 ];
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 }
125
126 }