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