Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.68 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Description of UserChangePassword
  4.  *
  5.  * @author jon
  6.  */
  7. class Form_UserChangePassword
  8.     extends Zend_Form
  9. {
  10.     /**
  11.      *
  12.      * @var \Model_User
  13.      */
  14.     protected $_user;
  15.    
  16.     /**
  17.      *
  18.      * @param \Model_User $user
  19.      * @param array $options
  20.      */
  21.     public function __construct(\Model_User $user,$options = array())
  22.     {
  23.         parent::__construct($options);
  24.         $this->_user = $user;
  25.     }
  26.  
  27.     public function init()
  28.     {
  29.         $this->addElements(array(
  30.             $this->createElement('password' , 'currentPassword'),
  31.             $this->createElement('password'     , 'newPassword'),
  32.             $this->createElement('password' , 'newPasswordConfirm')->setRequired(true)
  33.         ));
  34.     }
  35.  
  36.     public function process($data)
  37.     {
  38.         if (!($this->isValid($data)))
  39.         {      
  40.             throw new Exception('Form Validation Failed');
  41.         }
  42.         // we can do some more validation?
  43.         if ($this->newPassword->getValue() != $this->newPasswordConfirm->getValue()) {
  44.             throw new Exception('Passwords don\'t match');
  45.         }
  46.  
  47.         if ($this->currentPassword->getValue() != $this->_user->password) {
  48.             throw new Exception('Current password is incorrect');
  49.         }
  50.  
  51.         // Do the actual processing
  52.         $this->_user->password = $this->newPassword->getValue();
  53.         $this->_user->save();
  54.     }
  55.  
  56. }
  57.  
  58.  
  59.  
  60. <?php
  61. /**
  62.  * Description of UserChangedPasswordTest
  63.  *
  64.  * @author jon
  65.  */
  66. class Form_UserChangePasswordTest
  67.     extends PHPUnit_Framework_TestCase
  68. {
  69.     public function testCanCreateForm()
  70.     {
  71.         $u = new Model_User();
  72.         $f = new Form_UserChangePassword($u);
  73.         $this->assertType('Form_UserChangePassword',$f);
  74.     }
  75.    
  76.     public function testCanProcessValidCredentials()
  77.     {
  78.         $u = new Model_User();
  79.         $u->username = 'myuser';
  80.         $u->password = 'abc123';
  81.  
  82.         $form = new Form_UserChangePassword($u);
  83.  
  84.         $formData = array(
  85.           'currentPassword'     => 'abc123',
  86.           'newPassword'         => 'pass2',
  87.           'newPasswordConfirm'  => 'pass2'
  88.         );
  89.  
  90.         $form->process($formData);
  91.  
  92.         $this->assertEquals('pass2',$u->password);
  93.     }
  94.  
  95.     public function testCanCatchInvalidFormValues()
  96.     {
  97.         $u = new Model_User();
  98.         $u->username = 'myuser';
  99.         $u->password = 'abc123';
  100.  
  101.         $form = new Form_UserChangePassword($u);
  102.  
  103.         $formData = array(
  104.           'currentPassword'     => 'XXX',
  105.           'newPassword'         => 'pass2',
  106.           'newPasswordConfirm'  => 'pass2'
  107.         );
  108.         try
  109.         {
  110.             $form->process($formData);
  111.             $this->fail('error not trapped');
  112.         }
  113.         catch (Exception $e)
  114.         {
  115.             $this->assertEquals('Current password is incorrect',$e->getMessage());
  116.         }
  117.  
  118.         $formData = array(
  119.           'currentPassword'     => 'XXX',
  120.           'newPassword'  => 'pass2'
  121.         );
  122.         try
  123.         {
  124.             $form->process($formData);
  125.             $this->fail('error not trapped');
  126.         }
  127.         catch (Exception $e)
  128.         {
  129.             $this->assertEquals('Form Validation Failed',$e->getMessage());
  130.         }
  131.  
  132.         $formData = array(
  133.           'currentPassword'     => 'abc123',
  134.           'newPassword'         => 'pass2',
  135.           'newPasswordConfirm'  => 'dsds'
  136.         );
  137.         try
  138.         {
  139.             $form->process($formData);
  140.             $this->fail('error not trapped');
  141.         }
  142.         catch (Exception $e)
  143.         {
  144.             $this->assertEquals('Passwords don\'t match',$e->getMessage());
  145.         }
  146.  
  147.        
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement