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