ShareHelperTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Share20;
  7. use OC\Share20\ShareHelper;
  8. use OCP\Files\Node;
  9. use OCP\Files\NotFoundException;
  10. use OCP\Share\IManager;
  11. use Test\TestCase;
  12. class ShareHelperTest extends TestCase {
  13. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  14. private $manager;
  15. /** @var ShareHelper */
  16. private $helper;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->manager = $this->createMock(IManager::class);
  20. $this->helper = new ShareHelper($this->manager);
  21. }
  22. public function dataGetPathsForAccessList() {
  23. return [
  24. [[], [], false, [], [], false, [
  25. 'users' => [],
  26. 'remotes' => [],
  27. ]],
  28. [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
  29. 'users' => ['user1' => 'foo', 'user2' => 'bar'],
  30. 'remotes' => [],
  31. ]],
  32. [[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
  33. 'users' => [],
  34. 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
  35. ]],
  36. [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
  37. 'users' => ['user1' => 'foo', 'user2' => 'bar'],
  38. 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
  39. ]],
  40. ];
  41. }
  42. /**
  43. * @dataProvider dataGetPathsForAccessList
  44. */
  45. public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
  46. $this->manager->expects($this->once())
  47. ->method('getAccessList')
  48. ->willReturn([
  49. 'users' => $userList,
  50. 'remote' => $remoteList,
  51. ]);
  52. /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */
  53. $node = $this->createMock(Node::class);
  54. /** @var ShareHelper|\PHPUnit\Framework\MockObject\MockObject $helper */
  55. $helper = $this->getMockBuilder(ShareHelper::class)
  56. ->setConstructorArgs([$this->manager])
  57. ->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
  58. ->getMock();
  59. $helper->expects($resolveUsers ? $this->once() : $this->never())
  60. ->method('getPathsForUsers')
  61. ->with($node, $userList)
  62. ->willReturn($userMap);
  63. $helper->expects($resolveRemotes ? $this->once() : $this->never())
  64. ->method('getPathsForRemotes')
  65. ->with($node, $remoteList)
  66. ->willReturn($remoteMap);
  67. $this->assertSame($expected, $helper->getPathsForAccessList($node));
  68. }
  69. public function dataGetPathsForUsers() {
  70. return [
  71. [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
  72. [
  73. [
  74. 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
  75. 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
  76. 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
  77. 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
  78. ],
  79. [16 => 'SixTeen', 23 => 'TwentyThree', 42 => 'FortyTwo'],
  80. [
  81. 'test1' => '/foo/TwentyThree/FortyTwo',
  82. 'test2' => '/bar/FortyTwo',
  83. 'test3' => '/cat',
  84. ],
  85. ],
  86. ];
  87. }
  88. /**
  89. * @dataProvider dataGetPathsForUsers
  90. *
  91. * @param array $users
  92. * @param array $nodes
  93. * @param array $expected
  94. */
  95. public function testGetPathsForUsers(array $users, array $nodes, array $expected) {
  96. $lastNode = null;
  97. foreach ($nodes as $nodeId => $nodeName) {
  98. /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */
  99. $node = $this->createMock(Node::class);
  100. $node->expects($this->any())
  101. ->method('getId')
  102. ->willReturn($nodeId);
  103. $node->expects($this->any())
  104. ->method('getName')
  105. ->willReturn($nodeName);
  106. if ($lastNode === null) {
  107. $node->expects($this->any())
  108. ->method('getParent')
  109. ->willThrowException(new NotFoundException());
  110. } else {
  111. $node->expects($this->any())
  112. ->method('getParent')
  113. ->willReturn($lastNode);
  114. }
  115. $lastNode = $node;
  116. }
  117. $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users]));
  118. }
  119. public function dataGetPathsForRemotes() {
  120. return [
  121. [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
  122. [
  123. [
  124. 'test1' => ['node_id' => 16, 'token' => 't1'],
  125. 'test2' => ['node_id' => 23, 'token' => 't2'],
  126. 'test3' => ['node_id' => 42, 'token' => 't3'],
  127. 'test4' => ['node_id' => 48, 'token' => 't4'],
  128. ],
  129. [
  130. 16 => '/admin/files/SixTeen',
  131. 23 => '/admin/files/SixTeen/TwentyThree',
  132. 42 => '/admin/files/SixTeen/TwentyThree/FortyTwo',
  133. ],
  134. [
  135. 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
  136. 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
  137. 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
  138. ],
  139. ],
  140. ];
  141. }
  142. /**
  143. * @dataProvider dataGetPathsForRemotes
  144. *
  145. * @param array $remotes
  146. * @param array $nodes
  147. * @param array $expected
  148. */
  149. public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected) {
  150. $lastNode = null;
  151. foreach ($nodes as $nodeId => $nodePath) {
  152. /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */
  153. $node = $this->createMock(Node::class);
  154. $node->expects($this->any())
  155. ->method('getId')
  156. ->willReturn($nodeId);
  157. $node->expects($this->any())
  158. ->method('getPath')
  159. ->willReturn($nodePath);
  160. if ($lastNode === null) {
  161. $node->expects($this->any())
  162. ->method('getParent')
  163. ->willThrowException(new NotFoundException());
  164. } else {
  165. $node->expects($this->any())
  166. ->method('getParent')
  167. ->willReturn($lastNode);
  168. }
  169. $lastNode = $node;
  170. }
  171. $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes]));
  172. }
  173. public function dataGetMountedPath() {
  174. return [
  175. ['/admin/files/foobar', '/foobar'],
  176. ['/admin/files/foo/bar', '/foo/bar'],
  177. ];
  178. }
  179. /**
  180. * @dataProvider dataGetMountedPath
  181. * @param string $path
  182. * @param string $expected
  183. */
  184. public function testGetMountedPath($path, $expected) {
  185. /** @var Node|\PHPUnit\Framework\MockObject\MockObject $node */
  186. $node = $this->createMock(Node::class);
  187. $node->expects($this->once())
  188. ->method('getPath')
  189. ->willReturn($path);
  190. $this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
  191. }
  192. }