3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 namespace Civi\Angular
;
31 * Test the Angular base page.
33 class ManagerTest
extends \CiviUnitTestCase
{
41 * @var \CRM_Core_Resources
48 protected function setUp() {
49 $this->useTransaction(TRUE);
51 $this->createLoggedInUser();
52 $this->res
= \CRM_Core_Resources
::singleton();
53 $this->angular
= new Manager($this->res
);
57 * Modules appear to be well-defined.
59 public function testGetModules() {
60 $modules = $this->angular
->getModules();
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)));
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)));
86 if (isset($module['partials'])) {
87 $this->assertTrue(is_array($module['partials']));
88 foreach ((array) $module['partials'] as $basedir) {
89 $this->assertTrue(is_dir($this->res
->getPath($module['ext']) . '/' . $basedir));
90 $counts['partials']++
;
93 if (isset($module['settings'])) {
94 $this->assertTrue(is_array($module['settings']));
95 foreach ($module['settings'] as $name => $value) {
96 $counts['settings']++
;
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');
104 $this->assertTrue($counts['settings'] > 0, 'Expect to find at least one setting');
108 * Get HTML fragments from an example module.
110 public function testGetPartials() {
111 $partials = $this->angular
->getPartials('crmMailing');
112 $this->assertRegExp('/ng-form="crmMailingSubform">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
113 // If crmMailing changes, feel free to use a different example.
117 * Get HTML fragments from an example module. The HTML is modified via hook.
119 public function testGetPartials_Hooked() {
120 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', array($this, 'hook_civicrm_alterAngular'));
122 $partials = $this->angular
->getPartials('crmMailing');
123 $this->assertRegExp('/ng-form="crmMailingSubform" cat-stevens="ts\\(\'wild world\'\\)">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
124 // If crmMailing changes, feel free to use a different example.
127 public function testGetJs_Asset() {
128 \CRM_Utils_Hook
::singleton()->setHook('civicrm_angularModules', array($this, 'hook_civicrm_angularModules_fooBar'));
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]));
134 $this->assertRegExp('/Common.js/', $paths[1]);
135 $this->assertRegExp('/console/', file_get_contents($paths[1]));
139 * Get a translatable string from an example module.
141 public function testGetStrings() {
142 $strings = $this->angular
->getStrings('crmMailing');
143 $this->assertTrue(in_array('Save Draft', $strings));
144 $this->assertFalse(in_array('wild world', $strings));
145 // If crmMailing changes, feel free to use a different example.
149 * Get a translatable string from an example module. The HTML is modified via hook.
151 public function testGetStrings_Hooked() {
152 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', array($this, 'hook_civicrm_alterAngular'));
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.
160 * Get the list of dependencies for an Angular module.
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']));
166 // If crmMailing changes, feel free to use a different example.
170 * Get the list of dependencies for an Angular module. It can be modified via hook.
172 public function testGetRequires_Hooked() {
173 \CRM_Utils_Hook
::singleton()->setHook('civicrm_alterAngular', array($this, 'hook_civicrm_alterAngular'));
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.
182 * Get the full, recursive list of dependencies for a set of Angular modules.
184 public function testResolveDeps() {
185 // If crmMailing changes, feel free to use a different example.
200 $input = array('crmMailing', 'crmCxn');
201 $actual = $this->angular
->resolveDependencies($input);
204 $this->assertEquals($expected, $actual);
208 * Example hook. Modifies `2step.html` by adding the attribute
209 * `cat-stevens="ts('wild world')"`.
211 * @param \Civi\Angular\Manager $angular
212 * @see \CRM_Utils_Hook::alterAngular
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\')');
223 public function hook_civicrm_angularModules_fooBar(&$angularModules) {
224 $angularModules['fooBar'] = array(
227 'assetBuilder://visual-bundle.js',
228 'ext://civicrm/js/Common.js',