Commit | Line | Data |
---|---|---|
16072ce1 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
16072ce1 | 5 | | | |
7d61e75f TO |
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 | | |
16072ce1 | 9 | +--------------------------------------------------------------------+ |
cbcb7579 | 10 | */ |
16072ce1 TO |
11 | |
12 | namespace Civi\Angular; | |
13 | ||
16072ce1 TO |
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 | ||
9099cab3 | 46 | $counts = [ |
16072ce1 TO |
47 | 'js' => 0, |
48 | 'css' => 0, | |
49 | 'partials' => 0, | |
1da632e0 | 50 | 'settings' => 0, |
9099cab3 | 51 | ]; |
16072ce1 TO |
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'])); | |
a2dc0f82 TO |
72 | foreach ((array) $module['partials'] as $basedir) { |
73 | $this->assertTrue(is_dir($this->res->getPath($module['ext']) . '/' . $basedir)); | |
16072ce1 TO |
74 | $counts['partials']++; |
75 | } | |
76 | } | |
1da632e0 TO |
77 | if (isset($module['settings'])) { |
78 | $this->assertTrue(is_array($module['settings'])); | |
79 | foreach ($module['settings'] as $name => $value) { | |
80 | $counts['settings']++; | |
81 | } | |
82 | } | |
16072ce1 TO |
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'); | |
a83af40b | 88 | $this->assertTrue($counts['settings'] > 0, 'Expect to find at least one setting'); |
16072ce1 TO |
89 | } |
90 | ||
91 | /** | |
92 | * Get HTML fragments from an example module. | |
93 | */ | |
94 | public function testGetPartials() { | |
95 | $partials = $this->angular->getPartials('crmMailing'); | |
6dc348de TO |
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() { | |
9099cab3 | 104 | \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']); |
6dc348de TO |
105 | |
106 | $partials = $this->angular->getPartials('crmMailing'); | |
107 | $this->assertRegExp('/ng-form="crmMailingSubform" cat-stevens="ts\\(\'wild world\'\\)">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']); | |
16072ce1 TO |
108 | // If crmMailing changes, feel free to use a different example. |
109 | } | |
110 | ||
e5c376e7 | 111 | public function testGetJs_Asset() { |
9099cab3 | 112 | \CRM_Utils_Hook::singleton()->setHook('civicrm_angularModules', [$this, 'hook_civicrm_angularModules_fooBar']); |
e5c376e7 | 113 | |
9099cab3 | 114 | $paths = $this->angular->getResources(['fooBar'], 'js', 'path'); |
e5c376e7 TO |
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 | ||
16072ce1 TO |
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)); | |
6dc348de TO |
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() { | |
9099cab3 | 136 | \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']); |
6dc348de TO |
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() { | |
9099cab3 | 147 | $requires = $this->angular->getResources(['crmMailing'], 'requires', 'requires'); |
6dc348de TO |
148 | $this->assertTrue(in_array('ngRoute', $requires['crmMailing'])); |
149 | $this->assertFalse(in_array('crmCatStevens', $requires['crmMailing'])); | |
16072ce1 TO |
150 | // If crmMailing changes, feel free to use a different example. |
151 | } | |
cbcb7579 | 152 | |
6dc348de TO |
153 | /** |
154 | * Get the list of dependencies for an Angular module. It can be modified via hook. | |
155 | */ | |
156 | public function testGetRequires_Hooked() { | |
9099cab3 | 157 | \CRM_Utils_Hook::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']); |
6dc348de | 158 | |
9099cab3 | 159 | $requires = $this->angular->getResources(['crmMailing'], 'requires', 'requires'); |
6dc348de TO |
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 | ||
8da6c9b8 TO |
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. | |
9099cab3 | 170 | $expected = [ |
8da6c9b8 TO |
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', | |
9099cab3 CW |
183 | ]; |
184 | $input = ['crmMailing', 'crmCxn']; | |
8da6c9b8 TO |
185 | $actual = $this->angular->resolveDependencies($input); |
186 | sort($expected); | |
187 | sort($actual); | |
188 | $this->assertEquals($expected, $actual); | |
189 | } | |
190 | ||
6dc348de TO |
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') | |
39b959db | 201 | ->alterHtml('~/crmMailing/EditMailingCtrl/2step.html', function(\phpQueryObject $doc) { |
6dc348de TO |
202 | $doc->find('[ng-form="crmMailingSubform"]')->attr('cat-stevens', 'ts(\'wild world\')'); |
203 | }) | |
204 | ); | |
205 | } | |
206 | ||
e5c376e7 | 207 | public function hook_civicrm_angularModules_fooBar(&$angularModules) { |
9099cab3 | 208 | $angularModules['fooBar'] = [ |
e5c376e7 | 209 | 'ext' => 'civicrm', |
9099cab3 | 210 | 'js' => [ |
e5c376e7 TO |
211 | 'assetBuilder://visual-bundle.js', |
212 | 'ext://civicrm/js/Common.js', | |
9099cab3 CW |
213 | ], |
214 | ]; | |
e5c376e7 TO |
215 | } |
216 | ||
16072ce1 | 217 | } |