| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CrudAuthorizeTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @package Cake.Test.Case.Controller.Component.Auth
|
|
|
15 |
* @since CakePHP(tm) v 2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('CrudAuthorize', 'Controller/Component/Auth');
|
|
|
20 |
App::uses('ComponentCollection', 'Controller');
|
|
|
21 |
App::uses('AclComponent', 'Controller/Component');
|
|
|
22 |
App::uses('CakeRequest', 'Network');
|
|
|
23 |
App::uses('CakeResponse', 'Network');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Class CrudAuthorizeTest
|
|
|
27 |
*
|
|
|
28 |
* @package Cake.Test.Case.Controller.Component.Auth
|
|
|
29 |
*/
|
|
|
30 |
class CrudAuthorizeTest extends CakeTestCase {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* setup
|
|
|
34 |
*
|
|
|
35 |
* @return void
|
|
|
36 |
*/
|
|
|
37 |
public function setUp() {
|
|
|
38 |
parent::setUp();
|
|
|
39 |
Configure::write('Routing.prefixes', array());
|
|
|
40 |
Router::reload();
|
|
|
41 |
|
|
|
42 |
$this->Acl = $this->getMock('AclComponent', array(), array(), '', false);
|
|
|
43 |
$this->Components = $this->getMock('ComponentCollection');
|
|
|
44 |
|
|
|
45 |
$this->auth = new CrudAuthorize($this->Components);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* setup the mock acl.
|
|
|
50 |
*
|
|
|
51 |
* @return void
|
|
|
52 |
*/
|
|
|
53 |
protected function _mockAcl() {
|
|
|
54 |
$this->Components->expects($this->any())
|
|
|
55 |
->method('load')
|
|
|
56 |
->with('Acl')
|
|
|
57 |
->will($this->returnValue($this->Acl));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* test authorize() without a mapped action, ensure an error is generated.
|
|
|
62 |
*
|
|
|
63 |
* @expectedException PHPUnit_Framework_Error_Warning
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function testAuthorizeNoMappedAction() {
|
|
|
67 |
$request = new CakeRequest('/posts/foobar', false);
|
|
|
68 |
$request->addParams(array(
|
|
|
69 |
'controller' => 'posts',
|
|
|
70 |
'action' => 'foobar'
|
|
|
71 |
));
|
|
|
72 |
$user = array('User' => array('user' => 'mark'));
|
|
|
73 |
|
|
|
74 |
$this->auth->authorize($user, $request);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* test check() passing
|
|
|
79 |
*
|
|
|
80 |
* @return void
|
|
|
81 |
*/
|
|
|
82 |
public function testAuthorizeCheckSuccess() {
|
|
|
83 |
$request = new CakeRequest('posts/index', false);
|
|
|
84 |
$request->addParams(array(
|
|
|
85 |
'controller' => 'posts',
|
|
|
86 |
'action' => 'index'
|
|
|
87 |
));
|
|
|
88 |
$user = array('User' => array('user' => 'mark'));
|
|
|
89 |
|
|
|
90 |
$this->_mockAcl();
|
|
|
91 |
$this->Acl->expects($this->once())
|
|
|
92 |
->method('check')
|
|
|
93 |
->with($user, 'Posts', 'read')
|
|
|
94 |
->will($this->returnValue(true));
|
|
|
95 |
|
|
|
96 |
$this->assertTrue($this->auth->authorize($user['User'], $request));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* test check() failing
|
|
|
101 |
*
|
|
|
102 |
* @return void
|
|
|
103 |
*/
|
|
|
104 |
public function testAuthorizeCheckFailure() {
|
|
|
105 |
$request = new CakeRequest('posts/index', false);
|
|
|
106 |
$request->addParams(array(
|
|
|
107 |
'controller' => 'posts',
|
|
|
108 |
'action' => 'index'
|
|
|
109 |
));
|
|
|
110 |
$user = array('User' => array('user' => 'mark'));
|
|
|
111 |
|
|
|
112 |
$this->_mockAcl();
|
|
|
113 |
$this->Acl->expects($this->once())
|
|
|
114 |
->method('check')
|
|
|
115 |
->with($user, 'Posts', 'read')
|
|
|
116 |
->will($this->returnValue(false));
|
|
|
117 |
|
|
|
118 |
$this->assertFalse($this->auth->authorize($user['User'], $request));
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* test getting actionMap
|
|
|
123 |
*
|
|
|
124 |
* @return void
|
|
|
125 |
*/
|
|
|
126 |
public function testMapActionsGet() {
|
|
|
127 |
$result = $this->auth->mapActions();
|
|
|
128 |
$expected = array(
|
|
|
129 |
'create' => 'create',
|
|
|
130 |
'read' => 'read',
|
|
|
131 |
'update' => 'update',
|
|
|
132 |
'delete' => 'delete',
|
|
|
133 |
'index' => 'read',
|
|
|
134 |
'add' => 'create',
|
|
|
135 |
'edit' => 'update',
|
|
|
136 |
'view' => 'read',
|
|
|
137 |
'remove' => 'delete'
|
|
|
138 |
);
|
|
|
139 |
$this->assertEquals($expected, $result);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* test adding into mapActions
|
|
|
144 |
*
|
|
|
145 |
* @return void
|
|
|
146 |
*/
|
|
|
147 |
public function testMapActionsSet() {
|
|
|
148 |
$map = array(
|
|
|
149 |
'create' => array('generate'),
|
|
|
150 |
'read' => array('listing', 'show'),
|
|
|
151 |
'update' => array('update'),
|
|
|
152 |
'random' => 'custom'
|
|
|
153 |
);
|
|
|
154 |
$result = $this->auth->mapActions($map);
|
|
|
155 |
$this->assertNull($result);
|
|
|
156 |
|
|
|
157 |
$result = $this->auth->mapActions();
|
|
|
158 |
$expected = array(
|
|
|
159 |
'add' => 'create',
|
|
|
160 |
'create' => 'create',
|
|
|
161 |
'read' => 'read',
|
|
|
162 |
'index' => 'read',
|
|
|
163 |
'edit' => 'update',
|
|
|
164 |
'view' => 'read',
|
|
|
165 |
'delete' => 'delete',
|
|
|
166 |
'remove' => 'delete',
|
|
|
167 |
'generate' => 'create',
|
|
|
168 |
'listing' => 'read',
|
|
|
169 |
'show' => 'read',
|
|
|
170 |
'update' => 'update',
|
|
|
171 |
'random' => 'custom',
|
|
|
172 |
);
|
|
|
173 |
$this->assertEquals($expected, $result);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* test prefix routes getting auto mapped.
|
|
|
178 |
*
|
|
|
179 |
* @return void
|
|
|
180 |
*/
|
|
|
181 |
public function testAutoPrefixMapActions() {
|
|
|
182 |
Configure::write('Routing.prefixes', array('admin', 'manager'));
|
|
|
183 |
Router::reload();
|
|
|
184 |
|
|
|
185 |
$auth = new CrudAuthorize($this->Components);
|
|
|
186 |
$this->assertTrue(isset($auth->settings['actionMap']['admin_index']));
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
}
|