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