1
0

CssControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Controller;
  7. use OC\Core\Controller\CssController;
  8. use OC\Files\AppData\AppData;
  9. use OC\Files\AppData\Factory;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\FileDisplayResponse;
  12. use OCP\AppFramework\Http\NotFoundResponse;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\Files\IAppData;
  15. use OCP\Files\NotFoundException;
  16. use OCP\Files\SimpleFS\ISimpleFile;
  17. use OCP\Files\SimpleFS\ISimpleFolder;
  18. use OCP\IRequest;
  19. use Test\TestCase;
  20. class CssControllerTest extends TestCase {
  21. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  22. private $appData;
  23. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  24. private $request;
  25. /** @var CssController */
  26. private $controller;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
  30. $factory = $this->createMock(Factory::class);
  31. $this->appData = $this->createMock(AppData::class);
  32. $factory->expects($this->once())
  33. ->method('get')
  34. ->with('css')
  35. ->willReturn($this->appData);
  36. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject $timeFactory */
  37. $timeFactory = $this->createMock(ITimeFactory::class);
  38. $timeFactory->method('getTime')
  39. ->willReturn(1337);
  40. $this->request = $this->createMock(IRequest::class);
  41. $this->controller = new CssController(
  42. 'core',
  43. $this->request,
  44. $factory,
  45. $timeFactory
  46. );
  47. }
  48. public function testNoCssFolderForApp(): void {
  49. $this->appData->method('getFolder')
  50. ->with('myapp')
  51. ->willThrowException(new NotFoundException());
  52. $result = $this->controller->getCss('file.css', 'myapp');
  53. $this->assertInstanceOf(NotFoundResponse::class, $result);
  54. }
  55. public function testNoCssFile(): void {
  56. $folder = $this->createMock(ISimpleFolder::class);
  57. $this->appData->method('getFolder')
  58. ->with('myapp')
  59. ->willReturn($folder);
  60. $folder->method('getFile')
  61. ->willThrowException(new NotFoundException());
  62. $result = $this->controller->getCss('file.css', 'myapp');
  63. $this->assertInstanceOf(NotFoundResponse::class, $result);
  64. }
  65. public function testGetFile(): void {
  66. $folder = $this->createMock(ISimpleFolder::class);
  67. $file = $this->createMock(ISimpleFile::class);
  68. $file->method('getName')->willReturn('my name');
  69. $file->method('getMTime')->willReturn(42);
  70. $this->appData->method('getFolder')
  71. ->with('myapp')
  72. ->willReturn($folder);
  73. $folder->method('getFile')
  74. ->with('file.css')
  75. ->willReturn($file);
  76. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  77. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  78. $expires = new \DateTime();
  79. $expires->setTimestamp(1337);
  80. $expires->add(new \DateInterval('PT31536000S'));
  81. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  82. $result = $this->controller->getCss('file.css', 'myapp');
  83. $this->assertEquals($expected, $result);
  84. }
  85. public function testGetGzipFile(): void {
  86. $folder = $this->createMock(ISimpleFolder::class);
  87. $gzipFile = $this->createMock(ISimpleFile::class);
  88. $gzipFile->method('getName')->willReturn('my name');
  89. $gzipFile->method('getMTime')->willReturn(42);
  90. $this->appData->method('getFolder')
  91. ->with('myapp')
  92. ->willReturn($folder);
  93. $folder->method('getFile')
  94. ->with('file.css.gzip')
  95. ->willReturn($gzipFile);
  96. $this->request->method('getHeader')
  97. ->with('Accept-Encoding')
  98. ->willReturn('gzip, deflate');
  99. $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  100. $expected->addHeader('Content-Encoding', 'gzip');
  101. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  102. $expires = new \DateTime();
  103. $expires->setTimestamp(1337);
  104. $expires->add(new \DateInterval('PT31536000S'));
  105. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  106. $result = $this->controller->getCss('file.css', 'myapp');
  107. $this->assertEquals($expected, $result);
  108. }
  109. public function testGetGzipFileNotFound(): void {
  110. $folder = $this->createMock(ISimpleFolder::class);
  111. $file = $this->createMock(ISimpleFile::class);
  112. $file->method('getName')->willReturn('my name');
  113. $file->method('getMTime')->willReturn(42);
  114. $this->appData->method('getFolder')
  115. ->with('myapp')
  116. ->willReturn($folder);
  117. $folder->method('getFile')
  118. ->willReturnCallback(
  119. function ($fileName) use ($file) {
  120. if ($fileName === 'file.css') {
  121. return $file;
  122. }
  123. throw new NotFoundException();
  124. }
  125. );
  126. $this->request->method('getHeader')
  127. ->with('Accept-Encoding')
  128. ->willReturn('gzip, deflate');
  129. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  130. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  131. $expires = new \DateTime();
  132. $expires->setTimestamp(1337);
  133. $expires->add(new \DateInterval('PT31536000S'));
  134. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  135. $result = $this->controller->getCss('file.css', 'myapp');
  136. $this->assertEquals($expected, $result);
  137. }
  138. }