How to test Zend framework2 form which implements CSRF element? -
i have created login form using zend/form , contains input elements csrf element.
<!-- sample login.phtml code snippet --> <form method="post" action="/xyz"> <?php echo $this->formelement($form->get('email')); echo $this->formelement($form->get('password')); echo $this->formelement($form->get('csrf')); echo $this->formelement($form->get('submit')); ?> </form>
the following testcase fails because of validation against csrf element.
public function testlogin() { //code goes here $params = array('email' => 'example@test.com', 'password' => 'example@test.com'); $this->dispatch($url, $method, $params); //if given username , password correct login page redirected default home page $this->assertresponsestatuscode(302); }
in above testcase set email , password values don't know how set csrf element. how test form implements csrf element?
this can achieved following:
$form = new loginform(); $csrf = $form->get('csrf')->getvalue(); $params = array('email' => 'example@test.com', 'password' => 'example@test.com', 'csrf' => $csrf );
unless form needs form manager when going have use rather instantiating class that's pretty easy in unit test well.
update:
another method works:
$form = new loginform(); $form->prepare(); $params = new \zend\stdlib\parameters( array( 'csrf' => $_session['zend_validator_csrf_salt_csrf']['hash'], 'yourotherparams' => 'thingsandstuff' ); $this->dispatch($url, $method, $params);
Comments
Post a Comment