diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/avatarmanagertest.php | 51 | ||||
-rw-r--r-- | tests/lib/share20/managertest.php | 97 | ||||
-rw-r--r-- | tests/lib/systemtag/systemtagobjectmappertest.php | 106 | ||||
-rw-r--r-- | tests/settings/controller/userscontrollertest.php | 26 | ||||
-rw-r--r-- | tests/settings/middleware/subadminmiddlewaretest.php | 17 |
5 files changed, 218 insertions, 79 deletions
diff --git a/tests/lib/avatarmanagertest.php b/tests/lib/avatarmanagertest.php index cb9068c46a6..f5cdd99176d 100644 --- a/tests/lib/avatarmanagertest.php +++ b/tests/lib/avatarmanagertest.php @@ -19,30 +19,32 @@ * */ use OC\AvatarManager; -use OCP\Files\IRootFolder; -use OCP\IUserManager; +use Test\Traits\UserTrait; +use Test\Traits\MountProviderTrait; +/** + * Class AvatarManagerTest + * @group DB + */ class AvatarManagerTest extends \Test\TestCase { - /** @var IRootFolder */ - private $rootFolder; + use UserTrait; + use MountProviderTrait; - /** @var AvatarManager */ + /** @var AvatarManager */ private $avatarManager; - /** @var IUserManager */ - private $userManager; + /** @var \OC\Files\Storage\Temporary */ + private $storage; 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);; + $this->createUser('valid-user', 'valid-user'); + + $this->storage = new \OC\Files\Storage\Temporary(); + $this->registerMount('valid-user', $this->storage, '/valid-user/'); + + $this->avatarManager = \OC::$server->getAvatarManager(); } /** @@ -54,23 +56,10 @@ class AvatarManagerTest extends \Test\TestCase { } public function testGetAvatarValidUser() { - $this->userManager->expects($this->once()) - ->method('get') - ->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'); + $avatar = $this->avatarManager->getAvatar('valid-user'); + $this->assertInstanceOf('\OCP\IAvatar', $avatar); + $this->assertFalse($this->storage->file_exists('files')); } } diff --git a/tests/lib/share20/managertest.php b/tests/lib/share20/managertest.php index fe94b72c4e6..c41f0754396 100644 --- a/tests/lib/share20/managertest.php +++ b/tests/lib/share20/managertest.php @@ -1429,9 +1429,11 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); + $storage = $this->getMock('\OCP\Files\Storage'); $path = $this->getMock('\OCP\Files\File'); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); + $path->method('getStorage')->willReturn($storage); $share = $this->createShare( null, @@ -1480,9 +1482,11 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); + $storage = $this->getMock('\OCP\Files\Storage'); $path = $this->getMock('\OCP\Files\File'); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); + $path->method('getStorage')->willReturn($storage); $share = $this->createShare( null, @@ -1539,10 +1543,12 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); + $storage = $this->getMock('\OCP\Files\Storage'); $path = $this->getMock('\OCP\Files\File'); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); $path->method('getId')->willReturn(1); + $path->method('getStorage')->willReturn($storage); $date = new \DateTime(); @@ -1663,9 +1669,11 @@ class ManagerTest extends \Test\TestCase { $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); + $storage = $this->getMock('\OCP\Files\Storage'); $path = $this->getMock('\OCP\Files\File'); $path->method('getOwner')->willReturn($shareOwner); $path->method('getName')->willReturn('target'); + $path->method('getStorage')->willReturn($storage); $share = $this->createShare( null, @@ -1697,8 +1705,86 @@ class ManagerTest extends \Test\TestCase { ->method('setTarget') ->with('/target'); - $dummy = new DummyCreate(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $dummy, 'listner'); + $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock(); + \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre'); + $hookListner->expects($this->once()) + ->method('pre') + ->will($this->returnCallback(function (array $data) { + $data['run'] = false; + $data['error'] = 'I won\'t let you share!'; + })); + + $manager->createShare($share); + } + + public function testCreateShareOfIncommingFederatedShare() { + $manager = $this->createManagerMock() + ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks']) + ->getMock(); + + $shareOwner = $this->getMock('\OCP\IUser'); + $shareOwner->method('getUID')->willReturn('shareOwner'); + + $storage = $this->getMock('\OCP\Files\Storage'); + $storage->method('instanceOfStorage') + ->with('OCA\Files_Sharing\External\Storage') + ->willReturn(true); + + $storage2 = $this->getMock('\OCP\Files\Storage'); + $storage2->method('instanceOfStorage') + ->with('OCA\Files_Sharing\External\Storage') + ->willReturn(false); + + $path = $this->getMock('\OCP\Files\File'); + $path->expects($this->never())->method('getOwner'); + $path->method('getName')->willReturn('target'); + $path->method('getStorage')->willReturn($storage); + + $parent = $this->getMock('\OCP\Files\Folder'); + $parent->method('getStorage')->willReturn($storage); + + $parentParent = $this->getMock('\OCP\Files\Folder'); + $parentParent->method('getStorage')->willReturn($storage2); + $parentParent->method('getOwner')->willReturn($shareOwner); + + $path->method('getParent')->willReturn($parent); + $parent->method('getParent')->willReturn($parentParent); + + $share = $this->createShare( + null, + \OCP\Share::SHARE_TYPE_USER, + $path, + 'sharedWith', + 'sharedBy', + null, + \OCP\Constants::PERMISSION_ALL); + + $manager->expects($this->once()) + ->method('canShare') + ->with($share) + ->willReturn(true); + $manager->expects($this->once()) + ->method('generalCreateChecks') + ->with($share);; + $manager->expects($this->once()) + ->method('userCreateChecks') + ->with($share);; + $manager->expects($this->once()) + ->method('pathCreateChecks') + ->with($path); + + $this->defaultProvider + ->expects($this->once()) + ->method('create') + ->with($share) + ->will($this->returnArgument(0)); + + $share->expects($this->once()) + ->method('setShareOwner') + ->with('shareOwner'); + $share->expects($this->once()) + ->method('setTarget') + ->with('/target'); $manager->createShare($share); } @@ -2269,13 +2355,6 @@ class DummyPassword { } } -class DummyCreate { - public function listner($array) { - $array['run'] = false; - $array['error'] = 'I won\'t let you share!'; - } -} - class DummyFactory implements IProviderFactory { /** @var IShareProvider */ diff --git a/tests/lib/systemtag/systemtagobjectmappertest.php b/tests/lib/systemtag/systemtagobjectmappertest.php index 5c8204f6a87..861eb240674 100644 --- a/tests/lib/systemtag/systemtagobjectmappertest.php +++ b/tests/lib/systemtag/systemtagobjectmappertest.php @@ -102,10 +102,10 @@ class SystemTagObjectMapperTest extends TestCase { return $result; })); - $this->tagMapper->assignTags(1, 'testtype', $this->tag1->getId()); - $this->tagMapper->assignTags(1, 'testtype', $this->tag2->getId()); - $this->tagMapper->assignTags(2, 'testtype', $this->tag1->getId()); - $this->tagMapper->assignTags(3, 'anothertype', $this->tag1->getId()); + $this->tagMapper->assignTags('1', 'testtype', $this->tag1->getId()); + $this->tagMapper->assignTags('1', 'testtype', $this->tag2->getId()); + $this->tagMapper->assignTags('2', 'testtype', $this->tag1->getId()); + $this->tagMapper->assignTags('3', 'anothertype', $this->tag1->getId()); } public function tearDown() { @@ -121,15 +121,15 @@ class SystemTagObjectMapperTest extends TestCase { public function testGetTagsForObjects() { $tagIdMapping = $this->tagMapper->getTagIdsForObjects( - [1, 2, 3, 4], + ['1', '2', '3', '4'], 'testtype' ); $this->assertEquals([ - 1 => [$this->tag1->getId(), $this->tag2->getId()], - 2 => [$this->tag1->getId()], - 3 => [], - 4 => [], + '1' => [$this->tag1->getId(), $this->tag2->getId()], + '2' => [$this->tag1->getId()], + '3' => [], + '4' => [], ], $tagIdMapping); } @@ -140,7 +140,43 @@ class SystemTagObjectMapperTest extends TestCase { ); $this->assertEquals([ + '1', + '2', + ], $objectIds); + } + + public function testGetObjectsForTagsLimit() { + $objectIds = $this->tagMapper->getObjectIdsForTags( + [$this->tag1->getId()], + 'testtype', + 1 + ); + + $this->assertEquals([ 1, + ], $objectIds); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testGetObjectsForTagsLimitWithMultipleTags() { + $this->tagMapper->getObjectIdsForTags( + [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], + 'testtype', + 1 + ); + } + + public function testGetObjectsForTagsLimitOffset() { + $objectIds = $this->tagMapper->getObjectIdsForTags( + [$this->tag1->getId()], + 'testtype', + 1, + '1' + ); + + $this->assertEquals([ 2, ], $objectIds); } @@ -156,29 +192,29 @@ class SystemTagObjectMapperTest extends TestCase { } public function testAssignUnassignTags() { - $this->tagMapper->unassignTags(1, 'testtype', [$this->tag1->getId()]); + $this->tagMapper->unassignTags('1', 'testtype', [$this->tag1->getId()]); - $tagIdMapping = $this->tagMapper->getTagIdsForObjects(1, 'testtype'); + $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype'); $this->assertEquals([ 1 => [$this->tag2->getId()], ], $tagIdMapping); - $this->tagMapper->assignTags(1, 'testtype', [$this->tag1->getId()]); - $this->tagMapper->assignTags(1, 'testtype', $this->tag3->getId()); + $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]); + $this->tagMapper->assignTags('1', 'testtype', $this->tag3->getId()); - $tagIdMapping = $this->tagMapper->getTagIdsForObjects(1, 'testtype'); + $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype'); $this->assertEquals([ - 1 => [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], + '1' => [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()], ], $tagIdMapping); } public function testReAssignUnassignTags() { // reassign tag1 - $this->tagMapper->assignTags(1, 'testtype', [$this->tag1->getId()]); + $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]); // tag 3 was never assigned - $this->tagMapper->unassignTags(1, 'testtype', [$this->tag3->getId()]); + $this->tagMapper->unassignTags('1', 'testtype', [$this->tag3->getId()]); $this->assertTrue(true, 'No error when reassigning/unassigning'); } @@ -187,13 +223,13 @@ class SystemTagObjectMapperTest extends TestCase { * @expectedException \OCP\SystemTag\TagNotFoundException */ public function testAssignNonExistingTags() { - $this->tagMapper->assignTags(1, 'testtype', [100]); + $this->tagMapper->assignTags('1', 'testtype', [100]); } public function testAssignNonExistingTagInArray() { $caught = false; try { - $this->tagMapper->assignTags(1, 'testtype', [100, $this->tag3->getId()]); + $this->tagMapper->assignTags('1', 'testtype', [100, $this->tag3->getId()]); } catch (TagNotFoundException $e) { $caught = true; } @@ -201,12 +237,12 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertTrue($caught, 'Exception thrown'); $tagIdMapping = $this->tagMapper->getTagIdsForObjects( - [1], + ['1'], 'testtype' ); $this->assertEquals([ - 1 => [$this->tag1->getId(), $this->tag2->getId()], + '1' => [$this->tag1->getId(), $this->tag2->getId()], ], $tagIdMapping, 'None of the tags got assigned'); } @@ -214,13 +250,13 @@ class SystemTagObjectMapperTest extends TestCase { * @expectedException \OCP\SystemTag\TagNotFoundException */ public function testUnassignNonExistingTags() { - $this->tagMapper->unassignTags(1, 'testtype', [100]); + $this->tagMapper->unassignTags('1', 'testtype', [100]); } public function testUnassignNonExistingTagsInArray() { $caught = false; try { - $this->tagMapper->unassignTags(1, 'testtype', [100, $this->tag1->getId()]); + $this->tagMapper->unassignTags('1', 'testtype', [100, $this->tag1->getId()]); } catch (TagNotFoundException $e) { $caught = true; } @@ -233,14 +269,14 @@ class SystemTagObjectMapperTest extends TestCase { ); $this->assertEquals([ - 1 => [$this->tag1->getId(), $this->tag2->getId()], + '1' => [$this->tag1->getId(), $this->tag2->getId()], ], $tagIdMapping, 'None of the tags got unassigned'); } public function testHaveTagAllMatches() { $this->assertTrue( $this->tagMapper->haveTag( - [1], + ['1'], 'testtype', $this->tag1->getId(), true @@ -250,7 +286,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertTrue( $this->tagMapper->haveTag( - [1, 2], + ['1', '2'], 'testtype', $this->tag1->getId(), true @@ -260,7 +296,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertFalse( $this->tagMapper->haveTag( - [1, 2], + ['1', '2'], 'testtype', $this->tag2->getId(), true @@ -270,7 +306,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertFalse( $this->tagMapper->haveTag( - [2], + ['2'], 'testtype', $this->tag2->getId(), true @@ -280,7 +316,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertFalse( $this->tagMapper->haveTag( - [3], + ['3'], 'testtype', $this->tag2->getId(), true @@ -292,7 +328,7 @@ class SystemTagObjectMapperTest extends TestCase { public function testHaveTagAtLeastOneMatch() { $this->assertTrue( $this->tagMapper->haveTag( - [1], + ['1'], 'testtype', $this->tag1->getId(), false @@ -302,7 +338,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertTrue( $this->tagMapper->haveTag( - [1, 2], + ['1', '2'], 'testtype', $this->tag1->getId(), false @@ -312,7 +348,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertTrue( $this->tagMapper->haveTag( - [1, 2], + ['1', '2'], 'testtype', $this->tag2->getId(), false @@ -322,7 +358,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertFalse( $this->tagMapper->haveTag( - [2], + ['2'], 'testtype', $this->tag2->getId(), false @@ -332,7 +368,7 @@ class SystemTagObjectMapperTest extends TestCase { $this->assertFalse( $this->tagMapper->haveTag( - [3], + ['3'], 'testtype', $this->tag2->getId(), false @@ -346,7 +382,7 @@ class SystemTagObjectMapperTest extends TestCase { */ public function testHaveTagNonExisting() { $this->tagMapper->haveTag( - [1], + ['1'], 'testtype', 100 ); diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php index 947540fa67b..6f07f34ba8d 100644 --- a/tests/settings/controller/userscontrollertest.php +++ b/tests/settings/controller/userscontrollertest.php @@ -1696,6 +1696,32 @@ class UsersControllerTest extends \Test\TestCase { $this->assertEquals($expectedResult, $result); } + public function testNoAvatar() { + $this->container['IsAdmin'] = true; + + list($user, $expectedResult) = $this->mockUser(); + + $subadmin = $this->getMockBuilder('\OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subadmin->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($user) + ->will($this->returnValue([])); + $this->container['GroupManager'] + ->expects($this->any()) + ->method('getSubAdmin') + ->will($this->returnValue($subadmin)); + + $this->container['OCP\\IAvatarManager'] + ->method('getAvatar') + ->will($this->throwException(new \OCP\Files\NotFoundException())); + $expectedResult['isAvatarAvailable'] = false; + + $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $this->assertEquals($expectedResult, $result); + } + /** * @return array */ diff --git a/tests/settings/middleware/subadminmiddlewaretest.php b/tests/settings/middleware/subadminmiddlewaretest.php index d0da19f60e1..2b76e4beaa9 100644 --- a/tests/settings/middleware/subadminmiddlewaretest.php +++ b/tests/settings/middleware/subadminmiddlewaretest.php @@ -10,6 +10,7 @@ namespace OC\Settings\Middleware; +use OC\Appframework\Middleware\Security\Exceptions\NotAdminException; use OC\AppFramework\Utility\ControllerMethodReflector; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\TemplateResponse; @@ -41,8 +42,7 @@ class SubadminMiddlewareTest extends \Test\TestCase { } /** - * @expectedException \Exception - * @expectedExceptionMessage Logged in user must be a subadmin + * @expectedException \OC\Appframework\Middleware\Security\Exceptions\NotAdminException */ public function testBeforeControllerAsUserWithExemption() { $this->reflector @@ -81,9 +81,18 @@ class SubadminMiddlewareTest extends \Test\TestCase { $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); } - public function testAfterException() { + public function testAfterNotAdminException() { + $expectedResponse = new TemplateResponse('core', '403', array(), 'guest'); + $expectedResponse->setStatus(403); + $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException())); + } + + /** + * @expectedException \Exception + */ + public function testAfterRegularException() { $expectedResponse = new TemplateResponse('core', '403', array(), 'guest'); $expectedResponse->setStatus(403); - $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception())); + $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception()); } } |