From 59ccb5c253797a594484b7735025a9061f8145d5 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 18 Dec 2019 21:42:24 -0800 Subject: [PATCH] AfformUsageTest - Add a basic test case of prefill/submit APIs --- .../tests/phpunit/api/v4/AfformUsageTest.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php 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 index 0000000000..128475d1f8 --- /dev/null +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php @@ -0,0 +1,87 @@ + + +
+ + +
+ +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(); + } + +} -- 2.25.1