Merge pull request #17143 from mattwire/donthidedisabledmemberships
[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 // angular one-time binding notation - great when there are no args
43 $cases[] = [
44 '<div>{{:: ts("One-time binding is more efficient!") }}</div>',
45 ['One-time binding is more efficient!'],
46 ];
47 // text, with arg
48 $cases[] = [
49 '<div>{{ts("Hello world", {1: "whiz"})}}</div>',
50 ['Hello world'],
51 ];
52 // text, not really ts(), no arg
53 $cases[] = [
54 '<div>{{clients("Hello world")}}</div>',
55 [],
56 ];
57 // text, not really ts(), with arg
58 $cases[] = [
59 '<div>{{clients("Hello world", {1: "whiz"})}}</div>',
60 [],
61 ];
62 // two strings, duplicate
63 $cases[] = [
64 '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>',
65 ['Hello world'],
66 ];
67 // two strings, addition
68 $cases[] = [
69 '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>',
70 ['Hello world', 'How do you do?'],
71 ];
72 // two strings, separate calls
73 $cases[] = [
74 '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>',
75 ['Hello world', 'How do you do?'],
76 ];
77 // single quoted
78 $cases[] = [
79 '<div>{{ts(\'Hello world\')}}</div>',
80 ['Hello world'],
81 ];
82 // unclear string
83 $cases[] = [
84 '<div>{{ts(message)}}</div>',
85 [],
86 ];
87 // ts() within a string
88 $cases[] = [
89 '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>',
90 ['Does the ts(\'example\') notation work?'],
91 ];
92 // attribute, no arg
93 $cases[] = [
94 '<div crm-title="ts("Hello world")"></div>',
95 ['Hello world'],
96 ];
97 // attribute, with arg
98 $cases[] = [
99 '<div crm-title="ts("Hello world", {1: "whiz"})"></div>',
100 ['Hello world'],
101 ];
102 // attribute, two strings, with arg
103 $cases[] = [
104 '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>',
105 ['Hello world', 'How do you do, %1?'],
106 ];
107 // trick question! Not used on Smarty templates.
108 $cases[] = [
109 '<div>{ts}Hello world{/ts}</div>',
110 [],
111 ];
112
113 return $cases;
114 }
115
116 /**
117 * @param string $html
118 * Example HTML input.
119 * @param array $expectedStrings
120 * List of expected strings.
121 * @dataProvider translateExamples
122 */
123 public function testParseStrings($html, $expectedStrings) {
124 // Magic! The JS parser works with HTML!
125 $actualStrings = CRM_Utils_JS::parseStrings($html);
126 sort($expectedStrings);
127 sort($actualStrings);
128 $this->assertEquals($expectedStrings, $actualStrings);
129 }
130
131 }