afform_clear() - Fix error when running on non-cached container
authorTim Otten <totten@civicrm.org>
Fri, 18 Sep 2020 02:14:06 +0000 (19:14 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 18 Sep 2020 02:21:33 +0000 (19:21 -0700)
After switching the tests from an effective policy of `CIVICRM_CONTAINER_CACHE=auto`
to an effective policy of `CIVICRM_CONTAINER_CACHE=never`, this started to fail:

```
1) api_v4_AfformTest::testGetUpdateRevert with data set #0 ('mockPage', array('', '', 'civicrm/mock-page', 'access Foobar'))
Symfony\Component\DependencyInjection\Exception\BadMethodCallException: Setting service "angular" for an unknown or non-synthetic service definition on a compiled container is not allowed.

/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/vendor/symfony/dependency-injection/ContainerBuilder.php:532
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/ext/afform/core/afform.php:520
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/ext/afform/core/Civi/Api4/Afform.php:99
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/Civi/Api4/Generic/BasicBatchAction.php:88
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/Civi/Api4/Generic/BasicBatchAction.php:68
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/Civi/Api4/Provider/ActionObjectProvider.php:68
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/Civi/API/Kernel.php:150
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/Civi/Api4/Generic/AbstractAction.php:238
/home/me/bknix/build/dmaster/web/sites/all/modules/civicrm/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php:53
/home/me/bknix/extern/phpunit6/phpunit6.phar:570
```

Civi/Angular/Manager.php
ext/afform/core/afform.php

index 4b21537d3fba5b0446152f54c12e8cbcf250b547..a763cf9e716dfa048731474dcbea2b1dd8bba8b0 100644 (file)
@@ -53,6 +53,20 @@ class Manager {
     $this->cache = $cache ? $cache : new \CRM_Utils_Cache_ArrayCache([]);
   }
 
+  /**
+   * Clear out any runtime-cached metadata.
+   *
+   * This is useful if, eg, you have recently added or destroyed Angular modules.
+   *
+   * @return static
+   */
+  public function clear() {
+    $this->cache->clear();
+    $this->modules = NULL;
+    $this->changeSets = NULL;
+    return $this;
+  }
+
   /**
    * Get a list of AngularJS modules which should be autoloaded.
    *
index 9d445ab6a648325eb1c4a99c0adf8d25848575f0..d63c34994f4fd9b90d8e2c617ed65cde1b9192f2 100644 (file)
@@ -514,10 +514,7 @@ function afform_civicrm_permission_check($permission, &$granted, $contactId) {
 function _afform_clear() {
   $container = \Civi::container();
   $container->get('afform_scanner')->clear();
-
-  // Civi\Angular\Manager doesn't currently have a way to clear its in-memory
-  // data, so we just reset the whole object.
-  $container->set('angular', NULL);
+  $container->get('angular')->clear();
 }
 
 /**