--- /dev/null
+<div>Hello {{routeParams.name}}</div>
--- /dev/null
+{"server_route": "civicrm/example-page"}
}
// 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);
--- /dev/null
+<?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');
+}
+++ /dev/null
-<?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']);
- }
-
-}
+++ /dev/null
-<?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']);
- }
-
-}
--- /dev/null
+<?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);
+ }
+
+}