| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 linking to resource files |
| 30 | * @group headless |
| 31 | */ |
| 32 | class CRM_Utils_JSTest extends CiviUnitTestCase { |
| 33 | /** |
| 34 | * @return array |
| 35 | */ |
| 36 | public function translateExamples() { |
| 37 | $cases = array(); |
| 38 | $cases[] = array( |
| 39 | '', |
| 40 | array(), |
| 41 | ); |
| 42 | $cases[] = array(// missing ts |
| 43 | 'alert("Hello world")', |
| 44 | array(), |
| 45 | ); |
| 46 | $cases[] = array(// basic function call |
| 47 | 'alert(ts("Hello world"));', |
| 48 | array('Hello world'), |
| 49 | ); |
| 50 | $cases[] = array(// with arg |
| 51 | 'alert(ts("Hello world", {1: "whiz"}));', |
| 52 | array('Hello world'), |
| 53 | ); |
| 54 | $cases[] = array(// not really ts() |
| 55 | 'alert(clients("Hello world"));', |
| 56 | array(), |
| 57 | ); |
| 58 | $cases[] = array(// not really ts() |
| 59 | 'alert(clients("Hello world", {1: "whiz"}));', |
| 60 | array(), |
| 61 | ); |
| 62 | $cases[] = array(// with arg |
| 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", |
| 71 | array('Hello'), |
| 72 | ); |
| 73 | $cases[] = array(// duplicate |
| 74 | 'alert(ts("Hello world") + "-" + ts("Hello world"));', |
| 75 | array('Hello world'), |
| 76 | ); |
| 77 | $cases[] = array(// two strings, addition |
| 78 | 'alert(ts("Hello world") + "-" + ts("How do you do?"));', |
| 79 | array('Hello world', 'How do you do?'), |
| 80 | ); |
| 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?'), |
| 84 | ); |
| 85 | $cases[] = array( |
| 86 | 'alert(ts(\'Single quoted\'));', |
| 87 | array('Single quoted'), |
| 88 | ); |
| 89 | $cases[] = array(// unclear string |
| 90 | 'alert(ts(message));', |
| 91 | array(), |
| 92 | ); |
| 93 | $cases[] = array(// ts() within a string |
| 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 | */ |
| 105 | public function testParseStrings($jsCode, $expectedStrings) { |
| 106 | $actualStrings = CRM_Utils_JS::parseStrings($jsCode); |
| 107 | sort($expectedStrings); |
| 108 | sort($actualStrings); |
| 109 | $this->assertEquals($expectedStrings, $actualStrings); |
| 110 | } |
| 111 | |
| 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 | } |
| 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 | |
| 190 | } |