Merge pull request #14320 from sushantpaste/reporthook
[civicrm-core.git] / tests / phpunit / Civi / Angular / ChangeSetTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 namespace Civi\Angular;
29
30 /**
31 * Test the reading-writing of HTML snippets.
32 */
33 class ChangeSetTest extends \CiviUnitTestCase {
34
35 /**
36 * Insert content using after().
37 */
38 public function testInsertAfter() {
39 $changeSet = ChangeSet::create(__FUNCTION__);
40 $counts = ['~/foo.html' => 0];
41
42 $changeSet->alterHtml('~/foo.html', function (\phpQueryObject $doc, $file) use (&$counts) {
43 $counts[$file]++;
44 $doc->find('.foo')->after('<p ng-if="alpha.beta() && true">world</p>');
45 });
46 $changeSet->alterHtml('~/f*.html', function (\phpQueryObject $doc, $file) use (&$counts) {
47 $counts[$file]++;
48 $doc->find('.bar')->after('<p>cruel world</p>');
49 });
50 $changeSet->alterHtml('/path/does/not/exist.html', function(\phpQueryObject $doc) {
51 throw new \Exception("This should not be called. The file does not exist!");
52 });
53
54 $results = ChangeSet::applyResourceFilters([$changeSet], 'partials', [
55 '~/foo.html' => '<span><p class="foo">Hello</p><p class="bar">Goodbye</p></span>',
56 ]);
57
58 $this->assertHtmlEquals(
59 '<span><p class="foo">Hello</p><p ng-if="alpha.beta() && true">world</p><p class="bar">Goodbye</p><p>cruel world</p></span>',
60 $results['~/foo.html']
61 );
62 $this->assertEquals(2, $counts['~/foo.html']);
63 }
64
65 /**
66 * Insert content using append() and prepend().
67 */
68 public function testAppendPrepend() {
69 $changeSet = ChangeSet::create(__FUNCTION__);
70 $counts = ['~/foo.html' => 0];
71
72 $changeSet->alterHtml('~/foo.html', function (\phpQueryObject $doc, $file) use (&$counts) {
73 $counts[$file]++;
74 $doc->find('.foo')->append('<p ng-if="!!gamma()">world</p>');
75 });
76 $changeSet->alterHtml('~/*.html', function (\phpQueryObject $doc, $file) use (&$counts) {
77 $counts[$file]++;
78 $doc->find('.bar')->prepend('<span>Cruel world,</span>');
79 });
80 $changeSet->alterHtml('/path/does/not/exist.html', function(\phpQueryObject $doc) {
81 throw new \Exception("This should not be called. The file does not exist!");
82 });
83
84 $originals = [
85 '~/foo.html' => '<span><p class="foo">Hello</p><p class="bar">Goodbye</p></span>',
86 ];
87 $results = ChangeSet::applyResourceFilters([$changeSet], 'partials', $originals);
88
89 $this->assertHtmlEquals(
90 '<span><p class="foo">Hello<p ng-if="!!gamma()">world</p></p><p class="bar"><span>Cruel world,</span>Goodbye</p></span>',
91 $results['~/foo.html']
92 );
93 $this->assertEquals(2, $counts['~/foo.html']);
94 }
95
96 protected function assertHtmlEquals($expected, $actual, $message = '') {
97 $expected = preg_replace(';>[ \r\n\t]+;', '>', $expected);
98 $actual = preg_replace(';>[ \r\n\t]+;', '>', $actual);
99 $this->assertEquals($expected, $actual, $message);
100 }
101
102 }