2013-09-18 02:37:00 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Test_Tags extends PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
protected $objectType;
|
|
|
|
protected $user;
|
|
|
|
protected $backupGlobals = FALSE;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
|
|
|
|
OC_User::clearBackends();
|
|
|
|
OC_User::useBackend('dummy');
|
|
|
|
$this->user = uniqid('user_');
|
|
|
|
$this->objectType = uniqid('type_');
|
|
|
|
OC_User::createUser($this->user, 'pass');
|
|
|
|
OC_User::setUserId($this->user);
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->tagMgr = new OC\TagManager($this->user);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
//$query = OC_DB::prepare('DELETE FROM `*PREFIX*vcategories` WHERE `item_type` = ?');
|
|
|
|
//$query->execute(array('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInstantiateWithDefaults() {
|
|
|
|
$defaultTags = array('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
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddTags() {
|
|
|
|
$tags = array('Friends', 'Family', 'Work', 'Other');
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-18 02:37:00 +04: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() {
|
|
|
|
$tags = array('Friends', 'Family', 'Work', 'Other');
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-19 13:27:13 +04: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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->assertCount(4, $tagger->getTags(), 'Not all tags added');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testdeleteTags() {
|
|
|
|
$defaultTags = array('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
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger->delete(array('Friends', 'Work', 'Other'));
|
|
|
|
$this->assertEquals(0, count($tagger->getTags()));
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRenameTag() {
|
|
|
|
$defaultTags = array('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'));
|
|
|
|
$this->assertFalse($tagger->hastag('Wrok'));
|
|
|
|
$this->assertFalse($tagger->rename('Wrok', 'Work'));
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTagAs() {
|
|
|
|
$objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
$tagger = $this->tagMgr->load($this->objectType);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
foreach($objids as $id) {
|
2013-09-24 19:10:01 +04:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testTagAs
|
|
|
|
*/
|
|
|
|
public function testUnTag() {
|
|
|
|
$objIds = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
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));
|
|
|
|
$this->assertTrue($tagger->removeFromFavorites(1));
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|