From d23191ba3f6a21f10d8c0b0bffd38283cc1459fc Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Thu, 2 Feb 2023 13:31:03 +1300 Subject: [PATCH] MNT Update strong typization, tests cases --- src/Dev/FunctionalTest.php | 10 +++---- src/Dev/TestSession.php | 24 ++++++++-------- tests/php/Forms/FormTest.php | 55 +++++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/Dev/FunctionalTest.php b/src/Dev/FunctionalTest.php index 81f83ae994b..88d6ad6b20a 100644 --- a/src/Dev/FunctionalTest.php +++ b/src/Dev/FunctionalTest.php @@ -188,13 +188,13 @@ public function post($url, $data, $headers = null, $session = null, $body = null * * @see http://www.simpletest.org/en/form_testing_documentation.html * - * @param string $formID HTML 'id' attribute of a form (loaded through a previous response) - * @param string $button HTML 'name' attribute of the button (NOT the 'id' attribute) - * @param array $data Map of GET/POST data. - * @param bool $withSecurityToken Submit with the form's security token if there is one. + * @param $formID HTML 'id' attribute of a form (loaded through a previous response) + * @param $button HTML 'name' attribute of the button (NOT the 'id' attribute) + * @param $data Map of GET/POST data. + * @param $withSecurityToken Submit with the form's security token if there is one. * @return HTTPResponse */ - public function submitForm($formID, $button = null, $data = [], $withSecurityToken = true) + public function submitForm(string $formID, string $button = null, array $data = [], bool $withSecurityToken = true): HTTPResponse { $this->cssParser = null; $response = $this->mainSession->submitForm($formID, $button, $data, $withSecurityToken); diff --git a/src/Dev/TestSession.php b/src/Dev/TestSession.php index 096b6da0377..3d8d13b05bc 100644 --- a/src/Dev/TestSession.php +++ b/src/Dev/TestSession.php @@ -201,14 +201,13 @@ public function sendRequest($method, $url, $data, $headers = null, $session = nu * Wrong: array('mycheckboxvalues' => array(1 => 'one', 2 => 'two')) * Right: array('mycheckboxvalues[1]' => 'one', 'mycheckboxvalues[2]' => 'two') * - * @param string $formID HTML 'id' attribute of a form (loaded through a previous response) - * @param string $button HTML 'name' attribute of the button (NOT the 'id' attribute) - * @param array $data Map of GET/POST data. - * @param bool $withSecurityToken Submit with the form's security token if there is one. - * @return HTTPResponse + * @param $formID HTML 'id' attribute of a form (loaded through a previous response) + * @param $button HTML 'name' attribute of the button (NOT the 'id' attribute) + * @param $data Map of GET/POST data. + * @param $withSecurityToken Submit with the form's security token if there is one. * @throws Exception */ - public function submitForm($formID, $button = null, $data = [], $withSecurityToken = true) + public function submitForm(string $formID, string $button = null, array $data = [], bool $withSecurityToken = true): HTTPResponse { /** @var Crawler $page */ $page = $this->lastPage(); @@ -217,13 +216,12 @@ public function submitForm($formID, $button = null, $data = [], $withSecurityTok $formCrawler = $page->filterXPath("//form[@id='$formID']"); $form = $formCrawler->form(); } catch (InvalidArgumentException $e) { - // throw $e; user_error("TestSession::submitForm failed to find the form {$formID}"); } - foreach ($data as $k => $v) { - if ($form->has($k)) { - $form->get($k)->setValue($v); + foreach ($data as $fieldName => $value) { + if ($form->has($fieldName)) { + $form->get($fieldName)->setValue($value); } } @@ -251,7 +249,7 @@ public function submitForm($formID, $button = null, $data = [], $withSecurityTok ); } else { user_error("TestSession::submitForm called when there is no form loaded." - . " Visit the page with the form first", E_USER_WARNING); + . " Visit the page with the form first", E_USER_WARNING); } } @@ -327,9 +325,9 @@ public function cssParser() /** * Get a DOM Crawler for the last response * - * @return Crawler A DOM Crawler for the given response + * A DOM Crawler for the given response */ - public function lastPage() + public function lastPage(): Crawler { return new Crawler($this->lastContent(), Director::absoluteURL($this->lastUrl())); } diff --git a/tests/php/Forms/FormTest.php b/tests/php/Forms/FormTest.php index c8ed7e7e311..df940a416db 100644 --- a/tests/php/Forms/FormTest.php +++ b/tests/php/Forms/FormTest.php @@ -593,8 +593,39 @@ public function testDisableSecurityTokenDoesntAddTokenFormField() ); } - public function testDisableSecurityTokenAcceptsSubmissionWithoutToken() + public function provideFormsSet() { + return [ + 'with security token' => + [ + ['Form_Form', null,[ 'Email' => 'test@test.com' ], true], + 200, + 'Submission succeeds with security token', + ], + 'without security token' => + [ + ['Form_Form', null,[ 'Email' => 'test@test.com' ], false], + 200, + 'Cannot submit form without security token', + ], + 'button with wrong name' => + [ + ['Form_Form', 'undefined',[ 'Email' => 'test@test.com' ], true], + 404, + "Can't find button 'undefined' to submit as part of test.", + ], + ]; + } + + /** + * @dataProvider provideFormsSet + */ + public function testDisableSecurityTokenAcceptsSubmissionWithoutToken( + array $formData, + int $statusCode, + string $testMessage, + ?string $wrongPage = null + ): void { SecurityToken::enable(); $expectedToken = SecurityToken::inst()->getValue(); @@ -646,15 +677,19 @@ public function testDisableSecurityTokenAcceptsSubmissionWithoutToken() count($tokenEls ?? []), 'Token form field added for controller without disableSecurityToken()' ); - $response = $this->submitForm( - 'Form_Form', - null, - [ - 'Email' => 'test@test.com', - ], - withSecurityToken: true - ); - $this->assertEquals(200, $response->getStatusCode(), 'Submission succeeds with security token'); + + [ $form, $button, $data, $withSecurityToken ] = [ ...$formData ]; + + if (is_null($button)) { + $response = $this->submitForm($form, $button, $data, $withSecurityToken); + $this->assertEquals($statusCode, $response->getStatusCode(), $testMessage); + } else { + // Test nonexistent button Exceptions + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Can't find button 'undefined' to submit as part of test."); + + $response = $this->submitForm($form, $button, $data, $withSecurityToken); + } } public function testStrictFormMethodChecking()