ControllerAuthorizeTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @return void
  45. */
  46. public function testControllerTypeError()
  47. {
  48. $this->skipIf(PHP_VERSION_ID >= 70000);
  49. $message = '/^Argument 1 passed to Cake\\\Auth\\\ControllerAuthorize::controller\(\) must be an instance of Cake\\\Controller\\\Controller, instance of stdClass given.*/';
  50. $this->setExpectedExceptionRegExp('PHPUnit_Framework_Error', $message);
  51. $this->auth->controller(new \stdClass());
  52. }
  53. /**
  54. * @return void
  55. */
  56. public function testControllerTypeErrorPhp7()
  57. {
  58. $this->skipIf(PHP_VERSION_ID < 70000);
  59. try {
  60. $this->auth->controller(new \stdClass());
  61. $this->fail();
  62. } catch (\BaseException $e) {
  63. $expectedMessage = 'Argument 1 passed to Cake\Auth\ControllerAuthorize::controller() must be an instance of Cake\Controller\Controller, instance of stdClass given';
  64. $this->assertContains($expectedMessage, $e->getMessage());
  65. }
  66. }
  67. /**
  68. * @expectedException \Cake\Core\Exception\Exception
  69. * @return void
  70. */
  71. public function testControllerErrorOnMissingMethod()
  72. {
  73. $this->auth->controller(new Controller());
  74. }
  75. /**
  76. * test failure
  77. *
  78. * @return void
  79. */
  80. public function testAuthorizeFailure()
  81. {
  82. $user = [];
  83. $request = new Request('/posts/index');
  84. $this->assertFalse($this->auth->authorize($user, $request));
  85. }
  86. /**
  87. * test isAuthorized working.
  88. *
  89. * @return void
  90. */
  91. public function testAuthorizeSuccess()
  92. {
  93. $user = ['User' => ['username' => 'mark']];
  94. $request = new Request('/posts/index');
  95. $this->controller->expects($this->once())
  96. ->method('isAuthorized')
  97. ->with($user)
  98. ->will($this->returnValue(true));
  99. $this->assertTrue($this->auth->authorize($user, $request));
  100. }
  101. }