CssControllerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\HintException;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\FileDisplayResponse;
  28. use OCP\AppFramework\Http\NotFoundResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Files\IAppData;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\SimpleFS\ISimpleFile;
  33. use OCP\Files\SimpleFS\ISimpleFolder;
  34. use OCP\IRequest;
  35. use Test\TestCase;
  36. class CssControllerTest extends TestCase {
  37. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  38. private $appData;
  39. /** @var CssController */
  40. private $controller;
  41. public function setUp() {
  42. parent::setUp();
  43. $this->appData = $this->createMock(IAppData::class);
  44. $timeFactory = $this->createMock(ITimeFactory::class);
  45. $timeFactory->method('getTime')
  46. ->willReturn(1337);
  47. $this->controller = new CssController(
  48. 'core',
  49. $this->createMock(IRequest::class),
  50. $this->appData,
  51. $timeFactory
  52. );
  53. }
  54. public function testNoCssFolderForApp() {
  55. $this->appData->method('getFolder')
  56. ->with('myapp')
  57. ->willThrowException(new NotFoundException());
  58. $result = $this->controller->getCss('file.css', 'myapp');
  59. $this->assertInstanceOf(NotFoundResponse::class, $result);
  60. }
  61. public function testNoCssFile() {
  62. $folder = $this->createMock(ISimpleFolder::class);
  63. $this->appData->method('getFolder')
  64. ->with('myapp')
  65. ->willReturn($folder);
  66. $folder->method('getFile')
  67. ->willThrowException(new NotFoundException());
  68. $result = $this->controller->getCss('file.css', 'myapp');
  69. $this->assertInstanceOf(NotFoundResponse::class, $result);
  70. }
  71. public function testGetFile() {
  72. $folder = $this->createMock(ISimpleFolder::class);
  73. $file = $this->createMock(ISimpleFile::class);
  74. $this->appData->method('getFolder')
  75. ->with('myapp')
  76. ->willReturn($folder);
  77. $folder->method('getFile')
  78. ->with('file.css')
  79. ->willReturn($file);
  80. $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  81. $expected->cacheFor(86400);
  82. $expires = new \DateTime();
  83. $expires->setTimestamp(1337);
  84. $expires->add(new \DateInterval('PT24H'));
  85. $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  86. $expected->addHeader('Pragma', 'cache');
  87. $result = $this->controller->getCss('file.css', 'myapp');
  88. $this->assertEquals($expected, $result);
  89. }
  90. }