AdditionalScriptsMiddlewareTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Response;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\IUserSession;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
  33. /** @var EventDispatcherInterface|MockObject */
  34. private $dispatcher;
  35. /** @var IUserSession|MockObject */
  36. private $userSession;
  37. /** @var Controller */
  38. private $controller;
  39. /** @var AdditionalScriptsMiddleware */
  40. private $middleWare;
  41. public function setUp() {
  42. parent::setUp();
  43. $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
  44. $this->userSession = $this->createMock(IUserSession::class);
  45. $this->middleWare = new AdditionalScriptsMiddleware(
  46. $this->dispatcher,
  47. $this->userSession
  48. );
  49. $this->controller = $this->createMock(Controller::class);
  50. }
  51. public function testNoTemplateResponse() {
  52. $this->dispatcher->expects($this->never())
  53. ->method($this->anything());
  54. $this->userSession->expects($this->never())
  55. ->method($this->anything());
  56. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
  57. }
  58. public function testTemplateResponseNotLoggedIn() {
  59. $this->dispatcher->expects($this->once())
  60. ->method('dispatch')
  61. ->willReturnCallback(function($eventName) {
  62. if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
  63. return;
  64. }
  65. $this->fail('Wrong event dispatched');
  66. });
  67. $this->userSession->method('isLoggedIn')
  68. ->willReturn(false);
  69. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  70. }
  71. public function testTemplateResponseLoggedIn() {
  72. $events = [];
  73. $this->dispatcher->expects($this->exactly(2))
  74. ->method('dispatch')
  75. ->willReturnCallback(function($eventName) use (&$events) {
  76. if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS ||
  77. $eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN) {
  78. $events[] = $eventName;
  79. return;
  80. }
  81. $this->fail('Wrong event dispatched');
  82. });
  83. $this->userSession->method('isLoggedIn')
  84. ->willReturn(true);
  85. $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
  86. $this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, $events);
  87. $this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, $events);
  88. }
  89. }