-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18731 from colemanw/angularSettingsFactory
AngularLoader: Support 'settingsFactory' callbacks in angular modules.
- Loading branch information
Showing
3 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Civi\Angular; | ||
|
||
/** | ||
* Test the Angular loader. | ||
*/ | ||
class LoaderTest extends \CiviUnitTestCase { | ||
|
||
public static $dummy_setting_count = 0; | ||
public static $dummy_callback_count = 0; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->hookClass->setHook('civicrm_angularModules', [$this, 'hook_angularModules']); | ||
self::$dummy_setting_count = 0; | ||
self::$dummy_callback_count = 0; | ||
$this->createLoggedInUser(); | ||
} | ||
|
||
public function factoryScenarios() { | ||
return [ | ||
['dummy1', 2, 1], | ||
['dummy2', 2, 0], | ||
['dummy3', 2, 2], | ||
]; | ||
} | ||
|
||
/** | ||
* Tests that AngularLoader only conditionally loads settings via factory functions for in-use modules. | ||
* Our dummy settings callback functions keep a count of the number of times they have been called. | ||
* | ||
* @dataProvider factoryScenarios | ||
* @param $module | ||
* @param $expectedSettingCount | ||
* @param $expectedCallbackCount | ||
*/ | ||
public function testSettingFactory($module, $expectedSettingCount, $expectedCallbackCount) { | ||
(new \Civi\Angular\AngularLoader()) | ||
->setModules([$module]) | ||
->useApp() | ||
->load(); | ||
|
||
// Run factory callbacks | ||
$factorySettings = \Civi::resources()->getSettings(); | ||
|
||
// Dummy1 module's factory setting should be set if it is loaded directly or required by dummy3 | ||
$this->assertTrue(($expectedCallbackCount > 0) === isset($factorySettings['dummy1']['dummy_setting_factory'])); | ||
// Dummy3 module's factory setting should be set if it is loaded directly | ||
$this->assertTrue(($expectedCallbackCount > 1) === isset($factorySettings['dummy3']['dummy_setting_factory'])); | ||
|
||
// Dummy1 module's regular setting should be set if it is loaded directly or required by dummy3 | ||
$this->assertTrue(($module !== 'dummy2') === isset($factorySettings['dummy1']['dummy_setting'])); | ||
// Dummy2 module's regular setting should be set if loaded | ||
$this->assertTrue(($module === 'dummy2') === isset($factorySettings['dummy2']['dummy_setting'])); | ||
|
||
// Assert the callback functions ran the expected number of times | ||
$this->assertEquals($expectedSettingCount, self::$dummy_setting_count); | ||
$this->assertEquals($expectedCallbackCount, self::$dummy_callback_count); | ||
} | ||
|
||
public function hook_angularModules(&$modules) { | ||
$modules['dummy1'] = [ | ||
'ext' => 'civicrm', | ||
'settings' => $this->getDummySetting(), | ||
'settingsFactory' => [self::class, 'getDummySettingFactory'], | ||
]; | ||
$modules['dummy2'] = [ | ||
'ext' => 'civicrm', | ||
'settings' => $this->getDummySetting(), | ||
]; | ||
$modules['dummy3'] = [ | ||
'ext' => 'civicrm', | ||
// The string self::class is preferred but passing object $this should also work | ||
'settingsFactory' => [$this, 'getDummySettingFactory'], | ||
'requires' => ['dummy1'], | ||
]; | ||
} | ||
|
||
public function getDummySetting() { | ||
return ['dummy_setting' => self::$dummy_setting_count++]; | ||
} | ||
|
||
public static function getDummySettingFactory() { | ||
return ['dummy_setting_factory' => self::$dummy_callback_count++]; | ||
} | ||
|
||
} |