1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\ChangeEmailForm; |
7
|
|
|
|
8
|
|
|
class ChangeEmailFormTest extends \Codeception\Test\Unit { |
9
|
|
|
use \Codeception\Specify; |
10
|
|
|
|
11
|
|
|
public function testSimpleRulesShouldValidate() { |
12
|
|
|
$user = $this->getUser(); |
13
|
|
|
$form = new ChangeEmailForm($user); |
14
|
|
|
|
15
|
|
|
$form->attributes = []; |
16
|
|
|
expect('with no values, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
17
|
|
|
$form->attributes = ['desired_email' => 'not_an_email']; |
18
|
|
|
expect('with a value that is not an email, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
19
|
|
|
$form->attributes = ['desired_email' => '[email protected]']; |
20
|
|
|
expect('with a valid email, the form should pass validation', $this->assertTrue($form->validate())); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testChangeEmail() { |
24
|
|
|
|
25
|
|
|
$this->specify('changeEmail() should return true and do nothing when the email is already taken', function() { |
26
|
|
|
$user = $this->getUser(); |
27
|
|
|
$user |
28
|
|
|
->expects($this->never()) |
29
|
|
|
->method('save'); |
30
|
|
|
$desired_email = '[email protected]'; |
31
|
|
|
$user |
32
|
|
|
->method('findByEmail') |
|
|
|
|
33
|
|
|
->willReturn(true); // true, as a stand-in for some valid user object |
34
|
|
|
|
35
|
|
|
$form = new ChangeEmailForm($user); |
36
|
|
|
$form->desired_email = $desired_email; |
37
|
|
|
$this->assertTrue($form->changeEmail(), 'should always return true'); |
38
|
|
|
$this->assertNull($user->desired_email, 'should be null because we didn\'t set the new email'); |
|
|
|
|
39
|
|
|
$this->assertNotEquals($user->desired_email, $desired_email); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$this->specify('changeEmail() should return true, set values, and send an email when the email is available', function() { |
43
|
|
|
$user = $this->getUser(); |
44
|
|
|
$desired_email = '[email protected]'; |
45
|
|
|
$user |
46
|
|
|
->method('findByEmail') |
47
|
|
|
->willReturn(null); |
48
|
|
|
$user->email = '[email protected]'; |
|
|
|
|
49
|
|
|
|
50
|
|
|
$form = new ChangeEmailForm($user); |
51
|
|
|
$form->desired_email = $desired_email; |
52
|
|
|
|
53
|
|
|
expect('changeEmail should return true', $this->assertTrue($form->changeEmail())); |
|
|
|
|
54
|
|
|
|
55
|
|
|
expect('the user\'s desired email should now match what they entered in the form', $this->assertEquals($user->desired_email, $desired_email)); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->tester->seeEmailIsSent(); |
|
|
|
|
58
|
|
|
$emailMessage = $this->tester->grabLastSentEmail(); |
59
|
|
|
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); |
60
|
|
|
expect($emailMessage->getTo())->hasKey($desired_email); |
61
|
|
|
expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']); |
62
|
|
|
expect($emailMessage->getSubject())->equals(Yii::$app->name . ' -- your requested email change'); |
63
|
|
|
expect($emailMessage->toString())->contains($user->email); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getUser() { |
68
|
|
|
$user = $this->getmockbuilder('\common\models\user') |
69
|
|
|
->disableoriginalconstructor() |
70
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save']) |
71
|
|
|
->getmock(); |
72
|
|
|
$user->method('attributes')->willreturn([ |
73
|
|
|
'email', |
74
|
|
|
'desired_email', |
75
|
|
|
'change_email_token', |
76
|
|
|
]); |
77
|
|
|
return $user; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.