mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MNT Update strong typization, tests cases
Co-authored-by: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com>
This commit is contained in:
parent
7c20ade548
commit
9d13f35040
@ -192,9 +192,8 @@ abstract class FunctionalTest extends SapphireTest implements TestOnly
|
||||
* @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
|
||||
*/
|
||||
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);
|
||||
|
@ -205,10 +205,8 @@ class TestSession
|
||||
* @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
|
||||
* @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 +215,12 @@ class TestSession
|
||||
$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 +248,7 @@ class TestSession
|
||||
);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -326,10 +323,8 @@ class TestSession
|
||||
|
||||
/**
|
||||
* Get a DOM Crawler for the last response
|
||||
*
|
||||
* @return Crawler A DOM Crawler for the given response
|
||||
*/
|
||||
public function lastPage()
|
||||
public function lastPage(): Crawler
|
||||
{
|
||||
return new Crawler($this->lastContent(), Director::absoluteURL($this->lastUrl()));
|
||||
}
|
||||
|
@ -593,7 +593,7 @@ class FormTest extends FunctionalTest
|
||||
);
|
||||
}
|
||||
|
||||
public function testDisableSecurityTokenAcceptsSubmissionWithoutToken()
|
||||
public function testDisableSecurityTokenAcceptsSubmissionWithoutToken(): void
|
||||
{
|
||||
SecurityToken::enable();
|
||||
$expectedToken = SecurityToken::inst()->getValue();
|
||||
@ -646,15 +646,55 @@ class FormTest extends FunctionalTest
|
||||
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');
|
||||
}
|
||||
|
||||
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],
|
||||
null,
|
||||
"Can't find button 'undefined' to submit as part of test.",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideFormsSet
|
||||
*/
|
||||
public function testSubmitFormWithSpecifiedParameters(
|
||||
array $formData,
|
||||
?int $statusCode,
|
||||
string $testMessage
|
||||
): void {
|
||||
|
||||
$this->get('FormTest_ControllerWithSecurityToken');
|
||||
|
||||
[ $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($testMessage);
|
||||
|
||||
$this->submitForm($form, $button, $data, $withSecurityToken);
|
||||
}
|
||||
}
|
||||
|
||||
public function testStrictFormMethodChecking()
|
||||
|
Loading…
Reference in New Issue
Block a user