ControllerAuthorizeTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Auth;
  18. use Cake\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. * @return void
  44. */
  45. public function testControllerTypeError() {
  46. $this->auth->controller(new \StdClass());
  47. }
  48. /**
  49. * @expectedException \Cake\Core\Exception\Exception
  50. * @return void
  51. */
  52. public function testControllerErrorOnMissingMethod() {
  53. $this->auth->controller(new Controller());
  54. }
  55. /**
  56. * test failure
  57. *
  58. * @return void
  59. */
  60. public function testAuthorizeFailure() {
  61. $user = array();
  62. $request = new Request('/posts/index');
  63. $this->assertFalse($this->auth->authorize($user, $request));
  64. }
  65. /**
  66. * test isAuthorized working.
  67. *
  68. * @return void
  69. */
  70. public function testAuthorizeSuccess() {
  71. $user = array('User' => array('username' => 'mark'));
  72. $request = new Request('/posts/index');
  73. $this->controller->expects($this->once())
  74. ->method('isAuthorized')
  75. ->with($user)
  76. ->will($this->returnValue(true));
  77. $this->assertTrue($this->auth->authorize($user, $request));
  78. }
  79. }