ControllerAuthorizeTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 \PHPUnit_Framework_Error
  45. * @return void
  46. */
  47. public function testControllerTypeError()
  48. {
  49. $this->auth->controller(new \StdClass());
  50. }
  51. /**
  52. * @expectedException \Cake\Core\Exception\Exception
  53. * @return void
  54. */
  55. public function testControllerErrorOnMissingMethod()
  56. {
  57. $this->auth->controller(new Controller());
  58. }
  59. /**
  60. * test failure
  61. *
  62. * @return void
  63. */
  64. public function testAuthorizeFailure()
  65. {
  66. $user = [];
  67. $request = new Request('/posts/index');
  68. $this->assertFalse($this->auth->authorize($user, $request));
  69. }
  70. /**
  71. * test isAuthorized working.
  72. *
  73. * @return void
  74. */
  75. public function testAuthorizeSuccess()
  76. {
  77. $user = ['User' => ['username' => 'mark']];
  78. $request = new Request('/posts/index');
  79. $this->controller->expects($this->once())
  80. ->method('isAuthorized')
  81. ->with($user)
  82. ->will($this->returnValue(true));
  83. $this->assertTrue($this->auth->authorize($user, $request));
  84. }
  85. }