TagServiceTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files\Tests\Service;
  29. use OCA\Files\Service\TagService;
  30. use OCP\Activity\IManager;
  31. use OCP\ITags;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  35. /**
  36. * Class TagServiceTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\Files
  41. */
  42. class TagServiceTest extends \Test\TestCase {
  43. /**
  44. * @var string
  45. */
  46. private $user;
  47. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  48. private $userSession;
  49. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  50. private $activityManager;
  51. /**
  52. * @var \OCP\Files\Folder
  53. */
  54. private $root;
  55. /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
  56. private $dispatcher;
  57. /**
  58. * @var \OCA\Files\Service\TagService|\PHPUnit\Framework\MockObject\MockObject
  59. */
  60. private $tagService;
  61. /**
  62. * @var \OCP\ITags
  63. */
  64. private $tagger;
  65. protected function setUp(): void {
  66. parent::setUp();
  67. $this->user = static::getUniqueID('user');
  68. $this->activityManager = $this->createMock(IManager::class);
  69. \OC::$server->getUserManager()->createUser($this->user, 'test');
  70. \OC_User::setUserId($this->user);
  71. \OC_Util::setupFS($this->user);
  72. $user = $this->createMock(IUser::class);
  73. /**
  74. * @var \OCP\IUserSession
  75. */
  76. $this->userSession = $this->createMock(IUserSession::class);
  77. $this->userSession->expects($this->any())
  78. ->method('getUser')
  79. ->withAnyParameters()
  80. ->willReturn($user);
  81. $this->root = \OC::$server->getUserFolder();
  82. $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
  83. $this->tagger = \OC::$server->getTagManager()->load('files');
  84. $this->tagService = $this->getTagService(['addActivity']);
  85. }
  86. /**
  87. * @param array $methods
  88. * @return TagService|\PHPUnit\Framework\MockObject\MockObject
  89. */
  90. protected function getTagService(array $methods = []) {
  91. return $this->getMockBuilder(TagService::class)
  92. ->setConstructorArgs([
  93. $this->userSession,
  94. $this->activityManager,
  95. $this->tagger,
  96. $this->root,
  97. $this->dispatcher,
  98. ])
  99. ->setMethods($methods)
  100. ->getMock();
  101. }
  102. protected function tearDown(): void {
  103. \OC_User::setUserId('');
  104. $user = \OC::$server->getUserManager()->get($this->user);
  105. if ($user !== null) {
  106. $user->delete();
  107. }
  108. }
  109. public function testUpdateFileTags() {
  110. $tag1 = 'tag1';
  111. $tag2 = 'tag2';
  112. $this->tagService->expects($this->never())
  113. ->method('addActivity');
  114. $subdir = $this->root->newFolder('subdir');
  115. $testFile = $subdir->newFile('test.txt');
  116. $testFile->putContent('test contents');
  117. $fileId = $testFile->getId();
  118. // set tags
  119. $this->tagService->updateFileTags('subdir/test.txt', [$tag1, $tag2]);
  120. $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag1));
  121. $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2));
  122. // remove tag
  123. $this->tagService->updateFileTags('subdir/test.txt', [$tag2]);
  124. $this->assertEquals([], $this->tagger->getIdsForTag($tag1));
  125. $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2));
  126. // clear tags
  127. $this->tagService->updateFileTags('subdir/test.txt', []);
  128. $this->assertEquals([], $this->tagger->getIdsForTag($tag1));
  129. $this->assertEquals([], $this->tagger->getIdsForTag($tag2));
  130. // non-existing file
  131. $caught = false;
  132. try {
  133. $this->tagService->updateFileTags('subdir/unexist.txt', [$tag1]);
  134. } catch (\OCP\Files\NotFoundException $e) {
  135. $caught = true;
  136. }
  137. $this->assertTrue($caught);
  138. $subdir->delete();
  139. }
  140. public function testFavoriteActivity() {
  141. $subdir = $this->root->newFolder('subdir');
  142. $file = $subdir->newFile('test.txt');
  143. $this->tagService->expects($this->exactly(2))
  144. ->method('addActivity')
  145. ->withConsecutive(
  146. [true, $file->getId(), 'subdir/test.txt'],
  147. [false, $file->getId(), 'subdir/test.txt']
  148. );
  149. // set tags
  150. $this->tagService->updateFileTags('subdir/test.txt', [ITags::TAG_FAVORITE]);
  151. // remove tag
  152. $this->tagService->updateFileTags('subdir/test.txt', []);
  153. $subdir->delete();
  154. }
  155. }