I'm using the CSRF hidden hash element with Zend_Form and trying to Unit Test the login but don't know how to write a Unit Test to include that element. Looked in the docs and read as many tutorials as I could find. I even delicioused them all, but no one mentions this.
相关问题
- Multiple Django sites on the same domain - CSRF fa
- Zend Auth locked session
- selenium driver, option inside optgroup fails to e
- Zend/PHP: Problem uploading/downloading file to/fr
- CSRF Middleware - change csrf_token output (from x
相关文章
- Possible disadvantages of Zend [closed]
- Is there a way to tell whether there are failures
- In what case can CSRF-exempt be dangerous?
- HowTo PHPUnit assertFunction
- How to setup CSRF in JavaScript for laravel?
- Zend Framework Modules with common resources
- Zend Form Validator Callback: How to exclude a use
- Exception while setting up the wurfl in zend
I set an environment variable in my Apache vhost file, which tells the code which server it's running on: development, staging, or production
The line for the vhost file is:
Then I just make my forms react to the appropriate environment:
I use this same technique for lots of stuff. For example, if it IS dev, I redirect all outgoing email to me, etc.
Solution for ZF2 is creating your form in test, and getting value from your csrf form element:
I answered a more recent question similar to this one. I'm putting my answer here as well in case it helps anybody in the future.
I recently found a great way of testing forms with hash elements. This will use a mock object to stub away the hash element and you won't have to worry about it. You won't even have to do a session_start or anything this way. You won't have to 'prerender' the form either.
First create a 'stub' class like so
Then, add the following to the form somewhere.
The set method is there just for testing purposes really. You probably won't use it at all during real use but now in phpunit you can right the following.
You HAVE to create your own stub. You can't just call the phpunit method
getMockObject()
because that will directly extend the hash element and the normal hash element does 'evil' stuff in its constructor.With this method you don't even need to be connected to a database to test your forms! It took me a while to think of this.
If you want, you can push the
setHashElement()
method ( along with the variable and the get method ) into some FormAbstract base class.REMEMBER, in phpunit you HAVE to pass the hash element during form construction. If you don't, your
init()
method will get called before your stub hash can be set with the set method and you'll end up using the regular hash element. You'll know you're using the regular hash element because you'll probably get some session error if you're NOT connected to a database.Let me know if you find this helpful or if you use it.
The correct hash is stored in the session, and the Hash form element has a Zend_Session_Namespace instance which contains the namespace for the hash.
To unit test the element, you would replace the Zend_Session_Namespace instance in the element (with setSession) with one you create yourself which contains the correct hash (the hash is stored in key "hash")
For further examples you could probably look at the Zend Framework unit tests for the Zend_Form_Element_Hash class. I would assume they have had to deal with this as well.
Csrf value is generated each time form is rendered. Hidden element of the form gets prefilled with that value. This value also gets stored in session. After submitting form, validation checks if value posted from the form is stored in session, if not then validation fails. It is essential, that form must be rendered during the test (so it can generate the hidden value and store it to session), then we can extract what is the hidden value out of rendered html, and later we can add hidden hash value into our request. Consider this example: