1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
class ChangePasswordFormTest extends \Codeception\Test\Unit |
8
|
|
|
{ |
9
|
|
|
private $user; |
10
|
|
|
|
11
|
|
|
public function setUp() { |
12
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
13
|
|
|
$this->user = $this->getMockBuilder('\site\tests\_support\MockUser') |
14
|
|
|
->setMethods(['validatePassword', 'setPassword', 'save']) |
15
|
|
|
->getMock(); |
16
|
|
|
parent::setUp(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function tearDown() { |
20
|
|
|
$this->user = null; |
21
|
|
|
parent::tearDown(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testSimpleRulesShouldValidate() { |
25
|
|
|
$user = $this->user; |
26
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
27
|
|
|
$form = $this->container->get('\site\models\ChangePasswordForm', [$user]); |
28
|
|
|
|
29
|
|
|
$form->attributes = []; |
30
|
|
|
expect('with no values, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
31
|
|
|
$form->attributes = ['old_password' => '12345678']; |
32
|
|
|
expect('with one value, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
33
|
|
|
$form->attributes = ['new_password' => '12345678']; |
34
|
|
|
expect('with one value, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$user |
37
|
|
|
->expects($this->never()) |
38
|
|
|
->method('validatePassword'); |
39
|
|
|
$form->attributes = [ |
40
|
|
|
'old_password' => '123456', |
41
|
|
|
'new_password' => '789012', |
42
|
|
|
]; |
43
|
|
|
expect('with a new password of < 8 chars, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testValidatePasswordShouldFailWithABadPassword() { |
47
|
|
|
$user = $this->user; |
48
|
|
|
|
49
|
|
|
$old = 'not_my_current_pass'; |
50
|
|
|
$user |
51
|
|
|
->expects($this->once()) |
52
|
|
|
->method('validatePassword') |
53
|
|
|
->with($this->equalTo($old)); |
54
|
|
|
$user |
55
|
|
|
->method('validatePassword') |
56
|
|
|
->willReturn(false); |
57
|
|
|
|
58
|
|
|
$form = $this->container->get('\site\models\ChangePasswordForm', [$user]); |
59
|
|
|
$form->attributes = [ |
60
|
|
|
'old_password' => $old, |
61
|
|
|
'new_password' => 'my_desired_Pass', |
62
|
|
|
]; |
63
|
|
|
expect('a failed password should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
64
|
|
|
expect('a failed password should add an error fail validation', $this->assertGreaterThan(0, count($form->errors))); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testValidatePasswordShouldPassWithAGoodPassword() { |
68
|
|
|
$user = $this->user; |
69
|
|
|
$old = 'my_current_pass'; |
70
|
|
|
|
71
|
|
|
$user |
72
|
|
|
->method('validatePassword') |
73
|
|
|
->willReturn(true); |
74
|
|
|
$user |
75
|
|
|
->expects($this->once()) |
76
|
|
|
->method('validatePassword') |
77
|
|
|
->with($this->equalTo($old)); |
78
|
|
|
|
79
|
|
|
$form = $this->container->get('\site\models\ChangePasswordForm', [$user]); |
80
|
|
|
$form->attributes = [ |
81
|
|
|
'old_password' => $old, |
82
|
|
|
'new_password' => 'my_desired_Pass', |
83
|
|
|
]; |
84
|
|
|
expect('a good password should pass validation', $this->assertTrue($form->validate())); |
|
|
|
|
85
|
|
|
expect('a good password should not add any errors', $this->assertEquals(0, count($form->errors))); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testChangePassword() { |
89
|
|
|
$user = $this->user; |
90
|
|
|
$old = 'my_current_pass'; |
91
|
|
|
$new = 'my_desired_Pass'; |
92
|
|
|
|
93
|
|
|
$user |
94
|
|
|
->method('save') |
95
|
|
|
->willReturn(true); |
96
|
|
|
$user |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('setPassword') |
99
|
|
|
->with($this->equalTo($new)); |
100
|
|
|
|
101
|
|
|
$form = $this->container->get('\site\models\ChangePasswordForm', [$user]); |
102
|
|
|
$form->attributes = [ |
103
|
|
|
'old_password' => $old, |
104
|
|
|
'new_password' => $new, |
105
|
|
|
]; |
106
|
|
|
expect('changePassword should return true if save returns true', $this->assertTrue($form->changePassword())); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|