3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 namespace Civi\Angular
;
15 * Test the Angular base page.
17 class ManagerTest
extends \CiviUnitTestCase
{
25 * @var \CRM_Core_Resources
32 protected function setUp() {
33 $this->useTransaction(TRUE);
35 $this->createLoggedInUser();
36 $this->res
= \CRM_Core_Resources
::singleton();
37 $this->angular
= new Manager($this->res
);
41 * Modules appear to be well-defined.
43 public function testGetModules() {
44 $modules = $this->angular
->getModules();
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)));
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)));
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']++
;
77 if (isset($module['settings'])) {
78 $this->assertTrue(is_array($module['settings']));
79 foreach ($module['settings'] as $name => $value) {
80 $counts['settings']++
;
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');
92 * Get HTML fragments from an example module.
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.
101 * Get HTML fragments from an example module. The HTML is modified via hook.
103 public function testGetPartials_Hooked() {
104 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
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.
111 public function testGetJs_Asset() {
112 \CRM_Utils_Hook
::singleton()->setHook('civicrm_angularModules', [$this, 'hook_civicrm_angularModules_fooBar']);
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]));
118 $this->assertRegExp('/Common.js/', $paths[1]);
119 $this->assertRegExp('/console/', file_get_contents($paths[1]));
123 * Get a translatable string from an example module.
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.
133 * Get a translatable string from an example module. The HTML is modified via hook.
135 public function testGetStrings_Hooked() {
136 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
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.
144 * Get the list of dependencies for an Angular module.
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.
154 * Get the list of dependencies for an Angular module. It can be modified via hook.
156 public function testGetRequires_Hooked() {
157 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', [$this, 'hook_civicrm_alterAngular']);
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.
166 * Get the full, recursive list of dependencies for a set of Angular modules.
168 public function testResolveDeps() {
169 // If crmMailing changes, feel free to use a different example.
184 $input = ['crmMailing', 'crmCxn'];
185 $actual = $this->angular
->resolveDependencies($input);
188 $this->assertEquals($expected, $actual);
192 * Example hook. Modifies `2step.html` by adding the attribute
193 * `cat-stevens="ts('wild world')"`.
195 * @param \Civi\Angular\Manager $angular
196 * @see \CRM_Utils_Hook::alterAngular
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\')');
207 public function hook_civicrm_angularModules_fooBar(&$angularModules) {
208 $angularModules['fooBar'] = [
211 'assetBuilder://visual-bundle.js',
212 'ext://civicrm/js/Common.js',