forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRememberMeComponentTest.php
More file actions
164 lines (142 loc) · 4.24 KB
/
Copy pathRememberMeComponentTest.php
File metadata and controls
164 lines (142 loc) · 4.24 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
<?php
/**
* Copyright 2010 - 2013, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2013, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
App::uses('RememberMeComponent', 'Users.Controller/Component');
/**
* CookieComponentTestController class
*
* @package Cake.Test.Case.Controller.Component
*/
class RememberMeComponentTestController extends Controller {
/**
* components property
*
* @var array
*/
public $components = array(
'Users.RememberMe',
'Auth');
}
class RememberMeComponentTest extends CakeTestCase {
/**
* Controller property
*
* @var CookieComponentTestController
*/
public $Controller;
/**
* User data
* @var array
*/
public $usersData = array(
'test' => array(
'email' => 'test@cakedc.com',
'password' => 'test'),
'admin' => array(
'email' => 'admin@cakedc.com',
'password' => 'admin'));
/**
* start
*
* @return void
*/
public function setUp() {
$_COOKIE = array();
Configure::write('Config.language', 'eng');
$this->Controller = new RememberMeComponentTestController(new CakeRequest(), new CakeResponse());
$this->Controller->constructClasses();
$this->RememberMe = $this->Controller->RememberMe;
$this->RememberMe->Cookie = $this->getMock('CookieComponent',
array(),
array($this->Controller->Components));
$this->RememberMe->Auth = $this->getMock('AuthComponent',
array(),
array($this->Controller->Components));
}
/**
* testSetCookie
*
* @return void
*/
public function testSetCookie() {
$this->RememberMe->Cookie->expects($this->once())
->method('write')
->with('rememberMe', array(
'email' => 'email',
'password' => 'password'), true);
$this->RememberMe->setCookie(array(
'User' => array(
'email' => 'email',
'password' => 'password')));
}
/**
* testRestoreLoginFromCookie
*
* @return void
*/
public function testRestoreLoginFromCookie() {
$this->RememberMe->Cookie->expects($this->once())
->method('read')
->with($this->equalTo('rememberMe'))
->will($this->returnValue($this->usersData['admin']));
$this->RememberMe->Auth->expects($this->once())
->method('login')
->will($this->returnValue(true));
$this->__setPostData(array('User' => $this->usersData['test']));
$this->RememberMe->restoreLoginFromCookie();
// even if we post "test" user, we have a remember me cookie set and will priorize the cookie over the post
// NOTE we check if the user is logged in in the startup method of the Component
$this->assertEqual($this->RememberMe->request->data, array(
'User' => $this->usersData['admin']));
}
/**
* testRestoreLoginFromCookieIncorrectLogin
*
* We check the post request data is not modified when the cookie holds incorrect login credentials
*
* @return void
*/
public function testRestoreLoginFromCookieIncorrectLogin() {
// cookie will hold "admin" data, and post request will have "test"
$this->RememberMe->Cookie->expects($this->once())
->method('read')
->with($this->equalTo('rememberMe'))
->will($this->returnValue($this->usersData['admin']));
// admin will not login
$this->RememberMe->Auth->expects($this->once())
->method('login')
->will($this->returnValue(false));
// post has "test" data
$this->__setPostData(array('User' => $this->usersData['test']));
$this->RememberMe->restoreLoginFromCookie();
$this->assertEqual($this->RememberMe->request->data, array(
'User' => $this->usersData['test']));
}
/**
* testDestroyCookie
*
* @return void
*/
public function testDestroyCookie() {
$_COOKIE['User'] = 'defined';
$this->RememberMe->Cookie->expects($this->once())
->method('destroy');
$this->RememberMe->destroyCookie();
}
/**
* Set post data to the test controller
* @param type $data
*/
private function __setPostData($data = array()) {
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->RememberMe->request->data = array_merge($data);
}
}