Skip to content

Commit

Permalink
MNT Update strong typization, tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Feb 2, 2023
1 parent 7c20ade commit d23191b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/Dev/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 11 additions & 13 deletions src/Dev/TestSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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()));
}
Expand Down
55 changes: 45 additions & 10 deletions tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,39 @@ public function testDisableSecurityTokenDoesntAddTokenFormField()
);
}

public function testDisableSecurityTokenAcceptsSubmissionWithoutToken()
public function provideFormsSet()
{
return [
'with security token' =>
[
['Form_Form', null,[ 'Email' => '[email protected]' ], true],
200,
'Submission succeeds with security token',
],
'without security token' =>
[
['Form_Form', null,[ 'Email' => '[email protected]' ], false],
200,
'Cannot submit form without security token',
],
'button with wrong name' =>
[
['Form_Form', 'undefined',[ 'Email' => '[email protected]' ], 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();

Expand Down Expand Up @@ -646,15 +677,19 @@ public function testDisableSecurityTokenAcceptsSubmissionWithoutToken()
count($tokenEls ?? []),
'Token form field added for controller without disableSecurityToken()'
);
$response = $this->submitForm(
'Form_Form',
null,
[
'Email' => '[email protected]',
],
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()
Expand Down

0 comments on commit d23191b

Please sign in to comment.