1
0

MigrationTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing\Tests;
  27. use OCA\Files_Sharing\Migration;
  28. /**
  29. * Class MigrationTest
  30. *
  31. * @group DB
  32. */
  33. class MigrationTest extends TestCase {
  34. /** @var \OCP\IDBConnection */
  35. private $connection;
  36. /** @var \OCP\IConfig */
  37. private $config;
  38. /** @var Migration */
  39. private $migration;
  40. private $table = 'share';
  41. public function setUp() {
  42. parent::setUp();
  43. $this->connection = \OC::$server->getDatabaseConnection();
  44. $this->config = \OC::$server->getConfig();
  45. $this->migration = new Migration($this->connection, $this->config);
  46. $this->cleanDB();
  47. }
  48. public function tearDown() {
  49. parent::tearDown();
  50. $this->cleanDB();
  51. }
  52. private function cleanDB() {
  53. $query = $this->connection->getQueryBuilder();
  54. $query->delete($this->table)->execute();
  55. }
  56. public function addDummyValues() {
  57. $query = $this->connection->getQueryBuilder();
  58. $query->insert($this->table)
  59. ->values(
  60. array(
  61. 'share_type' => $query->createParameter('share_type'),
  62. 'share_with' => $query->createParameter('share_with'),
  63. 'uid_owner' => $query->createParameter('uid_owner'),
  64. 'uid_initiator' => $query->createParameter('uid_initiator'),
  65. 'parent' => $query->createParameter('parent'),
  66. 'item_type' => $query->createParameter('item_type'),
  67. 'item_source' => $query->createParameter('item_source'),
  68. 'item_target' => $query->createParameter('item_target'),
  69. 'file_source' => $query->createParameter('file_source'),
  70. 'file_target' => $query->createParameter('file_target'),
  71. 'permissions' => $query->createParameter('permissions'),
  72. 'stime' => $query->createParameter('stime'),
  73. )
  74. );
  75. // shared contact, shouldn't be modified
  76. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_CONTACT)
  77. ->setParameter('share_with', 'user1')
  78. ->setParameter('uid_owner', 'owner1')
  79. ->setParameter('uid_initiator', '')
  80. ->setParameter('parent', null)
  81. ->setParameter('item_type', 'contact')
  82. ->setParameter('item_source', '2')
  83. ->setParameter('item_target', '/2')
  84. ->setParameter('file_source', null)
  85. ->setParameter('file_target', null)
  86. ->setParameter('permissions', 31)
  87. ->setParameter('stime', time());
  88. $this->assertSame(1,
  89. $query->execute()
  90. );
  91. // shared calendar, shouldn't be modified
  92. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  93. ->setParameter('share_with', 'user1')
  94. ->setParameter('uid_owner', 'owner1')
  95. ->setParameter('uid_initiator', '')
  96. ->setParameter('parent', null)
  97. ->setParameter('item_type', 'calendar')
  98. ->setParameter('item_source', '2')
  99. ->setParameter('item_target', '/2')
  100. ->setParameter('file_source', null)
  101. ->setParameter('file_target', null)
  102. ->setParameter('permissions', 31)
  103. ->setParameter('stime', time());
  104. $this->assertSame(1,
  105. $query->execute()
  106. );
  107. // single user share, shouldn't be modified
  108. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  109. ->setParameter('share_with', 'user1')
  110. ->setParameter('uid_owner', 'owner1')
  111. ->setParameter('uid_initiator', '')
  112. ->setParameter('parent', null)
  113. ->setParameter('item_type', 'file')
  114. ->setParameter('item_source', '2')
  115. ->setParameter('item_target', '/2')
  116. ->setParameter('file_source', 2)
  117. ->setParameter('file_target', '/foo')
  118. ->setParameter('permissions', 31)
  119. ->setParameter('stime', time());
  120. $this->assertSame(1,
  121. $query->execute()
  122. );
  123. // single group share, shouldn't be modified
  124. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_GROUP)
  125. ->setParameter('share_with', 'group1')
  126. ->setParameter('uid_owner', 'owner1')
  127. ->setParameter('uid_initiator', '')
  128. ->setParameter('parent', null)
  129. ->setParameter('item_type', 'file')
  130. ->setParameter('item_source', '2')
  131. ->setParameter('item_target', '/2')
  132. ->setParameter('file_source', 2)
  133. ->setParameter('file_target', '/foo')
  134. ->setParameter('permissions', 31)
  135. ->setParameter('stime', time());
  136. $this->assertSame(1,
  137. $query->execute()
  138. );
  139. $parent = $query->getLastInsertId();
  140. // unique target for group share, shouldn't be modified
  141. $query->setParameter('share_type', 2)
  142. ->setParameter('share_with', 'group1')
  143. ->setParameter('uid_owner', 'owner1')
  144. ->setParameter('uid_initiator', '')
  145. ->setParameter('parent', $parent)
  146. ->setParameter('item_type', 'file')
  147. ->setParameter('item_source', '2')
  148. ->setParameter('item_target', '/2')
  149. ->setParameter('file_source', 2)
  150. ->setParameter('file_target', '/foo renamed')
  151. ->setParameter('permissions', 31)
  152. ->setParameter('stime', time());
  153. $this->assertSame(1,
  154. $query->execute()
  155. );
  156. // first user share, shouldn't be modified
  157. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  158. ->setParameter('share_with', 'user1')
  159. ->setParameter('uid_owner', 'owner2')
  160. ->setParameter('uid_initiator', '')
  161. ->setParameter('parent', null)
  162. ->setParameter('item_type', 'file')
  163. ->setParameter('item_source', '2')
  164. ->setParameter('item_target', '/2')
  165. ->setParameter('file_source', 2)
  166. ->setParameter('file_target', '/foobar')
  167. ->setParameter('permissions', 31)
  168. ->setParameter('stime', time());
  169. $this->assertSame(1,
  170. $query->execute()
  171. );
  172. $parent = $query->getLastInsertId();
  173. // first re-share, should be attached to the first user share after migration
  174. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  175. ->setParameter('share_with', 'user2')
  176. ->setParameter('uid_owner', 'user1')
  177. ->setParameter('uid_initiator', '')
  178. ->setParameter('parent', $parent)
  179. ->setParameter('item_type', 'file')
  180. ->setParameter('item_source', '2')
  181. ->setParameter('item_target', '/2')
  182. ->setParameter('file_source', 2)
  183. ->setParameter('file_target', '/foobar')
  184. ->setParameter('permissions', 31)
  185. ->setParameter('stime', time());
  186. $this->assertSame(1,
  187. $query->execute()
  188. );
  189. $parent = $query->getLastInsertId();
  190. // second re-share, should be attached to the first user share after migration
  191. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  192. ->setParameter('share_with', 'user3')
  193. ->setParameter('uid_owner', 'user2')
  194. ->setParameter('uid_initiator', '')
  195. ->setParameter('parent', $parent)
  196. ->setParameter('item_type', 'file')
  197. ->setParameter('item_source', '2')
  198. ->setParameter('item_target', '/2')
  199. ->setParameter('file_source', 2)
  200. ->setParameter('file_target', '/foobar')
  201. ->setParameter('permissions', 31)
  202. ->setParameter('stime', time());
  203. $this->assertSame(1,
  204. $query->execute()
  205. );
  206. $parent = $query->getLastInsertId();
  207. // third re-share, should be attached to the first user share after migration
  208. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_REMOTE)
  209. ->setParameter('share_with', 'user@server.com')
  210. ->setParameter('uid_owner', 'user3')
  211. ->setParameter('uid_initiator', '')
  212. ->setParameter('parent', $parent)
  213. ->setParameter('item_type', 'file')
  214. ->setParameter('item_source', '2')
  215. ->setParameter('item_target', '/2')
  216. ->setParameter('file_source', 2)
  217. ->setParameter('file_target', '/foobar')
  218. ->setParameter('permissions', 31)
  219. ->setParameter('stime', time());
  220. $this->assertSame(1,
  221. $query->execute()
  222. );
  223. // Link reshare should keep its parent
  224. $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_LINK)
  225. ->setParameter('share_with', null)
  226. ->setParameter('uid_owner', 'user3')
  227. ->setParameter('uid_initiator', '')
  228. ->setParameter('parent', $parent)
  229. ->setParameter('item_type', 'file')
  230. ->setParameter('item_source', '2')
  231. ->setParameter('item_target', '/2')
  232. ->setParameter('file_source', 2)
  233. ->setParameter('file_target', '/foobar')
  234. ->setParameter('permissions', 31)
  235. ->setParameter('stime', time());
  236. $this->assertSame(1,
  237. $query->execute()
  238. );
  239. }
  240. public function testRemoveReShares() {
  241. $this->addDummyValues();
  242. $this->migration->removeReShares();
  243. $this->verifyResult();
  244. }
  245. public function verifyResult() {
  246. $query = $this->connection->getQueryBuilder();
  247. $query->select('*')->from($this->table)->orderBy('id');
  248. $result = $query->execute()->fetchAll();
  249. $this->assertSame(10, count($result));
  250. // shares which shouldn't be modified
  251. for ($i = 0; $i < 4; $i++) {
  252. $this->assertSame('owner1', $result[$i]['uid_owner']);
  253. $this->assertEmpty($result[$i]['uid_initiator']);
  254. $this->assertNull($result[$i]['parent']);
  255. }
  256. // group share with unique target
  257. $this->assertSame('owner1', $result[4]['uid_owner']);
  258. $this->assertEmpty($result[4]['uid_initiator']);
  259. $this->assertNotEmpty($result[4]['parent']);
  260. // initial user share which was re-shared
  261. $this->assertSame('owner2', $result[5]['uid_owner']);
  262. $this->assertEmpty($result[5]['uid_initiator']);
  263. $this->assertNull($result[5]['parent']);
  264. // flatted re-shares
  265. for($i = 6; $i < 9; $i++) {
  266. $this->assertSame('owner2', $result[$i]['uid_owner']);
  267. $user = 'user' . ($i - 5);
  268. $this->assertSame($user, $result[$i]['uid_initiator']);
  269. $this->assertNull($result[$i]['parent']);
  270. }
  271. /*
  272. * The link share is flattend but has an owner to avoid invisible shares
  273. * see: https://github.com/owncloud/core/pull/22317
  274. */
  275. $this->assertSame('owner2', $result[9]['uid_owner']);
  276. $this->assertSame('user3', $result[9]['uid_initiator']);
  277. $this->assertSame($result[7]['id'], $result[9]['parent']);
  278. }
  279. public function test1001DeepReshares() {
  280. $parent = null;
  281. for ($i = 0; $i < 1001; $i++) {
  282. $query = $this->connection->getQueryBuilder();
  283. $query->insert($this->table)
  284. ->values(
  285. [
  286. 'share_type' => $query->createParameter('share_type'),
  287. 'share_with' => $query->createParameter('share_with'),
  288. 'uid_owner' => $query->createParameter('uid_owner'),
  289. 'uid_initiator' => $query->createParameter('uid_initiator'),
  290. 'parent' => $query->createParameter('parent'),
  291. 'item_type' => $query->createParameter('item_type'),
  292. 'item_source' => $query->createParameter('item_source'),
  293. 'item_target' => $query->createParameter('item_target'),
  294. 'file_source' => $query->createParameter('file_source'),
  295. 'file_target' => $query->createParameter('file_target'),
  296. 'permissions' => $query->createParameter('permissions'),
  297. 'stime' => $query->createParameter('stime'),
  298. ]
  299. )
  300. ->setParameter('share_type', \OCP\Share::SHARE_TYPE_USER)
  301. ->setParameter('share_with', 'user'.($i+1))
  302. ->setParameter('uid_owner', 'user'.($i))
  303. ->setParameter('uid_initiator', null)
  304. ->setParameter('parent', $parent)
  305. ->setParameter('item_type', 'file')
  306. ->setParameter('item_source', '2')
  307. ->setParameter('item_target', '/2')
  308. ->setParameter('file_source', 2)
  309. ->setParameter('file_target', '/foobar')
  310. ->setParameter('permissions', 31)
  311. ->setParameter('stime', time());
  312. $this->assertSame(1, $query->execute());
  313. $parent = $query->getLastInsertId();
  314. }
  315. $this->migration->removeReShares();
  316. $this->migration->updateInitiatorInfo();
  317. $qb = $this->connection->getQueryBuilder();
  318. $stmt = $qb->select('id', 'share_with', 'uid_owner', 'uid_initiator', 'parent')
  319. ->from('share')
  320. ->orderBy('id', 'asc')
  321. ->execute();
  322. $i = 0;
  323. while($share = $stmt->fetch()) {
  324. $this->assertEquals('user'.($i+1), $share['share_with']);
  325. $this->assertEquals('user' . ($i), $share['uid_initiator']);
  326. $this->assertEquals('user0', $share['uid_owner']);
  327. $this->assertEquals(null, $share['parent']);
  328. $i++;
  329. }
  330. $stmt->closeCursor();
  331. $this->assertEquals(1001, $i);
  332. }
  333. /**
  334. * test that we really remove the "shareapi_allow_mail_notification" setting only
  335. */
  336. public function testRemoveSendMailOption() {
  337. $this->config->setAppValue('core', 'shareapi_setting1', 'dummy-value');
  338. $this->config->setAppValue('core', 'shareapi_allow_mail_notification', 'no');
  339. $this->config->setAppValue('core', 'shareapi_allow_public_notification', 'no');
  340. $this->migration->removeSendMailOption();
  341. $this->assertNull(
  342. $this->config->getAppValue('core', 'shareapi_allow_mail_notification', null)
  343. );
  344. $this->assertNull(
  345. $this->config->getAppValue('core', 'shareapi_allow_public_notification', null)
  346. );
  347. $this->assertSame('dummy-value',
  348. $this->config->getAppValue('core', 'shareapi_setting1', null)
  349. );
  350. }
  351. }