AdditionalScriptsMiddlewareTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
  36. /** @var IUserSession|MockObject */
  37. private $userSession;
  38. /** @var Controller */
  39. private $controller;
  40. /** @var AdditionalScriptsMiddleware */
  41. private $middleWare;
  42. /** @var IEventDispatcher|MockObject */
  43. private $dispatcher;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->userSession = $this->createMock(IUserSession::class);
  47. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  48. $this->middleWare = new AdditionalScriptsMiddleware(
  49. $this->userSession,
  50. $this->dispatcher
  51. );
  52. $this->controller = $this->createMock(Controller::class);
  53. }
  54. public function testNoTemplateResponse() {
  55. $this->userSession->expects($this->never())
  56. ->method($this->anything());
  57. $this->dispatcher->expects($this->never())
  58. ->method($this->anything());
  59. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
  60. }
  61. public function testPublicShareController() {
  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->createMock(PublicShareController::class), 'myMethod', $this->createMock(Response::class));
  67. }
  68. public function testStandaloneTemplateResponse() {
  69. $this->userSession->expects($this->never())
  70. ->method($this->anything());
  71. $this->dispatcher->expects($this->once())
  72. ->method('dispatchTyped')
  73. ->willReturnCallback(function ($event) {
  74. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  75. return;
  76. }
  77. $this->fail('Wrong event dispatched');
  78. });
  79. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(StandaloneTemplateResponse::class));
  80. }
  81. public function testTemplateResponseNotLoggedIn() {
  82. $this->userSession->method('isLoggedIn')
  83. ->willReturn(false);
  84. $this->dispatcher->expects($this->once())
  85. ->method('dispatchTyped')
  86. ->willReturnCallback(function ($event) {
  87. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
  88. return;
  89. }
  90. $this->fail('Wrong event dispatched');
  91. });
  92. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  93. }
  94. public function testTemplateResponseLoggedIn() {
  95. $events = [];
  96. $this->userSession->method('isLoggedIn')
  97. ->willReturn(true);
  98. $this->dispatcher->expects($this->once())
  99. ->method('dispatchTyped')
  100. ->willReturnCallback(function ($event) {
  101. if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === true) {
  102. return;
  103. }
  104. $this->fail('Wrong event dispatched');
  105. });
  106. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  107. }
  108. }