forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserValidationTraitTest.php
More file actions
194 lines (180 loc) · 5.77 KB
/
Copy pathUserValidationTraitTest.php
File metadata and controls
194 lines (180 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Test\TestCase\Controller\Traits;
use CakeDC\Users\Test\TestCase\Controller\Traits\BaseTraitTest;
use Cake\Network\Request;
class UserValidationTraitTest extends BaseTraitTest
{
/**
* setup
*
* @return void
*/
public function setUp()
{
$this->traitClassName = 'CakeDC\Users\Controller\Traits\UserValidationTrait';
$this->traitMockMethods = ['dispatchEvent', 'isStopped', 'redirect', 'getUsersTable', 'set'];
$this->mockDefaultEmail = true;
parent::setUp();
}
/**
* test
*
* @return void
*/
public function testValidateHappyEmail()
{
$this->_mockFlash();
$user = $this->table->findByToken('token-3')->first();
$this->assertFalse($user->active);
$this->Trait->Flash->expects($this->once())
->method('success')
->with('User account validated successfully');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->validate('email', 'token-3');
$user = $this->table->findById($user->id)->first();
$this->assertTrue($user->active);
}
/**
* test
*
* @return void
*/
public function testValidateUserNotFound()
{
$this->_mockFlash();
$this->Trait->Flash->expects($this->once())
->method('error')
->with('Invalid token or user account already validated');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->validate('email', 'not-found');
}
/**
* test
*
* @return void
*/
public function testValidateTokenExpired()
{
$this->_mockFlash();
$this->Trait->Flash->expects($this->once())
->method('error')
->with('Token already expired');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->validate('email', '6614f65816754310a5f0553436dd89e9');
}
/**
* test
*
* @return void
*/
public function testValidateInvalidOp()
{
$this->_mockFlash();
$this->Trait->Flash->expects($this->once())
->method('error')
->with('Invalid validation type');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->validate('invalid-op', '6614f65816754310a5f0553436dd89e9');
}
/**
* test
*
* @return void
*/
public function testValidateHappyPassword()
{
$this->_mockRequestGet();
$this->_mockFlash();
$user = $this->table->findByToken('token-4')->first();
$this->assertTrue($user->active);
$this->Trait->Flash->expects($this->once())
->method('success')
->with('Reset password token was validated successfully');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'changePassword']);
$this->Trait->validate('password', 'token-4');
$user = $this->table->findById($user->id)->first();
$this->assertTrue($user->active);
}
/**
* test
*
* @return void
*/
public function testResendTokenValidationHappy()
{
$this->_mockRequestPost();
$this->_mockFlash();
$this->Trait->request->expects($this->once())
->method('getData')
->with('reference')
->will($this->returnValue('user-3'));
$this->Trait->Flash->expects($this->once())
->method('success')
->with('Token has been reset successfully. Please check your email.');
$this->Trait->expects($this->once())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->resendTokenValidation();
}
/**
* test
*
* @return void
*/
public function testResendTokenValidationAlreadyActive()
{
$this->_mockRequestPost();
$this->_mockFlash();
$this->Trait->request->expects($this->once())
->method('getData')
->with('reference')
->will($this->returnValue('user-4'));
$this->Trait->Flash->expects($this->once())
->method('error')
->with('User user-4 is already active');
$this->Trait->expects($this->never())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->resendTokenValidation();
}
/**
* test
*
* @return void
*/
public function testResendTokenValidationNotFound()
{
$this->_mockRequestPost();
$this->_mockFlash();
$this->Trait->request->expects($this->once())
->method('getData')
->with('reference')
->will($this->returnValue('not-found'));
$this->Trait->Flash->expects($this->once())
->method('error')
->with('User not-found was not found');
$this->Trait->expects($this->never())
->method('redirect')
->with(['action' => 'login']);
$this->Trait->resendTokenValidation();
}
}