Add Afform.revert. Fix Afform.create. Add lifecycle test-case.
authorTim Otten <totten@civicrm.org>
Wed, 8 Aug 2018 05:31:00 +0000 (22:31 -0700)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:16 +0000 (19:13 -0700)
ext/afform/afform/examplepage/layout.html [new file with mode: 0644]
ext/afform/afform/examplepage/meta.json [new file with mode: 0644]
ext/afform/api/v3/Afform/Create.php
ext/afform/api/v3/Afform/Revert.php [new file with mode: 0644]
ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php [deleted file]
ext/afform/tests/phpunit/api/v3/Afform/GetTest.php [deleted file]
ext/afform/tests/phpunit/api/v3/AfformTest.php [new file with mode: 0644]

diff --git a/ext/afform/afform/examplepage/layout.html b/ext/afform/afform/examplepage/layout.html
new file mode 100644 (file)
index 0000000..0f1db46
--- /dev/null
@@ -0,0 +1 @@
+<div>Hello {{routeParams.name}}</div>
diff --git a/ext/afform/afform/examplepage/meta.json b/ext/afform/afform/examplepage/meta.json
new file mode 100644 (file)
index 0000000..fbdd9d3
--- /dev/null
@@ -0,0 +1 @@
+{"server_route": "civicrm/example-page"}
index 412e721b191882a1f4330347153cf334736ad33a..599c763149133f9b2e033ec43ca9d063772cbffe 100644 (file)
@@ -45,14 +45,14 @@ function civicrm_api3_afform_create($params) {
   }
 
   // Create or update meta.json.
