ControllerAuthorizeTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * @var \Cake\Auth\ControllerAuthorize
  30. */
  31. protected $auth;
  32. /**
  33. * setup
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. $this->controller = $this->getMockBuilder(Controller::class)
  41. ->setMethods(['isAuthorized'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->components = $this->getMockBuilder(ComponentRegistry::class)->getMock();
  45. $this->components->expects($this->any())
  46. ->method('getController')
  47. ->will($this->returnValue($this->controller));
  48. $this->auth = new ControllerAuthorize($this->components);
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function testControllerErrorOnMissingMethod()
  54. {
  55. $this->expectException(\Cake\Core\Exception\Exception::class);
  56. $this->auth->controller(new Controller());
  57. }
  58. /**
  59. * test failure
  60. *
  61. * @return void
  62. */
  63. public function testAuthorizeFailure()
  64. {
  65. $user = [];
  66. $request = new ServerRequest('/posts/index');
  67. $this->assertFalse($this->auth->authorize($user, $request));
  68. }
  69. /**
  70. * test isAuthorized working.
  71. *
  72. * @return void
  73. */
  74. public function testAuthorizeSuccess()
  75. {
  76. $user = ['User' => ['username' => 'mark']];
  77. $request = new ServerRequest('/posts/index');
  78. $this->controller->expects($this->once())
  79. ->method('isAuthorized')
  80. ->with($user)
  81. ->will($this->returnValue(true));
  82. $this->assertTrue($this->auth->authorize($user, $request));
  83. }
  84. }