Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Description of UserChangePassword
- *
- * @author jon
- */
- class Form_UserChangePassword
- extends Zend_Form
- {
- /**
- *
- * @var \Model_User
- */
- protected $_user;
- /**
- *
- * @param \Model_User $user
- * @param array $options
- */
- public function __construct(\Model_User $user,$options = array())
- {
- parent::__construct($options);
- $this->_user = $user;
- }
- public function init()
- {
- $this->addElements(array(
- $this->createElement('password' , 'currentPassword'),
- $this->createElement('password' , 'newPassword'),
- $this->createElement('password' , 'newPasswordConfirm')->setRequired(true)
- ));
- }
- public function process($data)
- {
- if (!($this->isValid($data)))
- {
- throw new Exception('Form Validation Failed');
- }
- // we can do some more validation?
- if ($this->newPassword->getValue() != $this->newPasswordConfirm->getValue()) {
- throw new Exception('Passwords don\'t match');
- }
- if ($this->currentPassword->getValue() != $this->_user->password) {
- throw new Exception('Current password is incorrect');
- }
- // Do the actual processing
- $this->_user->password = $this->newPassword->getValue();
- $this->_user->save();
- }
- }
- <?php
- /**
- * Description of UserChangedPasswordTest
- *
- * @author jon
- */
- class Form_UserChangePasswordTest
- extends PHPUnit_Framework_TestCase
- {
- public function testCanCreateForm()
- {
- $u = new Model_User();
- $f = new Form_UserChangePassword($u);
- $this->assertType('Form_UserChangePassword',$f);
- }
- public function testCanProcessValidCredentials()
- {
- $u = new Model_User();
- $u->username = 'myuser';
- $u->password = 'abc123';
- $form = new Form_UserChangePassword($u);
- $formData = array(
- 'currentPassword' => 'abc123',
- 'newPassword' => 'pass2',
- 'newPasswordConfirm' => 'pass2'
- );
- $form->process($formData);
- $this->assertEquals('pass2',$u->password);
- }
- public function testCanCatchInvalidFormValues()
- {
- $u = new Model_User();
- $u->username = 'myuser';
- $u->password = 'abc123';
- $form = new Form_UserChangePassword($u);
- $formData = array(
- 'currentPassword' => 'XXX',
- 'newPassword' => 'pass2',
- 'newPasswordConfirm' => 'pass2'
- );
- try
- {
- $form->process($formData);
- $this->fail('error not trapped');
- }
- catch (Exception $e)
- {
- $this->assertEquals('Current password is incorrect',$e->getMessage());
- }
- $formData = array(
- 'currentPassword' => 'XXX',
- 'newPassword' => 'pass2'
- );
- try
- {
- $form->process($formData);
- $this->fail('error not trapped');
- }
- catch (Exception $e)
- {
- $this->assertEquals('Form Validation Failed',$e->getMessage());
- }
- $formData = array(
- 'currentPassword' => 'abc123',
- 'newPassword' => 'pass2',
- 'newPasswordConfirm' => 'dsds'
- );
- try
- {
- $form->process($formData);
- $this->fail('error not trapped');
- }
- catch (Exception $e)
- {
- $this->assertEquals('Passwords don\'t match',$e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement