This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
/
UserBehaviorTest.php
241 lines (216 loc) · 4.83 KB
/
UserBehaviorTest.php
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Burzum\UserTools\Test\TestCase\Model\Behavior;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
/**
* UserBehaviorTest
*
* @author Florian Krämer
* ]@copyright 2013 - 2015 Florian Krämer
* @license MIT
*/
class UserToolUser extends Table {
public $name = 'User';
public $alias = 'User';
public $useTable = 'users';
public $actsAs = array(
'UserTools.User'
);
}
/**
* UserBehaviorTest
*/
class UserBehaviorTest extends TestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.Burzum\UserTools.User'
);
/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->User = TableRegistry::get('Users');
$this->User->addBehavior('Burzum/UserTools.User');
$this->User->behaviors()->User->config('emailConfig', [
'transport' => 'default',
'from' => 'you@localhost',
]);
}
/**
* tearDown
*
* @return void
*/
public function tearDown() {
unset($this->User);
}
/**
* testRegister
*
* @todo figure out why its not reading the default email config, better to mock it any way
* @return void
*/
public function testRegister() {
$this->markTestSkipped('');
$data = new Entity([
'username' => 'foobar',
'email' => 'foobar@foobar.com',
'password' => 'password',
'confirm_password' => 'password'
]);
$result = $this->User->register($data);
$this->assertTrue(is_a($result, '\Cake\ORM\Entity'));
}
/**
* testGeneratePassword
*
* @return void
*/
public function testGeneratePassword() {
$result = $this->User->generatePassword();
$this->assertTrue(is_string($result));
$this->assertEquals(strlen($result), 8);
$result = $this->User->generatePassword(5);
$this->assertTrue(is_string($result));
$this->assertEquals(strlen($result), 5);
}
/**
* testGeneratePassword
*
* @return void
*/
public function testGenerateToken() {
$result = $this->User->generateToken();
$this->assertTrue(is_string($result));
$this->assertEquals(strlen($result), 10);
$result = $this->User->generateToken(5);
$this->assertTrue(is_string($result));
$this->assertEquals(strlen($result), 5);
}
/**
* testVerifyToken
*
* @return void
*/
public function testVerifyToken() {
$this->User->save(new Entity([
'email_token_expires' => date('Y-m-d H:i:s', strtotime('-12 hours')),
'id' => 2
]), array(
'validate' => false
));
$result = $this->User->verifyToken('secondusertesttoken');
$this->assertTrue($result);
$this->User->save(new Entity([
'email_token_expires' => date('Y-m-d H:i:s', strtotime('-12 hours')),
'id' => 3
]), array(
'validate' => false
));
$result = $this->User->verifyToken('thirdusertesttoken', array(
'returnData' => true
));
$this->assertTrue(is_a($result, '\Cake\ORM\Entity'));
$this->assertTrue($result->token_is_expired);
}
/**
* testVerifyTokenNotFoundException
*
* @expectedException \Cake\Network\Exception\NotFoundException
* @return void
*/
public function testVerifyTokenNotFoundException() {
$this->User->verifyToken('DOES-NOT-EXIST');
}
/**
* testRemoveExpiredRegistrations
*
* @return void
*/
public function testRemoveExpiredRegistrations() {
$result = $this->User
->find()
->where([
'email_verified' => 0,
'email_token_expires <' => date('Y-m-d H:is:'
)])
->count();
$this->assertEquals($result, 1);
$result = $this->User->removeExpiredRegistrations();
$this->assertEquals($result, 1);
$result = $this->User
->find()
->where([
'email_verified' => 0,
'email_token_expires <' => date('Y-m-d H:is:')
])
->count();
$this->assertEquals($result, 0);
}
/**
* testGetUser
*
* @return void
*/
public function testGetUser() {
$result = $this->User->getUser('1');
$this->assertEquals($result->id, '1');
$this->assertEquals($result->username, 'adminuser');
}
/**
* testhashPassword
*
* @return void
*/
public function testhashPassword() {
$result = $this->User->hashPassword('password!');
$this->assertTrue(is_string($result));
}
/**
* testGetUserRecordNotFoundException
*
* @expectedException \Cake\Network\Exception\NotFoundException
* @return void
*/
public function testGetUserRecordNotFoundException() {
$this->User->getUser('DOES-NOT-EXIST');
}
/**
* testResetPassword
*
* @return void
*/
public function testResetPassword() {
}
/**
* loadBehaviour helper method
*
* @param array $options
* @return void
*/
public function loadBehaviour($options = []) {
$this->User->addBehavior('UserTools.User', $options);
}
/**
* testPasswordHasher
*
* @return void
*/
public function testPasswordHasher() {
$result = $this->User->passwordHasher();
$this->assertTrue(is_a($result, '\Cake\Auth\DefaultPasswordHasher'));
}
public function testSendNewpassword() {
//$result = $this->User->sendNewPassword('newuser@testuser.com');
}
}