CrudAuthorizeTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Controller\Component\Auth;
  18. use Cake\Controller\Component\Auth\CrudAuthorize;
  19. use Cake\Core\Configure;
  20. use Cake\Network\Request;
  21. use Cake\Routing\Router;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Class CrudAuthorizeTest
  25. *
  26. */
  27. class CrudAuthorizeTest extends TestCase {
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. Configure::write('Routing.prefixes', array());
  36. Router::reload();
  37. $this->Acl = $this->getMock('Cake\Controller\Component\AclComponent', array(), array(), '', false);
  38. $this->Components = $this->getMock('Cake\Controller\ComponentRegistry');
  39. $this->auth = new CrudAuthorize($this->Components);
  40. }
  41. /**
  42. * setup the mock acl.
  43. *
  44. * @return void
  45. */
  46. protected function _mockAcl() {
  47. $this->Components->expects($this->any())
  48. ->method('load')
  49. ->with('Acl')
  50. ->will($this->returnValue($this->Acl));
  51. }
  52. /**
  53. * test authorize() without a mapped action, ensure an error is generated.
  54. *
  55. * @expectedException PHPUnit_Framework_Error_Warning
  56. * @return void
  57. */
  58. public function testAuthorizeNoMappedAction() {
  59. $request = new Request('/posts/foobar');
  60. $request->addParams(array(
  61. 'controller' => 'posts',
  62. 'action' => 'foobar'
  63. ));
  64. $user = array('User' => array('username' => 'mark'));
  65. $this->auth->authorize($user, $request);
  66. }
  67. /**
  68. * test check() passing
  69. *
  70. * @return void
  71. */
  72. public function testAuthorizeCheckSuccess() {
  73. $request = new Request('posts/index');
  74. $request->addParams(array(
  75. 'controller' => 'posts',
  76. 'action' => 'index'
  77. ));
  78. $user = array('Users' => array('username' => 'mark'));
  79. $this->_mockAcl();
  80. $this->Acl->expects($this->once())
  81. ->method('check')
  82. ->with($user, 'Posts', 'read')
  83. ->will($this->returnValue(true));
  84. $this->assertTrue($this->auth->authorize($user['Users'], $request));
  85. }
  86. /**
  87. * test check() failing
  88. *
  89. * @return void
  90. */
  91. public function testAuthorizeCheckFailure() {
  92. $request = new Request('posts/index');
  93. $request->addParams(array(
  94. 'controller' => 'posts',
  95. 'action' => 'index'
  96. ));
  97. $user = array('Users' => array('username' => 'mark'));
  98. $this->_mockAcl();
  99. $this->Acl->expects($this->once())
  100. ->method('check')
  101. ->with($user, 'Posts', 'read')
  102. ->will($this->returnValue(false));
  103. $this->assertFalse($this->auth->authorize($user['Users'], $request));
  104. }
  105. /**
  106. * test getting actionMap
  107. *
  108. * @return void
  109. */
  110. public function testMapActionsGet() {
  111. $result = $this->auth->mapActions();
  112. $expected = array(
  113. 'create' => 'create',
  114. 'read' => 'read',
  115. 'update' => 'update',
  116. 'delete' => 'delete',
  117. 'index' => 'read',
  118. 'add' => 'create',
  119. 'edit' => 'update',
  120. 'view' => 'read',
  121. 'remove' => 'delete'
  122. );
  123. $this->assertEquals($expected, $result);
  124. }
  125. /**
  126. * test adding into mapActions
  127. *
  128. * @return void
  129. */
  130. public function testMapActionsSet() {
  131. $map = array(
  132. 'create' => array('generate'),
  133. 'read' => array('listing', 'show'),
  134. 'update' => array('update'),
  135. 'random' => 'custom'
  136. );
  137. $result = $this->auth->mapActions($map);
  138. $this->assertNull($result);
  139. $result = $this->auth->mapActions();
  140. $expected = array(
  141. 'add' => 'create',
  142. 'create' => 'create',
  143. 'read' => 'read',
  144. 'index' => 'read',
  145. 'edit' => 'update',
  146. 'view' => 'read',
  147. 'delete' => 'delete',
  148. 'remove' => 'delete',
  149. 'generate' => 'create',
  150. 'listing' => 'read',
  151. 'show' => 'read',
  152. 'update' => 'update',
  153. 'random' => 'custom',
  154. );
  155. $this->assertEquals($expected, $result);
  156. }
  157. /**
  158. * test prefix routes getting auto mapped.
  159. *
  160. * @return void
  161. */
  162. public function testAutoPrefixMapActions() {
  163. Configure::write('Routing.prefixes', array('admin', 'manager'));
  164. Router::reload();
  165. $auth = new CrudAuthorize($this->Components);
  166. $this->assertTrue((bool)$auth->config('actionMap.admin_index'), 'admin_index should now be a mapped action');
  167. }
  168. }