-  $orig = civicrm_api('afform', 'get', ['name' => $name, 'sequential' => 1]);
-  if (is_array($orig['values'][0])) {
+  $orig = civicrm_api3('afform', 'get', ['name' => $name, 'sequential' => 1]);
+  if (isset($orig['values'][0])) {
     $meta = _afform_fields_filter(array_merge($orig['values'][0], $updates));
   }
   else {
     $meta = $updates;
   }
-  unset($meta['definition']);
+  unset($meta['layout']);
   unset($meta['name']);
   if (!empty($meta)) {
     $metaPath = $scanner->createSiteLocalPath($name, CRM_Afform_AfformScanner::METADATA_FILE);
diff --git a/ext/afform/api/v3/Afform/Revert.php b/ext/afform/api/v3/Afform/Revert.php
new file mode 100644 (file)
index 0000000..3dced76
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+use CRM_Afform_ExtensionUtil as E;
+
+/**
+ * Afform.Revert API specification (optional)
+ * This is used for documentation and validation.
+ *
+ * @param array $spec description of fields supported by this API call
+ * @return void
+ * @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
+ */
+function _civicrm_api3_afform_revert_spec(&$spec) {
+  require_once __DIR__ . DIRECTORY_SEPARATOR . 'Get.php';
+  $getSpec = [];
+  _civicrm_api3_afform_get_spec($getSpec);
+  $spec['name'] = $getSpec['name'];
+}
+
+/**
+ * Afform.Revert API
+ *
+ * @param array $params
+ * @return array API result descriptor
+ * @see civicrm_api3_revert_success
+ * @see civicrm_api3_revert_error
+ * @throws API_Exception
+ */
+function civicrm_api3_afform_revert($params) {
+  $scanner = new CRM_Afform_AfformScanner();
+
+  if (empty($params['name']) || !preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $params['name'])) {
+    throw new \API_Exception("Afform.revert: name is a mandatory field. It should use alphanumerics and dashes.");
+  }
+  $name = $params['name'];
+
+  foreach ([CRM_Afform_AfformScanner::METADATA_FILE, 'layout.html'] as $file) {
+    $metaPath = $scanner->createSiteLocalPath($name, $file);
+    if (file_exists($metaPath)) {
+      if (!@unlink($metaPath)) {
+        throw new API_Exception("Failed to remove afform overrides in $file");
+      }
+    }
+  }
+
+  // FIXME if `server_route` changes, then flush the menu cache.
+  // FIXME if asset-caching is enabled, then flush the asset cache.
+
+  return civicrm_api3_create_success(1, $params, 'Afform', 'revert');
+}
diff --git a/ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php b/ext/afform/tests/phpunit/api/v3/Afform/CreateTest.php
deleted file mode 100644 (file)
index 5fe2c9f..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-
-use Civi\Test\HeadlessInterface;
-use Civi\Test\HookInterface;
-use Civi\Test\TransactionalInterface;
-
-/**
- * Afform.Create API Test Case
- * This is a generic test class implemented with PHPUnit.
- * @group headless
- */
-class api_v3_Afform_CreateTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
-
-  /**
-   * Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
-   * See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
-   */
-  public function setUpHeadless() {
-    return \Civi\Test::headless()
-      ->installMe(__DIR__)
-      ->apply();
-  }
-
-  /**
-   * The setup() method is executed before the test is executed (optional).
-   */
-  public function setUp() {
-    parent::setUp();
-  }
-
-  /**
-   * The tearDown() method is executed after the test was executed (optional)
-   * This can be used for cleanup.
-   */
-  public function tearDown() {
-    parent::tearDown();
-  }
-
-  /**
-   * Simple example test case.
-   *
-   * Note how the function name begins with the word "test".
-   */
-  public function testApiExample() {
-    $result = civicrm_api3('Afform', 'Create', array('magicword' => 'sesame'));
-    $this->assertEquals('Twelve', $result['values'][12]['name']);
-  }
-
-}
diff --git a/ext/afform/tests/phpunit/api/v3/Afform/GetTest.php b/ext/afform/tests/phpunit/api/v3/Afform/GetTest.php
deleted file mode 100644 (file)
index 6fd8701..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-
-use Civi\Test\HeadlessInterface;
-use Civi\Test\HookInterface;
-use Civi\Test\TransactionalInterface;
-
-/**
- * Afform.Get API Test Case
- * This is a generic test class implemented with PHPUnit.
- * @group headless
- */
-class api_v3_Afform_GetTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
-
-  /**
-   * Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
-   * See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
-   */
-  public function setUpHeadless() {
-    return \Civi\Test::headless()
-      ->installMe(__DIR__)
-      ->apply();
-  }
-
-  /**
-   * The setup() method is executed before the test is executed (optional).
-   */
-  public function setUp() {
-    parent::setUp();
-  }
-
-  /**
-   * The tearDown() method is executed after the test was executed (optional)
-   * This can be used for cleanup.
-   */
-  public function tearDown() {
-    parent::tearDown();
-  }
-
-  /**
-   * Simple example test case.
-   *
-   * Note how the function name begins with the word "test".
-   */
-  public function testApiExample() {
-    $result = civicrm_api3('Afform', 'Get', array('magicword' => 'sesame'));
-    $this->assertEquals('Twelve', $result['values'][12]['name']);
-  }
-
-}
diff --git a/ext/afform/tests/phpunit/api/v3/AfformTest.php b/ext/afform/tests/phpunit/api/v3/AfformTest.php
new file mode 100644 (file)
index 0000000..0c639f8
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+
+use Civi\Test\HeadlessInterface;
+use Civi\Test\HookInterface;
+use Civi\Test\TransactionalInterface;
+
+/**
+ * Afform.Get API Test Case
+ * This is a generic test class implemented with PHPUnit.
+ * @group headless
+ */
+class api_v3_AfformTest extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
+
+  use \Civi\Test\Api3TestTrait;
+
+  /**
+   * Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
+   * See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
+   */
+  public function setUpHeadless() {
+    return \Civi\Test::headless()
+      ->installMe(__DIR__)
+      ->apply();
+  }
+
+  /**
+   * The setup() method is executed before the test is executed (optional).
+   */
+  public function setUp() {
+    parent::setUp();
+  }
+
+  /**
+   * The tearDown() method is executed after the test was executed (optional)
+   * This can be used for cleanup.
+   */
+  public function tearDown() {
+    parent::tearDown();
+  }
+
+  /**
+   * This takes the bundled `examplepage` and performs some API calls on it.
+   */
+  public function testGetUpdateRevert() {
+    $this->callAPISuccess('Afform', 'revert', ['name' => 'examplepage']);
+
+    $message = 'The initial Afform.get should return default data';
+    $result = $this->callAPISuccess('Afform', 'get', ['name' => 'examplepage']);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['id'], $message);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['name'], $message);
+    $this->assertEquals('', $result['values']['examplepage']['title'], $message);
+    $this->assertEquals('', $result['values']['examplepage']['description'], $message);
+    $this->assertEquals('civicrm/example-page', $result['values']['examplepage']['server_route'], $message);
+    $this->assertTrue(is_array($result['values']['examplepage']['layout']), $message);
+
+    $message = 'After updating with Afform.create, the revised data should be returned';
+    $result = $this->callAPISuccess('Afform', 'create', [
+      'name' => 'examplepage',
+      'description' => 'The temporary description',
+    ]);
+    $this->assertEquals('examplepage', $result['values']['name'], $message);
+    $this->assertEquals('The temporary description', $result['values']['description'], $message);
+
+    $message = 'After updating, the Afform.get API should return blended data';
+    $result = $this->callAPISuccess('Afform', 'get', ['name' => 'examplepage']);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['id'], $message);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['name'], $message);
+    $this->assertEquals('', $result['values']['examplepage']['title'], $message);
+    $this->assertEquals('The temporary description', $result['values']['examplepage']['description'], $message);
+    $this->assertEquals('civicrm/example-page', $result['values']['examplepage']['server_route'], $message);
+    $this->assertTrue(is_array($result['values']['examplepage']['layout']), $message);
+
+    $this->callAPISuccess('Afform', 'revert', ['name' => 'examplepage']);
+    $message = 'After reverting, te final Afform.get should return default data';
+    $result = $this->callAPISuccess('Afform', 'get', ['name' => 'examplepage']);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['id'], $message);
+    $this->assertEquals('examplepage', $result['values']['examplepage']['name'], $message);
+    $this->assertEquals('', $result['values']['examplepage']['title'], $message);
+    $this->assertEquals('', $result['values']['examplepage']['description'], $message);
+    $this->assertEquals('civicrm/example-page', $result['values']['examplepage']['server_route'], $message);
+    $this->assertTrue(is_array($result['values']['examplepage']['layout']), $message);
+  }
+
+}