123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace Test\Share20;
- use OC\Share20\ShareHelper;
- use OCP\Files\Node;
- use OCP\Files\NotFoundException;
- use OCP\Share\IManager;
- use Test\TestCase;
- class ShareHelperTest extends TestCase {
-
- private $manager;
-
- private $helper;
- protected function setUp(): void {
- parent::setUp();
- $this->manager = $this->createMock(IManager::class);
- $this->helper = new ShareHelper($this->manager);
- }
- public function dataGetPathsForAccessList() {
- return [
- [[], [], false, [], [], false, [
- 'users' => [],
- 'remotes' => [],
- ]],
- [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
- 'users' => ['user1' => 'foo', 'user2' => 'bar'],
- 'remotes' => [],
- ]],
- [[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
- 'users' => [],
- 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
- ]],
- [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
- 'users' => ['user1' => 'foo', 'user2' => 'bar'],
- 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
- ]],
- ];
- }
-
- public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected): void {
- $this->manager->expects($this->once())
- ->method('getAccessList')
- ->willReturn([
- 'users' => $userList,
- 'remote' => $remoteList,
- ]);
-
- $node = $this->createMock(Node::class);
-
- $helper = $this->getMockBuilder(ShareHelper::class)
- ->setConstructorArgs([$this->manager])
- ->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
- ->getMock();
- $helper->expects($resolveUsers ? $this->once() : $this->never())
- ->method('getPathsForUsers')
- ->with($node, $userList)
- ->willReturn($userMap);
- $helper->expects($resolveRemotes ? $this->once() : $this->never())
- ->method('getPathsForRemotes')
- ->with($node, $remoteList)
- ->willReturn($remoteMap);
- $this->assertSame($expected, $helper->getPathsForAccessList($node));
- }
- public function dataGetPathsForUsers() {
- return [
- [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
- [
- [
- 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
- 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
- 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
- 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
- ],
- [16 => 'SixTeen', 23 => 'TwentyThree', 42 => 'FortyTwo'],
- [
- 'test1' => '/foo/TwentyThree/FortyTwo',
- 'test2' => '/bar/FortyTwo',
- 'test3' => '/cat',
- ],
- ],
- ];
- }
-
- public function testGetPathsForUsers(array $users, array $nodes, array $expected): void {
- $lastNode = null;
- foreach ($nodes as $nodeId => $nodeName) {
-
- $node = $this->createMock(Node::class);
- $node->expects($this->any())
- ->method('getId')
- ->willReturn($nodeId);
- $node->expects($this->any())
- ->method('getName')
- ->willReturn($nodeName);
- if ($lastNode === null) {
- $node->expects($this->any())
- ->method('getParent')
- ->willThrowException(new NotFoundException());
- } else {
- $node->expects($this->any())
- ->method('getParent')
- ->willReturn($lastNode);
- }
- $lastNode = $node;
- }
- $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users]));
- }
- public function dataGetPathsForRemotes() {
- return [
- [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
- [
- [
- 'test1' => ['node_id' => 16, 'token' => 't1'],
- 'test2' => ['node_id' => 23, 'token' => 't2'],
- 'test3' => ['node_id' => 42, 'token' => 't3'],
- 'test4' => ['node_id' => 48, 'token' => 't4'],
- ],
- [
- 16 => '/admin/files/SixTeen',
- 23 => '/admin/files/SixTeen/TwentyThree',
- 42 => '/admin/files/SixTeen/TwentyThree/FortyTwo',
- ],
- [
- 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
- 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
- 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
- ],
- ],
- ];
- }
-
- public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected): void {
- $lastNode = null;
- foreach ($nodes as $nodeId => $nodePath) {
-
- $node = $this->createMock(Node::class);
- $node->expects($this->any())
- ->method('getId')
- ->willReturn($nodeId);
- $node->expects($this->any())
- ->method('getPath')
- ->willReturn($nodePath);
- if ($lastNode === null) {
- $node->expects($this->any())
- ->method('getParent')
- ->willThrowException(new NotFoundException());
- } else {
- $node->expects($this->any())
- ->method('getParent')
- ->willReturn($lastNode);
- }
- $lastNode = $node;
- }
- $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes]));
- }
- public function dataGetMountedPath() {
- return [
- ['/admin/files/foobar', '/foobar'],
- ['/admin/files/foo/bar', '/foo/bar'],
- ];
- }
-
- public function testGetMountedPath($path, $expected): void {
-
- $node = $this->createMock(Node::class);
- $node->expects($this->once())
- ->method('getPath')
- ->willReturn($path);
- $this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
- }
- }
|