From 53294bfe93417031cd05d38e6e581ba828ea9d6b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 30 Jun 2024 22:34:52 -0700 Subject: [PATCH] Afform - Add a full mink/browser test for viewing authenticated email links --- .../AfformMock/MockPublicFormBrowserTest.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 ext/afform/mock/tests/phpunit/E2E/AfformMock/MockPublicFormBrowserTest.php diff --git a/ext/afform/mock/tests/phpunit/E2E/AfformMock/MockPublicFormBrowserTest.php b/ext/afform/mock/tests/phpunit/E2E/AfformMock/MockPublicFormBrowserTest.php new file mode 100644 index 000000000000..2963f3d539a0 --- /dev/null +++ b/ext/afform/mock/tests/phpunit/E2E/AfformMock/MockPublicFormBrowserTest.php @@ -0,0 +1,111 @@ +install(['org.civicrm.afform', 'org.civicrm.afform-mock']) + ->apply(); + } + + protected function setUp(): void { + parent::setUp(); + } + + protected function tearDown(): void { + parent::tearDown(); + Civi::settings()->revert('afform_mail_auth_token'); + } + + /** + * Create a contact with middle name "Donald". Use the custom form to change the middle + * name to "Donny". + * + * @return void + * @throws \Behat\Mink\Exception\ElementNotFoundException + * @throws \CRM_Core_Exception + */ + public function testUpdateMiddleName() { + Civi::settings()->set('afform_mail_auth_token', 'page'); + + $donny = $this->initializeTheodoreKerabatsos(); + $this->assertEquals('Donald', $this->getContact($donny)['middle_name'], 'Middle name has original value'); + + $session = $this->mink->getSession(); + $url = $this->renderToken('{afform.mockPublicFormUrl}', $donny); + $this->visit($url); + + // Goal: Wait for the fields to be populated. But how...? + // $session->wait(5000, 'document.querySelectorAll("input#middle-name-1").length > 0'); + // $session->wait(5000, '!!document.querySelectorAll("input#first-name-0").length && !!document.querySelectorAll("input#first-name-0")[0].value'); + // $session->wait(5000, '!!document.querySelectorAll("input#middle-name-1").length && document.querySelectorAll("input#middle-name-1")[0].value.length > 0'); + // $session->wait(5000, 'CRM.$(\'#middle-name-1:contains("Donald")\').length > 0'); + // $session->wait(5000, 'window.CRM.$(\'#middle-name-1:contains("Donald")\').length > 0'); + $session->wait(2000); /* FIXME: Cannot get wait-condition to be meaningfully used */ + + $middleName = $this->assertSession()->elementExists('css', 'input#middle-name-1'); + $this->assertEquals('Donald', $middleName->getValue()); + $middleName->setValue('Donny'); + + $submit = $this->assertSession()->elementExists('css', 'button.af-button.btn-primary'); + $submit->click(); + + // Goal: Wait for the "Saved" status message. But how...? + // $session->wait(5000, 'document.querySelectorAll(".crm-status-box-msg").length > 0'); + // $session->wait(5000, 'CRM.$(\'.crm-status-box-msg:contains("Saved")\').length > 1'); + $session->wait(2000); /* FIXME: Cannot get wait-condition to be meaningfully used */ + + $this->assertEquals('Donny', $this->getContact($donny)['middle_name'], 'Middle name has been updated'); + } + + protected function renderToken(string $token, int $cid): string { + $tp = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), []); + $tp->addRow()->context('contactId', $cid); + $tp->addMessage('example', $token, 'text/plain'); + $tp->evaluate(); + return $tp->getRow(0)->render('example'); + } + + protected function initializeTheodoreKerabatsos(): int { + $record = [ + 'contact_type' => 'Individual', + 'first_name' => 'Theodore', + 'middle_name' => 'Donald', + 'last_name' => 'Kerabatsos', + 'external_identifier' => __CLASS__, + ]; + $contact = \Civi\Api4\Contact::save(FALSE) + ->setRecords([$record]) + ->setMatch(['external_identifier']) + ->execute(); + return $contact[0]['id']; + } + + /** + * @param int $contactId + * @return string + * @throws \CRM_Core_Exception + */ + protected function getContact(int $contactId): array { + return Civi\Api4\Contact::get(FALSE) + ->addWhere('id', '=', $contactId) + ->addSelect('id', 'first_name', 'middle_name', 'last_name') + ->execute() + ->single(); + } + +}