ControllerAuthorizeTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * @package Cake.Test.Case.Controller.Component.Auth
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Controller', 'Controller');
  19. App::uses('ControllerAuthorize', 'Controller/Component/Auth');
  20. App::uses('CakeRequest', 'Network');
  21. App::uses('CakeResponse', 'Network');
  22. /**
  23. * Class ControllerAuthorizeTest
  24. *
  25. * @package Cake.Test.Case.Controller.Component.Auth
  26. */
  27. class ControllerAuthorizeTest extends CakeTestCase {
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->controller = $this->getMock('Controller', array('isAuthorized'), array(), '', false);
  36. $this->components = $this->getMock('ComponentCollection');
  37. $this->components->expects($this->any())
  38. ->method('getController')
  39. ->will($this->returnValue($this->controller));
  40. $this->auth = new ControllerAuthorize($this->components);
  41. }
  42. /**
  43. * @expectedException PHPUnit_Framework_Error
  44. * @return void
  45. */
  46. public function testControllerTypeError() {
  47. $this->auth->controller(new StdClass());
  48. }
  49. /**
  50. * @expectedException CakeException
  51. * @return void
  52. */
  53. public function testControllerErrorOnMissingMethod() {
  54. $this->auth->controller(new Controller());
  55. }
  56. /**
  57. * test failure
  58. *
  59. * @return void
  60. */
  61. public function testAuthorizeFailure() {
  62. $user = array();
  63. $request = new CakeRequest('/posts/index', false);
  64. $this->assertFalse($this->auth->authorize($user, $request));
  65. }
  66. /**
  67. * test isAuthorized working.
  68. *
  69. * @return void
  70. */
  71. public function testAuthorizeSuccess() {
  72. $user = array('User' => array('username' => 'mark'));
  73. $request = new CakeRequest('/posts/index', false);
  74. $this->controller->expects($this->once())
  75. ->method('isAuthorized')
  76. ->with($user)
  77. ->will($this->returnValue(true));
  78. $this->assertTrue($this->auth->authorize($user, $request));
  79. }
  80. }