ShareHelperTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Share20;
  24. use OC\Share20\ShareHelper;
  25. use OCP\Files\Node;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Share\IManager;
  28. use Test\TestCase;
  29. class ShareHelperTest extends TestCase {
  30. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  31. private $manager;
  32. /** @var ShareHelper */
  33. private $helper;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->manager = $this->createMock(IManager::class);
  37. $this->helper = new ShareHelper($this->manager);
  38. }
  39. public function dataGetPathsForAccessList() {
  40. return [
  41. [[], [], false, [], [], false, [
  42. 'users' => [],
  43. 'remotes' => [],
  44. ]],
  45. [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
  46. 'users' => ['user1' => 'foo', 'user2' => 'bar'],
  47. 'remotes' => [],
  48. ]],
  49. [[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
  50. 'users' => [],
  51. 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
  52. ]],
  53. [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
  54. 'users' => ['user1' => 'foo', 'user2' => 'bar'],
  55. 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
  56. ]],
  57. ];
  58. }
  59. /**
  60. * @dataProvider dataGetPathsForAccessList
  61. */
  62. public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
  63. $this->manager->expects($this->once())
  64. ->method('getAccessList')
  65. ->willReturn([
  66. 'users' => $userList,
  67. 'remote' => $remoteList,
  68. ]);
  69. /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
  70. $node = $this->createMock(Node::class);
  71. /** @var ShareHelper|\PHPUnit_Framework_MockObject_MockObject $helper */
  72. $helper = $this->getMockBuilder(ShareHelper::class)
  73. ->setConstructorArgs([$this->manager])
  74. ->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
  75. ->getMock();
  76. $helper->expects($resolveUsers ? $this->once() : $this->never())
  77. ->method('getPathsForUsers')
  78. ->with($node, $userList)
  79. ->willReturn($userMap);
  80. $helper->expects($resolveRemotes ? $this->once() : $this->never())
  81. ->method('getPathsForRemotes')
  82. ->with($node, $remoteList)
  83. ->willReturn($remoteMap);
  84. $this->assertSame($expected, $helper->getPathsForAccessList($node));
  85. }
  86. public function dataGetPathsForUsers() {
  87. return [
  88. [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
  89. [
  90. [
  91. 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
  92. 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
  93. 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
  94. 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
  95. ],
  96. [16 => 'SixTeen', 23 => 'TwentyThree', 42 => 'FortyTwo'],
  97. [
  98. 'test1' => '/foo/TwentyThree/FortyTwo',
  99. 'test2' => '/bar/FortyTwo',
  100. 'test3' => '/cat',
  101. ],
  102. ],
  103. ];
  104. }
  105. /**
  106. * @dataProvider dataGetPathsForUsers
  107. *
  108. * @param array $users
  109. * @param array $nodes
  110. * @param array $expected
  111. */
  112. public function testGetPathsForUsers(array $users, array $nodes, array $expected) {
  113. $lastNode = null;
  114. foreach ($nodes as $nodeId => $nodeName) {
  115. /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
  116. $node = $this->createMock(Node::class);
  117. $node->expects($this->any())
  118. ->method('getId')
  119. ->willReturn($nodeId);
  120. $node->expects($this->any())
  121. ->method('getName')
  122. ->willReturn($nodeName);
  123. if ($lastNode === null) {
  124. $node->expects($this->any())
  125. ->method('getParent')
  126. ->willThrowException(new NotFoundException());
  127. } else {
  128. $node->expects($this->any())
  129. ->method('getParent')
  130. ->willReturn($lastNode);
  131. }
  132. $lastNode = $node;
  133. }
  134. $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users]));
  135. }
  136. public function dataGetPathsForRemotes() {
  137. return [
  138. [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
  139. [
  140. [
  141. 'test1' => ['node_id' => 16, 'token' => 't1'],
  142. 'test2' => ['node_id' => 23, 'token' => 't2'],
  143. 'test3' => ['node_id' => 42, 'token' => 't3'],
  144. 'test4' => ['node_id' => 48, 'token' => 't4'],
  145. ],
  146. [
  147. 16 => '/admin/files/SixTeen',
  148. 23 => '/admin/files/SixTeen/TwentyThree',
  149. 42 => '/admin/files/SixTeen/TwentyThree/FortyTwo',
  150. ],
  151. [
  152. 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
  153. 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
  154. 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
  155. ],
  156. ],
  157. ];
  158. }
  159. /**
  160. * @dataProvider dataGetPathsForRemotes
  161. *
  162. * @param array $remotes
  163. * @param array $nodes
  164. * @param array $expected
  165. */
  166. public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected) {
  167. $lastNode = null;
  168. foreach ($nodes as $nodeId => $nodePath) {
  169. /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
  170. $node = $this->createMock(Node::class);
  171. $node->expects($this->any())
  172. ->method('getId')
  173. ->willReturn($nodeId);
  174. $node->expects($this->any())
  175. ->method('getPath')
  176. ->willReturn($nodePath);
  177. if ($lastNode === null) {
  178. $node->expects($this->any())
  179. ->method('getParent')
  180. ->willThrowException(new NotFoundException());
  181. } else {
  182. $node->expects($this->any())
  183. ->method('getParent')
  184. ->willReturn($lastNode);
  185. }
  186. $lastNode = $node;
  187. }
  188. $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes]));
  189. }
  190. public function dataGetMountedPath() {
  191. return [
  192. ['/admin/files/foobar', '/foobar'],
  193. ['/admin/files/foo/bar', '/foo/bar'],
  194. ];
  195. }
  196. /**
  197. * @dataProvider dataGetMountedPath
  198. * @param string $path
  199. * @param string $expected
  200. */
  201. public function testGetMountedPath($path, $expected) {
  202. /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
  203. $node = $this->createMock(Node::class);
  204. $node->expects($this->once())
  205. ->method('getPath')
  206. ->willReturn($path);
  207. $this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
  208. }
  209. }