You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TagServiceTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files\Tests\Service;
  26. use OC\Tags;
  27. use OCA\Files\Service\TagService;
  28. use OCP\Activity\IManager;
  29. use OCP\IUserSession;
  30. /**
  31. * Class TagServiceTest
  32. *
  33. * @group DB
  34. *
  35. * @package OCA\Files
  36. */
  37. class TagServiceTest extends \Test\TestCase {
  38. /**
  39. * @var string
  40. */
  41. private $user;
  42. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  43. private $userSession;
  44. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  45. private $activityManager;
  46. /**
  47. * @var \OCP\Files\Folder
  48. */
  49. private $root;
  50. /**
  51. * @var \OCA\Files\Service\TagService|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $tagService;
  54. /**
  55. * @var \OCP\ITags
  56. */
  57. private $tagger;
  58. protected function setUp() {
  59. parent::setUp();
  60. $this->user = $this->getUniqueID('user');
  61. $this->activityManager = $this->createMock(IManager::class);
  62. \OC::$server->getUserManager()->createUser($this->user, 'test');
  63. \OC_User::setUserId($this->user);
  64. \OC_Util::setupFS($this->user);
  65. /** @var \OCP\IUser */
  66. $user = new \OC\User\User($this->user, null);
  67. /**
  68. * @var \OCP\IUserSession
  69. */
  70. $this->userSession = $this->createMock(IUserSession::class);
  71. $this->userSession->expects($this->any())
  72. ->method('getUser')
  73. ->withAnyParameters()
  74. ->will($this->returnValue($user));
  75. $this->root = \OC::$server->getUserFolder();
  76. $this->tagger = \OC::$server->getTagManager()->load('files');
  77. $this->tagService = $this->getTagService(['addActivity']);
  78. }
  79. /**
  80. * @param array $methods
  81. * @return TagService|\PHPUnit_Framework_MockObject_MockObject
  82. */
  83. protected function getTagService(array $methods = []) {
  84. return $this->getMockBuilder(TagService::class)
  85. ->setConstructorArgs([
  86. $this->userSession,
  87. $this->activityManager,
  88. $this->tagger,
  89. $this->root,
  90. ])
  91. ->setMethods($methods)
  92. ->getMock();
  93. }
  94. protected function tearDown() {
  95. \OC_User::setUserId('');
  96. $user = \OC::$server->getUserManager()->get($this->user);
  97. if ($user !== null) { $user->delete(); }
  98. }
  99. public function testUpdateFileTags() {
  100. $tag1 = 'tag1';
  101. $tag2 = 'tag2';
  102. $this->tagService->expects($this->never())
  103. ->method('addActivity');
  104. $subdir = $this->root->newFolder('subdir');
  105. $testFile = $subdir->newFile('test.txt');
  106. $testFile->putContent('test contents');
  107. $fileId = $testFile->getId();
  108. // set tags
  109. $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2));
  110. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1));
  111. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
  112. // remove tag
  113. $this->tagService->updateFileTags('subdir/test.txt', array($tag2));
  114. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
  115. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
  116. // clear tags
  117. $this->tagService->updateFileTags('subdir/test.txt', array());
  118. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
  119. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2));
  120. // non-existing file
  121. $caught = false;
  122. try {
  123. $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1));
  124. } catch (\OCP\Files\NotFoundException $e) {
  125. $caught = true;
  126. }
  127. $this->assertTrue($caught);
  128. $subdir->delete();
  129. }
  130. public function testFavoriteActivity() {
  131. $subdir = $this->root->newFolder('subdir');
  132. $file = $subdir->newFile('test.txt');
  133. $this->tagService->expects($this->exactly(2))
  134. ->method('addActivity')
  135. ->withConsecutive(
  136. [true, $file->getId(), 'subdir/test.txt'],
  137. [false, $file->getId(), 'subdir/test.txt']
  138. );
  139. // set tags
  140. $this->tagService->updateFileTags('subdir/test.txt', [Tags::TAG_FAVORITE]);
  141. // remove tag
  142. $this->tagService->updateFileTags('subdir/test.txt', []);
  143. $subdir->delete();
  144. }
  145. }