~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/behaviors/acl.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * AclBehaviorTest file
 
4
 *
 
5
 * Test the Acl Behavior
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP : Rapid Development Framework (http://cakephp.org)
 
10
 * Copyright 2006-2010, Cake Software Foundation, Inc.
 
11
 *
 
12
 * Licensed under The MIT License
 
13
 * Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     Copyright 2006-2010, Cake Software Foundation, Inc.
 
16
 * @link          http://cakephp.org CakePHP Project
 
17
 * @package       cake
 
18
 * @subpackage    cake.cake.libs.tests.model.behaviors.acl
 
19
 * @since         CakePHP v 1.2.0.4487
 
20
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
21
 */
 
22
App::import('Behavior', 'Acl');
 
23
App::import('Core', 'db_acl');
 
24
 
 
25
/**
 
26
* Test Person class - self joined model
 
27
*
 
28
* @package       cake
 
29
* @subpackage    cake.tests.cases.libs.model.behaviors
 
30
*/
 
31
class AclPerson extends CakeTestModel {
 
32
 
 
33
/**
 
34
 * name property
 
35
 *
 
36
 * @var string
 
37
 * @access public
 
38
 */
 
39
        var $name = 'AclPerson';
 
40
 
 
41
/**
 
42
 * useTable property
 
43
 *
 
44
 * @var string
 
45
 * @access public
 
46
 */
 
47
        var $useTable = 'people';
 
48
 
 
49
/**
 
50
 * actsAs property
 
51
 *
 
52
 * @var array
 
53
 * @access public
 
54
 */
 
55
        var $actsAs = array('Acl' => 'requester');
 
56
 
 
57
/**
 
58
 * belongsTo property
 
59
 *
 
60
 * @var array
 
61
 * @access public
 
62
 */
 
63
        var $belongsTo = array(
 
64
                'Mother' => array(
 
65
                        'className' => 'AclPerson',
 
66
                        'foreignKey' => 'mother_id',
 
67
                )
 
68
        );
 
69
 
 
70
/**
 
71
 * hasMany property
 
72
 *
 
73
 * @var array
 
74
 * @access public
 
75
 */
 
76
        var $hasMany = array(
 
77
                'Child' => array(
 
78
                        'className' => 'AclPerson',
 
79
                        'foreignKey' => 'mother_id'
 
80
                )
 
81
        );
 
82
 
 
83
/**
 
84
 * parentNode method
 
85
 *
 
86
 * @return void
 
87
 * @access public
 
88
 */
 
89
        function parentNode() {
 
90
                if (!$this->id && empty($this->data)) {
 
91
                        return null;
 
92
                }
 
93
                if (isset($this->data['AclPerson']['mother_id'])) {
 
94
                        $motherId = $this->data['AclPerson']['mother_id'];
 
95
                } else {
 
96
                        $motherId = $this->field('mother_id');
 
97
                }
 
98
                if (!$motherId) {
 
99
                        return null;
 
100
                } else {
 
101
                        return array('AclPerson' => array('id' => $motherId));
 
102
                }
 
103
        }
 
104
}
 
105
 
 
106
/**
 
107
* AclUser class
 
108
*
 
109
* @package       cake
 
110
* @subpackage    cake.tests.cases.libs.model.behaviors
 
111
*/
 
112
class AclUser extends CakeTestModel {
 
113
 
 
114
/**
 
115
 * name property
 
116
 *
 
117
 * @var string
 
118
 * @access public
 
119
 */
 
120
        var $name = 'User';
 
121
 
 
122
/**
 
123
 * useTable property
 
124
 *
 
125
 * @var string
 
126
 * @access public
 
127
 */
 
128
        var $useTable = 'users';
 
129
 
 
130
/**
 
131
 * actsAs property
 
132
 *
 
133
 * @var array
 
134
 * @access public
 
135
 */
 
136
        var $actsAs = array('Acl');
 
137
 
 
138
/**
 
139
 * parentNode
 
140
 *
 
141
 * @access public
 
142
 */
 
143
        function parentNode() {
 
144
                return null;
 
145
        }
 
146
}
 
147
 
 
148
/**
 
149
* AclPost class
 
150
*
 
151
* @package       cake
 
152
* @subpackage    cake.tests.cases.libs.model.behaviors
 
153
*/
 
154
class AclPost extends CakeTestModel {
 
155
 
 
156
/**
 
157
 * name property
 
158
 *
 
159
 * @var string
 
160
 * @access public
 
161
 */
 
162
        var $name = 'Post';
 
163
 
 
164
/**
 
165
 * useTable property
 
166
 *
 
167
 * @var string
 
168
 * @access public
 
169
 */
 
170
        var $useTable = 'posts';
 
171
 
 
172
/**
 
173
 * actsAs property
 
174
 *
 
175
 * @var array
 
176
 * @access public
 
177
 */
 
178
        var $actsAs = array('Acl' => 'Controlled');
 
179
 
 
180
/**
 
181
 * parentNode
 
182
 *
 
183
 * @access public
 
184
 */
 
185
        function parentNode() {
 
186
                return null;
 
187
        }
 
188
}
 
189
 
 
190
/**
 
191
* AclBehaviorTest class
 
192
*
 
193
* @package       cake
 
194
* @subpackage    cake.tests.cases.libs.controller.components
 
195
*/
 
196
class AclBehaviorTestCase extends CakeTestCase {
 
197
 
 
198
/**
 
199
 * Aco property
 
200
 *
 
201
 * @var Aco
 
202
 * @access public
 
203
 */
 
204
        var $Aco;
 
205
 
 
206
/**
 
207
 * Aro property
 
208
 *
 
209
 * @var Aro
 
210
 * @access public
 
211
 */
 
212
        var $Aro;
 
213
 
 
214
/**
 
215
 * fixtures property
 
216
 *
 
217
 * @var array
 
218
 * @access public
 
219
 */
 
220
        var $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
 
221
 
 
222
/**
 
223
 * Set up the test
 
224
 *
 
225
 * @return void
 
226
 * @access public
 
227
 */
 
228
        function startTest() {
 
229
                Configure::write('Acl.database', 'test_suite');
 
230
 
 
231
                $this->Aco =& new Aco();
 
232
                $this->Aro =& new Aro();
 
233
        }
 
234
 
 
235
/**
 
236
 * tearDown method
 
237
 *
 
238
 * @return void
 
239
 * @access public
 
240
 */
 
241
        function tearDown() {
 
242
                ClassRegistry::flush();
 
243
                unset($this->Aro, $this->Aco);
 
244
        }
 
245
 
 
246
/**
 
247
 * Test Setup of AclBehavior
 
248
 *
 
249
 * @return void
 
250
 * @access public
 
251
 */
 
252
        function testSetup() {
 
253
                $User =& new AclUser();
 
254
                $this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
 
255
                $this->assertEqual($User->Behaviors->Acl->settings['User']['type'], 'requester');
 
256
                $this->assertTrue(is_object($User->Aro));
 
257
 
 
258
                $Post =& new AclPost();
 
259
                $this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
 
260
                $this->assertEqual($Post->Behaviors->Acl->settings['Post']['type'], 'controlled');
 
261
                $this->assertTrue(is_object($Post->Aco));
 
262
        }
 
263
 
 
264
/**
 
265
 * test After Save
 
266
 *
 
267
 * @return void
 
268
 * @access public
 
269
 */
 
270
        function testAfterSave() {
 
271
                $Post =& new AclPost();
 
272
                $data = array(
 
273
                        'Post' => array(
 
274
                                'author_id' => 1,
 
275
                                'title' => 'Acl Post',
 
276
                                'body' => 'post body',
 
277
                                'published' => 1
 
278
                        ),
 
279
                );
 
280
                $Post->save($data);
 
281
                $result = $this->Aco->find('first', array(
 
282
                        'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
 
283
                ));
 
284
                $this->assertTrue(is_array($result));
 
285
                $this->assertEqual($result['Aco']['model'], 'Post');
 
286
                $this->assertEqual($result['Aco']['foreign_key'], $Post->id);
 
287
 
 
288
                $aroData = array(
 
289
                        'Aro' => array(
 
290
                                'model' => 'AclPerson',
 
291
                                'foreign_key' => 2,
 
292
                                'parent_id' => null
 
293
                        )
 
294
                );
 
295
                $this->Aro->save($aroData);
 
296
 
 
297
                $Person =& new AclPerson();
 
298
                $data = array(
 
299
                        'AclPerson' => array(
 
300
                                'name' => 'Trent',
 
301
                                'mother_id' => 2,
 
302
                                'father_id' => 3,
 
303
                        ),
 
304
                );
 
305
                $Person->save($data);
 
306
                $result = $this->Aro->find('first', array(
 
307
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
 
308
                ));
 
309
                $this->assertTrue(is_array($result));
 
310
                $this->assertEqual($result['Aro']['parent_id'], 5);
 
311
 
 
312
                $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
 
313
                $this->assertEqual(count($node), 2);
 
314
                $this->assertEqual($node[0]['Aro']['parent_id'], 5);
 
315
                $this->assertEqual($node[1]['Aro']['parent_id'], null);
 
316
 
 
317
                $aroData = array(
 
318
                        'Aro' => array(
 
319
                        'model' => 'AclPerson',
 
320
                                'foreign_key' => 1,
 
321
                                'parent_id' => null
 
322
                        )
 
323
                );
 
324
                $this->Aro->create();
 
325
                $this->Aro->save($aroData);
 
326
 
 
327
                $Person->read(null, 8);
 
328
                $Person->set('mother_id', 1);
 
329
                $Person->save();
 
330
                $result = $this->Aro->find('first', array(
 
331
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
 
332
                ));
 
333
                 $this->assertTrue(is_array($result));
 
334
                 $this->assertEqual($result['Aro']['parent_id'], 7);
 
335
 
 
336
                $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
 
337
                $this->assertEqual(sizeof($node), 2);
 
338
                $this->assertEqual($node[0]['Aro']['parent_id'], 7);
 
339
                $this->assertEqual($node[1]['Aro']['parent_id'], null);
 
340
        }
 
341
 
 
342
/**
 
343
 * test that an afterSave on an update does not cause parent_id to become null.
 
344
 *
 
345
 * @return void
 
346
 */
 
347
        function testAfterSaveUpdateParentIdNotNull() {
 
348
                $aroData = array(
 
349
                        'Aro' => array(
 
350
                                'model' => 'AclPerson',
 
351
                                'foreign_key' => 2,
 
352
                                'parent_id' => null
 
353
                        )
 
354
                );
 
355
                $this->Aro->save($aroData);
 
356
 
 
357
                $Person =& new AclPerson();
 
358
                $data = array(
 
359
                        'AclPerson' => array(
 
360
                                'name' => 'Trent',
 
361
                                'mother_id' => 2,
 
362
                                'father_id' => 3,
 
363
                        ),
 
364
                );
 
365
                $Person->save($data);
 
366
                $result = $this->Aro->find('first', array(
 
367
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
 
368
                ));
 
369
                $this->assertTrue(is_array($result));
 
370
                $this->assertEqual($result['Aro']['parent_id'], 5);
 
371
 
 
372
                $Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
 
373
                $result = $this->Aro->find('first', array(
 
374
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
 
375
                ));
 
376
                $this->assertEqual($result['Aro']['parent_id'], 5);
 
377
        }
 
378
 
 
379
/**
 
380
 * Test After Delete
 
381
 *
 
382
 * @return void
 
383
 * @access public
 
384
 */
 
385
        function testAfterDelete() {
 
386
                $aroData = array(
 
387
                        'Aro' => array(
 
388
                                'model' => 'AclPerson',
 
389
                                'foreign_key' => 2,
 
390
                                'parent_id' => null
 
391
                        )
 
392
                );
 
393
                $this->Aro->save($aroData);
 
394
                $Person =& new AclPerson();
 
395
                $data = array(
 
396
                        'AclPerson' => array(
 
397
                                'name' => 'Trent',
 
398
                                'mother_id' => 2,
 
399
                                'father_id' => 3,
 
400
                        ),
 
401
                );
 
402
                $Person->save($data);
 
403
                $id = $Person->id;
 
404
                $node = $Person->node();
 
405
                $this->assertEqual(count($node), 2);
 
406
                $this->assertEqual($node[0]['Aro']['parent_id'], 5);
 
407
                $this->assertEqual($node[1]['Aro']['parent_id'], null);
 
408
 
 
409
                $Person->delete($id);
 
410
                $result = $this->Aro->find('first', array(
 
411
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
 
412
                ));
 
413
                $this->assertTrue(empty($result));
 
414
                $result = $this->Aro->find('first', array(
 
415
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
 
416
                ));
 
417
                $this->assertFalse(empty($result));
 
418
 
 
419
                $data = array(
 
420
                        'AclPerson' => array(
 
421
                                'name' => 'Trent',
 
422
                                'mother_id' => 2,
 
423
                                'father_id' => 3,
 
424
                        ),
 
425
                );
 
426
                $Person->save($data);
 
427
                $id = $Person->id;
 
428
                $Person->delete(2);
 
429
                $result = $this->Aro->find('first', array(
 
430
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
 
431
                ));
 
432
                $this->assertTrue(empty($result));
 
433
 
 
434
                $result = $this->Aro->find('first', array(
 
435
                        'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
 
436
                ));
 
437
                $this->assertTrue(empty($result));
 
438
        }
 
439
 
 
440
/**
 
441
 * Test Node()
 
442
 *
 
443
 * @return void
 
444
 * @access public
 
445
 */
 
446
        function testNode() {
 
447
                $Person =& new AclPerson();
 
448
                $aroData = array(
 
449
                        'Aro' => array(
 
450
                                'model' => 'AclPerson',
 
451
                                'foreign_key' => 2,
 
452
                                'parent_id' => null
 
453
                        )
 
454
                );
 
455
                $this->Aro->save($aroData);
 
456
 
 
457
                $Person->id = 2;
 
458
                $result = $Person->node();
 
459
                $this->assertTrue(is_array($result));
 
460
                $this->assertEqual(count($result), 1);
 
461
        }
 
462
}