/tests/lib/systemtag/systemtagmanagertest.php

https://gitlab.com/wuhang2003/core · PHP · 424 lines · 296 code · 52 blank · 76 comment · 0 complexity · a785596c479f199a5e260aa1719d6407 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. *
  8. */
  9. namespace Test\SystemTag;
  10. use OC\SystemTag\SystemTagManager;
  11. use OC\SystemTag\SystemTagObjectMapper;
  12. use OCP\IDBConnection;
  13. use OCP\SystemTag\ISystemTag;
  14. use OCP\SystemTag\ISystemTagManager;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Test\TestCase;
  17. /**
  18. * Class TestSystemTagManager
  19. *
  20. * @group DB
  21. * @package Test\SystemTag
  22. */
  23. class SystemTagManagerTest extends TestCase {
  24. /**
  25. * @var ISystemTagManager
  26. **/
  27. private $tagManager;
  28. /**
  29. * @var IDBConnection
  30. */
  31. private $connection;
  32. /**
  33. * @var EventDispatcherInterface
  34. */
  35. private $dispatcher;
  36. public function setUp() {
  37. parent::setUp();
  38. $this->connection = \OC::$server->getDatabaseConnection();
  39. $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
  40. ->getMock();
  41. $this->tagManager = new SystemTagManager(
  42. $this->connection,
  43. $this->dispatcher
  44. );
  45. $this->pruneTagsTables();
  46. }
  47. public function tearDown() {
  48. $this->pruneTagsTables();
  49. parent::tearDown();
  50. }
  51. protected function pruneTagsTables() {
  52. $query = $this->connection->getQueryBuilder();
  53. $query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute();
  54. $query->delete(SystemTagManager::TAG_TABLE)->execute();
  55. }
  56. public function getAllTagsDataProvider() {
  57. return [
  58. [
  59. // no tags at all
  60. []
  61. ],
  62. [
  63. // simple
  64. [
  65. ['one', false, false],
  66. ['two', false, false],
  67. ]
  68. ],
  69. [
  70. // duplicate names, different flags
  71. [
  72. ['one', false, false],
  73. ['one', true, false],
  74. ['one', false, true],
  75. ['one', true, true],
  76. ['two', false, false],
  77. ['two', false, true],
  78. ]
  79. ]
  80. ];
  81. }
  82. /**
  83. * @dataProvider getAllTagsDataProvider
  84. */
  85. public function testGetAllTags($testTags) {
  86. $testTagsById = [];
  87. foreach ($testTags as $testTag) {
  88. $tag = $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]);
  89. $testTagsById[$tag->getId()] = $tag;
  90. }
  91. $tagList = $this->tagManager->getAllTags();
  92. $this->assertCount(count($testTags), $tagList);
  93. foreach ($testTagsById as $testTagId => $testTag) {
  94. $this->assertTrue(isset($tagList[$testTagId]));
  95. $this->assertSameTag($tagList[$testTagId], $testTag);
  96. }
  97. }
  98. public function getAllTagsFilteredDataProvider() {
  99. return [
  100. [
  101. [
  102. // no tags at all
  103. ],
  104. null,
  105. null,
  106. []
  107. ],
  108. // filter by visible only
  109. [
  110. // none visible
  111. [
  112. ['one', false, false],
  113. ['two', false, false],
  114. ],
  115. true,
  116. null,
  117. []
  118. ],
  119. [
  120. // one visible
  121. [
  122. ['one', true, false],
  123. ['two', false, false],
  124. ],
  125. true,
  126. null,
  127. [
  128. ['one', true, false],
  129. ]
  130. ],
  131. [
  132. // one invisible
  133. [
  134. ['one', true, false],
  135. ['two', false, false],
  136. ],
  137. false,
  138. null,
  139. [
  140. ['two', false, false],
  141. ]
  142. ],
  143. // filter by name pattern
  144. [
  145. [
  146. ['one', true, false],
  147. ['one', false, false],
  148. ['two', true, false],
  149. ],
  150. null,
  151. 'on',
  152. [
  153. ['one', true, false],
  154. ['one', false, false],
  155. ]
  156. ],
  157. // filter by name pattern and visibility
  158. [
  159. // one visible
  160. [
  161. ['one', true, false],
  162. ['two', true, false],
  163. ['one', false, false],
  164. ],
  165. true,
  166. 'on',
  167. [
  168. ['one', true, false],
  169. ]
  170. ],
  171. // filter by name pattern in the middle
  172. [
  173. // one visible
  174. [
  175. ['abcdefghi', true, false],
  176. ['two', true, false],
  177. ],
  178. null,
  179. 'def',
  180. [
  181. ['abcdefghi', true, false],
  182. ]
  183. ]
  184. ];
  185. }
  186. /**
  187. * @dataProvider getAllTagsFilteredDataProvider
  188. */
  189. public function testGetAllTagsFiltered($testTags, $visibilityFilter, $nameSearch, $expectedResults) {
  190. foreach ($testTags as $testTag) {
  191. $this->tagManager->createTag($testTag[0], $testTag[1], $testTag[2]);
  192. }
  193. $testTagsById = [];
  194. foreach ($expectedResults as $expectedTag) {
  195. $tag = $this->tagManager->getTag($expectedTag[0], $expectedTag[1], $expectedTag[2]);
  196. $testTagsById[$tag->getId()] = $tag;
  197. }
  198. $tagList = $this->tagManager->getAllTags($visibilityFilter, $nameSearch);
  199. $this->assertCount(count($testTagsById), $tagList);
  200. foreach ($testTagsById as $testTagId => $testTag) {
  201. $this->assertTrue(isset($tagList[$testTagId]));
  202. $this->assertSameTag($tagList[$testTagId], $testTag);
  203. }
  204. }
  205. public function oneTagMultipleFlagsProvider() {
  206. return [
  207. ['one', false, false],
  208. ['one', true, false],
  209. ['one', false, true],
  210. ['one', true, true],
  211. ];
  212. }
  213. /**
  214. * @dataProvider oneTagMultipleFlagsProvider
  215. * @expectedException \OCP\SystemTag\TagAlreadyExistsException
  216. */
  217. public function testCreateDuplicate($name, $userVisible, $userAssignable) {
  218. try {
  219. $this->tagManager->createTag($name, $userVisible, $userAssignable);
  220. } catch (\Exception $e) {
  221. $this->assertTrue(false, 'No exception thrown for the first create call');
  222. }
  223. $this->tagManager->createTag($name, $userVisible, $userAssignable);
  224. }
  225. /**
  226. * @dataProvider oneTagMultipleFlagsProvider
  227. */
  228. public function testGetExistingTag($name, $userVisible, $userAssignable) {
  229. $tag1 = $this->tagManager->createTag($name, $userVisible, $userAssignable);
  230. $tag2 = $this->tagManager->getTag($name, $userVisible, $userAssignable);
  231. $this->assertSameTag($tag1, $tag2);
  232. }
  233. public function testGetExistingTagById() {
  234. $tag1 = $this->tagManager->createTag('one', true, false);
  235. $tag2 = $this->tagManager->createTag('two', false, true);
  236. $tagList = $this->tagManager->getTagsByIds([$tag1->getId(), $tag2->getId()]);
  237. $this->assertCount(2, $tagList);
  238. $this->assertSameTag($tag1, $tagList[$tag1->getId()]);
  239. $this->assertSameTag($tag2, $tagList[$tag2->getId()]);
  240. }
  241. /**
  242. * @expectedException \OCP\SystemTag\TagNotFoundException
  243. */
  244. public function testGetNonExistingTag() {
  245. $this->tagManager->getTag('nonexist', false, false);
  246. }
  247. /**
  248. * @expectedException \OCP\SystemTag\TagNotFoundException
  249. */
  250. public function testGetNonExistingTagsById() {
  251. $tag1 = $this->tagManager->createTag('one', true, false);
  252. $this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]);
  253. }
  254. /**
  255. * @expectedException \InvalidArgumentException
  256. */
  257. public function testGetInvalidTagIdFormat() {
  258. $tag1 = $this->tagManager->createTag('one', true, false);
  259. $this->tagManager->getTagsByIds([$tag1->getId() . 'suffix']);
  260. }
  261. public function updateTagProvider() {
  262. return [
  263. [
  264. // update name
  265. ['one', true, true],
  266. ['two', true, true]
  267. ],
  268. [
  269. // update one flag
  270. ['one', false, true],
  271. ['one', true, true]
  272. ],
  273. [
  274. // update all flags
  275. ['one', false, false],
  276. ['one', true, true]
  277. ],
  278. [
  279. // update all
  280. ['one', false, false],
  281. ['two', true, true]
  282. ],
  283. ];
  284. }
  285. /**
  286. * @dataProvider updateTagProvider
  287. */
  288. public function testUpdateTag($tagCreate, $tagUpdated) {
  289. $tag1 = $this->tagManager->createTag(
  290. $tagCreate[0],
  291. $tagCreate[1],
  292. $tagCreate[2]
  293. );
  294. $this->tagManager->updateTag(
  295. $tag1->getId(),
  296. $tagUpdated[0],
  297. $tagUpdated[1],
  298. $tagUpdated[2]
  299. );
  300. $tag2 = $this->tagManager->getTag(
  301. $tagUpdated[0],
  302. $tagUpdated[1],
  303. $tagUpdated[2]
  304. );
  305. $this->assertEquals($tag2->getId(), $tag1->getId());
  306. $this->assertEquals($tag2->getName(), $tagUpdated[0]);
  307. $this->assertEquals($tag2->isUserVisible(), $tagUpdated[1]);
  308. $this->assertEquals($tag2->isUserAssignable(), $tagUpdated[2]);
  309. }
  310. /**
  311. * @dataProvider updateTagProvider
  312. * @expectedException \OCP\SystemTag\TagAlreadyExistsException
  313. */
  314. public function testUpdateTagDuplicate($tagCreate, $tagUpdated) {
  315. $this->tagManager->createTag(
  316. $tagCreate[0],
  317. $tagCreate[1],
  318. $tagCreate[2]
  319. );
  320. $tag2 = $this->tagManager->createTag(
  321. $tagUpdated[0],
  322. $tagUpdated[1],
  323. $tagUpdated[2]
  324. );
  325. // update to match the first tag
  326. $this->tagManager->updateTag(
  327. $tag2->getId(),
  328. $tagCreate[0],
  329. $tagCreate[1],
  330. $tagCreate[2]
  331. );
  332. }
  333. public function testDeleteTags() {
  334. $tag1 = $this->tagManager->createTag('one', true, false);
  335. $tag2 = $this->tagManager->createTag('two', false, true);
  336. $this->tagManager->deleteTags([$tag1->getId(), $tag2->getId()]);
  337. $this->assertEmpty($this->tagManager->getAllTags());
  338. }
  339. /**
  340. * @expectedException \OCP\SystemTag\TagNotFoundException
  341. */
  342. public function testDeleteNonExistingTag() {
  343. $this->tagManager->deleteTags([100]);
  344. }
  345. public function testDeleteTagRemovesRelations() {
  346. $tag1 = $this->tagManager->createTag('one', true, false);
  347. $tag2 = $this->tagManager->createTag('two', true, true);
  348. $tagMapper = new SystemTagObjectMapper($this->connection, $this->tagManager, $this->dispatcher);
  349. $tagMapper->assignTags(1, 'testtype', $tag1->getId());
  350. $tagMapper->assignTags(1, 'testtype', $tag2->getId());
  351. $tagMapper->assignTags(2, 'testtype', $tag1->getId());
  352. $this->tagManager->deleteTags($tag1->getId());
  353. $tagIdMapping = $tagMapper->getTagIdsForObjects(
  354. [1, 2],
  355. 'testtype'
  356. );
  357. $this->assertEquals([
  358. 1 => [$tag2->getId()],
  359. 2 => [],
  360. ], $tagIdMapping);
  361. }
  362. /**
  363. * @param ISystemTag $tag1
  364. * @param ISystemTag $tag2
  365. */
  366. private function assertSameTag($tag1, $tag2) {
  367. $this->assertEquals($tag1->getId(), $tag2->getId());
  368. $this->assertEquals($tag1->getName(), $tag2->getName());
  369. $this->assertEquals($tag1->isUserVisible(), $tag2->isUserVisible());
  370. $this->assertEquals($tag1->isUserAssignable(), $tag2->isUserAssignable());
  371. }
  372. }