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