123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- /**
- * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
- *
- */
- namespace Test\SystemTag;
- use OC\SystemTag\SystemTag;
- use OC\SystemTag\SystemTagManager;
- use OC\SystemTag\SystemTagObjectMapper;
- use OCP\IDBConnection;
- use OCP\SystemTag\ISystemTag;
- use OCP\SystemTag\ISystemTagManager;
- use OCP\SystemTag\ISystemTagObjectMapper;
- use OCP\SystemTag\TagNotFoundException;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Test\TestCase;
- /**
- * Class TestSystemTagObjectMapper
- *
- * @group DB
- * @package Test\SystemTag
- */
- class SystemTagObjectMapperTest extends TestCase {
- /**
- * @var ISystemTagManager
- **/
- private $tagManager;
- /**
- * @var ISystemTagObjectMapper
- **/
- private $tagMapper;
- /**
- * @var IDBConnection
- */
- private $connection;
- /**
- * @var EventDispatcherInterface
- */
- private $dispatcher;
- /**
- * @var ISystemTag
- */
- private $tag1;
- /**
- * @var ISystemTag
- */
- private $tag2;
- /**
- * @var ISystemTag
- */
- private $tag3;
- public function setUp() {
- parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->pruneTagsTables();
- $this->tagManager = $this->getMockBuilder('OCP\SystemTag\ISystemTagManager')
- ->getMock();
- $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
- ->getMock();
- $this->tagMapper = new SystemTagObjectMapper(
- $this->connection,
- $this->tagManager,
- $this->dispatcher
- );
- $this->tag1 = new SystemTag(1, 'testtag1', false, false);
- $this->tag2 = new SystemTag(2, 'testtag2', true, false);
- $this->tag3 = new SystemTag(3, 'testtag3', false, false);
- $this->tagManager->expects($this->any())
- ->method('getTagsByIds')
- ->will($this->returnCallback(function($tagIds) {
- $result = [];
- if (in_array(1, $tagIds)) {
- $result[1] = $this->tag1;
- }
- if (in_array(2, $tagIds)) {
- $result[2] = $this->tag2;
- }
- if (in_array(3, $tagIds)) {
- $result[3] = $this->tag3;
- }
- return $result;
- }));
- $this->tagMapper->assignTags('1', 'testtype', $this->tag1->getId());
- $this->tagMapper->assignTags('1', 'testtype', $this->tag2->getId());
- $this->tagMapper->assignTags('2', 'testtype', $this->tag1->getId());
- $this->tagMapper->assignTags('3', 'anothertype', $this->tag1->getId());
- }
- public function tearDown() {
- $this->pruneTagsTables();
- parent::tearDown();
- }
- protected function pruneTagsTables() {
- $query = $this->connection->getQueryBuilder();
- $query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute();
- $query->delete(SystemTagManager::TAG_TABLE)->execute();
- }
- public function testGetTagIdsForObjects() {
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
- ['1', '2', '3', '4'],
- 'testtype'
- );
- $this->assertEquals([
- '1' => [$this->tag1->getId(), $this->tag2->getId()],
- '2' => [$this->tag1->getId()],
- '3' => [],
- '4' => [],
- ], $tagIdMapping);
- }
- public function testGetTagIdsForNoObjects() {
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
- [],
- 'testtype'
- );
- $this->assertEquals([], $tagIdMapping);
- }
- public function testGetObjectsForTags() {
- $objectIds = $this->tagMapper->getObjectIdsForTags(
- [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
- 'testtype'
- );
- $this->assertEquals([
- '1',
- '2',
- ], $objectIds);
- }
- public function testGetObjectsForTagsLimit() {
- $objectIds = $this->tagMapper->getObjectIdsForTags(
- [$this->tag1->getId()],
- 'testtype',
- 1
- );
- $this->assertEquals([
- 1,
- ], $objectIds);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testGetObjectsForTagsLimitWithMultipleTags() {
- $this->tagMapper->getObjectIdsForTags(
- [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
- 'testtype',
- 1
- );
- }
- public function testGetObjectsForTagsLimitOffset() {
- $objectIds = $this->tagMapper->getObjectIdsForTags(
- [$this->tag1->getId()],
- 'testtype',
- 1,
- '1'
- );
- $this->assertEquals([
- 2,
- ], $objectIds);
- }
- /**
- * @expectedException \OCP\SystemTag\TagNotFoundException
- */
- public function testGetObjectsForNonExistingTag() {
- $this->tagMapper->getObjectIdsForTags(
- [100],
- 'testtype'
- );
- }
- public function testAssignUnassignTags() {
- $this->tagMapper->unassignTags('1', 'testtype', [$this->tag1->getId()]);
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');
- $this->assertEquals([
- 1 => [$this->tag2->getId()],
- ], $tagIdMapping);
- $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);
- $this->tagMapper->assignTags('1', 'testtype', $this->tag3->getId());
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');
- $this->assertEquals([
- '1' => [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
- ], $tagIdMapping);
- }
- public function testReAssignUnassignTags() {
- // reassign tag1
- $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);
- // tag 3 was never assigned
- $this->tagMapper->unassignTags('1', 'testtype', [$this->tag3->getId()]);
- $this->assertTrue(true, 'No error when reassigning/unassigning');
- }
- /**
- * @expectedException \OCP\SystemTag\TagNotFoundException
- */
- public function testAssignNonExistingTags() {
- $this->tagMapper->assignTags('1', 'testtype', [100]);
- }
- public function testAssignNonExistingTagInArray() {
- $caught = false;
- try {
- $this->tagMapper->assignTags('1', 'testtype', [100, $this->tag3->getId()]);
- } catch (TagNotFoundException $e) {
- $caught = true;
- }
- $this->assertTrue($caught, 'Exception thrown');
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
- ['1'],
- 'testtype'
- );
- $this->assertEquals([
- '1' => [$this->tag1->getId(), $this->tag2->getId()],
- ], $tagIdMapping, 'None of the tags got assigned');
- }
- /**
- * @expectedException \OCP\SystemTag\TagNotFoundException
- */
- public function testUnassignNonExistingTags() {
- $this->tagMapper->unassignTags('1', 'testtype', [100]);
- }
- public function testUnassignNonExistingTagsInArray() {
- $caught = false;
- try {
- $this->tagMapper->unassignTags('1', 'testtype', [100, $this->tag1->getId()]);
- } catch (TagNotFoundException $e) {
- $caught = true;
- }
- $this->assertTrue($caught, 'Exception thrown');
- $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
- [1],
- 'testtype'
- );
- $this->assertEquals([
- '1' => [$this->tag1->getId(), $this->tag2->getId()],
- ], $tagIdMapping, 'None of the tags got unassigned');
- }
- public function testHaveTagAllMatches() {
- $this->assertTrue(
- $this->tagMapper->haveTag(
- ['1'],
- 'testtype',
- $this->tag1->getId(),
- true
- ),
- 'object 1 has the tag tag1'
- );
- $this->assertTrue(
- $this->tagMapper->haveTag(
- ['1', '2'],
- 'testtype',
- $this->tag1->getId(),
- true
- ),
- 'object 1 and object 2 ALL have the tag tag1'
- );
- $this->assertFalse(
- $this->tagMapper->haveTag(
- ['1', '2'],
- 'testtype',
- $this->tag2->getId(),
- true
- ),
- 'object 1 has tag2 but not object 2, so not ALL of them'
- );
- $this->assertFalse(
- $this->tagMapper->haveTag(
- ['2'],
- 'testtype',
- $this->tag2->getId(),
- true
- ),
- 'object 2 does not have tag2'
- );
- $this->assertFalse(
- $this->tagMapper->haveTag(
- ['3'],
- 'testtype',
- $this->tag2->getId(),
- true
- ),
- 'object 3 does not have tag1 due to different type'
- );
- }
- public function testHaveTagAtLeastOneMatch() {
- $this->assertTrue(
- $this->tagMapper->haveTag(
- ['1'],
- 'testtype',
- $this->tag1->getId(),
- false
- ),
- 'object1 has the tag tag1'
- );
- $this->assertTrue(
- $this->tagMapper->haveTag(
- ['1', '2'],
- 'testtype',
- $this->tag1->getId(),
- false
- ),
- 'object 1 and object 2 both the tag tag1'
- );
- $this->assertTrue(
- $this->tagMapper->haveTag(
- ['1', '2'],
- 'testtype',
- $this->tag2->getId(),
- false
- ),
- 'at least object 1 has the tag tag2'
- );
- $this->assertFalse(
- $this->tagMapper->haveTag(
- ['2'],
- 'testtype',
- $this->tag2->getId(),
- false
- ),
- 'object 2 does not have tag2'
- );
- $this->assertFalse(
- $this->tagMapper->haveTag(
- ['3'],
- 'testtype',
- $this->tag2->getId(),
- false
- ),
- 'object 3 does not have tag1 due to different type'
- );
- }
- /**
- * @expectedException \OCP\SystemTag\TagNotFoundException
- */
- public function testHaveTagNonExisting() {
- $this->tagMapper->haveTag(
- ['1'],
- 'testtype',
- 100
- );
- }
- }
|