AfformUsageTest - Add a basic test case of prefill/submit APIs
authorTim Otten <totten@civicrm.org>
Thu, 19 Dec 2019 05:42:24 +0000 (21:42 -0800)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:20 +0000 (19:13 -0700)
ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php [new file with mode: 0644]

diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php
new file mode 100644 (file)
index 0000000..128475d
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+/**
+ * Test case for Afform.prefill and Afform.submit.
+ *
+ * @group headless
+ */
+class api_v4_AfformUsageTest extends api_v4_AfformTestCase {
+  use \Civi\Test\Api3TestTrait;
+  use \Civi\Test\ContactTestTrait;
+
+  protected static $layouts = [];
+
+  protected $formName;
+
+  public static function setUpBeforeClass() {
+    parent::setUpBeforeClass();
+    self::$layouts['aboutMe'] = <<<EOHTML
+<af-form ctrl="modelListCtrl">
+  <af-entity type="Contact" data="{contact_type: 'Individual'}" name="me" label="Myself" url-autofill="1" autofill="user" />
+  <fieldset af-fieldset="me">
+      <af-field name="first_name" />
+      <af-field name="last_name" />
+  </fieldset>
+</af-form>
+EOHTML;
+  }
+
+  public function setUp() {
+    parent::setUp();
+    $this->formName = 'mock' . rand(0, 100000);
+  }
+
+  public function tearDown() {
+    Civi\Api4\Afform::revert()
+      ->setCheckPermissions(FALSE)
+      ->addWhere('name', '=', $this->formName)
+      ->execute();
+    parent::tearDown();
+  }
+
+  public function testAboutMeAllowed() {
+    $this->useValues([
+      'layout' => self::$layouts['aboutMe'],
+      'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
+    ]);
+
+    $cid = $this->createLoggedInUser();
+    CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
+
+    $prefill = Civi\Api4\Afform::prefill()
+      ->setName($this->formName)
+      ->setArgs([])
+      ->execute()
+      ->indexBy('name');
+    $this->assertEquals('Logged In', $prefill['me']['values']['first_name']);
+    $this->assertRegExp('/^User/', $prefill['me']['values']['last_name']);
+
+    $me = $prefill['me']['values'];
+    $me['first_name'] = 'Firsty';
+    $me['last_name'] = 'Lasty';
+
+    Civi\Api4\Afform::submit()
+      ->setName($this->formName)
+      ->setArgs([])
+      ->setValues(['me' => $me])
+      ->execute();
+
+    $contact = Civi\Api4\Contact::get()->setCheckPermissions(FALSE)->addWhere('id', '=', $cid)->execute()->first();
+    $this->assertEquals('Firsty', $contact['first_name']);
+    $this->assertEquals('Lasty', $contact['last_name']);
+  }
+
+  protected function useValues($values) {
+    $defaults = [
+      'title' => 'My form',
+      'name' => $this->formName,
+    ];
+    $full = array_merge($defaults, $values);
+    Civi\Api4\Afform::create()
+      ->setCheckPermissions(FALSE)
+      ->setLayoutFormat('html')
+      ->setValues($full)
+      ->execute();
+  }
+
+}