diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Comments/ManagerTest.php | 30 | ||||
-rw-r--r-- | tests/lib/Group/GroupTest.php | 34 | ||||
-rw-r--r-- | tests/lib/Group/ManagerTest.php | 61 | ||||
-rw-r--r-- | tests/lib/Template/JSResourceLocatorTest.php | 9 | ||||
-rw-r--r-- | tests/lib/User/SessionTest.php | 10 | ||||
-rw-r--r-- | tests/lib/Util/Group/Dummy.php | 46 |
6 files changed, 108 insertions, 82 deletions
diff --git a/tests/lib/Comments/ManagerTest.php b/tests/lib/Comments/ManagerTest.php index bc1692a5958..5fa1beee374 100644 --- a/tests/lib/Comments/ManagerTest.php +++ b/tests/lib/Comments/ManagerTest.php @@ -516,15 +516,41 @@ class ManagerTest extends TestCase { ->setActor('users', 'alice') ->setObject('files', 'file64') ->setMessage('very beautiful, I am impressed!') - ->setVerb('comment'); + ->setVerb('comment') + ->setExpireDate(new \DateTime('+2 hours')); $manager->save($comment); - $comment->setMessage('very beautiful, I am really so much impressed!'); + $loadedComment = $manager->get($comment->getId()); + // Compare current object with database values + $this->assertSame($comment->getMessage(), $loadedComment->getMessage()); + $this->assertSame( + $comment->getExpireDate()->format('Y-m-d H:i:s'), + $loadedComment->getExpireDate()->format('Y-m-d H:i:s') + ); + + // Preserve the original comment to compare after update + $original = clone $comment; + + // Update values + $comment->setMessage('very beautiful, I am really so much impressed!') + ->setExpireDate(new \DateTime('+1 hours')); $manager->save($comment); $loadedComment = $manager->get($comment->getId()); + // Compare current object with database values $this->assertSame($comment->getMessage(), $loadedComment->getMessage()); + $this->assertSame( + $comment->getExpireDate()->format('Y-m-d H:i:s'), + $loadedComment->getExpireDate()->format('Y-m-d H:i:s') + ); + + // Compare original object with database values + $this->assertNotSame($original->getMessage(), $loadedComment->getMessage()); + $this->assertNotSame( + $original->getExpireDate()->format('Y-m-d H:i:s'), + $loadedComment->getExpireDate()->format('Y-m-d H:i:s') + ); } diff --git a/tests/lib/Group/GroupTest.php b/tests/lib/Group/GroupTest.php index 60f15a65732..ac648fd7b4b 100644 --- a/tests/lib/Group/GroupTest.php +++ b/tests/lib/Group/GroupTest.php @@ -303,14 +303,14 @@ class GroupTest extends \Test\TestCase { $group = new \OC\Group\Group('group1', [$backend], $this->dispatcher, $userManager); $backend->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2']); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('2'); $this->assertEquals(1, count($users)); - $user2 = $users['user2']; + $user2 = reset($users); $this->assertEquals('user2', $user2->getUID()); } @@ -325,18 +325,18 @@ class GroupTest extends \Test\TestCase { $group = new \OC\Group\Group('group1', [$backend1, $backend2], $this->dispatcher, $userManager); $backend1->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2']); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $backend2->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', '2') - ->willReturn(['user2']); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('2'); $this->assertEquals(1, count($users)); - $user2 = $users['user2']; + $user2 = reset($users); $this->assertEquals('user2', $user2->getUID()); } @@ -348,14 +348,14 @@ class GroupTest extends \Test\TestCase { $group = new \OC\Group\Group('group1', [$backend], $this->dispatcher, $userManager); $backend->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', 'user', 1, 1) - ->willReturn(['user2']); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $users = $group->searchUsers('user', 1, 1); $this->assertEquals(1, count($users)); - $user2 = $users['user2']; + $user2 = reset($users); $this->assertEquals('user2', $user2->getUID()); } @@ -370,19 +370,19 @@ class GroupTest extends \Test\TestCase { $group = new \OC\Group\Group('group1', [$backend1, $backend2], $this->dispatcher, $userManager); $backend1->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user2']); + ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]); $backend2->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('group1', 'user', 2, 1) - ->willReturn(['user1']); + ->willReturn(['user1' => new \OC\User\User('user1', null, $this->dispatcher)]); $users = $group->searchUsers('user', 2, 1); $this->assertEquals(2, count($users)); - $user2 = $users['user2']; - $user1 = $users['user1']; + $user2 = reset($users); + $user1 = next($users); $this->assertEquals('user2', $user2->getUID()); $this->assertEquals('user1', $user1->getUID()); } diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php index 5e29f974706..710d3888d55 100644 --- a/tests/lib/Group/ManagerTest.php +++ b/tests/lib/Group/ManagerTest.php @@ -24,8 +24,10 @@ namespace Test\Group; use OC\Group\Database; +use OC\User\User; use OC\User\Manager; use OCP\GroupInterface; +use OCP\Group\Backend\ISearchableGroupBackend; use OCP\ICacheFactory; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; @@ -33,6 +35,9 @@ use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; +interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterface { +} + class ManagerTest extends TestCase { /** @var Manager|MockObject */ protected $userManager; @@ -78,7 +83,7 @@ class ManagerTest extends TestCase { } // need to declare it this way due to optional methods // thanks to the implementsActions logic - $backend = $this->getMockBuilder(GroupInterface::class) + $backend = $this->getMockBuilder(ISearchableGroupInterface::class) ->disableOriginalConstructor() ->setMethods([ 'getGroupDetails', @@ -91,6 +96,7 @@ class ManagerTest extends TestCase { 'createGroup', 'addToGroup', 'removeFromGroup', + 'searchInGroup', ]) ->getMock(); $backend->expects($this->any()) @@ -724,7 +730,7 @@ class ManagerTest extends TestCase { public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() { /** - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend + * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ $backend = $this->getTestBackend(); $backend->expects($this->exactly(1)) @@ -733,22 +739,11 @@ class ManagerTest extends TestCase { ->willReturn(true); $backend->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('testgroup', '', -1, 0) - ->willReturn(['user2', 'user33']); + ->willReturn(['user2' => $this->getTestUser('user2'), 'user33' => $this->getTestUser('user33')]); - $this->userManager->expects($this->any()) - ->method('get') - ->willReturnCallback(function ($uid) { - switch ($uid) { - case 'user1': return $this->getTestUser('user1'); - case 'user2': return $this->getTestUser('user2'); - case 'user3': return $this->getTestUser('user3'); - case 'user33': return $this->getTestUser('user33'); - default: - return null; - } - }); + $this->userManager->expects($this->never())->method('get'); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache); $manager->addBackend($backend); @@ -772,22 +767,11 @@ class ManagerTest extends TestCase { ->willReturn(true); $backend->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('testgroup', '', 1, 0) - ->willReturn(['user2']); + ->willReturn([new User('user2', null, $this->dispatcher)]); - $this->userManager->expects($this->any()) - ->method('get') - ->willReturnCallback(function ($uid) { - switch ($uid) { - case 'user1': return $this->getTestUser('user1'); - case 'user2': return $this->getTestUser('user2'); - case 'user3': return $this->getTestUser('user3'); - case 'user33': return $this->getTestUser('user33'); - default: - return null; - } - }); + $this->userManager->expects($this->never())->method('get'); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache); $manager->addBackend($backend); @@ -811,22 +795,11 @@ class ManagerTest extends TestCase { ->willReturn(true); $backend->expects($this->once()) - ->method('usersInGroup') + ->method('searchInGroup') ->with('testgroup', '', 1, 1) - ->willReturn(['user33']); + ->willReturn(['user33' => $this->getTestUser('user33')]); - $this->userManager->expects($this->any()) - ->method('get') - ->willReturnCallback(function ($uid) { - switch ($uid) { - case 'user1': return $this->getTestUser('user1'); - case 'user2': return $this->getTestUser('user2'); - case 'user3': return $this->getTestUser('user3'); - case 'user33': return $this->getTestUser('user33'); - default: - return null; - } - }); + $this->userManager->expects($this->never())->method('get'); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache); $manager->addBackend($backend); diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php index 627fe676680..9ac6a32e22c 100644 --- a/tests/lib/Template/JSResourceLocatorTest.php +++ b/tests/lib/Template/JSResourceLocatorTest.php @@ -135,9 +135,8 @@ class JSResourceLocatorTest extends \Test\TestCase { $this->assertEquals($expectedWebRoot, $webRoot); $this->assertEquals($expectedFile, $file); - array_pop(\OC::$APPSROOTS); - //unlink($new_apps_path_symlink); - //$this->rrmdir($new_apps_path); + unlink($new_apps_path_symlink); + $this->rrmdir($new_apps_path); } public function testFindModuleJSWithFallback() { @@ -165,7 +164,6 @@ class JSResourceLocatorTest extends \Test\TestCase { $resources = $locator->getResources(); $this->assertCount(3, $resources); - $expectedRoot = $new_apps_path . '/test-js-app'; $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app'; $expectedFiles = ['module.mjs', 'both.mjs', 'plain.js']; @@ -173,8 +171,7 @@ class JSResourceLocatorTest extends \Test\TestCase { $this->assertEquals($expectedWebRoot, $resources[$idx][1]); $this->assertEquals($expectedFiles[$idx], $resources[$idx][2]); } - - array_pop(\OC::$APPSROOTS); + $this->rrmdir($new_apps_path); } } diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index e0afb5330d9..2af48d8330c 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -362,7 +362,7 @@ class SessionTest extends \Test\TestCase { ->willReturn('192.168.0.1'); $this->throttler ->expects($this->once()) - ->method('sleepDelay') + ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler ->expects($this->any()) @@ -427,7 +427,7 @@ class SessionTest extends \Test\TestCase { ->willReturn('192.168.0.1'); $this->throttler ->expects($this->once()) - ->method('sleepDelay') + ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler ->expects($this->any()) @@ -472,7 +472,7 @@ class SessionTest extends \Test\TestCase { ->willReturn('192.168.0.1'); $this->throttler ->expects($this->once()) - ->method('sleepDelay') + ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler ->expects($this->any()) @@ -1085,7 +1085,7 @@ class SessionTest extends \Test\TestCase { ->willReturn('192.168.0.1'); $this->throttler ->expects($this->exactly(2)) - ->method('sleepDelay') + ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler ->expects($this->any()) @@ -1135,7 +1135,7 @@ class SessionTest extends \Test\TestCase { ->willReturn('192.168.0.1'); $this->throttler ->expects($this->exactly(2)) - ->method('sleepDelay') + ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler ->expects($this->any()) diff --git a/tests/lib/Util/Group/Dummy.php b/tests/lib/Util/Group/Dummy.php index 3735a5e1167..a864c8ce9d9 100644 --- a/tests/lib/Util/Group/Dummy.php +++ b/tests/lib/Util/Group/Dummy.php @@ -29,12 +29,18 @@ namespace Test\Util\Group; -use OC\Group\Backend; +use Test\Util\User\Dummy as DummyUser; +use OCP\Group\Backend\ABackend; +use OCP\Group\Backend\IDeleteGroupBackend; +use OCP\Group\Backend\IAddToGroupBackend; +use OCP\Group\Backend\IRemoveFromGroupBackend; +use OCP\Group\Backend\ICreateGroupBackend; +use OCP\Group\Backend\ICountUsersBackend; /** - * dummy group backend, does not keep state, only for testing use + * Dummy group backend, does not keep state, only for testing use */ -class Dummy extends Backend { +class Dummy extends ABackend implements ICreateGroupBackend, IDeleteGroupBackend, IAddToGroupBackend, IRemoveFromGroupBackend, ICountUsersBackend { private $groups = []; /** * Try to create a new group @@ -44,7 +50,7 @@ class Dummy extends Backend { * Tries to create a new group. If the group name already exists, false will * be returned. */ - public function createGroup($gid) { + public function createGroup(string $gid): bool { if (!isset($this->groups[$gid])) { $this->groups[$gid] = []; return true; @@ -60,7 +66,7 @@ class Dummy extends Backend { * * Deletes a group and removes it from the group_user-table */ - public function deleteGroup($gid) { + public function deleteGroup(string $gid): bool { if (isset($this->groups[$gid])) { unset($this->groups[$gid]); return true; @@ -93,7 +99,7 @@ class Dummy extends Backend { * * Adds a user to a group. */ - public function addToGroup($uid, $gid) { + public function addToGroup(string $uid, string $gid): bool { if (isset($this->groups[$gid])) { if (array_search($uid, $this->groups[$gid]) === false) { $this->groups[$gid][] = $uid; @@ -114,7 +120,7 @@ class Dummy extends Backend { * * removes the user from a group. */ - public function removeFromGroup($uid, $gid) { + public function removeFromGroup(string $uid, string $gid): bool { if (isset($this->groups[$gid])) { if (($index = array_search($uid, $this->groups[$gid])) !== false) { unset($this->groups[$gid][$index]); @@ -192,6 +198,25 @@ class Dummy extends Backend { } } + public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array { + if (isset($this->groups[$gid])) { + if (empty($search)) { + $length = $limit < 0 ? null : $limit; + $users = array_slice($this->groups[$gid], $offset, $length); + return array_map(fn ($user) => new DummyUser($user, '')); + } + $result = []; + foreach ($this->groups[$gid] as $user) { + if (stripos($user, $search) !== false) { + $result[$user] = new DummyUser($user, ''); + } + } + return $result; + } else { + return []; + } + } + /** * get the number of all users in a group * @param string $gid @@ -200,7 +225,7 @@ class Dummy extends Backend { * @param int $offset * @return int */ - public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + public function countUsersInGroup(string $gid, string $search = ''): int { if (isset($this->groups[$gid])) { if (empty($search)) { return count($this->groups[$gid]); @@ -213,5 +238,10 @@ class Dummy extends Backend { } return $count; } + return 0; + } + + public function groupExists($gid) { + return isset($this->groups[$gid]); } } |