1
0

ShareByMailProviderTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\ShareByMail\Tests;
  22. use OC\HintException;
  23. use OCA\ShareByMail\ShareByMailProvider;
  24. use OCP\Files\IRootFolder;
  25. use OCP\IDBConnection;
  26. use OCP\IL10N;
  27. use OCP\ILogger;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserManager;
  30. use OCP\Mail\IMailer;
  31. use OCP\Security\ISecureRandom;
  32. use OCP\Share\Exceptions\ShareNotFound;
  33. use OCP\Share\IManager;
  34. use OCP\Share\IShare;
  35. use Test\TestCase;
  36. /**
  37. * Class ShareByMailProviderTest
  38. *
  39. * @package OCA\ShareByMail\Tests
  40. * @group DB
  41. */
  42. class ShareByMailProviderTest extends TestCase {
  43. /** @var IDBConnection */
  44. private $connection;
  45. /** @var IManager */
  46. private $shareManager;
  47. /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
  48. private $l;
  49. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
  50. private $logger;
  51. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  52. private $rootFolder;
  53. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
  54. private $userManager;
  55. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject */
  56. private $secureRandom;
  57. /** @var IMailer | \PHPUnit_Framework_MockObject_MockObject */
  58. private $mailer;
  59. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
  60. private $urlGenerator;
  61. /** @var IShare | \PHPUnit_Framework_MockObject_MockObject */
  62. private $share;
  63. /** @var \OCP\Activity\IManager | \PHPUnit_Framework_MockObject_MockObject */
  64. private $activityManager;
  65. public function setUp() {
  66. parent::setUp();
  67. $this->shareManager = \OC::$server->getShareManager();
  68. $this->connection = \OC::$server->getDatabaseConnection();
  69. $this->l = $this->getMockBuilder('OCP\IL10N')->getMock();
  70. $this->l->method('t')
  71. ->will($this->returnCallback(function($text, $parameters = []) {
  72. return vsprintf($text, $parameters);
  73. }));
  74. $this->logger = $this->getMockBuilder('OCP\ILogger')->getMock();
  75. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  76. $this->userManager = $this->getMockBuilder('OCP\IUserManager')->getMock();
  77. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
  78. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')->getMock();
  79. $this->urlGenerator = $this->getMockBuilder('\OCP\IUrlGenerator')->getMock();
  80. $this->share = $this->getMockBuilder('\OCP\Share\IShare')->getMock();
  81. $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager')->getMock();
  82. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  83. }
  84. /**
  85. * get instance of Mocked ShareByMailProvider
  86. *
  87. * @param array $mockedMethods internal methods which should be mocked
  88. * @return \PHPUnit_Framework_MockObject_MockObject | ShareByMailProvider
  89. */
  90. private function getInstance(array $mockedMethods = []) {
  91. $instance = $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
  92. ->setConstructorArgs(
  93. [
  94. $this->connection,
  95. $this->secureRandom,
  96. $this->userManager,
  97. $this->rootFolder,
  98. $this->l,
  99. $this->logger,
  100. $this->mailer,
  101. $this->urlGenerator,
  102. $this->activityManager
  103. ]
  104. );
  105. if (!empty($mockedMethods)) {
  106. $instance->setMethods($mockedMethods);
  107. return $instance->getMock();
  108. }
  109. return new ShareByMailProvider(
  110. $this->connection,
  111. $this->secureRandom,
  112. $this->userManager,
  113. $this->rootFolder,
  114. $this->l,
  115. $this->logger,
  116. $this->mailer,
  117. $this->urlGenerator,
  118. $this->activityManager
  119. );
  120. }
  121. public function tearDown() {
  122. $this->connection->getQueryBuilder()->delete('share')->execute();
  123. return parent::tearDown();
  124. }
  125. public function testCreate() {
  126. $share = $this->getMockBuilder('\OCP\Share\IShare')->getMock();
  127. $share->expects($this->once())->method('getSharedWith')->willReturn('user1');
  128. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createActivity']);
  129. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  130. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  131. $instance->expects($this->once())->method('createActivity')->with($share);
  132. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn('rawShare');
  133. $instance->expects($this->once())->method('createShareObject')->with('rawShare')->willReturn('shareObject');
  134. $this->assertSame('shareObject',
  135. $instance->create($share)
  136. );
  137. }
  138. /**
  139. * @expectedException \Exception
  140. */
  141. public function testCreateFailed() {
  142. $this->share->expects($this->once())->method('getSharedWith')->willReturn('user1');
  143. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  144. $node->expects($this->any())->method('getName')->willReturn('fileName');
  145. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  146. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject']);
  147. $instance->expects($this->once())->method('getSharedWith')->willReturn(['found']);
  148. $instance->expects($this->never())->method('createMailShare');
  149. $instance->expects($this->never())->method('getRawShare');
  150. $instance->expects($this->never())->method('createShareObject');
  151. $this->assertSame('shareObject',
  152. $instance->create($this->share)
  153. );
  154. }
  155. public function testCreateMailShare() {
  156. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  157. $this->share->expects($this->once())->method('setToken')->with('token');
  158. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  159. $node->expects($this->any())->method('getName')->willReturn('fileName');
  160. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  161. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  162. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  163. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  164. $instance->expects($this->once())->method('sendMailNotification');
  165. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  166. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  167. $instance->expects($this->once())->method('sendMailNotification');
  168. $this->assertSame(42,
  169. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  170. );
  171. }
  172. /**
  173. * @expectedException \OC\HintException
  174. */
  175. public function testCreateMailShareFailed() {
  176. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  177. $this->share->expects($this->once())->method('setToken')->with('token');
  178. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  179. $node->expects($this->any())->method('getName')->willReturn('fileName');
  180. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  181. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  182. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  183. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  184. $instance->expects($this->once())->method('sendMailNotification');
  185. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  186. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  187. $instance->expects($this->once())->method('sendMailNotification')
  188. ->willReturnCallback(
  189. function() {
  190. throw new \Exception('should be converted to a hint exception');
  191. }
  192. );
  193. $this->assertSame(42,
  194. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  195. );
  196. }
  197. public function testGenerateToken() {
  198. $instance = $this->getInstance();
  199. $this->secureRandom->expects($this->once())->method('generate')->willReturn('token');
  200. $this->assertSame('token',
  201. $this->invokePrivate($instance, 'generateToken')
  202. );
  203. }
  204. public function testAddShareToDB() {
  205. $itemSource = 11;
  206. $itemType = 'file';
  207. $shareWith = 'user@server.com';
  208. $sharedBy = 'user1';
  209. $uidOwner = 'user2';
  210. $permissions = 1;
  211. $token = 'token';
  212. $instance = $this->getInstance();
  213. $id = $this->invokePrivate(
  214. $instance,
  215. 'addShareToDB',
  216. [
  217. $itemSource,
  218. $itemType,
  219. $shareWith,
  220. $sharedBy,
  221. $uidOwner,
  222. $permissions,
  223. $token
  224. ]
  225. );
  226. $qb = $this->connection->getQueryBuilder();
  227. $result = $qb->select('*')
  228. ->from('share')
  229. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  230. ->execute()->fetchAll();
  231. $this->assertSame(1, count($result));
  232. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  233. $this->assertSame($itemType, $result[0]['item_type']);
  234. $this->assertSame($shareWith, $result[0]['share_with']);
  235. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  236. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  237. $this->assertSame($permissions, (int)$result[0]['permissions']);
  238. $this->assertSame($token, $result[0]['token']);
  239. }
  240. public function testUpdate() {
  241. $itemSource = 11;
  242. $itemType = 'file';
  243. $shareWith = 'user@server.com';
  244. $sharedBy = 'user1';
  245. $uidOwner = 'user2';
  246. $permissions = 1;
  247. $token = 'token';
  248. $instance = $this->getInstance();
  249. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  250. $this->share->expects($this->once())->method('getPermissions')->willReturn($permissions + 1);
  251. $this->share->expects($this->once())->method('getShareOwner')->willReturn($uidOwner);
  252. $this->share->expects($this->once())->method('getSharedBy')->willReturn($sharedBy);
  253. $this->share->expects($this->once())->method('getId')->willReturn($id);
  254. $this->assertSame($this->share,
  255. $instance->update($this->share)
  256. );
  257. $qb = $this->connection->getQueryBuilder();
  258. $result = $qb->select('*')
  259. ->from('share')
  260. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  261. ->execute()->fetchAll();
  262. $this->assertSame(1, count($result));
  263. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  264. $this->assertSame($itemType, $result[0]['item_type']);
  265. $this->assertSame($shareWith, $result[0]['share_with']);
  266. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  267. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  268. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  269. $this->assertSame($token, $result[0]['token']);
  270. }
  271. public function testDelete() {
  272. $instance = $this->getInstance(['removeShareFromTable']);
  273. $this->share->expects($this->once())->method('getId')->willReturn(42);
  274. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  275. $instance->delete($this->share);
  276. }
  277. public function testGetShareById() {
  278. $instance = $this->getInstance(['createShareObject']);
  279. $itemSource = 11;
  280. $itemType = 'file';
  281. $shareWith = 'user@server.com';
  282. $sharedBy = 'user1';
  283. $uidOwner = 'user2';
  284. $permissions = 1;
  285. $token = 'token';
  286. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  287. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  288. $instance->expects($this->once())->method('createShareObject')
  289. ->willReturnCallback(
  290. function ($data) use ($uidOwner, $sharedBy, $id2) {
  291. $this->assertSame($uidOwner, $data['uid_owner']);
  292. $this->assertSame($sharedBy, $data['uid_initiator']);
  293. $this->assertSame($id2, (int)$data['id']);
  294. return $this->share;
  295. }
  296. );
  297. $result = $instance->getShareById($id2);
  298. $this->assertInstanceOf('OCP\Share\IShare', $result);
  299. }
  300. /**
  301. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  302. */
  303. public function testGetShareByIdFailed() {
  304. $instance = $this->getInstance(['createShareObject']);
  305. $itemSource = 11;
  306. $itemType = 'file';
  307. $shareWith = 'user@server.com';
  308. $sharedBy = 'user1';
  309. $uidOwner = 'user2';
  310. $permissions = 1;
  311. $token = 'token';
  312. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  313. $instance->getShareById($id+1);
  314. }
  315. public function testGetShareByPath() {
  316. $itemSource = 11;
  317. $itemType = 'file';
  318. $shareWith = 'user@server.com';
  319. $sharedBy = 'user1';
  320. $uidOwner = 'user2';
  321. $permissions = 1;
  322. $token = 'token';
  323. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  324. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  325. $instance = $this->getInstance(['createShareObject']);
  326. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  327. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  328. $instance->expects($this->once())->method('createShareObject')
  329. ->willReturnCallback(
  330. function ($data) use ($uidOwner, $sharedBy, $id) {
  331. $this->assertSame($uidOwner, $data['uid_owner']);
  332. $this->assertSame($sharedBy, $data['uid_initiator']);
  333. $this->assertSame($id, (int)$data['id']);
  334. return $this->share;
  335. }
  336. );
  337. $result = $instance->getSharesByPath($node);
  338. $this->assertTrue(is_array($result));
  339. $this->assertSame(1, count($result));
  340. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  341. }
  342. public function testGetShareByToken() {
  343. $itemSource = 11;
  344. $itemType = 'file';
  345. $shareWith = 'user@server.com';
  346. $sharedBy = 'user1';
  347. $uidOwner = 'user2';
  348. $permissions = 1;
  349. $token = 'token';
  350. $instance = $this->getInstance(['createShareObject']);
  351. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  352. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, \OCP\Share::SHARE_TYPE_LINK);
  353. $this->assertTrue($idMail !== $idPublic);
  354. $instance->expects($this->once())->method('createShareObject')
  355. ->willReturnCallback(
  356. function ($data) use ($idMail) {
  357. $this->assertSame($idMail, (int)$data['id']);
  358. return $this->share;
  359. }
  360. );
  361. $this->assertInstanceOf('OCP\Share\IShare',
  362. $instance->getShareByToken('token')
  363. );
  364. }
  365. /**
  366. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  367. */
  368. public function testGetShareByTokenFailed() {
  369. $itemSource = 11;
  370. $itemType = 'file';
  371. $shareWith = 'user@server.com';
  372. $sharedBy = 'user1';
  373. $uidOwner = 'user2';
  374. $permissions = 1;
  375. $token = 'token';
  376. $instance = $this->getInstance(['createShareObject']);
  377. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  378. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", \OCP\Share::SHARE_TYPE_LINK);
  379. $this->assertTrue($idMail !== $idPublic);
  380. $this->assertInstanceOf('OCP\Share\IShare',
  381. $instance->getShareByToken('token2')
  382. );
  383. }
  384. public function testRemoveShareFromTable() {
  385. $itemSource = 11;
  386. $itemType = 'file';
  387. $shareWith = 'user@server.com';
  388. $sharedBy = 'user1';
  389. $uidOwner = 'user2';
  390. $permissions = 1;
  391. $token = 'token';
  392. $instance = $this->getInstance();
  393. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  394. $query = $this->connection->getQueryBuilder();
  395. $query->select('*')->from('share')
  396. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  397. $before = $query->execute()->fetchAll();
  398. $this->assertTrue(is_array($before));
  399. $this->assertSame(1, count($before));
  400. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  401. $query = $this->connection->getQueryBuilder();
  402. $query->select('*')->from('share')
  403. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  404. $after = $query->execute()->fetchAll();
  405. $this->assertTrue(is_array($after));
  406. $this->assertEmpty($after);
  407. }
  408. public function testUserDeleted() {
  409. $itemSource = 11;
  410. $itemType = 'file';
  411. $shareWith = 'user@server.com';
  412. $sharedBy = 'user1';
  413. $uidOwner = 'user2';
  414. $permissions = 1;
  415. $token = 'token';
  416. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  417. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  418. $query = $this->connection->getQueryBuilder();
  419. $query->select('*')->from('share');
  420. $before = $query->execute()->fetchAll();
  421. $this->assertTrue(is_array($before));
  422. $this->assertSame(2, count($before));
  423. $instance = $this->getInstance();
  424. $instance->userDeleted($uidOwner, \OCP\Share::SHARE_TYPE_EMAIL);
  425. $query = $this->connection->getQueryBuilder();
  426. $query->select('*')->from('share');
  427. $after = $query->execute()->fetchAll();
  428. $this->assertTrue(is_array($after));
  429. $this->assertSame(1, count($after));
  430. $this->assertSame($id, (int)$after[0]['id']);
  431. }
  432. public function testGetRawShare() {
  433. $itemSource = 11;
  434. $itemType = 'file';
  435. $shareWith = 'user@server.com';
  436. $sharedBy = 'user1';
  437. $uidOwner = 'user2';
  438. $permissions = 1;
  439. $token = 'token';
  440. $instance = $this->getInstance();
  441. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  442. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  443. $this->assertTrue(is_array($result));
  444. $this->assertSame($itemSource, (int)$result['item_source']);
  445. $this->assertSame($itemType, $result['item_type']);
  446. $this->assertSame($shareWith, $result['share_with']);
  447. $this->assertSame($sharedBy, $result['uid_initiator']);
  448. $this->assertSame($uidOwner, $result['uid_owner']);
  449. $this->assertSame($permissions, (int)$result['permissions']);
  450. $this->assertSame($token, $result['token']);
  451. }
  452. /**
  453. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  454. */
  455. public function testGetRawShareFailed() {
  456. $itemSource = 11;
  457. $itemType = 'file';
  458. $shareWith = 'user@server.com';
  459. $sharedBy = 'user1';
  460. $uidOwner = 'user2';
  461. $permissions = 1;
  462. $token = 'token';
  463. $instance = $this->getInstance();
  464. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  465. $this->invokePrivate($instance, 'getRawShare', [$id+1]);
  466. }
  467. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType = \OCP\Share::SHARE_TYPE_EMAIL) {
  468. $qb = $this->connection->getQueryBuilder();
  469. $qb->insert('share')
  470. ->setValue('share_type', $qb->createNamedParameter($shareType))
  471. ->setValue('item_type', $qb->createNamedParameter($itemType))
  472. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  473. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  474. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  475. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  476. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  477. ->setValue('permissions', $qb->createNamedParameter($permissions))
  478. ->setValue('token', $qb->createNamedParameter($token))
  479. ->setValue('stime', $qb->createNamedParameter(time()));
  480. /*
  481. * Added to fix https://github.com/owncloud/core/issues/22215
  482. * Can be removed once we get rid of ajax/share.php
  483. */
  484. $qb->setValue('file_target', $qb->createNamedParameter(''));
  485. $qb->execute();
  486. $id = $qb->getLastInsertId();
  487. return (int)$id;
  488. }
  489. public function testGetSharesInFolder() {
  490. $userManager = \OC::$server->getUserManager();
  491. $rootFolder = \OC::$server->getRootFolder();
  492. $provider = $this->getInstance(['sendMailNotification', 'createActivity']);
  493. $u1 = $userManager->createUser('testFed', md5(time()));
  494. $u2 = $userManager->createUser('testFed2', md5(time()));
  495. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  496. $file1 = $folder1->newFile('bar1');
  497. $file2 = $folder1->newFile('bar2');
  498. $share1 = $this->shareManager->newShare();
  499. $share1->setSharedWith('user@server.com')
  500. ->setSharedBy($u1->getUID())
  501. ->setShareOwner($u1->getUID())
  502. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  503. ->setNode($file1);
  504. $provider->create($share1);
  505. $share2 = $this->shareManager->newShare();
  506. $share2->setSharedWith('user@server.com')
  507. ->setSharedBy($u2->getUID())
  508. ->setShareOwner($u1->getUID())
  509. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  510. ->setNode($file2);
  511. $provider->create($share2);
  512. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  513. $this->assertCount(1, $result);
  514. $this->assertCount(1, $result[$file1->getId()]);
  515. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  516. $this->assertCount(2, $result);
  517. $this->assertCount(1, $result[$file1->getId()]);
  518. $this->assertCount(1, $result[$file2->getId()]);
  519. $u1->delete();
  520. $u2->delete();
  521. }
  522. }