JsControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $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.js')
  92. ->willReturn($file);
  93. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  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. $result = $this->controller->getJs('file.js', 'myapp');
  100. $this->assertEquals($expected, $result);
  101. }
  102. public function testGetGzipFile() {
  103. $folder = $this->createMock(ISimpleFolder::class);
  104. $gzipFile = $this->createMock(ISimpleFile::class);
  105. $gzipFile->method('getName')->willReturn('my name');
  106. $gzipFile->method('getMTime')->willReturn(42);
  107. $this->appData->method('getFolder')
  108. ->with('myapp')
  109. ->willReturn($folder);
  110. $folder->method('getFile')
  111. ->with('file.js.gzip')
  112. ->willReturn($gzipFile);
  113. $this->request->method('getHeader')
  114. ->with('Accept-Encoding')
  115. ->willReturn('gzip, deflate');
  116. $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  117. $expected->addHeader('Content-Encoding', 'gzip');
  118. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  119. $expires = new \DateTime();
  120. $expires->setTimestamp(1337);
  121. $expires->add(new \DateInterval('PT31536000S'));
  122. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  123. $result = $this->controller->getJs('file.js', 'myapp');
  124. $this->assertEquals($expected, $result);
  125. }
  126. public function testGetGzipFileNotFound() {
  127. $folder = $this->createMock(ISimpleFolder::class);
  128. $file = $this->createMock(ISimpleFile::class);
  129. $file->method('getName')->willReturn('my name');
  130. $file->method('getMTime')->willReturn(42);
  131. $this->appData->method('getFolder')
  132. ->with('myapp')
  133. ->willReturn($folder);
  134. $folder->method('getFile')
  135. ->willReturnCallback(
  136. function ($fileName) use ($file) {
  137. if ($fileName === 'file.js') {
  138. return $file;
  139. }
  140. throw new NotFoundException();
  141. }
  142. );
  143. $this->request->method('getHeader')
  144. ->with('Accept-Encoding')
  145. ->willReturn('gzip, deflate');
  146. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  147. $expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
  148. $expires = new \DateTime();
  149. $expires->setTimestamp(1337);
  150. $expires->add(new \DateInterval('PT31536000S'));
  151. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  152. $result = $this->controller->getJs('file.js', 'myapp');
  153. $this->assertEquals($expected, $result);
  154. }
  155. }