1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\DeleteAccountForm; |
7
|
|
|
|
8
|
|
|
class DeleteAccountFormTest extends \Codeception\Test\Unit { |
9
|
|
|
use \Codeception\Specify; |
10
|
|
|
|
11
|
|
|
public function testRulesShouldValidate() { |
12
|
|
|
$user = $this->getUser(); |
13
|
|
|
$form = new DeleteAccountForm($user); |
14
|
|
|
|
15
|
|
|
$form->attributes = []; |
16
|
|
|
expect('with no values, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
17
|
|
|
$form->attributes = ['password' => null]; |
18
|
|
|
expect('the form should not validate if the password is not provided', $this->assertFalse($form->validate())); |
|
|
|
|
19
|
|
|
$form->attributes = ['password' => '1']; |
20
|
|
|
expect('the form should not validate if the password is too short', $this->assertFalse($form->validate())); |
|
|
|
|
21
|
|
|
$form->attributes = ['password' => 'super-secret-password-here']; |
22
|
|
|
expect('the form should validate if the provided password is long enough', $this->assertTrue($form->validate())); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testDeleteAccount() { |
26
|
|
|
$this->specify('deleteAccount() should return false if the form does not validate', function() { |
27
|
|
|
$user = $this->getUser(); |
28
|
|
|
$user |
29
|
|
|
->expects($this->never()) |
30
|
|
|
->method('delete'); |
31
|
|
|
|
32
|
|
|
$form = new DeleteAccountForm($user); |
33
|
|
|
$form->password = '1'; |
34
|
|
|
$this->assertFalse($form->deleteAccount(), 'deleteAccount should not be successful'); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$this->specify('deleteAccount() should return false if the user\'s password is incorrect', function() { |
38
|
|
|
$user = $this->getUser(); |
39
|
|
|
$user |
40
|
|
|
->expects($this->never()) |
41
|
|
|
->method('delete'); |
42
|
|
|
|
43
|
|
|
$form = new DeleteAccountForm($user); |
44
|
|
|
$form->password = '1'; |
45
|
|
|
$this->assertFalse($form->deleteAccount(), 'deleteAccount should not be successful'); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
$this->specify('deleteAccount() should return true, send a notification email, and delete the user when the form validates and the user\'s password is correct', function() { |
49
|
|
|
$password = 'password'; |
50
|
|
|
$user = $this->getUser(); |
51
|
|
|
$user->email = '[email protected]'; |
|
|
|
|
52
|
|
|
$user->partner_email1 = '[email protected]'; |
|
|
|
|
53
|
|
|
$user->setPassword($password); |
|
|
|
|
54
|
|
|
$user |
55
|
|
|
->method('isPartnerEnabled') |
|
|
|
|
56
|
|
|
->willReturn(true); |
57
|
|
|
|
58
|
|
|
$user |
59
|
|
|
->expects($this->once()) |
60
|
|
|
->method('delete'); |
61
|
|
|
|
62
|
|
|
$form = new DeleteAccountForm($user); |
63
|
|
|
$form->password = $password; |
64
|
|
|
|
65
|
|
|
$this->assertTrue($form->deleteAccount(), 'should be successful'); |
66
|
|
|
|
67
|
|
|
$this->tester->seeEmailIsSent(); |
|
|
|
|
68
|
|
|
$emailMessage = $this->tester->grabLastSentEmail(); |
69
|
|
|
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); |
70
|
|
|
// an email was sent to both the user |
71
|
|
|
// and to partner1. We grab the most recently sent one to examine though, thus partner1 |
72
|
|
|
expect($emailMessage->getTo())->hasKey('[email protected]'); |
73
|
|
|
expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']); |
74
|
|
|
expect($emailMessage->getSubject())->equals('[email protected] has deleted their The Faster Scale App account'); |
75
|
|
|
expect($emailMessage->toString())->contains($user->email); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function getUser() { |
80
|
|
|
$user = $this->getmockbuilder('\common\models\user') |
81
|
|
|
->disableoriginalconstructor() |
82
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save', 'delete', 'ispartnerenabled']) |
83
|
|
|
->getmock(); |
84
|
|
|
$user->method('attributes')->willreturn([ |
85
|
|
|
'email', |
86
|
|
|
'password', |
87
|
|
|
'password_hash', |
88
|
|
|
'partner_email1', |
89
|
|
|
'partner_email2', |
90
|
|
|
'partner_email3', |
91
|
|
|
]); |
92
|
|
|
return $user; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.