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.

tagservice.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  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, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files;
  22. use \OCA\Files\Service\TagService;
  23. class TagServiceTest extends \Test\TestCase {
  24. /**
  25. * @var string
  26. */
  27. private $user;
  28. /**
  29. * @var \OCP\Files\Folder
  30. */
  31. private $root;
  32. /**
  33. * @var \OCA\Files\Service\TagService
  34. */
  35. private $tagService;
  36. /**
  37. * @var \OCP\ITags
  38. */
  39. private $tagger;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->user = $this->getUniqueId('user');
  43. \OC_User::createUser($this->user, 'test');
  44. \OC_User::setUserId($this->user);
  45. \OC_Util::setupFS($this->user);
  46. /**
  47. * @var \OCP\IUser
  48. */
  49. $user = new \OC\User\User($this->user, null);
  50. /**
  51. * @var \OCP\IUserSession
  52. */
  53. $userSession = $this->getMock('\OCP\IUserSession');
  54. $userSession->expects($this->any())
  55. ->method('getUser')
  56. ->withAnyParameters()
  57. ->will($this->returnValue($user));
  58. $this->root = \OC::$server->getUserFolder();
  59. $this->tagger = \OC::$server->getTagManager()->load('files');
  60. $this->tagService = new TagService(
  61. $userSession,
  62. $this->tagger,
  63. $this->root
  64. );
  65. }
  66. protected function tearDown() {
  67. \OC_User::setUserId('');
  68. \OC_User::deleteUser($this->user);
  69. }
  70. public function testUpdateFileTags() {
  71. $tag1 = 'tag1';
  72. $tag2 = 'tag2';
  73. $subdir = $this->root->newFolder('subdir');
  74. $testFile = $subdir->newFile('test.txt');
  75. $testFile->putContent('test contents');
  76. $fileId = $testFile->getId();
  77. // set tags
  78. $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2));
  79. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1));
  80. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
  81. // remove tag
  82. $result = $this->tagService->updateFileTags('subdir/test.txt', array($tag2));
  83. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
  84. $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
  85. // clear tags
  86. $result = $this->tagService->updateFileTags('subdir/test.txt', array());
  87. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
  88. $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2));
  89. // non-existing file
  90. $caught = false;
  91. try {
  92. $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1));
  93. } catch (\OCP\Files\NotFoundException $e) {
  94. $caught = true;
  95. }
  96. $this->assertTrue($caught);
  97. $subdir->delete();
  98. }
  99. }