Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * AclShell Test file
4
 *
5
 * CakePHP :  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 Project
14
 * @package       Cake.Test.Case.Console.Command
15
 * @since         CakePHP v 1.2.0.7726
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ConsoleOutput', 'Console');
20
App::uses('ConsoleInput', 'Console');
21
App::uses('ShellDispatcher', 'Console');
22
App::uses('Shell', 'Console');
23
App::uses('AclShell', 'Console/Command');
24
App::uses('ComponentCollection', 'Controller');
25
 
26
/**
27
 * AclShellTest class
28
 *
29
 * @package       Cake.Test.Case.Console.Command
30
 */
31
class AclShellTest extends CakeTestCase {
32
 
33
/**
34
 * Fixtures
35
 *
36
 * @var array
37
 */
38
	public $fixtures = array('core.aco', 'core.aro', 'core.aros_aco');
39
 
40
/**
41
 * setUp method
42
 *
43
 * @return void
44
 */
45
	public function setUp() {
46
		parent::setUp();
47
		Configure::write('Acl.database', 'test');
48
		Configure::write('Acl.classname', 'DbAcl');
49
 
50
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
51
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
52
 
53
		$this->Task = $this->getMock(
54
			'AclShell',
55
			array('in', 'out', 'hr', 'createFile', 'error', 'err', 'clear', 'dispatchShell'),
56
			array($out, $out, $in)
57
		);
58
		$collection = new ComponentCollection();
59
		$this->Task->Acl = new AclComponent($collection);
60
		$this->Task->params['datasource'] = 'test';
61
	}
62
 
63
/**
64
 * test that model.foreign_key output works when looking at acl rows
65
 *
66
 * @return void
67
 */
68
	public function testViewWithModelForeignKeyOutput() {
69
		$this->Task->command = 'view';
70
		$this->Task->startup();
71
		$data = array(
72
			'parent_id' => null,
73
			'model' => 'MyModel',
74
			'foreign_key' => 2,
75
		);
76
		$this->Task->Acl->Aro->create($data);
77
		$this->Task->Acl->Aro->save();
78
		$this->Task->args[0] = 'aro';
79
 
80
		$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
81
		$this->Task->expects($this->at(2))->method('out')
82
			->with($this->stringContains('[1] ROOT'));
83
 
84
		$this->Task->expects($this->at(4))->method('out')
85
			->with($this->stringContains('[3] Gandalf'));
86
 
87
		$this->Task->expects($this->at(6))->method('out')
88
			->with($this->stringContains('[5] MyModel.2'));
89
 
90
		$this->Task->view();
91
	}
92
 
93
/**
94
 * test view with an argument
95
 *
96
 * @return void
97
 */
98
	public function testViewWithArgument() {
99
		$this->Task->args = array('aro', 'admins');
100
 
101
		$this->Task->expects($this->at(0))->method('out')->with('Aro tree:');
102
		$this->Task->expects($this->at(2))->method('out')->with('  [2] admins');
103
		$this->Task->expects($this->at(3))->method('out')->with('    [3] Gandalf');
104
		$this->Task->expects($this->at(4))->method('out')->with('    [4] Elrond');
105
 
106
		$this->Task->view();
107
	}
108
 
109
/**
110
 * test the method that splits model.foreign key. and that it returns an array.
111
 *
112
 * @return void
113
 */
114
	public function testParsingModelAndForeignKey() {
115
		$result = $this->Task->parseIdentifier('Model.foreignKey');
116
		$expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
117
		$this->assertEquals($expected, $result);
118
 
119
		$result = $this->Task->parseIdentifier('mySuperUser');
120
		$this->assertEquals('mySuperUser', $result);
121
 
122
		$result = $this->Task->parseIdentifier('111234');
123
		$this->assertEquals('111234', $result);
124
	}
125
 
126
/**
127
 * test creating aro/aco nodes
128
 *
129
 * @return void
130
 */
131
	public function testCreate() {
132
		$this->Task->args = array('aro', 'root', 'User.1');
133
		$this->Task->expects($this->at(0))->method('out')->with("<success>New Aro</success> 'User.1' created.", 2);
134
		$this->Task->expects($this->at(1))->method('out')->with("<success>New Aro</success> 'User.3' created.", 2);
135
		$this->Task->expects($this->at(2))->method('out')->with("<success>New Aro</success> 'somealias' created.", 2);
136
 
137
		$this->Task->create();
138
 
139
		$Aro = ClassRegistry::init('Aro');
140
		$Aro->cacheQueries = false;
141
		$result = $Aro->read();
142
		$this->assertEquals('User', $result['Aro']['model']);
143
		$this->assertEquals(1, $result['Aro']['foreign_key']);
144
		$this->assertEquals(null, $result['Aro']['parent_id']);
145
		$id = $result['Aro']['id'];
146
 
147
		$this->Task->args = array('aro', 'User.1', 'User.3');
148
		$this->Task->create();
149
 
150
		$Aro = ClassRegistry::init('Aro');
151
		$result = $Aro->read();
152
		$this->assertEquals('User', $result['Aro']['model']);
153
		$this->assertEquals(3, $result['Aro']['foreign_key']);
154
		$this->assertEquals($id, $result['Aro']['parent_id']);
155
 
156
		$this->Task->args = array('aro', 'root', 'somealias');
157
		$this->Task->create();
158
 
159
		$Aro = ClassRegistry::init('Aro');
160
		$result = $Aro->read();
161
		$this->assertEquals('somealias', $result['Aro']['alias']);
162
		$this->assertEquals(null, $result['Aro']['model']);
163
		$this->assertEquals(null, $result['Aro']['foreign_key']);
164
		$this->assertEquals(null, $result['Aro']['parent_id']);
165
	}
166
 
167
/**
168
 * test the delete method with different node types.
169
 *
170
 * @return void
171
 */
172
	public function testDelete() {
173
		$this->Task->args = array('aro', 'AuthUser.1');
174
		$this->Task->expects($this->at(0))->method('out')
175
			->with("<success>Aro deleted.</success>", 2);
176
		$this->Task->delete();
177
 
178
		$Aro = ClassRegistry::init('Aro');
179
		$result = $Aro->findById(3);
180
		$this->assertSame(array(), $result);
181
	}
182
 
183
/**
184
 * test setParent method.
185
 *
186
 * @return void
187
 */
188
	public function testSetParent() {
189
		$this->Task->args = array('aro', 'AuthUser.2', 'root');
190
		$this->Task->setParent();
191
 
192
		$Aro = ClassRegistry::init('Aro');
193
		$result = $Aro->read(null, 4);
194
		$this->assertEquals(null, $result['Aro']['parent_id']);
195
	}
196
 
197
/**
198
 * test grant
199
 *
200
 * @return void
201
 */
202
	public function testGrant() {
203
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
204
		$this->Task->expects($this->at(0))->method('out')
205
			->with($this->matchesRegularExpression('/granted/'), true);
206
		$this->Task->grant();
207
		$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
208
		$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
209
 
210
		$this->assertFalse(empty($node['Aco'][0]));
211
		$this->assertEquals(1, $node['Aco'][0]['Permission']['_create']);
212
	}
213
 
214
/**
215
 * test deny
216
 *
217
 * @return void
218
 */
219
	public function testDeny() {
220
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
221
		$this->Task->expects($this->at(0))->method('out')
222
			->with($this->stringContains('Permission denied'), true);
223
 
224
		$this->Task->deny();
225
 
226
		$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
227
		$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
228
		$this->assertFalse(empty($node['Aco'][0]));
229
		$this->assertEquals(-1, $node['Aco'][0]['Permission']['_create']);
230
	}
231
 
232
/**
233
 * test checking allowed and denied perms
234
 *
235
 * @return void
236
 */
237
	public function testCheck() {
238
		$this->Task->expects($this->at(0))->method('out')
239
			->with($this->matchesRegularExpression('/not allowed/'), true);
240
		$this->Task->expects($this->at(1))->method('out')
241
			->with($this->matchesRegularExpression('/granted/'), true);
242
		$this->Task->expects($this->at(2))->method('out')
243
			->with($this->matchesRegularExpression('/is.*allowed/'), true);
244
		$this->Task->expects($this->at(3))->method('out')
245
			->with($this->matchesRegularExpression('/not.*allowed/'), true);
246
 
247
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
248
		$this->Task->check();
249
 
250
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
251
		$this->Task->grant();
252
 
253
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
254
		$this->Task->check();
255
 
256
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', '*');
257
		$this->Task->check();
258
	}
259
 
260
/**
261
 * test inherit and that it 0's the permission fields.
262
 *
263
 * @return void
264
 */
265
	public function testInherit() {
266
		$this->Task->expects($this->at(0))->method('out')
267
			->with($this->matchesRegularExpression('/Permission .*granted/'), true);
268
		$this->Task->expects($this->at(1))->method('out')
269
			->with($this->matchesRegularExpression('/Permission .*inherited/'), true);
270
 
271
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'create');
272
		$this->Task->grant();
273
 
274
		$this->Task->args = array('AuthUser.2', 'ROOT/Controller1', 'all');
275
		$this->Task->inherit();
276
 
277
		$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
278
		$node = $this->Task->Acl->Aro->read(null, $node[0]['Aro']['id']);
279
		$this->assertFalse(empty($node['Aco'][0]));
280
		$this->assertEquals(0, $node['Aco'][0]['Permission']['_create']);
281
	}
282
 
283
/**
284
 * test getting the path for an aro/aco
285
 *
286
 * @return void
287
 */
288
	public function testGetPath() {
289
		$this->Task->args = array('aro', 'AuthUser.2');
290
		$node = $this->Task->Acl->Aro->node(array('model' => 'AuthUser', 'foreign_key' => 2));
291
		$first = $node[0]['Aro']['id'];
292
		$second = $node[1]['Aro']['id'];
293
		$last = $node[2]['Aro']['id'];
294
		$this->Task->expects($this->at(2))->method('out')->with('[' . $last . '] ROOT');
295
		$this->Task->expects($this->at(3))->method('out')->with('  [' . $second . '] admins');
296
		$this->Task->expects($this->at(4))->method('out')->with('    [' . $first . '] Elrond');
297
		$this->Task->getPath();
298
	}
299
 
300
/**
301
 * test that initdb makes the correct call.
302
 *
303
 * @return void
304
 */
305
	public function testInitDb() {
306
		$this->Task->expects($this->once())->method('dispatchShell')
307
			->with('schema create DbAcl');
308
 
309
		$this->Task->initdb();
310
	}
311
}