AdditionalScriptsMiddlewareTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\Middleware;
  25. use OC\AppFramework\Middleware\AdditionalScriptsMiddleware;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  28. use OCP\AppFramework\Http\Response;
  29. use OCP\AppFramework\Http\StandaloneTemplateResponse;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\PublicShareController;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\IUserSession;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  36. class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
  37. /** @var EventDispatcherInterface|MockObject */
  38. private $legacyDispatcher;
  39. /** @var IUserSession|MockObject */
  40. private $userSession;
  41. /** @var Controller */
  42. private $controller;
  43. /** @var AdditionalScriptsMiddleware */
  44. private $middleWare;
  45. /** @var IEventDispatcher|MockObject */
  46. private $dispatcher;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
  50. $this->userSession = $this->createMock(IUserSession::class);
  51. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  52. $this->middleWare = new AdditionalScriptsMiddleware(
  53. $this->legacyDispatcher,
  54. $this->userSession,
  55. $this->dispatcher
  56. );
  57. $this->controller = $this->createMock(Controller::class);
  58. }
  59. public function testNoTemplateResponse() {
  60. $this->legacyDispatcher->expects($this->never())
  61. ->method($this->anything());
  62. $this->userSession->expects($this->never())
  63. ->method($this->anything());
  64. $this->dispatcher->expects($this->never())
  65. ->method($this->anything());
  66. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
  67. }
  68. public function testPublicShareController() {
  69. $this->legacyDispatcher->expects($this->never())
  70. ->method($this->anything());
  71. $this->userSession->expects($this->never())
  72. ->method($this->anything());
  73. $this->dispatcher->expects($this->never())
  74. ->method($this->anything());
  75. $this->middleWare->afterController($this->createMock(PublicShareController::class), 'myMethod', $this->createMock(Response::class));
  76. }
  77. public function testStandaloneTemplateResponse() {
  78. $this->legacyDispatcher->expects($this->once())
  79. ->method('dispatch')
  80. ->willReturnCallback(function ($eventName) {
  81. if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
  82. return;
  83. }
  84. $this->fail('Wrong event dispatched');
  85. });
  86. $this->userSession->expects($this->never())
  87. ->method($this->anything());
  88. $this->dispatcher->expects($this->once())
  89. ->method('dispatchTyped')
  90. ->willReturnCallback(function ($event) {
  91. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  92. return;
  93. }
  94. $this->fail('Wrong event dispatched');
  95. });
  96. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(StandaloneTemplateResponse::class));
  97. }
  98. public function testTemplateResponseNotLoggedIn() {
  99. $this->legacyDispatcher->expects($this->once())
  100. ->method('dispatch')
  101. ->willReturnCallback(function ($eventName) {
  102. if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
  103. return;
  104. }
  105. $this->fail('Wrong event dispatched');
  106. });
  107. $this->userSession->method('isLoggedIn')
  108. ->willReturn(false);
  109. $this->dispatcher->expects($this->once())
  110. ->method('dispatchTyped')
  111. ->willReturnCallback(function ($event) {
  112. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  113. return;
  114. }
  115. $this->fail('Wrong event dispatched');
  116. });
  117. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  118. }
  119. public function testTemplateResponseLoggedIn() {
  120. $events = [];
  121. $this->legacyDispatcher->expects($this->exactly(2))
  122. ->method('dispatch')
  123. ->willReturnCallback(function ($eventName) use (&$events) {
  124. if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS ||
  125. $eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN) {
  126. $events[] = $eventName;
  127. return;
  128. }
  129. $this->fail('Wrong event dispatched');
  130. });
  131. $this->userSession->method('isLoggedIn')
  132. ->willReturn(true);
  133. $this->dispatcher->expects($this->once())
  134. ->method('dispatchTyped')
  135. ->willReturnCallback(function ($event) {
  136. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === true) {
  137. return;
  138. }
  139. $this->fail('Wrong event dispatched');
  140. });
  141. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  142. $this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, $events);
  143. $this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, $events);
  144. }
  145. }