ControllerAuthorizeTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * ControllerAuthorizeTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Auth;
  18. use Cake\Auth\ControllerAuthorize;
  19. use Cake\Controller\ComponentRegistry;
  20. use Cake\Controller\Controller;
  21. use Cake\Http\ServerRequest;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * ControllerAuthorizeTest
  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->getMockBuilder(Controller::class)
  37. ->setMethods(['isAuthorized'])
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->components = $this->getMockBuilder(ComponentRegistry::class)->getMock();
  41. $this->components->expects($this->any())
  42. ->method('getController')
  43. ->will($this->returnValue($this->controller));
  44. $this->auth = new ControllerAuthorize($this->components);
  45. }
  46. /**
  47. * @return void
  48. */
  49. public function testControllerErrorOnMissingMethod()
  50. {
  51. $this->expectException(\Cake\Core\Exception\Exception::class);
  52. $this->auth->controller(new Controller());
  53. }
  54. /**
  55. * test failure
  56. *
  57. * @return void
  58. */
  59. public function testAuthorizeFailure()
  60. {
  61. $user = [];
  62. $request = new ServerRequest('/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. {
  72. $user = ['User' => ['username' => 'mark']];
  73. $request = new ServerRequest('/posts/index');
  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. }