ControllerAuthorizeTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->controller = $this->getMock('Cake\Controller\Controller', ['isAuthorized'], [], '', false);
  37. $this->components = $this->getMock('Cake\Controller\ComponentRegistry');
  38. $this->components->expects($this->any())
  39. ->method('getController')
  40. ->will($this->returnValue($this->controller));
  41. $this->auth = new ControllerAuthorize($this->components);
  42. }
  43. /**
  44. * @expectedException \Cake\Core\Exception\Exception
  45. * @return void
  46. */
  47. public function testControllerErrorOnMissingMethod()
  48. {
  49. $this->auth->controller(new Controller());
  50. }
  51. /**
  52. * test failure
  53. *
  54. * @return void
  55. */
  56. public function testAuthorizeFailure()
  57. {
  58. $user = [];
  59. $request = new Request('/posts/index');
  60. $this->assertFalse($this->auth->authorize($user, $request));
  61. }
  62. /**
  63. * test isAuthorized working.
  64. *
  65. * @return void
  66. */
  67. public function testAuthorizeSuccess()
  68. {
  69. $user = ['User' => ['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. }