(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / tests / phpunit / Civi / Angular / PartialSyntaxTest.php
1 <?php
2 /*
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 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\Angular;
13
14 /**
15 * Ensure that all Angular *.html partials are well-formed.
16 */
17 class PartialSyntaxTest extends \CiviUnitTestCase {
18
19 /**
20 * @var Manager
21 */
22 protected $angular;
23
24 /**
25 * @var \CRM_Core_Resources
26 */
27 protected $res;
28
29 /**
30 * @inheritDoc
31 */
32 protected function setUp() {
33 $this->useTransaction(TRUE);
34 parent::setUp();
35 $this->createLoggedInUser();
36 $this->res = \CRM_Core_Resources::singleton();
37 $this->angular = new Manager($this->res);
38 }
39
40 public function basicConsistencyExamples() {
41 $cases = [];
42
43 $cases[0] = [
44 '<div foo="bar"></div>',
45 '<div foo="bar"></div>',
46 ];
47 $cases[1] = [
48 '<div foo="bar"/>',
49 '<div foo="bar"></div>',
50 ];
51 $cases[2] = [
52 '<div foo=\'bar\'></div>',
53 '<div foo="bar"></div>',
54 ];
55 $cases[3] = [
56 '<div foo=\'ts("Hello world")\'></div>',
57 '<div foo=\'ts("Hello world")\'></div>',
58 ];
59 $cases[4] = [
60 '<div foo="ts(\'Hello world\')\"></div>',
61 '<div foo="ts(\'Hello world\')\"></div>',
62 ];
63 $cases[5] = [
64 '<a href="{{foo}}" title="{{bar}}"></a>',
65 '<a href="{{foo}}" title="{{bar}}"></a>',
66 ];
67 $cases[6] = [
68 '<div ng-if="a && b"></div>',
69 '<div ng-if="a && b"></div>',
70 ];
71
72 return $cases;
73 }
74
75 /**
76 * @param string $inputHtml
77 * @param string $expectHtml
78 * @dataProvider basicConsistencyExamples
79 */
80 public function testConsistencyExamples($inputHtml, $expectHtml) {
81 $coder = new Coder();
82 $this->assertEquals($expectHtml, $coder->recode($inputHtml));
83 }
84
85 /**
86 */
87 public function testAllPartials() {
88 $coder = new \Civi\Angular\Coder();
89 $errors = [];
90 $count = 0;
91 foreach ($this->angular->getModules() as $module => $moduleDefn) {
92 $partials = $this->angular->getPartials($module);
93 foreach ($partials as $path => $html) {
94 $count++;
95 if (!$coder->checkConsistentHtml($html)) {
96 $recodedHtml = $coder->recode($html);
97 $this->assertEquals($html, $recodedHtml, "File $path has inconsistent HTML. Use tools/scripts/check-angular.php to debug. ");
98 }
99 }
100 }
101
102 $this->assertTrue($count > 0);
103 }
104
105 }