Merge pull request #14974 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Utils / HTMLTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Tests for parsing translatable strings in HTML content.
30 * @group headless
31 */
32 class CRM_Utils_HTMLTest extends CiviUnitTestCase {
33
34 /**
35 * @return array
36 */
37 public function translateExamples() {
38 $cases = [];
39 $cases[] = [
40 '',
41 [],
42 ];
43 // missing ts
44 $cases[] = [
45 '<div>Hello world</div>',
46 [],
47 ];
48 // text, no arg
49 $cases[] = [
50 '<div>{{ts("Hello world")}}</div>',
51 ['Hello world'],
52 ];
53 // text, no arg, alternate text
54 $cases[] = [
55 '<div>{{ts("Good morning, Dave")}}</div>',
56 ['Good morning, Dave'],
57 ];
58 // text, with arg
59 $cases[] = [
60 '<div>{{ts("Hello world", {1: "whiz"})}}</div>',
61 ['Hello world'],
62 ];
63 // text, not really ts(), no arg
64 $cases[] = [
65 '<div>{{clients("Hello world")}}</div>',
66 [],
67 ];
68 // text, not really ts(), with arg
69 $cases[] = [
70 '<div>{{clients("Hello world", {1: "whiz"})}}</div>',
71 [],
72 ];
73 // two strings, duplicate
74 $cases[] = [
75 '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>',
76 ['Hello world'],
77 ];
78 // two strings, addition
79 $cases[] = [
80 '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>',
81 ['Hello world', 'How do you do?'],
82 ];
83 // two strings, separate calls
84 $cases[] = [
85 '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>',
86 ['Hello world', 'How do you do?'],
87 ];
88 // single quoted
89 $cases[] = [
90 '<div>{{ts(\'Hello world\')}}</div>',
91 ['Hello world'],
92 ];
93 // unclear string
94 $cases[] = [
95 '<div>{{ts(message)}}</div>',
96 [],
97 ];
98 // ts() within a string
99 $cases[] = [
100 '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>',
101 ['Does the ts(\'example\') notation work?'],
102 ];
103 // attribute, no arg
104 $cases[] = [
105 '<div crm-title="ts("Hello world")"></div>',
106 ['Hello world'],
107 ];
108 // attribute, with arg
109 $cases[] = [
110 '<div crm-title="ts("Hello world", {1: "whiz"})"></div>',
111 ['Hello world'],
112 ];
113 // attribute, two strings, with arg
114 $cases[] = [
115 '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>',
116 ['Hello world', 'How do you do, %1?'],
117 ];
118 // trick question! Not used on Smarty templates.
119 $cases[] = [
120 '<div>{ts}Hello world{/ts}</div>',
121 [],
122 ];
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 }
141
142 }