JsControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\JsController;
  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 JsControllerTest extends TestCase {
  38. /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
  39. private $appData;
  40. /** @var JsController */
  41. private $controller;
  42. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  43. private $request;
  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('js')
  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 JsController(
  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->getJs('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->getJs('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. $this->appData->method('getFolder')
  86. ->with('myapp')
  87. ->willReturn($folder);
  88. $folder->method('getFile')
  89. ->with('file.js')
  90. ->willReturn($file);
  91. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  92. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  93. $expires = new \DateTime();
  94. $expires->setTimestamp(1337);
  95. $expires->add(new \DateInterval('PT31536000S'));
  96. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  97. $expected->addHeader('Pragma', 'cache');
  98. $result = $this->controller->getJs('file.js', 'myapp');
  99. $this->assertEquals($expected, $result);
  100. }
  101. public function testGetGzipFile() {
  102. $folder = $this->createMock(ISimpleFolder::class);
  103. $gzipFile = $this->createMock(ISimpleFile::class);
  104. $this->appData->method('getFolder')
  105. ->with('myapp')
  106. ->willReturn($folder);
  107. $folder->method('getFile')
  108. ->with('file.js.gzip')
  109. ->willReturn($gzipFile);
  110. $this->request->method('getHeader')
  111. ->with('Accept-Encoding')
  112. ->willReturn('gzip, deflate');
  113. $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  114. $expected->addHeader('Content-Encoding', 'gzip');
  115. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  116. $expires = new \DateTime();
  117. $expires->setTimestamp(1337);
  118. $expires->add(new \DateInterval('PT31536000S'));
  119. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  120. $expected->addHeader('Pragma', 'cache');
  121. $result = $this->controller->getJs('file.js', 'myapp');
  122. $this->assertEquals($expected, $result);
  123. }
  124. public function testGetGzipFileNotFound() {
  125. $folder = $this->createMock(ISimpleFolder::class);
  126. $file = $this->createMock(ISimpleFile::class);
  127. $this->appData->method('getFolder')
  128. ->with('myapp')
  129. ->willReturn($folder);
  130. $folder->method('getFile')
  131. ->willReturnCallback(
  132. function ($fileName) use ($file) {
  133. if ($fileName === 'file.js') {
  134. return $file;
  135. }
  136. throw new NotFoundException();
  137. }
  138. );
  139. $this->request->method('getHeader')
  140. ->with('Accept-Encoding')
  141. ->willReturn('gzip, deflate');
  142. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  143. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  144. $expires = new \DateTime();
  145. $expires->setTimestamp(1337);
  146. $expires->add(new \DateInterval('PT31536000S'));
  147. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  148. $expected->addHeader('Pragma', 'cache');
  149. $result = $this->controller->getJs('file.js', 'myapp');
  150. $this->assertEquals($expected, $result);
  151. }
  152. }