2013-09-18 02:37:00 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2012-13 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
namespace Test;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
use OCP\IUser;
|
2016-09-12 22:40:00 +03:00
|
|
|
use OCP\IUserSession;
|
2016-05-19 10:38:52 +03:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
2016-05-19 10:38:52 +03:00
|
|
|
* Class TagsTest
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-05-19 10:38:52 +03:00
|
|
|
class TagsTest extends \Test\TestCase {
|
2013-09-18 02:37:00 +04:00
|
|
|
protected $objectType;
|
2015-11-03 03:52:41 +03:00
|
|
|
/** @var \OCP\IUser */
|
2013-09-18 02:37:00 +04:00
|
|
|
protected $user;
|
2015-11-03 03:52:41 +03:00
|
|
|
/** @var \OCP\IUserSession */
|
2014-12-10 17:59:41 +03:00
|
|
|
protected $userSession;
|
2020-03-25 23:53:04 +03:00
|
|
|
protected $backupGlobals = false;
|
2014-12-02 15:51:36 +03:00
|
|
|
/** @var \OC\Tagging\TagMapper */
|
|
|
|
protected $tagMapper;
|
2015-01-19 19:05:44 +03:00
|
|
|
/** @var \OCP\ITagManager */
|
2014-12-02 15:51:36 +03:00
|
|
|
protected $tagMgr;
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::setUp();
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
\OC_User::clearBackends();
|
|
|
|
\OC_User::useBackend('dummy');
|
2014-12-10 17:59:41 +03:00
|
|
|
$userId = $this->getUniqueID('user_');
|
2015-12-17 17:10:11 +03:00
|
|
|
\OC::$server->getUserManager()->createUser($userId, 'pass');
|
2016-05-19 10:38:52 +03:00
|
|
|
\OC_User::setUserId($userId);
|
2019-02-22 15:07:26 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
|
|
|
$this->user->method('getUID')
|
|
|
|
->willReturn($userId);
|
2016-09-12 22:40:00 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2014-12-10 17:59:41 +03:00
|
|
|
$this->userSession
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($this->user);
|
2014-12-10 17:59:41 +03:00
|
|
|
|
2014-12-02 15:51:36 +03:00
|
|
|
$this->objectType = $this->getUniqueID('type_');
|
2016-05-19 10:38:52 +03:00
|
|
|
$this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->getDatabaseConnection());
|
2020-10-05 15:57:13 +03:00
|
|
|
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection());
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2016-01-07 12:26:00 +03:00
|
|
|
$conn = \OC::$server->getDatabaseConnection();
|
2015-01-19 19:05:44 +03:00
|
|
|
$conn->executeQuery('DELETE FROM `*PREFIX*vcategory_to_object`');
|
|
|
|
$conn->executeQuery('DELETE FROM `*PREFIX*vcategory`');
|
2014-11-11 00:59:50 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2015-02-25 19:20:26 +03:00
|
|
|
public function testTagManagerWithoutUserReturnsNull() {
|
2016-09-12 22:40:00 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2015-02-25 19:20:26 +03:00
|
|
|
$this->userSession
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2020-10-05 15:57:13 +03:00
|
|
|
$this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection());
|
2015-02-25 19:20:26 +03:00
|
|
|
$this->assertNull($this->tagMgr->load($this->objectType));
|
|
|
|
}
|
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
public function testInstantiateWithDefaults() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$defaultTags = ['Friends', 'Family', 'Work', 'Other'];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(4, count($tagger->getTags()));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddTags() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$tags = ['Friends', 'Family', 'Work', 'Other'];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tags as $tag) {
|
2013-09-24 19:10:01 +04:00
|
|
|
$result = $tagger->add($tag);
|
2013-09-23 17:52:50 +04:00
|
|
|
$this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
|
2013-09-18 02:37:00 +04:00
|
|
|
$this->assertTrue((bool)$result);
|
|
|
|
}
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertFalse($tagger->add('Family'));
|
|
|
|
$this->assertFalse($tagger->add('fAMILY'));
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertCount(4, $tagger->getTags(), 'Wrong number of added tags');
|
2013-09-19 13:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMultiple() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$tags = ['Friends', 'Family', 'Work', 'Other'];
|
2013-09-19 13:27:13 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-19 13:27:13 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tags as $tag) {
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertFalse($tagger->hasTag($tag));
|
2013-09-19 13:27:13 +04:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$result = $tagger->addMultiple($tags);
|
2013-09-19 13:27:13 +04:00
|
|
|
$this->assertTrue((bool)$result);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tags as $tag) {
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertTrue($tagger->hasTag($tag));
|
2013-09-19 13:27:13 +04:00
|
|
|
}
|
|
|
|
|
2014-09-30 14:19:08 +04:00
|
|
|
$tagMaps = $tagger->getTags();
|
|
|
|
$this->assertCount(4, $tagMaps, 'Not all tags added');
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tagMaps as $tagMap) {
|
2014-09-30 14:19:08 +04:00
|
|
|
$this->assertEquals(null, $tagMap['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// As addMultiple has been called without $sync=true, the tags aren't
|
|
|
|
// saved to the database, so they're gone when we reload $tagger:
|
|
|
|
|
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
|
|
|
$this->assertEquals(0, count($tagger->getTags()));
|
|
|
|
|
|
|
|
// Now, we call addMultiple() with $sync=true so the tags will be
|
|
|
|
// be saved to the database.
|
|
|
|
$result = $tagger->addMultiple($tags, true);
|
|
|
|
$this->assertTrue((bool)$result);
|
|
|
|
|
|
|
|
$tagMaps = $tagger->getTags();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tagMaps as $tagMap) {
|
2014-09-30 14:19:08 +04:00
|
|
|
$this->assertNotEquals(null, $tagMap['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload the tagger.
|
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($tags as $tag) {
|
2014-09-30 14:19:08 +04:00
|
|
|
$this->assertTrue($tagger->hasTag($tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(4, $tagger->getTags(), 'Not all previously saved tags found');
|
2013-09-19 13:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEmpty() {
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-19 13:27:13 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(0, count($tagger->getTags()));
|
|
|
|
$this->assertTrue($tagger->isEmpty());
|
2013-09-23 17:52:50 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$result = $tagger->add('Tag');
|
2013-09-23 17:52:50 +04:00
|
|
|
$this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
|
|
|
|
$this->assertNotEquals(false, $result, 'add() returned false');
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertFalse($tagger->isEmpty());
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2014-11-24 23:51:45 +03:00
|
|
|
public function testGetTagsForObjects() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$defaultTags = ['Friends', 'Family', 'Work', 'Other'];
|
2014-11-24 23:51:45 +03:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
|
|
|
|
|
|
|
|
$tagger->tagAs(1, 'Friends');
|
|
|
|
$tagger->tagAs(1, 'Other');
|
|
|
|
$tagger->tagAs(2, 'Family');
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$tags = $tagger->getTagsForObjects([1]);
|
2014-11-24 23:51:45 +03:00
|
|
|
$this->assertEquals(1, count($tags));
|
|
|
|
$tags = current($tags);
|
|
|
|
sort($tags);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertSame(['Friends', 'Other'], $tags);
|
2014-11-24 23:51:45 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$tags = $tagger->getTagsForObjects([1, 2]);
|
2014-11-24 23:51:45 +03:00
|
|
|
$this->assertEquals(2, count($tags));
|
|
|
|
$tags1 = $tags[1];
|
|
|
|
sort($tags1);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertSame(['Friends', 'Other'], $tags1);
|
|
|
|
$this->assertSame(['Family'], $tags[2]);
|
2014-11-24 23:51:45 +03:00
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[],
|
|
|
|
$tagger->getTagsForObjects([4])
|
2014-11-24 23:51:45 +03:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-03-26 11:30:18 +03:00
|
|
|
[],
|
|
|
|
$tagger->getTagsForObjects([4, 5])
|
2014-11-24 23:51:45 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-19 19:05:44 +03:00
|
|
|
public function testGetTagsForObjectsMassiveResults() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$defaultTags = ['tag1'];
|
2015-01-19 19:05:44 +03:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
|
|
|
|
$tagData = $tagger->getTags();
|
|
|
|
$tagId = $tagData[0]['id'];
|
|
|
|
$tagType = $tagData[0]['type'];
|
|
|
|
|
2016-01-07 12:26:00 +03:00
|
|
|
$conn = \OC::$server->getDatabaseConnection();
|
2015-01-19 19:05:44 +03:00
|
|
|
$statement = $conn->prepare(
|
|
|
|
'INSERT INTO `*PREFIX*vcategory_to_object` ' .
|
|
|
|
'(`objid`, `categoryid`, `type`) VALUES ' .
|
|
|
|
'(?, ?, ?)'
|
|
|
|
);
|
|
|
|
|
|
|
|
// insert lots of entries
|
2020-03-26 11:30:18 +03:00
|
|
|
$idsArray = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
for ($i = 1; $i <= 1500; $i++) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$statement->execute([$i, $tagId, $tagType]);
|
2015-01-19 19:05:44 +03:00
|
|
|
$idsArray[] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tags = $tagger->getTagsForObjects($idsArray);
|
|
|
|
$this->assertEquals(1500, count($tags));
|
|
|
|
}
|
|
|
|
|
2014-12-10 17:59:41 +03:00
|
|
|
public function testDeleteTags() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$defaultTags = ['Friends', 'Family', 'Work', 'Other'];
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(4, count($tagger->getTags()));
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger->delete('family');
|
|
|
|
$this->assertEquals(3, count($tagger->getTags()));
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$tagger->delete(['Friends', 'Work', 'Other']);
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(0, count($tagger->getTags()));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenameTag() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$defaultTags = ['Friends', 'Family', 'Wrok', 'Other'];
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertTrue($tagger->rename('Wrok', 'Work'));
|
|
|
|
$this->assertTrue($tagger->hasTag('Work'));
|
2016-05-19 10:38:52 +03:00
|
|
|
$this->assertFalse($tagger->hasTag('Wrok'));
|
2014-10-03 21:32:39 +04:00
|
|
|
$this->assertFalse($tagger->rename('Wrok', 'Work')); // Rename non-existant tag.
|
|
|
|
$this->assertFalse($tagger->rename('Work', 'Family')); // Collide with existing tag.
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagAs() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$objids = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($objids as $id) {
|
2014-03-05 03:26:06 +04:00
|
|
|
$this->assertTrue($tagger->tagAs($id, 'Family'));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(1, count($tagger->getTags()));
|
|
|
|
$this->assertEquals(9, count($tagger->getIdsForTag('Family')));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* @depends testTagAs
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function testUnTag() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$objIds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
// Is this "legal"?
|
|
|
|
$this->testTagAs();
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($objIds as $id) {
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertTrue(in_array($id, $tagger->getIdsForTag('Family')));
|
|
|
|
$tagger->unTag($id, 'Family');
|
|
|
|
$this->assertFalse(in_array($id, $tagger->getIdsForTag('Family')));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertEquals(1, count($tagger->getTags()));
|
|
|
|
$this->assertEquals(0, count($tagger->getIdsForTag('Family')));
|
2013-09-19 13:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFavorite() {
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
|
|
|
$this->assertTrue($tagger->addToFavorites(1));
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([1], $tagger->getFavorites());
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertTrue($tagger->removeFromFavorites(1));
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([], $tagger->getFavorites());
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|