|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use \site\models\ResetPasswordForm; |
|
7
|
|
|
use \yii\base\InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class ResetPasswordFormTest extends \Codeception\Test\Unit { |
|
10
|
|
|
use \Codeception\Specify; |
|
11
|
|
|
|
|
12
|
|
|
public function testShouldThrowWhenNotPassedAWellFormattedToken() { |
|
13
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
14
|
|
|
new ResetPasswordForm('', $this->getUser()); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testShouldThrowWhenTokenIsNotAssociatedWithAUser() { |
|
18
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
19
|
|
|
$token = 'this_is_fine'; |
|
20
|
|
|
$user = $this->getUser(); |
|
21
|
|
|
$user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn(null); |
|
|
|
|
|
|
22
|
|
|
new ResetPasswordForm($token, $user); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testRulesShouldValidate() { |
|
26
|
|
|
$token = 'doesnt_matter'; |
|
27
|
|
|
$user = $this->getUser(); |
|
28
|
|
|
$user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn($user); |
|
29
|
|
|
$form = new ResetPasswordForm($token, $user); |
|
30
|
|
|
|
|
31
|
|
|
$form->attributes = []; |
|
32
|
|
|
expect('with no values, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
|
|
33
|
|
|
$form->attributes = ['password' => null]; |
|
34
|
|
|
expect('the form should not validate if the password is not provided', $this->assertFalse($form->validate())); |
|
|
|
|
|
|
35
|
|
|
$form->attributes = ['password' => '1']; |
|
36
|
|
|
expect('the form should not validate if the password is too short', $this->assertFalse($form->validate())); |
|
|
|
|
|
|
37
|
|
|
$form->attributes = ['password' => 'super-secret-password-here']; |
|
38
|
|
|
expect('the form should validate if the provided password is long enough', $this->assertTrue($form->validate())); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testResetPassword() { |
|
42
|
|
|
$user = $this->getUser(); |
|
43
|
|
|
$token = 'doesnt_matter'; |
|
44
|
|
|
$user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn($user); |
|
45
|
|
|
$user->password = 'old'; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$form = new ResetPasswordForm($token, $user); |
|
48
|
|
|
|
|
49
|
|
|
$user |
|
50
|
|
|
->expects($this->once()) |
|
51
|
|
|
->method('removepasswordresettoken'); |
|
52
|
|
|
$user |
|
53
|
|
|
->expects($this->once()) |
|
54
|
|
|
->method('save'); |
|
55
|
|
|
|
|
56
|
|
|
$form->password = 'new'; |
|
57
|
|
|
$form->resetPassword(); |
|
58
|
|
|
$this->assertNotEquals($user->password, 'old'); |
|
59
|
|
|
$this->assertEquals($user->password, 'new'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function getUser() { |
|
63
|
|
|
$user = $this->getmockbuilder('\common\models\user') |
|
64
|
|
|
->disableoriginalconstructor() |
|
65
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'save', 'findbypasswordresettoken', 'removepasswordresettoken']) |
|
66
|
|
|
->getmock(); |
|
67
|
|
|
$user->method('attributes')->willReturn([ |
|
68
|
|
|
'email', |
|
69
|
|
|
'password', |
|
70
|
|
|
'password_hash', |
|
71
|
|
|
'partner_email1', |
|
72
|
|
|
'partner_email2', |
|
73
|
|
|
'partner_email3', |
|
74
|
|
|
]); |
|
75
|
|
|
return $user; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.