3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Tests for linking to resource files
16 class CRM_Utils_JSTest
extends CiviUnitTestCase
{
21 public function translateExamples() {
29 'alert("Hello world")',
32 // basic function call
34 'alert(ts("Hello world"));',
39 'alert(ts("Hello world", {1: "whiz"}));',
44 'alert(clients("Hello world"));',
49 'alert(clients("Hello world", {1: "whiz"}));',
55 "public function whits() {\n" .
57 " mitts(\"wallaby\", function(zoo) {\n" .
58 " alert(zoo + ts(\"Hello\"))\n" .
66 'alert(ts("Hello world") + "-" + ts("Hello world"));',
69 // two strings, addition
71 'alert(ts("Hello world") + "-" + ts("How do you do?"));',
72 ['Hello world', 'How do you do?'],
74 // two strings, separate calls
76 'alert(ts("Hello world");\nalert(ts("How do you do?"));',
77 ['Hello world', 'How do you do?'],
80 'alert(ts(\'Single quoted\'));',
85 'alert(ts(message));',
88 // ts() within a string
90 'alert(ts("Does the ts(\'example\') notation work?"));',
91 ['Does the ts(\'example\') notation work?'],
97 * @param string $jsCode
98 * @param array $expectedStrings
99 * @dataProvider translateExamples
101 public function testParseStrings($jsCode, $expectedStrings) {
102 $actualStrings = CRM_Utils_JS
::parseStrings($jsCode);
103 sort($expectedStrings);
104 sort($actualStrings);
105 $this->assertEquals($expectedStrings, $actualStrings);
108 public function dedupeClosureExamples() {
109 // Each example string here is named for its body, eg the body of $a calls "a()".
110 $a = "(function (angular, $, _) {\na();\n})(angular, CRM.$, CRM._);";
111 $b = "(function(angular,$,_){\nb();\n})(angular,CRM.$,CRM._);";
112 $c = "(function( angular, $,_) {\nc();\n})(angular,CRM.$, CRM._);";
113 $d = "(function (angular, $, _, whiz) {\nd();\n})(angular, CRM.$, CRM._, CRM.whizbang);";
114 $m = "alert('i is the trickster (function( angular, $,_) {\nm();\n})(angular,CRM.$, CRM._);)'";
115 // Note: $d has a fundamentally different closure.
117 // Each example string here is a deduped combination of others,
118 // eg "$ab" is the deduping of $a+$b.
119 $ab = "(function (angular, $, _) {\na();\n\nb();\n})(angular,CRM.$,CRM._);";
120 $abc = "(function (angular, $, _) {\na();\n\nb();\n\nc();\n})(angular,CRM.$, CRM._);";
121 $cb = "(function( angular, $,_) {\nc();\n\nb();\n})(angular,CRM.$,CRM._);";
124 $cases[] = [[$a], "$a"];
125 $cases[] = [[$b], "$b"];
126 $cases[] = [[$c], "$c"];
127 $cases[] = [[$d], "$d"];
128 $cases[] = [[$m], "$m"];
129 $cases[] = [[$a, $b], "$ab"];
130 $cases[] = [[$a, $m, $b], "$a$m$b"];
131 $cases[] = [[$a, $d], "$a$d"];
132 $cases[] = [[$a, $d, $b], "$a$d$b"];
133 $cases[] = [[$a, $b, $c], "$abc"];
134 $cases[] = [[$a, $b, $d, $c, $b], "$ab$d$cb"];
139 * @param array $scripts
140 * @param string $expectedOutput
141 * @dataProvider dedupeClosureExamples
143 public function testDedupeClosure($scripts, $expectedOutput) {
144 $actualOutput = CRM_Utils_JS
::dedupeClosures(
146 ['angular', '$', '_'],
147 ['angular', 'CRM.$', 'CRM._']
149 $this->assertEquals($expectedOutput, implode("", $actualOutput));
152 public function stripCommentsExamples() {
155 "a();\n//# sourceMappingURL=../foo/bar/baz.js\nb();",
167 "/// foo\na();\n\t \t//bar\nb();\n// whiz",
171 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n//zoop\na();",
172 "alert('//# sourceMappingURL=../foo/bar/baz.js');\n\na();",
178 * @param string $input
179 * @param string $expectedOutput
180 * @dataProvider stripCommentsExamples
182 public function testStripComments($input, $expectedOutput) {
183 $this->assertEquals($expectedOutput, CRM_Utils_JS
::stripComments($input));
186 public static function decodeExamples() {
188 ['{a: \'Apple\', \'b\': "Banana", c: [1, 2, 3]}', ['a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3]]],
201 '{a: "parse error no closing bracket"',
205 '{a: ["foo", \'bar\'], "b": {a: [\'foo\', "bar"], b: {\'a\': ["foo", "bar"], b: {}}}}',
206 ['a' => ['foo', 'bar'], 'b' => ['a' => ['foo', 'bar'], 'b' => ['a' => ['foo', 'bar'], 'b' => []]]],
209 ' [{a: {aa: true}, b: [false, null, {x: 1, y: 2, z: 3}] , "c": -1}, ["fee", "fie", \'foe\']]',
210 [['a' => ['aa' => TRUE], 'b' => [FALSE, NULL, ['x' => 1, 'y' => 2, 'z' => 3]], "c" => -1], ["fee", "fie", "foe"]],
216 * @param string $input
217 * @param string $expectedOutput
218 * @dataProvider decodeExamples
220 public function testDecode($input, $expectedOutput) {
221 $this->assertEquals($expectedOutput, CRM_Utils_JS
::decode($input));
224 public static function encodeExamples() {
227 ['a' => 'Apple', 'b' => 'Banana', 'c' => [0, -2, 3.15]],
228 "{a: 'Apple', b: 'Banana', c: [0, -2, 3.15]}",
231 ['a' => ['foo', 'bar'], 'b' => ["'a'" => ['foo/bar&', 'bar(foo)'], 'b' => ['a' => ["fo\\\\'oo", '"bar"'], 'b' => []]]],
232 "{a: ['foo', 'bar'], b: {\"'a'\": ['foo/bar&', 'bar(foo)'], b: {a: ['fo\\\\\\\\\\'oo', '\"bar\"'], b: {}}}}",
239 ['"false"', "'\"false\"'"],
247 * @param string $input
248 * @param string $expectedOutput
249 * @dataProvider encodeExamples
251 public function testEncode($input, $expectedOutput) {
252 $result = CRM_Utils_JS
::encode($input);
253 $this->assertEquals($expectedOutput, $result);
254 $this->assertEquals($input, CRM_Utils_JS
::decode($result));
260 public static function objectExamples() {
263 '{a: \'Apple\', \'b\': "Banana", "c ": [1,2,3]}',
264 ['a' => "'Apple'", 'b' => '"Banana"', 'c ' => '[1,2,3]'],
265 '{a: \'Apple\', b: "Banana", \'c \': [1,2,3]}',
278 " {'fn' : function (foo, bar, baz) { return \"One, two, three\"; }, esc: /[1-9]\\\\/.test('5\\\\') , number : 55.5/2 } ",
279 ['fn' => 'function (foo, bar, baz) { return "One, two, three"; }', 'esc' => "/[1-9]\\\\/.test('5\\\\')", 'number' => '55.5/2'],
280 "{fn: function (foo, bar, baz) { return \"One, two, three\"; }, esc: /[1-9]\\\\/.test('5\\\\'), number: 55.5/2}",
284 'this, has(some : weird, \\'stuff [{}!' ,
285 expr: sum(1, 2, 3) / 2 + 1, ' notes ' : [Do, re mi],
287 ['string' => "'this, has(some : weird, \\'stuff [{}!'", 'expr' => 'sum(1, 2, 3) / 2 + 1', ' notes ' => "[Do, re mi]"],
288 "{string: 'this, has(some : weird, \\'stuff [{}!', expr: sum(1, 2, 3) / 2 + 1, ' notes ': [Do, re mi]}",
291 '{status: /^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\' , \'foo\&\': getFoo("Some \"quoted\" thing"), "ba\'[(r": function() {return "bar"}}',
292 ['status' => '/^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\'', 'foo&' => 'getFoo("Some \"quoted\" thing")', "ba'[(r" => 'function() {return "bar"}'],
293 '{status: /^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\', "foo&": getFoo("Some \"quoted\" thing"), "ba\'[(r": function() {return "bar"}}',
296 '{"some\"key": typeof foo === \'number\' ? true : false , "O\'Really?": ",((,", \'A"quote"\': 1 + 1 , "\\\\\\&\\/" : 0}',
297 ['some"key' => 'typeof foo === \'number\' ? true : false', "O'Really?" => '",((,"', 'A"quote"' => '1 + 1', '\\&/' => '0'],
298 '{\'some"key\': typeof foo === \'number\' ? true : false, "O\'Really?": ",((,", \'A"quote"\': 1 + 1, "\\\\&/": 0}',
301 '[foo ? 1 : 2 , 3 , function() {return 1 + 1;}, /^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\' , 3.14 ]',
302 ['foo ? 1 : 2', '3', 'function() {return 1 + 1;}', '/^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\'', '3.14'],
303 '[foo ? 1 : 2, 3, function() {return 1 + 1;}, /^http:\/\/civicrm\.com/.test(url) ? \'good\' : \'bad\', 3.14]',
309 * Test converting a js string to a php array and back again.
311 * @param string $input
312 * @param string $expectedPHP
314 * @dataProvider objectExamples
316 public function testObjectToAndFromString($input, $expectedPHP, $expectedJS) {
317 $objectProps = CRM_Utils_JS
::getRawProps($input);
318 $this->assertEquals($expectedPHP, $objectProps);
319 $reformattedJS = CRM_Utils_JS
::writeObject($objectProps);
320 $this->assertEquals($expectedJS, $reformattedJS);
321 $this->assertEquals($expectedPHP, CRM_Utils_JS
::getRawProps($reformattedJS));