ControllerAuthorizeTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package cake.tests.cases.libs.controller.components.auth
  13. * @since CakePHP(tm) v 2.0
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('Controller', 'Controller');
  17. App::uses('ControllerAuthorize', 'Controller/Component/Auth');
  18. App::uses('CakeRequest', 'Network');
  19. App::uses('CakeResponse', 'Network');
  20. class ControllerAuthorizeTest extends CakeTestCase {
  21. /**
  22. * setup
  23. *
  24. * @return void
  25. */
  26. function setUp() {
  27. parent::setUp();
  28. $this->controller = $this->getMock('Controller', array('isAuthorized'), array(), '', false);
  29. $this->components = $this->getMock('ComponentCollection');
  30. $this->components->expects($this->any())
  31. ->method('getController')
  32. ->will($this->returnValue($this->controller));
  33. $this->auth = new ControllerAuthorize($this->components);
  34. }
  35. /**
  36. *
  37. * @expectedException CakeException
  38. */
  39. function testControllerTypeError() {
  40. $this->auth->controller(new StdClass());
  41. }
  42. /**
  43. * @expectedException CakeException
  44. */
  45. function testControllerErrorOnMissingMethod() {
  46. $this->auth->controller(new Controller());
  47. }
  48. /**
  49. * test failure
  50. *
  51. * @return void
  52. */
  53. function testAuthorizeFailure() {
  54. $user = array();
  55. $request = new CakeRequest('/posts/index', false);
  56. $this->assertFalse($this->auth->authorize($user, $request));
  57. }
  58. /**
  59. * test isAuthorized working.
  60. *
  61. * @return void
  62. */
  63. function testAuthorizeSuccess() {
  64. $user = array('User' => array('username' => 'mark'));
  65. $request = new CakeRequest('/posts/index', false);
  66. $this->controller->expects($this->once())
  67. ->method('isAuthorized')
  68. ->with($user)
  69. ->will($this->returnValue(true));
  70. $this->assertTrue($this->auth->authorize($user, $request));
  71. }
  72. }