1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
/**
* ownCloud
*
* @author Vincent Petry
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files;
use \OCA\Files\Service\TagService;
class TagServiceTest extends \Test\TestCase {
/**
* @var string
*/
private $user;
/**
* @var \OCP\Files\Folder
*/
private $root;
/**
* @var \OCA\Files\Service\TagService
*/
private $tagService;
/**
* @var \OCP\ITags
*/
private $tagger;
protected function setUp() {
parent::setUp();
$this->user = $this->getUniqueId('user');
\OC_User::createUser($this->user, 'test');
\OC_User::setUserId($this->user);
\OC_Util::setupFS($this->user);
/**
* @var \OCP\IUser
*/
$user = new \OC\User\User($this->user, null);
/**
* @var \OCP\IUserSession
*/
$userSession = $this->getMock('\OCP\IUserSession');
$userSession->expects($this->any())
->method('getUser')
->withAnyParameters()
->will($this->returnValue($user));
$this->root = \OC::$server->getUserFolder();
$this->tagger = \OC::$server->getTagManager()->load('files');
$this->tagService = new TagService(
$userSession,
$this->tagger,
$this->root
);
}
protected function tearDown() {
\OC_User::setUserId('');
\OC_User::deleteUser($this->user);
}
public function testUpdateFileTags() {
$tag1 = 'tag1';
$tag2 = 'tag2';
$subdir = $this->root->newFolder('subdir');
$testFile = $subdir->newFile('test.txt');
$testFile->putContent('test contents');
$fileId = $testFile->getId();
// set tags
$this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2));
$this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1));
$this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
// remove tag
$result = $this->tagService->updateFileTags('subdir/test.txt', array($tag2));
$this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
$this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2));
// clear tags
$result = $this->tagService->updateFileTags('subdir/test.txt', array());
$this->assertEquals(array(), $this->tagger->getIdsForTag($tag1));
$this->assertEquals(array(), $this->tagger->getIdsForTag($tag2));
// non-existing file
$caught = false;
try {
$this->tagService->updateFileTags('subdir/unexist.txt', array($tag1));
} catch (\OCP\Files\NotFoundException $e) {
$caught = true;
}
$this->assertTrue($caught);
$subdir->delete();
}
}
|