Merge pull request #12259 from seamuslee001/dev_core_163
[civicrm-core.git] / tests / phpunit / CRM / Utils / JSTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3+--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
6a488035 5+--------------------------------------------------------------------+
8c9251b3 6| Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
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+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
6a488035
TO
28/**
29 * Tests for linking to resource files
acb109b7 30 * @group headless
6a488035
TO
31 */
32class CRM_Utils_JSTest extends CiviUnitTestCase {
4cbe18b8
EM
33 /**
34 * @return array
35 */
00be9182 36 public function translateExamples() {
6a488035
TO
37 $cases = array();
38 $cases[] = array(
39 '',
40 array(),
41 );
481a74f4 42 $cases[] = array(// missing ts
6a488035
TO
43 'alert("Hello world")',
44 array(),
45 );
481a74f4 46 $cases[] = array(// basic function call
6a488035
TO
47 'alert(ts("Hello world"));',
48 array('Hello world'),
49 );
481a74f4 50 $cases[] = array(// with arg
6a488035
TO
51 'alert(ts("Hello world", {1: "whiz"}));',
52 array('Hello world'),
53 );
481a74f4 54 $cases[] = array(// not really ts()
6a488035
TO
55 'alert(clients("Hello world"));',
56 array(),
57 );
481a74f4 58 $cases[] = array(// not really ts()
6a488035
TO
59 'alert(clients("Hello world", {1: "whiz"}));',
60 array(),
61 );
481a74f4 62 $cases[] = array(// with arg
0c7a8599
TO
63 "\n" .
64 "public function whits() {\n" .
65 " for (a in b) {\n" .
66 " mitts(\"wallaby\", function(zoo) {\n" .
67 " alert(zoo + ts(\"Hello\"))\n" .
68 " });\n" .
69 " }\n" .
70 "}\n",
6a488035
TO
71 array('Hello'),
72 );
481a74f4 73 $cases[] = array(// duplicate
6a488035
TO
74 'alert(ts("Hello world") + "-" + ts("Hello world"));',
75 array('Hello world'),
76 );
481a74f4 77 $cases[] = array(// two strings, addition
6a488035
TO
78 'alert(ts("Hello world") + "-" + ts("How do you do?"));',
79 array('Hello world', 'How do you do?'),
80 );
481a74f4 81 $cases[] = array(// two strings, separate calls
6a488035
TO
82 'alert(ts("Hello world");\nalert(ts("How do you do?"));',
83 array('Hello world', 'How do you do?'),
84 );
85 $cases[] = array(
86 'alert(ts(\'Single quoted\'));',
87 array('Single quoted'),
88 );
481a74f4 89 $cases[] = array(// unclear string
6a488035
TO
90 'alert(ts(message));',
91 array(),
92 );
481a74f4 93 $cases[] = array(// ts() within a string
6a488035
TO
94 'alert(ts("Does the ts(\'example\') notation work?"));',
95 array('Does the ts(\'example\') notation work?'),
96 );
97 return $cases;
98 }
99
100 /**
101 * @param string $jsCode
102 * @param array $expectedStrings
103 * @dataProvider translateExamples
104 */
00be9182 105 public function testParseStrings($jsCode, $expectedStrings) {
6a488035
TO
106 $actualStrings = CRM_Utils_JS::parseStrings($jsCode);
107 sort($expectedStrings);
108 sort($actualStrings);
109 $this->assertEquals($expectedStrings, $actualStrings);
110 }
96025800 111
ad295ca9
TO
112 public function dedupeClosureExamples() {
113 // Each example string here is named for its body, eg the body of $a calls "a()".
114 $a = "(function (angular, $, _) {\na();\n})(angular, CRM.$, CRM._);";
115 $b = "(function(angular,$,_){\nb();\n})(angular,CRM.$,CRM._);";
116 $c = "(function( angular, $,_) {\nc();\n})(angular,CRM.$, CRM._);";
117 $d = "(function (angular, $, _, whiz) {\nd();\n})(angular, CRM.$, CRM._, CRM.whizbang);";
118 $m = "alert('i is the trickster (function( angular, $,_) {\nm();\n})(angular,CRM.$, CRM._);)'";
119 // Note: $d has a fundamentally different closure.
120
121 // Each example string here is a deduped combination of others,
122 // eg "$ab" is the deduping of $a+$b.
123 $ab = "(function (angular, $, _) {\na();\n\nb();\n})(angular,CRM.$,CRM._);";
124 $abc = "(function (angular, $, _) {\na();\n\nb();\n\nc();\n})(angular,CRM.$, CRM._);";
125 $cb = "(function( angular, $,_) {\nc();\n\nb();\n})(angular,CRM.$,CRM._);";
126
127 $cases = array();
128 $cases[] = array(array($a), "$a");
129 $cases[] = array(array($b), "$b");
130 $cases[] = array(array($c), "$c");
131 $cases[] = array(array($d), "$d");
132 $cases[] = array(array($m), "$m");
133 $cases[] = array(array($a, $b), "$ab");
134 $cases[] = array(array($a, $m, $b), "$a$m$b");
135 $cases[] = array(array($a, $d), "$a$d");
136 $cases[] = array(array($a, $d, $b), "$a$d$b");
137 $cases[] = array(array($a, $b, $c), "$abc");
138 $cases[] = array(array($a, $b, $d, $c, $b), "$ab$d$cb");
139 return $cases;
140 }
141
142 /**
143 * @param array $scripts
144 * @param string $expectedOutput
145 * @dataProvider dedupeClosureExamples
146 */
147 public function testDedupeClosure($scripts, $expectedOutput) {
148 $actualOutput = CRM_Utils_JS::dedupeClosures(
149 $scripts,
150 array('angular', '$', '_'),
151 array('angular', 'CRM.$', 'CRM._')
152 );
153 $this->assertEquals($expectedOutput, implode("", $actualOutput));
154 }
b047e061
TO
155
156 public function stripCommentsExamples() {
157 $cases = array();
158 $cases[] = array(
159 "a();\n//# sourceMappingURL=../foo/bar/baz.js\nb();",
160 "a();\n\nb();",
161 );
162 $cases[] = array(
163 "// foo\na();",
164 "\na();",
165 );
166 $cases[] = array(
167 "b();\n // foo",
168 "b();\n",
169 );
170 $cases[] = array(
171 "/// foo\na();\n\t \t//bar\nb();\n// whiz",
172 "\na();\n\nb();\n",
173 );
174 $cases[] = array(
175 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n//zoop\na();",
176 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n\na();",
177 );
178 return $cases;
179 }
180
181 /**
182 * @param string $input
183 * @param string $expectedOutput
184 * @dataProvider stripCommentsExamples
185 */
186 public function testStripComments($input, $expectedOutput) {
187 $this->assertEquals($expectedOutput, CRM_Utils_JS::stripComments($input));
188 }
189
6a488035 190}