Unit test forms that have a hash element in the Zend Framework

I am trying to verify the valid form data in one of my Zend_Forms, however it does not work due to the presence of a hash element that is generated randomly, and I cannot access the generated hash to return it back to the statement data. For instance.

$form = new MyForm();
$data = array('username'=>'test');
$this->assertTrue($form->isValid($data));

This does not work because it does not contain a hash value.

+3
source share
3 answers

I had the same problem when my form had a captcha and I wanted to test it. Two possible solutions that I cannot think of:

  • Print the form first (then a hash will be generated), then take this element, take the value and use it to check the form.
  • - .
+4

. , . - :

  • .
  • .

, :

public function testLoginSetsSession()
{
    // must render the form first to generate the CSRF hash
    $form = new Form_Login();
    $form->render();

    $this->request
         ->setMethod('POST')
         ->setPost(array(
             'email' => 'test@test.co.uk',
             'password' => 'password',
             'hash' => $form->hash->getValue()
         ));
    $this->dispatch('/login');

    $this->assertTrue(Zend_Auth::getInstance()->hasIdentity());
}
+3

. , -, . session_start - . "prerender".

'stub',



    class My_Form_Element_HashStub extends Zend_Form_Element_Hash
    {
        public function __construct(){}
    }


-.


    class MyForm extends Zend_Form{

    protected $_hashElement;

    public function setHashElement( Zend_Form_Hash_Element $hash )
    { 
      $this->_hashElement = $hash; 
      return $this; 
    }

    protected function _getHashElement( $name = 'hashElement' )
    { 
      if( !isset( $this->_hashElement )
      {
          if( isset( $name ) )
          {
                $element = new Zend_Form_Element_Hash( $name, 
                                                      array( 'id' => $name ) );
          }
          else
          {
                $element = new Zend_Form_Element_Hash( 'hashElement', 
                                            array( 'id' => 'hashElement' ) );
          }

         $this->setHashElement( $element );
         return $this->_hashElement;
      }
    }
    /**
     *
     * In your init method you can now add the hash element like below
     *
     *
     *
     */

    public function init()
    {
        //other code
        $this->addElement( $this->_getHashElement( 'myotherhashelementname' );
        //other code
    }

set . , , , phpunit .

class My_Form_LoginTest
        extends PHPUnit_Framework_TestCase
{

    /**
     *
     * @var My_Form_Login
     */
    protected $_form;
    /**
     *
     * @var PHPUnit_Framework_MockObject_MockObject
     */
    protected $_hash;

    public function setUp()
    {
        parent::setUp();
        $this->_hash = $this->getMock( 'My_Form_Element_HashStub' );

        $this->_form = new My_Form_Login( array(
                    'action'                    => '/',
                    'hashElement'               => $this->_hash
    }

    public function testTrue()
    {   
        //The hash element will now always validate to true
        $this->_hash
             ->expects( $this->any() )
             ->method( 'isValid' )
             ->will( $this->returnValue( true ) );

        //OR if you need it to validate to false
                $this->_hash
             ->expects( $this->any() )
             ->method( 'isValid' )
             ->will( $this->returnValue( true ) );

    }

. phpunit getMockObject, -, - "" .

! , .

, setHashElement ( get) FormAbstract.

REMEMBER, in phpunit you need to pass a hash element during the construction of the form. If you do not, your init method will be called before your hash stub can be set using the set method, and you will end up using a regular hash element. You will find out that you are using a regular hash element because you are likely to get some session error if you are NOT connected to the database.

Let me know if you find it useful or use it.

+1
source

Source: https://habr.com/ru/post/1793821/


All Articles