Merge pull request #17557 from ngo360/master
[civicrm-core.git] / tests / phpunit / CRM / Utils / HTMLTest.php
CommitLineData
fd7dc3f3
TO
1<?php
2/*
7d61e75f
TO
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
cbcb7579 10 */
fd7dc3f3 11
fd7dc3f3
TO
12/**
13 * Tests for parsing translatable strings in HTML content.
acb109b7 14 * @group headless
fd7dc3f3
TO
15 */
16class CRM_Utils_HTMLTest extends CiviUnitTestCase {
39b959db 17
fd7dc3f3
TO
18 /**
19 * @return array
20 */
21 public function translateExamples() {
9099cab3
CW
22 $cases = [];
23 $cases[] = [
fd7dc3f3 24 '',
9099cab3
CW
25 [],
26 ];
39b959db 27 // missing ts
9099cab3 28 $cases[] = [
fd7dc3f3 29 '<div>Hello world</div>',
9099cab3
CW
30 [],
31 ];
39b959db 32 // text, no arg
9099cab3 33 $cases[] = [
fd7dc3f3 34 '<div>{{ts("Hello world")}}</div>',
9099cab3
CW
35 ['Hello world'],
36 ];
39b959db 37 // text, no arg, alternate text
9099cab3 38 $cases[] = [
fd7dc3f3 39 '<div>{{ts("Good morning, Dave")}}</div>',
9099cab3
CW
40 ['Good morning, Dave'],
41 ];
f9c5f498
CW
42 // angular one-time binding notation - great when there are no args
43 $cases[] = [
44 '<div>{{:: ts("One-time binding is more efficient!") }}</div>',
45 ['One-time binding is more efficient!'],
46 ];
39b959db 47 // text, with arg
9099cab3 48 $cases[] = [
fd7dc3f3 49 '<div>{{ts("Hello world", {1: "whiz"})}}</div>',
9099cab3
CW
50 ['Hello world'],
51 ];
39b959db 52 // text, not really ts(), no arg
9099cab3 53 $cases[] = [
fd7dc3f3 54 '<div>{{clients("Hello world")}}</div>',
9099cab3
CW
55 [],
56 ];
39b959db 57 // text, not really ts(), with arg
9099cab3 58 $cases[] = [
fd7dc3f3 59 '<div>{{clients("Hello world", {1: "whiz"})}}</div>',
9099cab3
CW
60 [],
61 ];
39b959db 62 // two strings, duplicate
9099cab3 63 $cases[] = [
fd7dc3f3 64 '<div>{{ts("Hello world")}}</div> <p>{{ts("Hello world")}}</p>',
9099cab3
CW
65 ['Hello world'],
66 ];
39b959db 67 // two strings, addition
9099cab3 68 $cases[] = [
fd7dc3f3 69 '<div>{{ts("Hello world") + "-" + ts("How do you do?")}}</p>',
9099cab3
CW
70 ['Hello world', 'How do you do?'],
71 ];
39b959db 72 // two strings, separate calls
9099cab3 73 $cases[] = [
fd7dc3f3 74 '<div>{{ts("Hello world")}}</div> <p>{{ts("How do you do?")}}</p>',
9099cab3
CW
75 ['Hello world', 'How do you do?'],
76 ];
39b959db 77 // single quoted
9099cab3 78 $cases[] = [
fd7dc3f3 79 '<div>{{ts(\'Hello world\')}}</div>',
9099cab3
CW
80 ['Hello world'],
81 ];
39b959db 82 // unclear string
9099cab3 83 $cases[] = [
fd7dc3f3 84 '<div>{{ts(message)}}</div>',
9099cab3
CW
85 [],
86 ];
39b959db 87 // ts() within a string
9099cab3 88 $cases[] = [
fd7dc3f3 89 '<div>{{ts("Does the ts(\'example\') notation work?")}}</div>',
9099cab3
CW
90 ['Does the ts(\'example\') notation work?'],
91 ];
39b959db 92 // attribute, no arg
9099cab3 93 $cases[] = [
fd7dc3f3 94 '<div crm-title="ts("Hello world")"></div>',
9099cab3
CW
95 ['Hello world'],
96 ];
39b959db 97 // attribute, with arg
9099cab3 98 $cases[] = [
fd7dc3f3 99 '<div crm-title="ts("Hello world", {1: "whiz"})"></div>',
9099cab3
CW
100 ['Hello world'],
101 ];
39b959db 102 // attribute, two strings, with arg
9099cab3 103 $cases[] = [
fd7dc3f3 104 '<div crm-title="ts("Hello world", {1: "whiz"}) + ts("How do you do, %1?", {2: "funky"})"></div>',
9099cab3
CW
105 ['Hello world', 'How do you do, %1?'],
106 ];
39b959db 107 // trick question! Not used on Smarty templates.
9099cab3 108 $cases[] = [
fd7dc3f3 109 '<div>{ts}Hello world{/ts}</div>',
9099cab3
CW
110 [],
111 ];
fd7dc3f3
TO
112
113 return $cases;
114 }
115
116 /**
117 * @param string $html
118 * Example HTML input.
119 * @param array $expectedStrings
120 * List of expected strings.
121 * @dataProvider translateExamples
122 */
123 public function testParseStrings($html, $expectedStrings) {
124 // Magic! The JS parser works with HTML!
125 $actualStrings = CRM_Utils_JS::parseStrings($html);
126 sort($expectedStrings);
127 sort($actualStrings);
128 $this->assertEquals($expectedStrings, $actualStrings);
129 }
cbcb7579 130
fd7dc3f3 131}