CssControllerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Core\Controller;
  24. use OC\Core\Controller\CssController;
  25. use OC\Files\AppData\AppData;
  26. use OC\Files\AppData\Factory;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\FileDisplayResponse;
  29. use OCP\AppFramework\Http\NotFoundResponse;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\Files\IAppData;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\SimpleFS\ISimpleFile;
  34. use OCP\Files\SimpleFS\ISimpleFolder;
  35. use OCP\IRequest;
  36. use Test\TestCase;
  37. class CssControllerTest extends TestCase {
  38. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  39. private $appData;
  40. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  41. private $request;
  42. /** @var CssController */
  43. private $controller;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. /** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
  47. $factory = $this->createMock(Factory::class);
  48. $this->appData = $this->createMock(AppData::class);
  49. $factory->expects($this->once())
  50. ->method('get')
  51. ->with('css')
  52. ->willReturn($this->appData);
  53. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject $timeFactory */
  54. $timeFactory = $this->createMock(ITimeFactory::class);
  55. $timeFactory->method('getTime')
  56. ->willReturn(1337);
  57. $this->request = $this->createMock(IRequest::class);
  58. $this->controller = new CssController(
  59. 'core',
  60. $this->request,
  61. $factory,
  62. $timeFactory
  63. );
  64. }
  65. public function testNoCssFolderForApp() {
  66. $this->appData->method('getFolder')
  67. ->with('myapp')
  68. ->willThrowException(new NotFoundException());
  69. $result = $this->controller->getCss('file.css', 'myapp');
  70. $this->assertInstanceOf(NotFoundResponse::class, $result);
  71. }
  72. public function testNoCssFile() {
  73. $folder = $this->createMock(ISimpleFolder::class);
  74. $this->appData->method('getFolder')
  75. ->with('myapp')
  76. ->willReturn($folder);
  77. $folder->method('getFile')
  78. ->willThrowException(new NotFoundException());
  79. $result = $this->controller->getCss('file.css', 'myapp');
  80. $this->assertInstanceOf(NotFoundResponse::class, $result);
  81. }
  82. public function testGetFile() {
  83. $folder = $this->createMock(ISimpleFolder::class);
  84. $file = $this->createMock(ISimpleFile::class);
  85. $file->method('getName')->willReturn('my name');
  86. $file->method('getMTime')->willReturn(42);
  87. $this->appData->method('getFolder')
  88. ->with('myapp')
  89. ->willReturn($folder);
  90. $folder->method('getFile')
  91. ->with('file.css')
  92. ->willReturn($file);
  93. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  94. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  95. $expires = new \DateTime();
  96. $expires->setTimestamp(1337);
  97. $expires->add(new \DateInterval('PT31536000S'));
  98. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  99. $expected->addHeader('Pragma', 'cache');
  100. $result = $this->controller->getCss('file.css', 'myapp');
  101. $this->assertEquals($expected, $result);
  102. }
  103. public function testGetGzipFile() {
  104. $folder = $this->createMock(ISimpleFolder::class);
  105. $gzipFile = $this->createMock(ISimpleFile::class);
  106. $gzipFile->method('getName')->willReturn('my name');
  107. $gzipFile->method('getMTime')->willReturn(42);
  108. $this->appData->method('getFolder')
  109. ->with('myapp')
  110. ->willReturn($folder);
  111. $folder->method('getFile')
  112. ->with('file.css.gzip')
  113. ->willReturn($gzipFile);
  114. $this->request->method('getHeader')
  115. ->with('Accept-Encoding')
  116. ->willReturn('gzip, deflate');
  117. $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  118. $expected->addHeader('Content-Encoding', 'gzip');
  119. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  120. $expires = new \DateTime();
  121. $expires->setTimestamp(1337);
  122. $expires->add(new \DateInterval('PT31536000S'));
  123. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  124. $expected->addHeader('Pragma', 'cache');
  125. $result = $this->controller->getCss('file.css', 'myapp');
  126. $this->assertEquals($expected, $result);
  127. }
  128. public function testGetGzipFileNotFound() {
  129. $folder = $this->createMock(ISimpleFolder::class);
  130. $file = $this->createMock(ISimpleFile::class);
  131. $file->method('getName')->willReturn('my name');
  132. $file->method('getMTime')->willReturn(42);
  133. $this->appData->method('getFolder')
  134. ->with('myapp')
  135. ->willReturn($folder);
  136. $folder->method('getFile')
  137. ->willReturnCallback(
  138. function ($fileName) use ($file) {
  139. if ($fileName === 'file.css') {
  140. return $file;
  141. }
  142. throw new NotFoundException();
  143. }
  144. );
  145. $this->request->method('getHeader')
  146. ->with('Accept-Encoding')
  147. ->willReturn('gzip, deflate');
  148. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  149. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  150. $expires = new \DateTime();
  151. $expires->setTimestamp(1337);
  152. $expires->add(new \DateInterval('PT31536000S'));
  153. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  154. $expected->addHeader('Pragma', 'cache');
  155. $result = $this->controller->getCss('file.css', 'myapp');
  156. $this->assertEquals($expected, $result);
  157. }
  158. }