]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix controller tests 4070/head
authorRoeland Jago Douma <roeland@famdouma.nl>
Tue, 28 Mar 2017 21:42:20 +0000 (23:42 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Tue, 28 Mar 2017 21:42:20 +0000 (23:42 +0200)
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
tests/Core/Controller/CssControllerTest.php
tests/Core/Controller/JsControllerTest.php

index 60fef9dddade58ed218232d3a475ce5f0589a719..7fa358e056efa300362d6526eccb82ccfae4ca97 100644 (file)
@@ -40,6 +40,9 @@ class CssControllerTest extends TestCase {
        /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
        private $appData;
 
+       /** @var IRequests|\PHPUnit_Framework_MockObject_MockObject */
+       private $request;
+
        /** @var CssController */
        private $controller;
 
@@ -52,9 +55,11 @@ class CssControllerTest extends TestCase {
                $timeFactory->method('getTime')
                        ->willReturn(1337);
 
+               $this->request = $this->createMock(IRequest::class);
+
                $this->controller = new CssController(
                        'core',
-                       $this->createMock(IRequest::class),
+                       $this->request,
                        $this->appData,
                        $timeFactory
                );
@@ -108,4 +113,65 @@ class CssControllerTest extends TestCase {
                $this->assertEquals($expected, $result);
        }
 
+       public function testGetGzipFile() {
+               $folder = $this->createMock(ISimpleFolder::class);
+               $gzipFile = $this->createMock(ISimpleFile::class);
+               $this->appData->method('getFolder')
+                       ->with('myapp')
+                       ->willReturn($folder);
+
+               $folder->method('getFile')
+                       ->with('file.css.gz')
+                       ->willReturn($gzipFile);
+
+               $this->request->method('getHeader')
+                       ->with('Accept-Encoding')
+                       ->willReturn('gzip, deflate');
+
+               $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
+               $expected->addHeader('Content-Encoding', 'gzip');
+               $expected->cacheFor(86400);
+               $expires = new \DateTime();
+               $expires->setTimestamp(1337);
+               $expires->add(new \DateInterval('PT24H'));
+               $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
+               $expected->addHeader('Pragma', 'cache');
+
+               $result = $this->controller->getCss('file.css', 'myapp');
+               $this->assertEquals($expected, $result);
+       }
+
+       public function testGetGzipFileNotFound() {
+               $folder = $this->createMock(ISimpleFolder::class);
+               $file = $this->createMock(ISimpleFile::class);
+               $this->appData->method('getFolder')
+                       ->with('myapp')
+                       ->willReturn($folder);
+
+               $folder->method('getFile')
+                       ->will($this->returnCallback(
+                               function($fileName) use ($file) {
+                                       if ($fileName === 'file.css') {
+                                               return $file;
+                                       }
+                                       throw new NotFoundException();
+                               })
+                       );
+
+               $this->request->method('getHeader')
+                       ->with('Accept-Encoding')
+                       ->willReturn('gzip, deflate');
+
+               $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
+               $expected->cacheFor(86400);
+               $expires = new \DateTime();
+               $expires->setTimestamp(1337);
+               $expires->add(new \DateInterval('PT24H'));
+               $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
+               $expected->addHeader('Pragma', 'cache');
+
+               $result = $this->controller->getCss('file.css', 'myapp');
+               $this->assertEquals($expected, $result);
+       }
+
 }
index febb785f60d82bbba13b4ede4522967949c85083..8f48a7c3390592a40960b266fc02a285ec9c7746 100644 (file)
@@ -42,6 +42,9 @@ class JsControllerTest extends TestCase {
        /** @var JsController */
        private $controller;
 
+       /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+       private $request;
+
        public function setUp() {
                parent::setUp();
 
@@ -51,9 +54,11 @@ class JsControllerTest extends TestCase {
                $timeFactory->method('getTime')
                        ->willReturn(1337);
 
+               $this->request = $this->createMock(IRequest::class);
+
                $this->controller = new JsController(
                        'core',
-                       $this->createMock(IRequest::class),
+                       $this->request,
                        $this->appData,
                        $timeFactory
                );
@@ -107,4 +112,65 @@ class JsControllerTest extends TestCase {
                $this->assertEquals($expected, $result);
        }
 
+       public function testGetGzipFile() {
+               $folder = $this->createMock(ISimpleFolder::class);
+               $gzipFile = $this->createMock(ISimpleFile::class);
+               $this->appData->method('getFolder')
+                       ->with('myapp')
+                       ->willReturn($folder);
+
+               $folder->method('getFile')
+                       ->with('file.js.gz')
+                       ->willReturn($gzipFile);
+
+               $this->request->method('getHeader')
+                       ->with('Accept-Encoding')
+                       ->willReturn('gzip, deflate');
+
+               $expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
+               $expected->addHeader('Content-Encoding', 'gzip');
+               $expected->cacheFor(86400);
+               $expires = new \DateTime();
+               $expires->setTimestamp(1337);
+               $expires->add(new \DateInterval('PT24H'));
+               $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
+               $expected->addHeader('Pragma', 'cache');
+
+               $result = $this->controller->getJs('file.js', 'myapp');
+               $this->assertEquals($expected, $result);
+       }
+
+       public function testGetGzipFileNotFound() {
+               $folder = $this->createMock(ISimpleFolder::class);
+               $file = $this->createMock(ISimpleFile::class);
+               $this->appData->method('getFolder')
+                       ->with('myapp')
+                       ->willReturn($folder);
+
+               $folder->method('getFile')
+                       ->will($this->returnCallback(
+                               function($fileName) use ($file) {
+                                       if ($fileName === 'file.js') {
+                                               return $file;
+                                       }
+                                       throw new NotFoundException();
+                               })
+                       );
+
+               $this->request->method('getHeader')
+                       ->with('Accept-Encoding')
+                       ->willReturn('gzip, deflate');
+
+               $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
+               $expected->cacheFor(86400);
+               $expires = new \DateTime();
+               $expires->setTimestamp(1337);
+               $expires->add(new \DateInterval('PT24H'));
+               $expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
+               $expected->addHeader('Pragma', 'cache');
+
+               $result = $this->controller->getJs('file.js', 'myapp');
+               $this->assertEquals($expected, $result);
+       }
+
 }