AdditionalScriptsMiddlewareTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Middleware;
  8. use OC\AppFramework\Middleware\AdditionalScriptsMiddleware;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  11. use OCP\AppFramework\Http\Response;
  12. use OCP\AppFramework\Http\StandaloneTemplateResponse;
  13. use OCP\AppFramework\Http\TemplateResponse;
  14. use OCP\AppFramework\PublicShareController;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\IUserSession;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
  19. /** @var IUserSession|MockObject */
  20. private $userSession;
  21. /** @var Controller */
  22. private $controller;
  23. /** @var AdditionalScriptsMiddleware */
  24. private $middleWare;
  25. /** @var IEventDispatcher|MockObject */
  26. private $dispatcher;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->userSession = $this->createMock(IUserSession::class);
  30. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  31. $this->middleWare = new AdditionalScriptsMiddleware(
  32. $this->userSession,
  33. $this->dispatcher
  34. );
  35. $this->controller = $this->createMock(Controller::class);
  36. }
  37. public function testNoTemplateResponse() {
  38. $this->userSession->expects($this->never())
  39. ->method($this->anything());
  40. $this->dispatcher->expects($this->never())
  41. ->method($this->anything());
  42. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
  43. }
  44. public function testPublicShareController() {
  45. $this->userSession->expects($this->never())
  46. ->method($this->anything());
  47. $this->dispatcher->expects($this->never())
  48. ->method($this->anything());
  49. $this->middleWare->afterController($this->createMock(PublicShareController::class), 'myMethod', $this->createMock(Response::class));
  50. }
  51. public function testStandaloneTemplateResponse() {
  52. $this->userSession->expects($this->never())
  53. ->method($this->anything());
  54. $this->dispatcher->expects($this->once())
  55. ->method('dispatchTyped')
  56. ->willReturnCallback(function ($event) {
  57. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  58. return;
  59. }
  60. $this->fail('Wrong event dispatched');
  61. });
  62. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(StandaloneTemplateResponse::class));
  63. }
  64. public function testTemplateResponseNotLoggedIn() {
  65. $this->userSession->method('isLoggedIn')
  66. ->willReturn(false);
  67. $this->dispatcher->expects($this->once())
  68. ->method('dispatchTyped')
  69. ->willReturnCallback(function ($event) {
  70. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  71. return;
  72. }
  73. $this->fail('Wrong event dispatched');
  74. });
  75. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  76. }
  77. public function testTemplateResponseLoggedIn() {
  78. $events = [];
  79. $this->userSession->method('isLoggedIn')
  80. ->willReturn(true);
  81. $this->dispatcher->expects($this->once())
  82. ->method('dispatchTyped')
  83. ->willReturnCallback(function ($event) {
  84. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === true) {
  85. return;
  86. }
  87. $this->fail('Wrong event dispatched');
  88. });
  89. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  90. }
  91. }