3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Tests for linking to resource files
32 class CRM_Utils_JSTest
extends CiviUnitTestCase
{
36 public function translateExamples() {
42 $cases[] = array(// missing ts
43 'alert("Hello world")',
46 $cases[] = array(// basic function call
47 'alert(ts("Hello world"));',
50 $cases[] = array(// with arg
51 'alert(ts("Hello world", {1: "whiz"}));',
54 $cases[] = array(// not really ts()
55 'alert(clients("Hello world"));',
58 $cases[] = array(// not really ts()
59 'alert(clients("Hello world", {1: "whiz"}));',
62 $cases[] = array(// with arg
64 "public function whits() {\n" .
66 " mitts(\"wallaby\", function(zoo) {\n" .
67 " alert(zoo + ts(\"Hello\"))\n" .
73 $cases[] = array(// duplicate
74 'alert(ts("Hello world") + "-" + ts("Hello world"));',
77 $cases[] = array(// two strings, addition
78 'alert(ts("Hello world") + "-" + ts("How do you do?"));',
79 array('Hello world', 'How do you do?'),
81 $cases[] = array(// two strings, separate calls
82 'alert(ts("Hello world");\nalert(ts("How do you do?"));',
83 array('Hello world', 'How do you do?'),
86 'alert(ts(\'Single quoted\'));',
87 array('Single quoted'),
89 $cases[] = array(// unclear string
90 'alert(ts(message));',
93 $cases[] = array(// ts() within a string
94 'alert(ts("Does the ts(\'example\') notation work?"));',
95 array('Does the ts(\'example\') notation work?'),
101 * @param string $jsCode
102 * @param array $expectedStrings
103 * @dataProvider translateExamples
105 public function testParseStrings($jsCode, $expectedStrings) {
106 $actualStrings = CRM_Utils_JS
::parseStrings($jsCode);
107 sort($expectedStrings);
108 sort($actualStrings);
109 $this->assertEquals($expectedStrings, $actualStrings);
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.
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._);";
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");
143 * @param array $scripts
144 * @param string $expectedOutput
145 * @dataProvider dedupeClosureExamples
147 public function testDedupeClosure($scripts, $expectedOutput) {
148 $actualOutput = CRM_Utils_JS
::dedupeClosures(
150 array('angular', '$', '_'),
151 array('angular', 'CRM.$', 'CRM._')
153 $this->assertEquals($expectedOutput, implode("", $actualOutput));
156 public function stripCommentsExamples() {
159 "a();\n//# sourceMappingURL=../foo/bar/baz.js\nb();",
171 "/// foo\na();\n\t \t//bar\nb();\n// whiz",
175 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n//zoop\na();",
176 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n\na();",
182 * @param string $input
183 * @param string $expectedOutput
184 * @dataProvider stripCommentsExamples
186 public function testStripComments($input, $expectedOutput) {
187 $this->assertEquals($expectedOutput, CRM_Utils_JS
::stripComments($input));