Merge pull request #15841 from mattwire/participant_cleanup_removeparticipantfrominput
[civicrm-core.git] / tests / phpunit / Civi / Angular / ManagerTest.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 * Test the Angular base page.
16 */
17 class ManagerTest 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 /**
41 * Modules appear to be well-defined.
42 */
43 public function testGetModules() {
44 $modules = $this->angular->getModules();
45
46 $counts = [
47 'js' => 0,
48 'css' => 0,
49 'partials' => 0,
50 'settings' => 0,
51 ];
52
53 foreach ($modules as $module) {
54 $this->assertTrue(is_array($module));
55 $this->assertTrue(is_string($module['ext']));
56 if (isset($module['js'])) {
57 $this->assertTrue(is_array($module['js']));
58 foreach ($module['js'] as $file) {
59 $this->assertTrue(file_exists($this->res->getPath($module['ext'], $file)));
60 $counts['js']++;
61 }
62 }
63 if (isset($module['css'])) {
64 $this->assertTrue(is_array($module['css']));
65 foreach ($module['css'] as $file) {
66 $this->assertTrue(file_exists($this->res->getPath($module['ext'], $file)));
67 $counts['css']++;
68 }
69 }
70 if (isset($module['partials'])) {
71 $this->assertTrue(is_array($module['partials']));
72 foreach ((array) $module['partials'] as $basedir) {
73 $this->assertTrue(is_dir($this->res->getPath($module['ext']) . '/' . $basedir));
74 $counts['partials']++;
75 }
76 }
77 if (isset($module['settings'])) {
78 $this->assertTrue(is_array($module['settings']));
79 foreach ($module['settings'] as $name => $value) {
80 $counts['settings']++;
81 }
82 }
83 }
84
85 $this->assertTrue($counts['js'] > 0, 'Expect to find at least one JS file');
86 $this->assertTrue($counts['css'] > 0, 'Expect to find at least one CSS file');
87 $this->assertTrue($counts['partials'] > 0, 'Expect to find at least one partial HTML file');
88 $this->assertTrue($counts['settings'] > 0, 'Expect to find at least one setting');
89 }
90
91 /**
92 * Get HTML fragments from an example module.
93 */
94 public function testGetPartials() {
95 $partials = $this->angular->getPartials('crmMailing');
96 $this->assertRegExp('/ng-form="crmMailingSubform">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
97 // If crmMailing changes, feel free to use a different example.
98 }
99
100 /**
101 * Get HTML fragments from an example module. The HTML is modified via hook.
102 */
103 public function testGetPartials_Hooked() {
104 \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
105
106 $partials = $this->angular->getPartials('crmMailing');
107 $this->assertRegExp('/ng-form="crmMailingSubform" cat-stevens="ts\\(\'wild world\'\\)">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
108 // If crmMailing changes, feel free to use a different example.
109 }
110
111 public function testGetJs_Asset() {
112 \CRM_Utils_Hook::singleton()->setHook('civicrm_angularModules', [$this, 'hook_civicrm_angularModules_fooBar']);
113
114 $paths = $this->angular->getResources(['fooBar'], 'js', 'path');
115 $this->assertRegExp('/visual-bundle.[a-z0-9]+.js/', $paths[0]);
116 $this->assertRegExp('/crossfilter/', file_get_contents($paths[0]));
117
118 $this->assertRegExp('/Common.js/', $paths[1]);
119 $this->assertRegExp('/console/', file_get_contents($paths[1]));
120 }
121
122 /**
123 * Get a translatable string from an example module.
124 */
125 public function testGetStrings() {
126 $strings = $this->angular->getStrings('crmMailing');
127 $this->assertTrue(in_array('Save Draft', $strings));
128 $this->assertFalse(in_array('wild world', $strings));
129 // If crmMailing changes, feel free to use a different example.
130 }
131
132 /**
133 * Get a translatable string from an example module. The HTML is modified via hook.
134 */
135 public function testGetStrings_Hooked() {
136 \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
137
138 $strings = $this->angular->getStrings('crmMailing');
139 $this->assertTrue(in_array('wild world', $strings));
140 // If crmMailing changes, feel free to use a different example.
141 }
142
143 /**
144 * Get the list of dependencies for an Angular module.
145 */
146 public function testGetRequires() {
147 $requires = $this->angular->getResources(['crmMailing'], 'requires', 'requires');
148 $this->assertTrue(in_array('ngRoute', $requires['crmMailing']));
149 $this->assertFalse(in_array('crmCatStevens', $requires['crmMailing']));
150 // If crmMailing changes, feel free to use a different example.
151 }
152
153 /**
154 * Get the list of dependencies for an Angular module. It can be modified via hook.
155 */
156 public function testGetRequires_Hooked() {
157 \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
158
159 $requires = $this->angular->getResources(['crmMailing'], 'requires', 'requires');
160 $this->assertTrue(in_array('ngRoute', $requires['crmMailing']));
161 $this->assertTrue(in_array('crmCatStevens', $requires['crmMailing']));
162 // If crmMailing changes, feel free to use a different example.
163 }
164
165 /**
166 * Get the full, recursive list of dependencies for a set of Angular modules.
167 */
168 public function testResolveDeps() {
169 // If crmMailing changes, feel free to use a different example.
170 $expected = [
171 'angularFileUpload',
172 'crmAttachment',
173 'crmAutosave',
174 'crmCxn',
175 'crmMailing',
176 'crmResource',
177 'crmUtil',
178 'crmUi',
179 'dialogService',
180 'ngRoute',
181 'ngSanitize',
182 'ui.utils',
183 ];
184 $input = ['crmMailing', 'crmCxn'];
185 $actual = $this->angular->resolveDependencies($input);
186 sort($expected);
187 sort($actual);
188 $this->assertEquals($expected, $actual);
189 }
190
191 /**
192 * Example hook. Modifies `2step.html` by adding the attribute
193 * `cat-stevens="ts('wild world')"`.
194 *
195 * @param \Civi\Angular\Manager $angular
196 * @see \CRM_Utils_Hook::alterAngular
197 */
198 public function hook_civicrm_alterAngular($angular) {
199 $angular->add(ChangeSet::create('cat-stevens')
200 ->requires('crmMailing', 'crmCatStevens')
201 ->alterHtml('~/crmMailing/EditMailingCtrl/2step.html', function(\phpQueryObject $doc) {
202 $doc->find('[ng-form="crmMailingSubform"]')->attr('cat-stevens', 'ts(\'wild world\')');
203 })
204 );
205 }
206
207 public function hook_civicrm_angularModules_fooBar(&$angularModules) {
208 $angularModules['fooBar'] = [
209 'ext' => 'civicrm',
210 'js' => [
211 'assetBuilder://visual-bundle.js',
212 'ext://civicrm/js/Common.js',
213 ],
214 ];
215 }
216
217 }