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