2014-11-18 20:53:45 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2014-11-18 20:53:45 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-11-18 20:53:45 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-11-18 20:53:45 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-11-18 20:53:45 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-11-18 20:53:45 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-11-18 20:53:45 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-05-12 13:07:06 +03:00
|
|
|
namespace OCA\Files\Tests\Service;
|
2014-11-18 20:53:45 +03:00
|
|
|
|
2016-05-12 13:07:06 +03:00
|
|
|
use OCA\Files\Service\TagService;
|
2016-12-04 14:54:34 +03:00
|
|
|
use OCP\Activity\IManager;
|
2020-02-11 23:40:01 +03:00
|
|
|
use OCP\ITags;
|
2019-02-22 15:07:26 +03:00
|
|
|
use OCP\IUser;
|
2016-09-02 11:40:48 +03:00
|
|
|
use OCP\IUserSession;
|
2018-01-10 13:20:21 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2014-11-18 20:53:45 +03:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
|
|
|
* Class TagServiceTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package OCA\Files
|
|
|
|
*/
|
2014-11-18 20:53:45 +03:00
|
|
|
class TagServiceTest extends \Test\TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-04 14:54:34 +03:00
|
|
|
private $userSession;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-04 14:54:34 +03:00
|
|
|
private $activityManager;
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
/**
|
|
|
|
* @var \OCP\Files\Folder
|
|
|
|
*/
|
|
|
|
private $root;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2018-01-10 13:20:21 +03:00
|
|
|
private $dispatcher;
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \OCA\Files\Service\TagService|\PHPUnit\Framework\MockObject\MockObject
|
2014-11-18 20:53:45 +03:00
|
|
|
*/
|
|
|
|
private $tagService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\ITags
|
|
|
|
*/
|
|
|
|
private $tagger;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-18 20:53:45 +03:00
|
|
|
parent::setUp();
|
2018-01-10 13:20:21 +03:00
|
|
|
$this->user = static::getUniqueID('user');
|
2016-12-04 14:54:34 +03:00
|
|
|
$this->activityManager = $this->createMock(IManager::class);
|
2015-12-17 17:10:11 +03:00
|
|
|
\OC::$server->getUserManager()->createUser($this->user, 'test');
|
2014-11-18 20:53:45 +03:00
|
|
|
\OC_User::setUserId($this->user);
|
|
|
|
\OC_Util::setupFS($this->user);
|
2019-02-22 15:07:26 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2014-11-18 20:53:45 +03:00
|
|
|
/**
|
|
|
|
* @var \OCP\IUserSession
|
|
|
|
*/
|
2016-12-04 14:54:34 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->userSession->expects($this->any())
|
2014-11-18 20:53:45 +03:00
|
|
|
->method('getUser')
|
|
|
|
->withAnyParameters()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($user);
|
2014-11-18 20:53:45 +03:00
|
|
|
|
|
|
|
$this->root = \OC::$server->getUserFolder();
|
2018-01-10 13:20:21 +03:00
|
|
|
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
2014-11-18 20:53:45 +03:00
|
|
|
|
|
|
|
$this->tagger = \OC::$server->getTagManager()->load('files');
|
2016-12-04 14:54:34 +03:00
|
|
|
$this->tagService = $this->getTagService(['addActivity']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $methods
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return TagService|\PHPUnit\Framework\MockObject\MockObject
|
2016-12-04 14:54:34 +03:00
|
|
|
*/
|
|
|
|
protected function getTagService(array $methods = []) {
|
|
|
|
return $this->getMockBuilder(TagService::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->userSession,
|
|
|
|
$this->activityManager,
|
|
|
|
$this->tagger,
|
|
|
|
$this->root,
|
2018-01-10 13:20:21 +03:00
|
|
|
$this->dispatcher,
|
2016-12-04 14:54:34 +03:00
|
|
|
])
|
|
|
|
->setMethods($methods)
|
|
|
|
->getMock();
|
2014-11-18 20:53:45 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2014-11-18 20:53:45 +03:00
|
|
|
\OC_User::setUserId('');
|
2015-12-17 17:59:23 +03:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->user);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($user !== null) {
|
|
|
|
$user->delete();
|
|
|
|
}
|
2014-11-18 20:53:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateFileTags() {
|
|
|
|
$tag1 = 'tag1';
|
|
|
|
$tag2 = 'tag2';
|
|
|
|
|
2016-12-04 14:54:34 +03:00
|
|
|
$this->tagService->expects($this->never())
|
|
|
|
->method('addActivity');
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
$subdir = $this->root->newFolder('subdir');
|
|
|
|
$testFile = $subdir->newFile('test.txt');
|
|
|
|
$testFile->putContent('test contents');
|
|
|
|
|
|
|
|
$fileId = $testFile->getId();
|
|
|
|
|
|
|
|
// set tags
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->tagService->updateFileTags('subdir/test.txt', [$tag1, $tag2]);
|
2014-11-18 20:53:45 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag1));
|
|
|
|
$this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2));
|
2014-11-18 20:53:45 +03:00
|
|
|
|
|
|
|
// remove tag
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->tagService->updateFileTags('subdir/test.txt', [$tag2]);
|
|
|
|
$this->assertEquals([], $this->tagger->getIdsForTag($tag1));
|
|
|
|
$this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2));
|
2014-11-18 20:53:45 +03:00
|
|
|
|
|
|
|
// clear tags
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->tagService->updateFileTags('subdir/test.txt', []);
|
|
|
|
$this->assertEquals([], $this->tagger->getIdsForTag($tag1));
|
|
|
|
$this->assertEquals([], $this->tagger->getIdsForTag($tag2));
|
2014-11-18 20:53:45 +03:00
|
|
|
|
|
|
|
// non-existing file
|
|
|
|
$caught = false;
|
|
|
|
try {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->tagService->updateFileTags('subdir/unexist.txt', [$tag1]);
|
2014-11-18 20:53:45 +03:00
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
$caught = true;
|
|
|
|
}
|
|
|
|
$this->assertTrue($caught);
|
|
|
|
|
2016-12-04 14:54:34 +03:00
|
|
|
$subdir->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFavoriteActivity() {
|
|
|
|
$subdir = $this->root->newFolder('subdir');
|
|
|
|
$file = $subdir->newFile('test.txt');
|
|
|
|
|
|
|
|
$this->tagService->expects($this->exactly(2))
|
|
|
|
->method('addActivity')
|
|
|
|
->withConsecutive(
|
|
|
|
[true, $file->getId(), 'subdir/test.txt'],
|
|
|
|
[false, $file->getId(), 'subdir/test.txt']
|
|
|
|
);
|
|
|
|
|
|
|
|
// set tags
|
2020-02-11 23:40:01 +03:00
|
|
|
$this->tagService->updateFileTags('subdir/test.txt', [ITags::TAG_FAVORITE]);
|
2016-12-04 14:54:34 +03:00
|
|
|
|
|
|
|
// remove tag
|
|
|
|
$this->tagService->updateFileTags('subdir/test.txt', []);
|
|
|
|
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
$subdir->delete();
|
|
|
|
}
|
|
|
|
}
|