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