SessionMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * ownCloud - App Framework
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later. See the COPYING file.
  8. *
  9. * @author Thomas Müller <deepdiver@owncloud.com>
  10. * @copyright Thomas Müller 2014
  11. */
  12. namespace Test\AppFramework\Middleware;
  13. use OC\AppFramework\Middleware\SessionMiddleware;
  14. use OC\AppFramework\Utility\ControllerMethodReflector;
  15. use OCP\AppFramework\Controller;
  16. use OCP\AppFramework\Http\Attribute\UseSession;
  17. use OCP\AppFramework\Http\Response;
  18. use OCP\IRequest;
  19. use OCP\ISession;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Test\TestCase;
  22. class SessionMiddlewareTest extends TestCase {
  23. private ControllerMethodReflector|MockObject $reflector;
  24. private ISession|MockObject $session;
  25. private Controller $controller;
  26. private SessionMiddleware $middleware;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  30. $this->session = $this->createMock(ISession::class);
  31. $this->controller = new class('app', $this->createMock(IRequest::class)) extends Controller {
  32. /**
  33. * @UseSession
  34. */
  35. public function withAnnotation() {
  36. }
  37. #[UseSession]
  38. public function withAttribute() {
  39. }
  40. public function without() {
  41. }
  42. };
  43. $this->middleware = new SessionMiddleware(
  44. $this->reflector,
  45. $this->session,
  46. );
  47. }
  48. public function testSessionNotClosedOnBeforeController(): void {
  49. $this->configureSessionMock(0, 1);
  50. $this->reflector->expects(self::once())
  51. ->method('hasAnnotation')
  52. ->with('UseSession')
  53. ->willReturn(true);
  54. $this->middleware->beforeController($this->controller, 'withAnnotation');
  55. }
  56. public function testSessionNotClosedOnBeforeControllerWithAttribute(): void {
  57. $this->configureSessionMock(0, 1);
  58. $this->reflector->expects(self::once())
  59. ->method('hasAnnotation')
  60. ->with('UseSession')
  61. ->willReturn(false);
  62. $this->middleware->beforeController($this->controller, 'withAttribute');
  63. }
  64. public function testSessionClosedOnAfterController(): void {
  65. $this->configureSessionMock(1);
  66. $this->reflector->expects(self::once())
  67. ->method('hasAnnotation')
  68. ->with('UseSession')
  69. ->willReturn(true);
  70. $this->middleware->afterController($this->controller, 'withAnnotation', new Response());
  71. }
  72. public function testSessionClosedOnAfterControllerWithAttribute(): void {
  73. $this->configureSessionMock(1);
  74. $this->reflector->expects(self::once())
  75. ->method('hasAnnotation')
  76. ->with('UseSession')
  77. ->willReturn(true);
  78. $this->middleware->afterController($this->controller, 'withAttribute', new Response());
  79. }
  80. public function testSessionReopenedAndClosedOnBeforeController(): void {
  81. $this->configureSessionMock(1, 1);
  82. $this->reflector->expects(self::exactly(2))
  83. ->method('hasAnnotation')
  84. ->with('UseSession')
  85. ->willReturn(true);
  86. $this->middleware->beforeController($this->controller, 'withAnnotation');
  87. $this->middleware->afterController($this->controller, 'withAnnotation', new Response());
  88. }
  89. public function testSessionReopenedAndClosedOnBeforeControllerWithAttribute(): void {
  90. $this->configureSessionMock(1, 1);
  91. $this->reflector->expects(self::exactly(2))
  92. ->method('hasAnnotation')
  93. ->with('UseSession')
  94. ->willReturn(false);
  95. $this->middleware->beforeController($this->controller, 'withAttribute');
  96. $this->middleware->afterController($this->controller, 'withAttribute', new Response());
  97. }
  98. public function testSessionClosedOnBeforeController(): void {
  99. $this->configureSessionMock(0);
  100. $this->reflector->expects(self::once())
  101. ->method('hasAnnotation')
  102. ->with('UseSession')
  103. ->willReturn(false);
  104. $this->middleware->beforeController($this->controller, 'without');
  105. }
  106. public function testSessionNotClosedOnAfterController(): void {
  107. $this->configureSessionMock(0);
  108. $this->reflector->expects(self::once())
  109. ->method('hasAnnotation')
  110. ->with('UseSession')
  111. ->willReturn(false);
  112. $this->middleware->afterController($this->controller, 'without', new Response());
  113. }
  114. private function configureSessionMock(int $expectedCloseCount, int $expectedReopenCount = 0): void {
  115. $this->session->expects($this->exactly($expectedCloseCount))
  116. ->method('close');
  117. $this->session->expects($this->exactly($expectedReopenCount))
  118. ->method('reopen');
  119. }
  120. }