From a6c490e97141393383069b83a41258756134c660 Mon Sep 17 00:00:00 2001 From: rarog Date: Thu, 26 Mar 2020 23:38:18 +0100 Subject: [PATCH] Working on forms - Adding CSRF. - Making them PSR-12 compliant. --- src/ZfcUser/Form/Base.php | 105 ++++++-------- src/ZfcUser/Form/ChangeEmail.php | 82 ++++++----- src/ZfcUser/Form/ChangeEmailFilter.php | 58 ++++---- src/ZfcUser/Form/ChangePassword.php | 82 ++++++----- src/ZfcUser/Form/ChangePasswordFilter.php | 112 ++++++++------- src/ZfcUser/Form/Login.php | 61 ++++---- src/ZfcUser/Form/LoginFilter.php | 45 +++--- src/ZfcUser/Form/ProvidesEventsForm.php | 1 + src/ZfcUser/Form/Register.php | 37 ++--- src/ZfcUser/Form/RegisterFilter.php | 134 ++++++++++-------- tests/ZfcUserTest/Form/ChangeEmailTest.php | 1 + tests/ZfcUserTest/Form/ChangePasswordTest.php | 1 + tests/ZfcUserTest/Form/LoginTest.php | 1 + tests/ZfcUserTest/Form/RegisterTest.php | 27 +--- 14 files changed, 385 insertions(+), 362 deletions(-) diff --git a/src/ZfcUser/Form/Base.php b/src/ZfcUser/Form/Base.php index 46c6f01c..c75aba04 100644 --- a/src/ZfcUser/Form/Base.php +++ b/src/ZfcUser/Form/Base.php @@ -2,7 +2,10 @@ namespace ZfcUser\Form; -use Laminas\Form\Element; +use Laminas\Form\Element\Button; +use Laminas\Form\Element\Hidden; +use Laminas\Form\Element\Password; +use Laminas\Form\Element\Text; class Base extends ProvidesEventsForm { @@ -10,89 +13,75 @@ public function __construct($name = null) { parent::__construct($name); - $this->add(array( + $this->add([ 'name' => 'username', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => 'Username', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'username', - 'type' => 'text', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'email', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => 'Email', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'email', - 'type' => 'text', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'display_name', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => 'Display Name', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'display_name', - 'type' => 'text', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'password', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'password', - 'type' => 'password', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'passwordVerify', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Password Verify', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'passwordVerify', - 'type' => 'password', - ), - )); + ], + ]); - $submitElement = new Element\Button('submit'); + $submitElement = new Button('submit'); $submitElement ->setLabel('Submit') - ->setAttributes(array( + ->setAttributes([ 'type' => 'submit', - )); + ]); - $this->add($submitElement, array( + $this->add($submitElement, [ 'priority' => -100, - )); + ]); - $this->add(array( + $this->add([ 'name' => 'userId', - 'type' => 'Laminas\Form\Element\Hidden', - 'attributes' => array( - 'type' => 'hidden' - ), - )); - - // @TODO: Fix this... getValidator() is a protected method. - //$csrf = new Element\Csrf('csrf'); - //$csrf->getValidator()->setTimeout($this->getRegistrationOptions()->getUserFormTimeout()); - //$this->add($csrf); - } - - public function init() - { + 'type' => Hidden::class, + ]); } } diff --git a/src/ZfcUser/Form/ChangeEmail.php b/src/ZfcUser/Form/ChangeEmail.php index 3e489ba5..6f0f7a17 100644 --- a/src/ZfcUser/Form/ChangeEmail.php +++ b/src/ZfcUser/Form/ChangeEmail.php @@ -2,6 +2,11 @@ namespace ZfcUser\Form; +use Laminas\Form\Element\Csrf; +use Laminas\Form\Element\Hidden; +use Laminas\Form\Element\Password; +use Laminas\Form\Element\Submit; +use Laminas\Form\Element\Text; use ZfcUser\Options\AuthenticationOptionsInterface; class ChangeEmail extends ProvidesEventsForm @@ -17,58 +22,67 @@ public function __construct($name, AuthenticationOptionsInterface $options) parent::__construct($name); - $this->add(array( + $this->add([ 'name' => 'identity', - 'options' => array( + 'type' => Hidden::class, + 'options' => [ 'label' => '', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'identity', - 'type' => 'hidden', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'newIdentity', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => 'New Email', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'newIdentity', - 'type' => 'text', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'newIdentityVerify', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => 'Verify New Email', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'newIdentityVerify', - 'type' => 'text', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'credential', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'credential', - 'type' => 'password', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'submit', - 'attributes' => array( + 'type' => Submit::class, + 'attributes' => [ 'value' => 'Submit', - 'type' => 'submit' - ), - )); + ], + ]); + + $this->add([ + 'name' => 'csrf', + 'type' => Csrf::class, + 'options' => [ + 'csrf_options' => [ + 'timeout' => $this->getAuthenticationOptions()->getLoginFormTimeout(), + ], + ], + ]); } /** diff --git a/src/ZfcUser/Form/ChangeEmailFilter.php b/src/ZfcUser/Form/ChangeEmailFilter.php index 02434924..02d50be1 100644 --- a/src/ZfcUser/Form/ChangeEmailFilter.php +++ b/src/ZfcUser/Form/ChangeEmailFilter.php @@ -3,6 +3,8 @@ namespace ZfcUser\Form; use Laminas\InputFilter\InputFilter; +use Laminas\Validator\EmailAddress; +use Laminas\Validator\Identical; use ZfcUser\Options\AuthenticationOptionsInterface; class ChangeEmailFilter extends InputFilter @@ -13,43 +15,43 @@ public function __construct(AuthenticationOptionsInterface $options, $emailValid { $this->emailValidator = $emailValidator; - $identityParams = array( - 'name' => 'identity', - 'required' => true, - 'validators' => array() - ); + $identityParams = [ + 'name' => 'identity', + 'required' => true, + 'validators' => [], + ]; $identityFields = $options->getAuthIdentityFields(); - if ($identityFields == array('email')) { - $validators = array('name' => 'EmailAddress'); + if ($identityFields == ['email']) { + $validators = ['name' => EmailAddress::class]; array_push($identityParams['validators'], $validators); } $this->add($identityParams); - $this->add(array( - 'name' => 'newIdentity', - 'required' => true, - 'validators' => array( - array( - 'name' => 'EmailAddress' - ), - $this->emailValidator - ), - )); + $this->add([ + 'name' => 'newIdentity', + 'required' => true, + 'validators' => [ + [ + 'name' => EmailAddress::class, + ], + $this->emailValidator, + ], + ]); - $this->add(array( - 'name' => 'newIdentityVerify', - 'required' => true, - 'validators' => array( - array( - 'name' => 'identical', - 'options' => array( + $this->add([ + 'name' => 'newIdentityVerify', + 'required' => true, + 'validators' => [ + [ + 'name' => Identical::class, + 'options' => [ 'token' => 'newIdentity' - ) - ), - ), - )); + ] + ], + ], + ]); } public function getEmailValidator() diff --git a/src/ZfcUser/Form/ChangePassword.php b/src/ZfcUser/Form/ChangePassword.php index 10c61089..2e1d00bf 100644 --- a/src/ZfcUser/Form/ChangePassword.php +++ b/src/ZfcUser/Form/ChangePassword.php @@ -2,6 +2,10 @@ namespace ZfcUser\Form; +use Laminas\Form\Element\Csrf; +use Laminas\Form\Element\Hidden; +use Laminas\Form\Element\Password; +use Laminas\Form\Element\Submit; use ZfcUser\Options\AuthenticationOptionsInterface; class ChangePassword extends ProvidesEventsForm @@ -17,59 +21,67 @@ public function __construct($name, AuthenticationOptionsInterface $options) parent::__construct($name); - $this->add(array( + $this->add([ 'name' => 'identity', - 'options' => array( + 'type' => Hidden::class, + 'options' => [ 'label' => '', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'identity', - 'type' => 'hidden', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'credential', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Current Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'credential', - 'type' => 'password', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'newCredential', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'New Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'newCredential', - 'type' => 'password', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'newCredentialVerify', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Verify New Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'newCredentialVerify', - 'type' => 'password', - ), - )); + ], + ]); - $this->add(array( + $this->add([ 'name' => 'submit', - 'attributes' => array( + 'type' => Submit::class, + 'attributes' => [ 'value' => 'Submit', - 'type' => 'submit' - ), - )); + ], + ]); + + $this->add([ + 'name' => 'csrf', + 'type' => Csrf::class, + 'options' => [ + 'csrf_options' => [ + 'timeout' => $this->getAuthenticationOptions()->getLoginFormTimeout(), + ], + ], + ]); } /** diff --git a/src/ZfcUser/Form/ChangePasswordFilter.php b/src/ZfcUser/Form/ChangePasswordFilter.php index 9a61e7c6..a7c79594 100644 --- a/src/ZfcUser/Form/ChangePasswordFilter.php +++ b/src/ZfcUser/Form/ChangePasswordFilter.php @@ -2,79 +2,83 @@ namespace ZfcUser\Form; +use Laminas\Filter\StringTrim; use Laminas\InputFilter\InputFilter; +use Laminas\Validator\EmailAddress; +use Laminas\Validator\Identical; +use Laminas\Validator\StringLength; use ZfcUser\Options\AuthenticationOptionsInterface; class ChangePasswordFilter extends InputFilter { public function __construct(AuthenticationOptionsInterface $options) { - $identityParams = array( - 'name' => 'identity', - 'required' => true, - 'validators' => array() - ); + $identityParams = [ + 'name' => 'identity', + 'required' => true, + 'validators' => [], + ]; $identityFields = $options->getAuthIdentityFields(); - if ($identityFields == array('email')) { - $validators = array('name' => 'EmailAddress'); + if ($identityFields == ['email']) { + $validators = ['name' => EmailAddress::class]; array_push($identityParams['validators'], $validators); } $this->add($identityParams); - $this->add(array( - 'name' => 'credential', - 'required' => true, - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'credential', + 'required' => true, + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - ), - 'filters' => array( - array('name' => 'StringTrim'), - ), - )); + ], + ], + ], + 'filters' => [ + ['name' => StringTrim::class], + ], + ]); - $this->add(array( - 'name' => 'newCredential', - 'required' => true, - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'newCredential', + 'required' => true, + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - ), - 'filters' => array( - array('name' => 'StringTrim'), - ), - )); + ], + ], + ], + 'filters' => [ + ['name' => StringTrim::class], + ], + ]); - $this->add(array( - 'name' => 'newCredentialVerify', - 'required' => true, - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'newCredentialVerify', + 'required' => true, + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - array( - 'name' => 'identical', - 'options' => array( + ], + ], + [ + 'name' => Identical::class, + 'options' => [ 'token' => 'newCredential' - ) - ), - ), - 'filters' => array( - array('name' => 'StringTrim'), - ), - )); + ] + ], + ], + 'filters' => [ + ['name' => StringTrim::class], + ], + ]); } } diff --git a/src/ZfcUser/Form/Login.php b/src/ZfcUser/Form/Login.php index 09b5708d..e84a935e 100644 --- a/src/ZfcUser/Form/Login.php +++ b/src/ZfcUser/Form/Login.php @@ -2,8 +2,11 @@ namespace ZfcUser\Form; -use Laminas\Form\Element; +use Laminas\Form\Element\Button; +use Laminas\Form\Element\Password; +use Laminas\Form\Element\Text; use ZfcUser\Options\AuthenticationOptionsInterface; +use Laminas\Form\Element\Csrf; class Login extends ProvidesEventsForm { @@ -18,56 +21,62 @@ public function __construct($name, AuthenticationOptionsInterface $options) parent::__construct($name); - $this->add(array( + $this->add([ 'name' => 'identity', - 'options' => array( + 'type' => Text::class, + 'options' => [ 'label' => '', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'identity', - 'type' => 'text', - ), - )); + ], + ]); $emailElement = $this->get('identity'); $label = $emailElement->getLabel('label'); // @TODO: make translation-friendly foreach ($this->getAuthenticationOptions()->getAuthIdentityFields() as $mode) { - $label = (!empty($label) ? $label . ' or ' : '') . ucfirst($mode); + $label = (! empty($label) ? $label . ' or ' : '') . ucfirst($mode); } $emailElement->setLabel($label); // - $this->add(array( + $this->add([ 'name' => 'credential', - 'type' => 'password', - 'options' => array( + 'type' => Password::class, + 'options' => [ 'label' => 'Password', - ), - 'attributes' => array( + ], + 'attributes' => [ 'id' => 'credential', - 'type' => 'password', - ), - )); + ], + ]); + + $this->add([ + 'name' => 'csrf', + 'type' => Csrf::class, + 'options' => [ + 'csrf_options' => [ + 'timeout' => $this->getAuthenticationOptions()->getLoginFormTimeout(), + ], + ], + ]); // @todo: Fix this // 1) getValidator() is a protected method // 2) i don't believe the login form is actually being validated by the login action // (but keep in mind we don't want to show invalid username vs invalid password or // anything like that, it should just say "login failed" without any additional info) - //$csrf = new Element\Csrf('csrf'); - //$csrf->getValidator()->setTimeout($options->getLoginFormTimeout()); - //$this->add($csrf); - $submitElement = new Element\Button('submit'); + $submitElement = new Button('submit'); $submitElement ->setLabel('Sign In') - ->setAttributes(array( - 'type' => 'submit', - )); + ->setAttributes([ + 'type' => 'submit', + ]); - $this->add($submitElement, array( + $this->add($submitElement, [ 'priority' => -100, - )); + ]); } /** diff --git a/src/ZfcUser/Form/LoginFilter.php b/src/ZfcUser/Form/LoginFilter.php index 37eae3f2..e9c1c421 100644 --- a/src/ZfcUser/Form/LoginFilter.php +++ b/src/ZfcUser/Form/LoginFilter.php @@ -2,6 +2,9 @@ namespace ZfcUser\Form; +use Laminas\Filter\StringTrim; +use Laminas\Validator\EmailAddress; +use Laminas\Validator\StringLength; use ZfcUser\InputFilter\ProvidesEventsInputFilter; use ZfcUser\Options\AuthenticationOptionsInterface; @@ -9,34 +12,34 @@ class LoginFilter extends ProvidesEventsInputFilter { public function __construct(AuthenticationOptionsInterface $options) { - $identityParams = array( - 'name' => 'identity', - 'required' => true, - 'validators' => array() - ); + $identityParams = [ + 'name' => 'identity', + 'required' => true, + 'validators' => [], + ]; $identityFields = $options->getAuthIdentityFields(); - if ($identityFields == array('email')) { - $validators = array('name' => 'EmailAddress'); + if ($identityFields == ['email']) { + $validators = ['name' => EmailAddress::class]; array_push($identityParams['validators'], $validators); } $this->add($identityParams); - $this->add(array( - 'name' => 'credential', - 'required' => true, - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'credential', + 'required' => true, + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - ), - 'filters' => array( - array('name' => 'StringTrim'), - ), - )); + ], + ], + ], + 'filters' => [ + ['name' => StringTrim::class], + ], + ]); } } diff --git a/src/ZfcUser/Form/ProvidesEventsForm.php b/src/ZfcUser/Form/ProvidesEventsForm.php index 62b56315..5c05d869 100644 --- a/src/ZfcUser/Form/ProvidesEventsForm.php +++ b/src/ZfcUser/Form/ProvidesEventsForm.php @@ -1,4 +1,5 @@ add([ + 'name' => 'csrf', + 'type' => Csrf::class, + 'options' => [ + 'csrf_options' => [ + 'timeout' => $this->getRegistrationOptions()->getUserFormTimeout(), + ], + ], + ]); + if ($this->getRegistrationOptions()->getUseRegistrationFormCaptcha()) { - $this->add(array( + $this->add([ 'name' => 'captcha', - 'type' => 'Laminas\Form\Element\Captcha', - 'options' => array( + 'type' => Captcha::class, + 'options' => [ 'label' => 'Please type the following text', 'captcha' => $this->getRegistrationOptions()->getFormCaptchaOptions(), - ), - )); + ], + ]); } $this->remove('userId'); - if (!$this->getRegistrationOptions()->getEnableUsername()) { + if (! $this->getRegistrationOptions()->getEnableUsername()) { $this->remove('username'); } - if (!$this->getRegistrationOptions()->getEnableDisplayName()) { + if (! $this->getRegistrationOptions()->getEnableDisplayName()) { $this->remove('display_name'); } - if ($this->getRegistrationOptions()->getUseRegistrationFormCaptcha() && $this->captchaElement) { - $this->add($this->captchaElement, array('name'=>'captcha')); - } $this->get('submit')->setLabel('Register'); } - public function setCaptchaElement(Captcha $captchaElement) - { - $this->captchaElement= $captchaElement; - } - /** * Set Registration Options * diff --git a/src/ZfcUser/Form/RegisterFilter.php b/src/ZfcUser/Form/RegisterFilter.php index 1e1319bb..649641d3 100644 --- a/src/ZfcUser/Form/RegisterFilter.php +++ b/src/ZfcUser/Form/RegisterFilter.php @@ -2,6 +2,10 @@ namespace ZfcUser\Form; +use Laminas\Filter\StringTrim; +use Laminas\Validator\EmailAddress; +use Laminas\Validator\Identical; +use Laminas\Validator\StringLength; use ZfcUser\InputFilter\ProvidesEventsInputFilter; use ZfcUser\Options\RegistrationOptionsInterface; @@ -22,83 +26,89 @@ public function __construct($emailValidator, $usernameValidator, RegistrationOpt $this->usernameValidator = $usernameValidator; if ($this->getOptions()->getEnableUsername()) { - $this->add(array( - 'name' => 'username', - 'required' => true, - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'username', + 'required' => true, + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 3, 'max' => 255, - ), - ), + ], + ], $this->usernameValidator, - ), - )); + ], + ]); } - $this->add(array( - 'name' => 'email', - 'required' => true, - 'validators' => array( - array( - 'name' => 'EmailAddress' - ), + $this->add([ + 'name' => 'email', + 'required' => true, + 'validators' => [ + [ + 'name' => EmailAddress::class + ], $this->emailValidator - ), - )); + ], + ]); if ($this->getOptions()->getEnableDisplayName()) { - $this->add(array( - 'name' => 'display_name', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'display_name', + 'required' => true, + 'filters' => [ + ['name' => StringTrim::class], + ], + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 3, 'max' => 128, - ), - ), - ), - )); + ], + ], + ], + ]); } - $this->add(array( - 'name' => 'password', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + $this->add([ + 'name' => 'password', + 'required' => true, + 'filters' => [ + ['name' => StringTrim::class], + ], + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - ), - )); - - $this->add(array( - 'name' => 'passwordVerify', - 'required' => true, - 'filters' => array(array('name' => 'StringTrim')), - 'validators' => array( - array( - 'name' => 'StringLength', - 'options' => array( + ], + ], + ], + ]); + + $this->add([ + 'name' => 'passwordVerify', + 'required' => true, + 'filters' => [ + ['name' => StringTrim::class], + ], + 'validators' => [ + [ + 'name' => StringLength::class, + 'options' => [ 'min' => 6, - ), - ), - array( - 'name' => 'Identical', - 'options' => array( + ], + ], + [ + 'name' => Identical::class, + 'options' => [ 'token' => 'password', - ), - ), - ), - )); + ], + ], + ], + ]); } public function getEmailValidator() diff --git a/tests/ZfcUserTest/Form/ChangeEmailTest.php b/tests/ZfcUserTest/Form/ChangeEmailTest.php index 32367687..edc29197 100644 --- a/tests/ZfcUserTest/Form/ChangeEmailTest.php +++ b/tests/ZfcUserTest/Form/ChangeEmailTest.php @@ -24,6 +24,7 @@ public function testConstruct(): void $this->assertArrayHasKey('newIdentity', $elements); $this->assertArrayHasKey('newIdentityVerify', $elements); $this->assertArrayHasKey('credential', $elements); + $this->assertArrayHasKey('csrf', $elements); } /** diff --git a/tests/ZfcUserTest/Form/ChangePasswordTest.php b/tests/ZfcUserTest/Form/ChangePasswordTest.php index 512f0973..543090f7 100644 --- a/tests/ZfcUserTest/Form/ChangePasswordTest.php +++ b/tests/ZfcUserTest/Form/ChangePasswordTest.php @@ -24,6 +24,7 @@ public function testConstruct(): void $this->assertArrayHasKey('credential', $elements); $this->assertArrayHasKey('newCredential', $elements); $this->assertArrayHasKey('newCredentialVerify', $elements); + $this->assertArrayHasKey('csrf', $elements); } /** diff --git a/tests/ZfcUserTest/Form/LoginTest.php b/tests/ZfcUserTest/Form/LoginTest.php index b4899b94..39010898 100644 --- a/tests/ZfcUserTest/Form/LoginTest.php +++ b/tests/ZfcUserTest/Form/LoginTest.php @@ -26,6 +26,7 @@ public function testConstruct($authIdentityFields = []): void $this->assertArrayHasKey('identity', $elements); $this->assertArrayHasKey('credential', $elements); + $this->assertArrayHasKey('csrf', $elements); $expectedLabel = ''; if (count($authIdentityFields) > 0) { diff --git a/tests/ZfcUserTest/Form/RegisterTest.php b/tests/ZfcUserTest/Form/RegisterTest.php index 79933236..f61656d3 100644 --- a/tests/ZfcUserTest/Form/RegisterTest.php +++ b/tests/ZfcUserTest/Form/RegisterTest.php @@ -3,7 +3,6 @@ namespace ZfcUserTest\Form; use Laminas\Captcha\AbstractAdapter; -use Laminas\Form\Element\Captcha; use PHPUnit\Framework\TestCase; use ZfcUser\Form\Register as Form; use ZfcUser\Options\RegistrationOptionsInterface; @@ -45,6 +44,7 @@ public function testConstruct($useCaptcha = false): void $this->assertArrayHasKey('email', $elements); $this->assertArrayHasKey('password', $elements); $this->assertArrayHasKey('passwordVerify', $elements); + $this->assertArrayHasKey('csrf', $elements); } public function providerTestConstruct(): array @@ -78,31 +78,6 @@ public function testSetGetRegistrationOptions(): void $this->assertSame($optionsNew, $form->getRegistrationOptions()); } - public function testSetCaptchaElement(): void - { - $options = $this->getMockBuilder(RegistrationOptionsInterface::class) - ->getMock(); - $options->expects($this->once()) - ->method('getEnableUsername') - ->will($this->returnValue(false)); - $options->expects($this->once()) - ->method('getEnableDisplayName') - ->will($this->returnValue(false)); - $options->expects($this->any()) - ->method('getUseRegistrationFormCaptcha') - ->will($this->returnValue(false)); - - $captcha = $this->getMockBuilder(Captcha::class) - ->getMock(); - $form = new Form(null, $options); - - $form->setCaptchaElement($captcha); - - $reflection = $this->helperMakePropertyAccessable($form, 'captchaElement'); - $this->assertSame($captcha, $reflection->getValue($form)); - } - - /** * * @param mixed $objectOrClass