1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\EditProfileForm; |
7
|
|
|
|
8
|
|
|
class EditProfileFormTest extends \Codeception\Test\Unit { |
9
|
|
|
public $graph; |
10
|
|
|
public $values = [ |
11
|
|
|
'timezone' => 'America/Los_Angeles', |
12
|
|
|
'send_email' => true, |
13
|
|
|
'email_category' => 4, |
14
|
|
|
'partner_email1' => '[email protected]', |
15
|
|
|
'partner_email2' => '[email protected]', |
16
|
|
|
'partner_email3' => '[email protected]', |
17
|
|
|
'expose_graph' => true, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
public function setUp() { |
21
|
|
|
$this->graph = $this->getMockBuilder(common\components\Graph::class) |
|
|
|
|
22
|
|
|
->setMethods(['create', 'destroy']) |
23
|
|
|
->getMock(); |
24
|
|
|
parent::setUp(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testLoadUser() { |
28
|
|
|
$user = $this->getUser(); |
29
|
|
|
$form = new \site\models\EditProfileForm($user); |
30
|
|
|
$form->loadUser(); |
31
|
|
|
expect('timezone should be equal', $this->assertEquals($form->timezone, $user->timezone)); |
|
|
|
|
32
|
|
|
expect('partner_email1 should be equal', $this->assertEquals($form->partner_email1, $user->partner_email1)); |
|
|
|
|
33
|
|
|
expect('partner_email2 should be equal', $this->assertEquals($form->partner_email2, $user->partner_email2)); |
|
|
|
|
34
|
|
|
expect('partner_email3 should be equal', $this->assertEquals($form->partner_email3, $user->partner_email3)); |
|
|
|
|
35
|
|
|
expect('expose_graph should be equal', $this->assertEquals($form->expose_graph, $user->expose_graph)); |
|
|
|
|
36
|
|
|
expect('send_email should be true', $this->assertTrue($form->send_email)); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$user->send_email = false; |
|
|
|
|
39
|
|
|
$form->loadUser(); |
40
|
|
|
expect('send_email should be false', $this->assertFalse($form->send_email)); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$user->partner_email1 = null; |
43
|
|
|
$user->partner_email2 = null; |
44
|
|
|
$user->partner_email3 = null; |
45
|
|
|
$form->loadUser(); |
46
|
|
|
expect('send_email should be false', $this->assertFalse($form->send_email)); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSaveProfile() { |
50
|
|
|
$user = $this->getUser(); |
51
|
|
|
$form = new \site\models\EditProfileForm($user); |
52
|
|
|
$form->loadUser(); |
53
|
|
|
|
54
|
|
|
// Set up dependency injections |
55
|
|
|
Yii::$container |
56
|
|
|
->set(\common\interfaces\TimeInterface::class, function () { return new \common\components\Time('UTC'); }); |
57
|
|
|
Yii::$container |
58
|
|
|
->set(\common\components\Graph::class, function () { return $this->graph; }); |
59
|
|
|
Yii::$container |
60
|
|
|
->set(\common\interfaces\UserBehaviorInterface::class, function () { return new FakeUserBehavior(); }); |
61
|
|
|
|
62
|
|
|
// Actually begin testing |
63
|
|
|
$form->attributes = $this->values; |
64
|
|
|
expect('saveProfile should return the user', $this->assertEquals($user, $form->saveProfile())); |
|
|
|
|
65
|
|
|
expect('saveProfile should set the user\'s attributes to be the form values', $this->assertEquals($this->values, $user->attributes)); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$form->send_email = 'not_a_boolean'; |
68
|
|
|
expect('saveProfile should return the user with the partner-related settings equal to null', $this->assertNull($form->saveProfile())); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$null_vals = [ |
71
|
|
|
'partner_email1' => null, |
72
|
|
|
'partner_email2' => null, |
73
|
|
|
'partner_email3' => null, |
74
|
|
|
'send_email' => false, |
75
|
|
|
'email_category' => 4, |
76
|
|
|
'timezone' => 'America/Los_Angeles', |
77
|
|
|
'expose_graph' => true, |
78
|
|
|
]; |
79
|
|
|
$form->send_email = false; |
80
|
|
|
$ret = $form->saveProfile(); |
|
|
|
|
81
|
|
|
expect('saveProfile should return the user with the partner-related settings equal to null', $this->assertEquals($null_vals, $user->attributes)); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->graph |
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('destroy'); |
86
|
|
|
$form->expose_graph = false; |
87
|
|
|
$ret = $form->saveProfile(); |
88
|
|
|
expect('if form->expose_graph is set to false, destroy the graph and set user->expose_graph to false', $this->assertFalse($ret->expose_graph)); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->graph |
91
|
|
|
->expects($this->once()) |
92
|
|
|
->method('create'); |
93
|
|
|
$form->expose_graph = true; |
94
|
|
|
$ret = $form->saveProfile(); |
95
|
|
|
expect('if form->expose_graph is set to true, create the graph and set user->expose_graph to true', $this->assertTrue($ret->expose_graph)); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$form->send_email = 'not_a_boolean'; |
98
|
|
|
expect('saveProfile should return null if the form validation fails', $this->assertNull($form->saveProfile())); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getUser() { |
102
|
|
|
$user = $this->getmockbuilder('\common\models\User') |
103
|
|
|
->disableoriginalconstructor() |
104
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'save', 'findbypasswordresettoken', 'removepasswordresettoken', 'getid']) |
105
|
|
|
->getmock(); |
106
|
|
|
$user->method('attributes')->willReturn([ |
107
|
|
|
'timezone', |
108
|
|
|
'partner_email1', |
109
|
|
|
'partner_email2', |
110
|
|
|
'partner_email3', |
111
|
|
|
'send_email', |
112
|
|
|
'email_category', |
113
|
|
|
'expose_graph', |
114
|
|
|
]); |
115
|
|
|
$user->timezone = 'America/Los_Angeles'; |
|
|
|
|
116
|
|
|
$user->partner_email1 = '[email protected]'; |
|
|
|
|
117
|
|
|
$user->partner_email2 = '[email protected]'; |
|
|
|
|
118
|
|
|
$user->partner_email3 = '[email protected]'; |
|
|
|
|
119
|
|
|
$user->send_email = true; |
|
|
|
|
120
|
|
|
$user->email_category = 4; |
|
|
|
|
121
|
|
|
$user->expose_graph = true; |
|
|
|
|
122
|
|
|
return $user; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
class FakeUserBehavior { |
127
|
|
|
public $scores = true; |
128
|
|
|
public function getCheckInBreakdown() { return $this->scores; } |
129
|
|
|
} |
130
|
|
|
|