afform_mock - Add test-coverage for auto-requires functionality
authorTim Otten <totten@civicrm.org>
Sat, 19 Oct 2019 03:16:14 +0000 (20:16 -0700)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:19 +0000 (19:13 -0700)
ext/afform/core/Civi/Api4/Action/Afform/Update.php
ext/afform/core/Civi/Api4/Afform.php
ext/afform/core/afform.php
ext/afform/mock/ang/mockPage.aff.json
ext/afform/mock/tests/phpunit/api/v4/AfformTest.php

index 212c252fb1fbffb60be680ef9e1907b3039b0023..4ee3130426b05ddaa9bb070d051311903b13f7a7 100644 (file)
@@ -56,7 +56,7 @@ class Update extends BasicUpdateAction {
     }
 
     // We may have changed list of files covered by the cache.
-    $scanner->clear();
+    _afform_clear();
 
     if (isset($updates['server_route']) && $updates['server_route'] !== $orig[0]['server_route']) {
       \CRM_Core_Menu::store();
index 75b2095d87e2d8e5389247c124b5600538cc0594..86e26a4d956798635a1e436abb664419039ca069 100644 (file)
@@ -40,7 +40,7 @@ class Afform extends AbstractEntity {
       }
 
       // We may have changed list of files covered by the cache.
-      $scanner->clear();
+      _afform_clear();
 
       // FIXME if `server_route` changes, then flush the menu cache.
       // FIXME if asset-caching is enabled, then flush the asset cache
index 605a186f184350984be9442d79da57cea3259fb0..167af68d67031421194737db03c83e92347841da 100644 (file)
@@ -423,6 +423,18 @@ function afform_civicrm_alterMenu(&$items) {
   }
 }
 
+/**
+ * Clear any local/in-memory caches based on afform data.
+ */
+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);
+}
+
 /**
  * @param string $fileBaseName
  *   Ex: foo-bar
index bd3a6baf25f80666b9295ef9f32b161b7be00237..9b45e0bfbebc575f1de2b4e2b3eda2c419df6f03 100644 (file)
@@ -1 +1 @@
-{"server_route": "civicrm/mock-page", "requires":["mockFoo", "mockBareFile"]}
+{"server_route": "civicrm/mock-page", "requires":["extraMock"]}
index 2bbc3e106cd0b5fcb347e80f2f01884e9a38b531..538e977e5e4a100c788186024d41a67a0a7fffa1 100644 (file)
@@ -6,6 +6,8 @@
  * @group headless
  */
 class api_v4_AfformTest extends api_v4_AfformTestCase {
+  use \Civi\Test\Api3TestTrait;
+  use \Civi\Test\ContactTestTrait;
 
   public function getBasicDirectives() {
     return [
@@ -115,4 +117,45 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
     Civi\Api4\Afform::revert()->addWhere('name', '=', $directiveName)->execute();
   }
 
+  public function testAutoRequires() {
+    $directiveName = 'mockPage';
+    $this->createLoggedInUser();
+
+    // The default mockPage has 1 explicit requirement + 2 automatic requirements.
+    Civi\Api4\Afform::revert()->addWhere('name', '=', $directiveName)->execute();
+    $angModule = Civi::service('angular')->getModule($directiveName);
+    $this->assertEquals(['afCore', 'extraMock', 'mockBareFile', 'mockFoo'], $angModule['requires']);
+    $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->addSelect('requires')->execute();
+    $this->assertEquals(['extraMock'], $storedRequires[0]['requires']);
+
+    // Knock down to 1 explicit + 1 automatic.
+    Civi\Api4\Afform::update()
+      ->addWhere('name', '=', $directiveName)
+      ->setLayoutFormat('html')
+      ->setValues(['layout' => '<div>The bare file says "<span mock-bare-file/>"</div>'])
+      ->execute();
+    $angModule = Civi::service('angular')->getModule($directiveName);
+    $this->assertEquals(['afCore', 'extraMock', 'mockBareFile'], $angModule['requires']);
+    $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->addSelect('requires')->execute();
+    $this->assertEquals(['extraMock'], $storedRequires[0]['requires']);
+
+    // Remove the last explict and implicit requirements.
+    Civi\Api4\Afform::update()
+      ->addWhere('name', '=', $directiveName)
+      ->setLayoutFormat('html')
+      ->setValues([
+        'layout' => '<div>The file has nothing! <strong>NOTHING!</strong> <em>JUST RANTING!</em></div>',
+        'requires' => [],
+      ])
+      ->execute();
+    $angModule = Civi::service('angular')->getModule($directiveName);
+    $this->assertEquals(['afCore'], $angModule['requires']);
+    $storedRequires = Civi\Api4\Afform::get()->addWhere('name', '=', $directiveName)->addSelect('requires')->execute();
+    $this->assertEquals([], $storedRequires[0]['requires']);
+
+    Civi\Api4\Afform::revert()->addWhere('name', '=', $directiveName)->execute();
+    $angModule = Civi::service('angular')->getModule($directiveName);
+    $this->assertEquals(['afCore', 'extraMock', 'mockBareFile', 'mockFoo'], $angModule['requires']);
+  }
+
 }