ControllerAuthorizeTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * ControllerAuthorizeTest 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 CakePHP(tm) v 2.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\ControllerAuthorize;
  19. use Cake\Controller\Controller;
  20. use Cake\Network\Request;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Class ControllerAuthorizeTest
  24. *
  25. */
  26. class ControllerAuthorizeTest extends TestCase {
  27. /**
  28. * setup
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->controller = $this->getMock('Cake\Controller\Controller', array('isAuthorized'), array(), '', false);
  35. $this->components = $this->getMock('Cake\Controller\ComponentRegistry');
  36. $this->components->expects($this->any())
  37. ->method('getController')
  38. ->will($this->returnValue($this->controller));
  39. $this->auth = new ControllerAuthorize($this->components);
  40. }
  41. /**
  42. * @expectedException \PHPUnit_Framework_Error
  43. */
  44. public function testControllerTypeError() {
  45. $this->auth->controller(new \StdClass());
  46. }
  47. /**
  48. * @expectedException Cake\Error\Exception
  49. */
  50. public function testControllerErrorOnMissingMethod() {
  51. $this->auth->controller(new Controller());
  52. }
  53. /**
  54. * test failure
  55. *
  56. * @return void
  57. */
  58. public function testAuthorizeFailure() {
  59. $user = array();
  60. $request = new Request('/posts/index');
  61. $this->assertFalse($this->auth->authorize($user, $request));
  62. }
  63. /**
  64. * test isAuthorized working.
  65. *
  66. * @return void
  67. */
  68. public function testAuthorizeSuccess() {
  69. $user = array('User' => array('username' => 'mark'));
  70. $request = new Request('/posts/index');
  71. $this->controller->expects($this->once())
  72. ->method('isAuthorized')
  73. ->with($user)
  74. ->will($this->returnValue(true));
  75. $this->assertTrue($this->auth->authorize($user, $request));
  76. }
  77. }