Skip to content
This repository
Browse code

Updating AuthComponent's mapped actions features to use Router::prefi…

…xes()

Adding tests for prefix interactions.
Adding tests for AuthComponent::logout, increasing code coverage.
  • Loading branch information...
commit ecea49f8239b901a28f57f6cf08f1c8d14bee968 1 parent 76780ab
Mark Story authored December 09, 2009
28  cake/libs/controller/components/auth.php
@@ -259,19 +259,21 @@ function initialize(&$controller) {
259 259
 		$this->actionMap = array_merge($this->actionMap, array_combine($crud, $crud));
260 260
 		$this->_methods = $controller->methods;
261 261
 
262  
-		$admin = Configure::read('Routing.admin');
263  
-		if (!empty($admin)) {
264  
-			$this->actionMap = array_merge($this->actionMap, array(
265  
-				$admin . '_index'	=> 'read',
266  
-				$admin . '_add'		=> 'create',
267  
-				$admin . '_edit'	=> 'update',
268  
-				$admin . '_view'	=> 'read',
269  
-				$admin . '_remove'	=> 'delete',
270  
-				$admin . '_create'	=> 'create',
271  
-				$admin . '_read'	=> 'read',
272  
-				$admin . '_update'	=> 'update',
273  
-				$admin . '_delete'	=> 'delete'
274  
-			));
  262
+		$prefixes = Router::prefixes();
  263
+		if (!empty($prefixes)) {
  264
+			foreach ($prefixes as $prefix) {
  265
+				$this->actionMap = array_merge($this->actionMap, array(
  266
+					$prefix . '_index' => 'read',
  267
+					$prefix . '_add' => 'create',
  268
+					$prefix . '_edit' => 'update',
  269
+					$prefix . '_view' => 'read',
  270
+					$prefix . '_remove' => 'delete',
  271
+					$prefix . '_create' => 'create',
  272
+					$prefix . '_read' => 'read',
  273
+					$prefix . '_update' => 'update',
  274
+					$prefix . '_delete' => 'delete'
  275
+				));
  276
+			}
275 277
 		}
276 278
 		if (Configure::read() > 0) {
277 279
 			App::import('Debugger');
42  cake/tests/cases/libs/controller/components/auth.test.php
@@ -1330,8 +1330,8 @@ function testCustomField() {
1330 1330
  * @return void
1331 1331
  */
1332 1332
 	function testAdminRoute() {
1333  
-		$admin = Configure::read('Routing.admin');
1334  
-		Configure::write('Routing.admin', 'admin');
  1333
+		$prefixes = Configure::read('Routing.prefixes');
  1334
+		Configure::write('Routing.prefixes', array('admin'));
1335 1335
 		Router::reload();
1336 1336
 
1337 1337
 		$url = '/admin/auth_test/add';
@@ -1358,7 +1358,7 @@ function testAdminRoute() {
1358 1358
 		$this->Controller->Auth->startup($this->Controller);
1359 1359
 		$this->assertEqual($this->Controller->testUrl, '/admin/auth_test/login');
1360 1360
 
1361  
-		Configure::write('Routing.admin', $admin);
  1361
+		Configure::write('Routing.prefixes', $prefixes);
1362 1362
 	}
1363 1363
 
1364 1364
 /**
@@ -1479,5 +1479,41 @@ function testShutDown() {
1479 1479
 		$this->Controller->Auth->shutdown($this->Controller);
1480 1480
 		$this->assertFalse($this->Controller->Session->read('Auth.redirect'));
1481 1481
 	}
  1482
+
  1483
+/**
  1484
+ * test the initialize callback and its interactions with Router::prefixes()
  1485
+ *
  1486
+ * @return void
  1487
+ */
  1488
+	function testInitializeAndRoutingPrefixes() {
  1489
+		$restore = Configure::read('Routing');
  1490
+		Configure::write('Routing.prefixes', array('admin', 'super_user'));
  1491
+		Router::reload();
  1492
+		$this->Controller->Auth->initialize($this->Controller);
  1493
+
  1494
+		$this->assertTrue(isset($this->Controller->Auth->actionMap['delete']));
  1495
+		$this->assertTrue(isset($this->Controller->Auth->actionMap['view']));
  1496
+		$this->assertTrue(isset($this->Controller->Auth->actionMap['add']));
  1497
+		$this->assertTrue(isset($this->Controller->Auth->actionMap['admin_view']));
  1498
+		$this->assertTrue(isset($this->Controller->Auth->actionMap['super_user_delete']));
  1499
+
  1500
+		Configure::write('Routing', $restore);
  1501
+	}
  1502
+
  1503
+/**
  1504
+ * test that logout deletes the session variables. and returns the correct url
  1505
+ *
  1506
+ * @return void
  1507
+ */
  1508
+	function testLogout() {
  1509
+		$this->Controller->Session->write('Auth.User.id', '1');
  1510
+		$this->Controller->Session->write('Auth.redirect', '/users/login');
  1511
+		$this->Controller->Auth->logoutRedirect = '/';
  1512
+		$result = $this->Controller->Auth->logout();
  1513
+
  1514
+		$this->assertEqual($result, '/');
  1515
+		$this->assertNull($this->Controller->Session->read('Auth.AuthUser'));
  1516
+		$this->assertNull($this->Controller->Session->read('Auth.redirect'));
  1517
+	}
1482 1518
 }
1483 1519
 ?>

0 notes on commit ecea49f

Please sign in to comment.
Something went wrong with that request. Please try again.