diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-21 03:43:32 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-21 03:43:32 +0300 |
commit | e99486a619581d8ea58cfc2c8918d53468b64af8 (patch) | |
tree | d7e6dd2ae9c59fd51b3fb2a74643808cd0ecd05f | |
parent | e8340da8fe6d3b333ad8b1d99a85ecb9ea2484c2 (diff) |
Tests for PageController
-rw-r--r-- | tests/unit/controller/FilesControllerTest.php | 2 | ||||
-rw-r--r-- | tests/unit/controller/PageControllerTest.php | 241 | ||||
-rw-r--r-- | tests/unit/controller/PreviewControllerTest.php | 2 |
3 files changed, 243 insertions, 2 deletions
diff --git a/tests/unit/controller/FilesControllerTest.php b/tests/unit/controller/FilesControllerTest.php index 4bb50254..c1e7f512 100644 --- a/tests/unit/controller/FilesControllerTest.php +++ b/tests/unit/controller/FilesControllerTest.php @@ -40,7 +40,7 @@ class FilesControllerTest extends \Test\TestCase { protected $appName = 'gallery'; /** @var IRequest */ protected $request; - /** @var ConfigController */ + /** @var FilesController */ protected $controller; /** @var IURLGenerator */ protected $urlGenerator; diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php new file mode 100644 index 00000000..956d98a3 --- /dev/null +++ b/tests/unit/controller/PageControllerTest.php @@ -0,0 +1,241 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Controller; + + +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; + +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Http\RedirectResponse; + +use OCA\Gallery\Environment\Environment; +use OCA\Gallery\Service\DownloadService; + +/** + * @package OCA\Gallery\Controller + */ +class PageControllerTest extends \Test\TestCase { + + /** @var string */ + private $appName = 'gallery'; + /** @var IRequest */ + private $request; + /** @var Environment */ + private $environment; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var DownloadService */ + private $downloadService; + /** @var IConfig */ + private $appConfig; + /** @var PageController */ + protected $controller; + + /** + * Test set up + */ + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->environment = $this->getMockBuilder('\OCA\Gallery\Environment\Environment') + ->disableOriginalConstructor() + ->getMock(); + $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator') + ->disableOriginalConstructor() + ->getMock(); + $this->downloadService = $this->getMockBuilder('\OCA\Gallery\Service\DownloadService') + ->disableOriginalConstructor() + ->getMock(); + $this->appConfig = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new PageController( + $this->appName, + $this->request, + $this->environment, + $this->urlGenerator, + $this->downloadService, + $this->appConfig + ); + } + + + public function testIndex() { + $params = ['appName' => $this->appName]; + $template = new TemplateResponse($this->appName, 'index', $params); + + $response = $this->controller->index(); + + $this->assertEquals($params, $response->getParams()); + $this->assertEquals('index', $response->getTemplateName()); + $this->assertTrue($response instanceof TemplateResponse); + $this->assertEquals($template->render(), $response->render()); + } + + public function testCspForImgContainsData() { + $response = $this->controller->index(); + + $this->assertContains( + "img-src 'self' data: data:", $response->getHeaders()['Content-Security-Policy'] + ); + } + + public function testCspForFontsContainsData() { + $response = $this->controller->index(); + + $this->assertContains( + "font-src 'self' data:", $response->getHeaders()['Content-Security-Policy'] + ); + } + + public function testSlideshow() { + $template = new TemplateResponse($this->appName, 'slideshow', [], 'blank'); + + $response = $this->controller->slideshow(); + + $this->assertEquals('slideshow', $response->getTemplateName()); + $this->assertTrue($response instanceof TemplateResponse); + $this->assertEquals($template->render(), $response->render()); + } + + public function testPublicIndexWithFolderToken() { + $token = 'aaaabbbbccccdddd'; + $password = 'I am a password'; + $displayName = 'User X'; + $albumName = 'My Shared Folder'; + $protected = 'true'; + $server2ServerSharing = true; + $server2ServerSharingEnabled = 'yes'; + $params = [ + 'appName' => $this->appName, + 'token' => $token, + 'displayName' => $displayName, + 'albumName' => $albumName, + 'server2ServerSharing' => $server2ServerSharing, + 'protected' => $protected, + 'filename' => $albumName + ]; + $this->mockGetSharedNode('dir', 12345); + $this->mockGetSharedFolderName($albumName); + $this->mockGetDisplayName($displayName); + $this->mockGetSharePassword($password); + $this->mockGetAppValue($server2ServerSharingEnabled); + + $template = new TemplateResponse($this->appName, 'public', $params, 'public'); + + $response = $this->controller->publicIndex($token, null); + + $this->assertEquals($params, $response->getParams()); + $this->assertEquals('public', $response->getTemplateName()); + $this->assertTrue($response instanceof TemplateResponse); + $this->assertEquals($template->render(), $response->render()); + } + + public function testPublicIndexWithFileToken() { + $token = 'aaaabbbbccccdddd'; + $filename = 'happy.jpg'; + $fileId = 12345; + + $this->mockGetSharedNode('file', $fileId); + $redirectUrl = 'http://owncloud/download/' . $filename; + $this->mockUrlToDownloadPage($token, $fileId, $filename, $redirectUrl); + $template = new RedirectResponse($redirectUrl); + + $response = $this->controller->publicIndex($token, $filename); + + $this->assertTrue($response instanceof RedirectResponse); + $this->assertEquals($template->getRedirectURL(), $response->getRedirectURL()); + } + + public function testErrorPage() { + $message = 'Not found!'; + $code = Http::STATUS_NOT_FOUND; + $params = [ + 'appName' => $this->appName, + 'message' => $message, + 'code' => $code, + ]; + $template = new TemplateResponse($this->appName, 'index', $params, 'guest'); + + $response = $this->controller->errorPage($message, $code); + + $this->assertEquals($params, $response->getParams()); + $this->assertEquals('index', $response->getTemplateName()); + $this->assertTrue($response instanceof TemplateResponse); + $this->assertEquals($template->render(), $response->render()); + } + + private function mockGetSharedNode($nodeType, $nodeId) { + $folder = $this->getMockBuilder('OCP\Files\Folder') + ->disableOriginalConstructor() + ->getMock(); + $folder->method('getType') + ->willReturn($nodeType); + $folder->method('getId') + ->willReturn($nodeId); + + $this->environment->expects($this->once()) + ->method('getSharedNode') + ->willReturn($folder); + } + + private function mockGetSharedFolderName($name) { + $this->environment->expects($this->once()) + ->method('getSharedFolderName') + ->willReturn($name); + } + + private function mockGetDisplayName($name) { + $this->environment->expects($this->once()) + ->method('getDisplayName') + ->willReturn($name); + } + + private function mockGetSharePassword($password) { + $this->environment->expects($this->once()) + ->method('getSharePassword') + ->willReturn($password); + } + + private function mockGetAppValue($status) { + $this->appConfig->expects($this->once()) + ->method('getAppValue') + ->with( + 'files_sharing', + 'outgoing_server2server_share_enabled', + 'yes' + ) + ->willReturn($status); + } + + private function mockUrlToDownloadPage($token, $fileId, $filename, $url) { + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with( + $this->appName . '.files_public.download', + [ + 'token' => $token, + 'fileId' => $fileId, + 'filename' => $filename + ] + ) + ->willReturn($url); + } + +}
\ No newline at end of file diff --git a/tests/unit/controller/PreviewControllerTest.php b/tests/unit/controller/PreviewControllerTest.php index 2d68a8e5..97ccdcf8 100644 --- a/tests/unit/controller/PreviewControllerTest.php +++ b/tests/unit/controller/PreviewControllerTest.php @@ -41,7 +41,7 @@ class PreviewControllerTest extends \Test\TestCase { protected $appName = 'gallery'; /** @var IRequest */ protected $request; - /** @var ConfigController */ + /** @var PreviewController */ protected $controller; /** @var IURLGenerator */ protected $urlGenerator; |