diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-01 22:08:42 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-01 22:15:43 +0100 |
commit | b00db2c9334874c6062a143de2a6b76b44dca35d (patch) | |
tree | db13d5d23114468e978786cc52031dc31c312584 /tests | |
parent | 8931ba4a0d574a05cf4415f2556bdd3ee0388ba9 (diff) | |
download | nextcloud-server-b00db2c9334874c6062a143de2a6b76b44dca35d.tar.gz nextcloud-server-b00db2c9334874c6062a143de2a6b76b44dca35d.zip |
DI in avatar code
* DI in avatar code
* Use the node API
* More unit tests
* Unit tests no longer require DB
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/avatar.php | 94 | ||||
-rw-r--r-- | tests/lib/avatarmanagertest.php | 76 | ||||
-rw-r--r-- | tests/lib/avatartest.php | 122 |
3 files changed, 198 insertions, 94 deletions
diff --git a/tests/lib/avatar.php b/tests/lib/avatar.php deleted file mode 100644 index 3fa7dc64547..00000000000 --- a/tests/lib/avatar.php +++ /dev/null @@ -1,94 +0,0 @@ -<?php - -/** - * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -use OC\Avatar; - -/** - * Class Test_Avatar - * - * @group DB - */ -class Test_Avatar extends \Test\TestCase { - private static $trashBinStatus; - - /** @var @var string */ - private $user; - - protected function setUp() { - parent::setUp(); - - $this->user = $this->getUniqueID(); - $storage = new \OC\Files\Storage\Temporary(array()); - \OC\Files\Filesystem::mount($storage, array(), '/' . $this->user . '/'); - } - - public static function setUpBeforeClass() { - self::$trashBinStatus = \OC_App::isEnabled('files_trashbin'); - \OC_App::disable('files_trashbin'); - } - - public static function tearDownAfterClass() { - if (self::$trashBinStatus) { - \OC_App::enable('files_trashbin'); - } - } - - /** - * @return array - */ - public function traversalProvider() { - return [ - ['Pot\..\entiallyDangerousUsername'], - ['Pot/..\entiallyDangerousUsername'], - ['PotentiallyDangerousUsername/..'], - ['PotentiallyDangerousUsername\../'], - ['/../PotentiallyDangerousUsername'], - ]; - } - - /** - * @dataProvider traversalProvider - * @expectedException \Exception - * @expectedExceptionMessage Username may not contain slashes - * @param string $dangerousUsername - */ - public function testAvatarTraversal($dangerousUsername) { - new Avatar($dangerousUsername); - } - - public function testAvatar() { - - $avatar = new Avatar($this->user); - - $this->assertEquals(false, $avatar->get()); - - $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $expected->resize(64); - $avatar->set($expected->data()); - $this->assertEquals($expected->data(), $avatar->get()->data()); - - $avatar->remove(); - $this->assertEquals(false, $avatar->get()); - } - - public function testAvatarApi() { - $avatarManager = \OC::$server->getAvatarManager(); - $avatar = $avatarManager->getAvatar($this->user); - - $this->assertEquals(false, $avatar->get()); - - $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); - $expected->resize(64); - $avatar->set($expected->data()); - $this->assertEquals($expected->data(), $avatar->get()->data()); - - $avatar->remove(); - $this->assertEquals(false, $avatar->get()); - } -} diff --git a/tests/lib/avatarmanagertest.php b/tests/lib/avatarmanagertest.php new file mode 100644 index 00000000000..40d07bb49ae --- /dev/null +++ b/tests/lib/avatarmanagertest.php @@ -0,0 +1,76 @@ +<?php +/** + * @author Roeland Jago Douma <rullzer@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +use OC\AvatarManager; +use OCP\Files\IRootFolder; +use OCP\IUserManager; + +class AvatarManagerTest extends \Test\TestCase { + /** @var IRootFolder */ + private $rootFolder; + + /** @var AvatarManager */ + private $avatarManager; + + /** @var IUserManager */ + private $userManager; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->getMock('\OCP\Files\IRootFolder'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $l = $this->getMock('\OCP\IL10N'); + $l->method('t')->will($this->returnArgument(0)); + $this->avatarManager = new \OC\AvatarManager( + $this->userManager, + $this->rootFolder, + $l);; + } + + /** + * @expectedException Exception + * @expectedExceptionMessage user does not exist + */ + public function testGetAvatarInvalidUser() { + $this->avatarManager->getAvatar('invalidUser'); + } + + public function testGetAvatarValidUser() { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with('validUser') + ->willReturn(true); + + $folder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('getUserFolder') + ->with('validUser') + ->willReturn($folder); + + $folder->expects($this->once()) + ->method('getParent') + ->will($this->returnSelf()); + + $this->avatarManager->getAvatar('validUser'); + + } + +} diff --git a/tests/lib/avatartest.php b/tests/lib/avatartest.php new file mode 100644 index 00000000000..49e8be98c83 --- /dev/null +++ b/tests/lib/avatartest.php @@ -0,0 +1,122 @@ +<?php + +/** + * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +use OC\Avatar; +use OCP\Files\Folder; + +class AvatarTest extends \Test\TestCase { + /** @var Folder */ + private $folder; + + /** @var \OC\Avatar */ + private $avatar; + + public function setUp() { + parent::setUp(); + + $this->folder = $this->getMock('\OCP\Files\Folder'); + $l = $this->getMock('\OCP\IL10N'); + $l->method('t')->will($this->returnArgument(0)); + $this->avatar = new \OC\Avatar($this->folder, $l); + + } + + public function testGetNoAvatar() { + $this->assertEquals(false, $this->avatar->get()); + } + + public function testGetAvatarSizeMatch() { + $this->folder->method('nodeExists') + ->will($this->returnValueMap([ + ['avatar.jpg', true], + ['avatar.128.jpg', true], + ])); + + $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + + $file = $this->getMock('\OCP\Files\File'); + $file->method('getContent')->willReturn($expected->data()); + $this->folder->method('get')->with('avatar.128.jpg')->willReturn($file); + + $this->assertEquals($expected->data(), $this->avatar->get(128)->data()); + } + + public function testGetAvatarNoSizeMatch() { + $this->folder->method('nodeExists') + ->will($this->returnValueMap([ + ['avatar.png', true], + ['avatar.32.png', false], + ])); + + $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + $expected2 = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + $expected2->resize(32); + + $file = $this->getMock('\OCP\Files\File'); + $file->method('getContent')->willReturn($expected->data()); + $this->folder->method('get')->with('avatar.png')->willReturn($file); + + $newFile = $this->getMock('\OCP\Files\File'); + $newFile->expects($this->once()) + ->method('putContent') + ->with($expected2->data()); + $this->folder->expects($this->once()) + ->method('newFile') + ->with('avatar.32.png') + ->willReturn($newFile); + + $this->assertEquals($expected2->data(), $this->avatar->get(32)->data()); + } + + public function testExistsNo() { + $this->assertFalse($this->avatar->exists()); + } + + public function testExiststJPG() { + $this->folder->method('nodeExists') + ->will($this->returnValueMap([ + ['avatar.jpg', true], + ['avatar.png', false], + ])); + $this->assertTrue($this->avatar->exists()); + } + + public function testExistsPNG() { + $this->folder->method('nodeExists') + ->will($this->returnValueMap([ + ['avatar.jpg', false], + ['avatar.png', true], + ])); + $this->assertTrue($this->avatar->exists()); + } + + public function testSetAvatar() { + $oldFile = $this->getMock('\OCP\Files\File'); + $this->folder->method('get') + ->will($this->returnValueMap([ + ['avatar.jpg', $oldFile], + ['avatar.png', $oldFile], + ])); + $oldFile->expects($this->exactly(2))->method('delete'); + + $newFile = $this->getMock('\OCP\Files\File'); + $this->folder->expects($this->once()) + ->method('newFile') + ->with('avatar.png') + ->willReturn($newFile); + + $image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); + $newFile->expects($this->once()) + ->method('putContent') + ->with($image->data()); + + $this->avatar->set($image->data()); + } + +} |