CRM-15832 - CRM_Core_Resources - Move translateScript to class. Add HTML support.
[civicrm-core.git] / tests / phpunit / CRM / Utils / HTMLTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Tests for parsing translatable strings in HTML content.
32 */
33 class CRM_Utils_HTMLTest extends CiviUnitTestCase {
34 /**
35 * @return array
36 */
37 public function translateExamples() {
38 $cases = array();
39 $cases[] = array(
40 '',
41 array(),
42 );
43 $cases[] = array(// missing ts
44 '<div>Hello world</div>',
45 array(),
46 );
47 $cases[] = array(// text, no arg
48 '<div>{{ts("Hello world")}}</div>',
49 array('Hello world'),
50 );
51 $cases[] = array(// text, no arg, alternate text
52 '<div>{{ts("Good morning, Dave")}}</div>',
53 array('Good morning, Dave'),
54 );
55 $cases[] = array(// text, with arg
56 '<div>{{ts("Hello world", {1: "whiz"})}}</div>',
57 array('Hello world'),
58 );
59 $cases[] = array(// text, not really ts(), no arg
60 '<div>{{clients("Hello world")}}</div>',
61 array(),
62 );
63 $cases[] = array(// text, not really ts(), with arg
64 '<div>{{clients("Hello world", {1: "whiz"})}}</div>',
65 array(),
66 );
67 $cases[] = array(// two strings, duplicate
68 '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>',
69 array('Hello world'),
70 );
71 $cases[] = array(// two strings, addition
72 '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>',
73 array('Hello world', 'How do you do?'),
74 );
75 $cases[] = array(// two strings, separate calls
76 '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>',
77 array('Hello world', 'How do you do?'),
78 );
79 $cases[] = array(// single quoted
80 '<div>{{ts(\'Hello world\')}}</div>',
81 array('Hello world'),
82 );
83 $cases[] = array(// unclear string
84 '<div>{{ts(message)}}</div>',
85 array(),
86 );
87 $cases[] = array(// ts() within a string
88 '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>',
89 array('Does the ts(\'example\') notation work?'),
90 );
91 $cases[] = array(// attribute, no arg
92 '<div crm-title="ts("Hello world")"></div>',
93 array('Hello world'),
94 );
95 $cases[] = array(// attribute, with arg
96 '<div crm-title="ts("Hello world", {1: "whiz"})"></div>',
97 array('Hello world'),
98 );
99 $cases[] = array(// attribute, two strings, with arg
100 '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>',
101 array('Hello world', 'How do you do, %1?'),
102 );
103 $cases[] = array(// trick question! Not used on Smarty templates.
104 '<div>{ts}Hello world{/ts}</div>',
105 array(),
106 );
107
108 return $cases;
109 }
110
111 /**
112 * @param string $html
113 * Example HTML input.
114 * @param array $expectedStrings
115 * List of expected strings.
116 * @dataProvider translateExamples
117 */
118 public function testParseStrings($html, $expectedStrings) {
119 // Magic! The JS parser works with HTML!
120 $actualStrings = CRM_Utils_JS::parseStrings($html);
121 sort($expectedStrings);
122 sort($actualStrings);
123 $this->assertEquals($expectedStrings, $actualStrings);
124 }
125 }