Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-10-18-35-58
[civicrm-core.git] / tests / phpunit / CRM / Utils / JSTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3+--------------------------------------------------------------------+
4| CiviCRM version 4.3 |
5+--------------------------------------------------------------------+
6| Copyright CiviCRM LLC (c) 2004-2013 |
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Tests for linking to resource files
32 */
33class CRM_Utils_JSTest extends CiviUnitTestCase {
34 function translateExamples() {
35 $cases = array();
36 $cases[] = array(
37 '',
38 array(),
39 );
40 $cases[] = array( // missing ts
41 'alert("Hello world")',
42 array(),
43 );
44 $cases[] = array( // basic function call
45 'alert(ts("Hello world"));',
46 array('Hello world'),
47 );
48 $cases[] = array( // with arg
49 'alert(ts("Hello world", {1: "whiz"}));',
50 array('Hello world'),
51 );
52 $cases[] = array( // not really ts()
53 'alert(clients("Hello world"));',
54 array(),
55 );
56 $cases[] = array( // not really ts()
57 'alert(clients("Hello world", {1: "whiz"}));',
58 array(),
59 );
60 $cases[] = array( // with arg
61 '
62 function whits() {
63 for (a in b) {
64 mitts("wallaby", function(zoo){
65 alert(zoo + ts("Hello"))
66 });
67 }
68 }
69 ',
70 array('Hello'),
71 );
72 $cases[] = array( // duplicate
73 'alert(ts("Hello world") + "-" + ts("Hello world"));',
74 array('Hello world'),
75 );
76 $cases[] = array( // two strings, addition
77 'alert(ts("Hello world") + "-" + ts("How do you do?"));',
78 array('Hello world', 'How do you do?'),
79 );
80 $cases[] = array( // two strings, separate calls
81 'alert(ts("Hello world");\nalert(ts("How do you do?"));',
82 array('Hello world', 'How do you do?'),
83 );
84 $cases[] = array(
85 'alert(ts(\'Single quoted\'));',
86 array('Single quoted'),
87 );
88 $cases[] = array( // unclear string
89 'alert(ts(message));',
90 array(),
91 );
92 $cases[] = array( // ts() within a string
93 'alert(ts("Does the ts(\'example\') notation work?"));',
94 array('Does the ts(\'example\') notation work?'),
95 );
96 return $cases;
97 }
98
99 /**
100 * @param string $jsCode
101 * @param array $expectedStrings
102 * @dataProvider translateExamples
103 */
104 function testParseStrings($jsCode, $expectedStrings) {
105 $actualStrings = CRM_Utils_JS::parseStrings($jsCode);
106 sort($expectedStrings);
107 sort($actualStrings);
108 $this->assertEquals($expectedStrings, $actualStrings);
109 }
110}