ShareRecipientSorterTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests\Collaboration;
  26. use OCA\Files_Sharing\Collaboration\ShareRecipientSorter;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\Node;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use OCP\Share\IManager;
  33. use Test\TestCase;
  34. class ShareRecipientSorterTest extends TestCase {
  35. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $shareManager;
  37. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $rootFolder;
  39. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $userSession;
  41. /** @var ShareRecipientSorter */
  42. protected $sorter;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->shareManager = $this->createMock(IManager::class);
  46. $this->rootFolder = $this->createMock(IRootFolder::class);
  47. $this->userSession = $this->createMock(IUserSession::class);
  48. $this->sorter = new ShareRecipientSorter($this->shareManager, $this->rootFolder, $this->userSession);
  49. }
  50. /**
  51. * @dataProvider sortDataProvider
  52. * @param $data
  53. */
  54. public function testSort($data) {
  55. $node = $this->createMock(Node::class);
  56. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */
  57. $folder = $this->createMock(Folder::class);
  58. $this->rootFolder->expects($this->any())
  59. ->method('getUserFolder')
  60. ->willReturn($folder);
  61. $user = $this->createMock(IUser::class);
  62. $user->expects($this->any())
  63. ->method('getUID')
  64. ->willReturn('yvonne');
  65. $this->userSession->expects($this->once())
  66. ->method('getUser')
  67. ->willReturn($user);
  68. if ($data['context']['itemType'] === 'files') {
  69. $folder->expects($this->once())
  70. ->method('getFirstNodeById')
  71. ->with($data['context']['itemId'])
  72. ->willReturn($node);
  73. $this->shareManager->expects($this->once())
  74. ->method('getAccessList')
  75. ->with($node)
  76. ->willReturn($data['accessList']);
  77. } else {
  78. $folder->expects($this->never())
  79. ->method('getFirstNodeById');
  80. $this->shareManager->expects($this->never())
  81. ->method('getAccessList');
  82. }
  83. $workArray = $data['input'];
  84. $this->sorter->sort($workArray, $data['context']);
  85. $this->assertEquals($data['expected'], $workArray);
  86. }
  87. public function testSortNoNodes() {
  88. /** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */
  89. $folder = $this->createMock(Folder::class);
  90. $this->rootFolder->expects($this->any())
  91. ->method('getUserFolder')
  92. ->willReturn($folder);
  93. $folder->expects($this->once())
  94. ->method('getFirstNodeById')
  95. ->willReturn(null);
  96. $user = $this->createMock(IUser::class);
  97. $user->expects($this->any())
  98. ->method('getUID')
  99. ->willReturn('yvonne');
  100. $this->userSession->expects($this->once())
  101. ->method('getUser')
  102. ->willReturn($user);
  103. $this->shareManager->expects($this->never())
  104. ->method('getAccessList');
  105. $originalArray = [
  106. 'users' => [
  107. ['value' => ['shareWith' => 'alice']],
  108. ['value' => ['shareWith' => 'bob']],
  109. ]
  110. ];
  111. $workArray = $originalArray;
  112. $this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => 404]);
  113. $this->assertEquals($originalArray, $workArray);
  114. }
  115. public function sortDataProvider() {
  116. return [[
  117. [
  118. #0 – sort properly and otherwise keep existing order
  119. 'context' => ['itemType' => 'files', 'itemId' => 42],
  120. 'accessList' => ['users' => ['celia', 'darius', 'faruk', 'gail'], 'bots' => ['r2-d2']],
  121. 'input' => [
  122. 'users' =>
  123. [
  124. ['value' => ['shareWith' => 'alice']],
  125. ['value' => ['shareWith' => 'bob']],
  126. ['value' => ['shareWith' => 'celia']],
  127. ['value' => ['shareWith' => 'darius']],
  128. ['value' => ['shareWith' => 'elena']],
  129. ['value' => ['shareWith' => 'faruk']],
  130. ['value' => ['shareWith' => 'gail']],
  131. ],
  132. 'bots' => [
  133. ['value' => ['shareWith' => 'c-3po']],
  134. ['value' => ['shareWith' => 'r2-d2']],
  135. ]
  136. ],
  137. 'expected' => [
  138. 'users' =>
  139. [
  140. ['value' => ['shareWith' => 'celia']],
  141. ['value' => ['shareWith' => 'darius']],
  142. ['value' => ['shareWith' => 'faruk']],
  143. ['value' => ['shareWith' => 'gail']],
  144. ['value' => ['shareWith' => 'alice']],
  145. ['value' => ['shareWith' => 'bob']],
  146. ['value' => ['shareWith' => 'elena']],
  147. ],
  148. 'bots' => [
  149. ['value' => ['shareWith' => 'r2-d2']],
  150. ['value' => ['shareWith' => 'c-3po']],
  151. ]
  152. ],
  153. ],
  154. [
  155. #1 – no recipients
  156. 'context' => ['itemType' => 'files', 'itemId' => 42],
  157. 'accessList' => ['users' => false],
  158. 'input' => [
  159. 'users' =>
  160. [
  161. ['value' => ['shareWith' => 'alice']],
  162. ['value' => ['shareWith' => 'bob']],
  163. ['value' => ['shareWith' => 'celia']],
  164. ['value' => ['shareWith' => 'darius']],
  165. ['value' => ['shareWith' => 'elena']],
  166. ['value' => ['shareWith' => 'faruk']],
  167. ['value' => ['shareWith' => 'gail']],
  168. ],
  169. 'bots' => [
  170. ['value' => ['shareWith' => 'c-3po']],
  171. ['value' => ['shareWith' => 'r2-d2']],
  172. ]
  173. ],
  174. 'expected' => [
  175. 'users' =>
  176. [
  177. ['value' => ['shareWith' => 'alice']],
  178. ['value' => ['shareWith' => 'bob']],
  179. ['value' => ['shareWith' => 'celia']],
  180. ['value' => ['shareWith' => 'darius']],
  181. ['value' => ['shareWith' => 'elena']],
  182. ['value' => ['shareWith' => 'faruk']],
  183. ['value' => ['shareWith' => 'gail']],
  184. ],
  185. 'bots' => [
  186. ['value' => ['shareWith' => 'c-3po']],
  187. ['value' => ['shareWith' => 'r2-d2']],
  188. ]
  189. ],
  190. ],
  191. [
  192. #2 – unsupported item type
  193. 'context' => ['itemType' => 'announcements', 'itemId' => 42],
  194. 'accessList' => null, // not needed
  195. 'input' => [
  196. 'users' =>
  197. [
  198. ['value' => ['shareWith' => 'alice']],
  199. ['value' => ['shareWith' => 'bob']],
  200. ['value' => ['shareWith' => 'celia']],
  201. ['value' => ['shareWith' => 'darius']],
  202. ['value' => ['shareWith' => 'elena']],
  203. ['value' => ['shareWith' => 'faruk']],
  204. ['value' => ['shareWith' => 'gail']],
  205. ],
  206. 'bots' => [
  207. ['value' => ['shareWith' => 'c-3po']],
  208. ['value' => ['shareWith' => 'r2-d2']],
  209. ]
  210. ],
  211. 'expected' => [
  212. 'users' =>
  213. [
  214. ['value' => ['shareWith' => 'alice']],
  215. ['value' => ['shareWith' => 'bob']],
  216. ['value' => ['shareWith' => 'celia']],
  217. ['value' => ['shareWith' => 'darius']],
  218. ['value' => ['shareWith' => 'elena']],
  219. ['value' => ['shareWith' => 'faruk']],
  220. ['value' => ['shareWith' => 'gail']],
  221. ],
  222. 'bots' => [
  223. ['value' => ['shareWith' => 'c-3po']],
  224. ['value' => ['shareWith' => 'r2-d2']],
  225. ]
  226. ],
  227. ],
  228. [
  229. #3 – no nothing
  230. 'context' => ['itemType' => 'files', 'itemId' => 42],
  231. 'accessList' => [],
  232. 'input' => [],
  233. 'expected' => [],
  234. ],
  235. ]];
  236. }
  237. }