From 1bbeb12e2e8acc47b94c23d6a17332fb45c2a97e Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Thu, 19 Sep 2013 11:27:13 +0200 Subject: [PATCH] Updated method names and added a few more tests. --- lib/public/itags.php | 6 ++--- lib/tags.php | 12 ++++----- tests/lib/tags.php | 59 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/lib/public/itags.php b/lib/public/itags.php index 047d4f5f40..1264340054 100644 --- a/lib/public/itags.php +++ b/lib/public/itags.php @@ -65,7 +65,7 @@ interface ITags { * * @returns array */ - public function tags(); + public function getTags(); /** * Get the a list if items tagged with $tag. @@ -75,7 +75,7 @@ interface ITags { * @param string|integer $tag Tag id or name. * @return array An array of object ids or false on error. */ - public function idsForTag($tag); + public function getIdsForTag($tag); /** * Checks whether a tag is already saved. @@ -111,7 +111,7 @@ interface ITags { * @param int|null $id int Optional object id to add to this|these tag(s) * @return bool Returns false on error. */ - public function addMulti($names, $sync=false, $id = null); + public function addMultiple($names, $sync=false, $id = null); /** * Delete tag/object relations from the db diff --git a/lib/tags.php b/lib/tags.php index 3320d9ea1a..2eaa603c1a 100644 --- a/lib/tags.php +++ b/lib/tags.php @@ -96,7 +96,7 @@ class Tags implements \OCP\ITags { } if(count($defaultTags) > 0 && count($this->tags) === 0) { - $this->addMulti($defaultTags, true); + $this->addMultiple($defaultTags, true); } \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), \OCP\Util::DEBUG); @@ -119,7 +119,7 @@ class Tags implements \OCP\ITags { \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } - return ($result->numRows() === 0); + return ((int)$result->numRows() === 0); } catch(\Exception $e) { \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); @@ -138,7 +138,7 @@ class Tags implements \OCP\ITags { * * @return array */ - public function tags() { + public function getTags() { if(!count($this->tags)) { return array(); } @@ -167,7 +167,7 @@ class Tags implements \OCP\ITags { * @param string|integer $tag Tag id or name. * @return array An array of object ids or false on error. */ - public function idsForTag($tag) { + public function getIdsForTag($tag) { $result = null; if(is_numeric($tag)) { $tagId = $tag; @@ -293,7 +293,7 @@ class Tags implements \OCP\ITags { * @param int|null $id int Optional object id to add to this|these tag(s) * @return bool Returns false on error. */ - public function addMulti($names, $sync=false, $id = null) { + public function addMultiple($names, $sync=false, $id = null) { if(!is_array($names)) { $names = array($names); } @@ -456,7 +456,7 @@ class Tags implements \OCP\ITags { */ public function getFavorites() { try { - return $this->idsForTag(self::TAG_FAVORITE); + return $this->getIdsForTag(self::TAG_FAVORITE); } catch(\Exception $e) { \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), \OCP\Util::ERROR); diff --git a/tests/lib/tags.php b/tests/lib/tags.php index 06baebc0af..16a03f5645 100644 --- a/tests/lib/tags.php +++ b/tests/lib/tags.php @@ -48,7 +48,7 @@ class Test_Tags extends PHPUnit_Framework_TestCase { $tagMgr = new OC\Tags($this->user); $tagMgr->loadTagsFor($this->objectType, $defaultTags); - $this->assertEquals(4, count($tagMgr->tags())); + $this->assertEquals(4, count($tagMgr->getTags())); } public function testAddTags() { @@ -65,7 +65,37 @@ class Test_Tags extends PHPUnit_Framework_TestCase { $this->assertFalse($tagMgr->add('Family')); $this->assertFalse($tagMgr->add('fAMILY')); - $this->assertEquals(4, count($tagMgr->tags())); + $this->assertEquals(4, count($tagMgr->getTags())); + } + + public function testAddMultiple() { + $tags = array('Friends', 'Family', 'Work', 'Other'); + + $tagMgr = new OC\Tags($this->user); + $tagMgr->loadTagsFor($this->objectType); + + foreach($tags as $tag) { + $this->assertFalse($tagMgr->hasTag($tag)); + } + + $result = $tagMgr->addMultiple($tags); + $this->assertTrue((bool)$result); + + foreach($tags as $tag) { + $this->assertTrue($tagMgr->hasTag($tag)); + } + + $this->assertEquals(4, count($tagMgr->getTags())); + } + + public function testIsEmpty() { + $tagMgr = new OC\Tags($this->user); + $tagMgr->loadTagsFor($this->objectType); + + $this->assertEquals(0, count($tagMgr->getTags())); + $this->assertTrue($tagMgr->isEmpty()); + $tagMgr->add('Tag'); + $this->assertFalse($tagMgr->isEmpty()); } public function testdeleteTags() { @@ -73,13 +103,13 @@ class Test_Tags extends PHPUnit_Framework_TestCase { $tagMgr = new OC\Tags($this->user); $tagMgr->loadTagsFor($this->objectType, $defaultTags); - $this->assertEquals(4, count($tagMgr->tags())); + $this->assertEquals(4, count($tagMgr->getTags())); $tagMgr->delete('family'); - $this->assertEquals(3, count($tagMgr->tags())); + $this->assertEquals(3, count($tagMgr->getTags())); $tagMgr->delete(array('Friends', 'Work', 'Other')); - $this->assertEquals(0, count($tagMgr->tags())); + $this->assertEquals(0, count($tagMgr->getTags())); } @@ -105,8 +135,8 @@ class Test_Tags extends PHPUnit_Framework_TestCase { $tagMgr->tagAs($id, 'Family'); } - $this->assertEquals(1, count($tagMgr->tags())); - $this->assertEquals(9, count($tagMgr->idsForTag('Family'))); + $this->assertEquals(1, count($tagMgr->getTags())); + $this->assertEquals(9, count($tagMgr->getIdsForTag('Family'))); } /** @@ -121,13 +151,20 @@ class Test_Tags extends PHPUnit_Framework_TestCase { $tagMgr->loadTagsFor($this->objectType); foreach($objIds as $id) { - $this->assertTrue(in_array($id, $tagMgr->idsForTag('Family'))); + $this->assertTrue(in_array($id, $tagMgr->getIdsForTag('Family'))); $tagMgr->unTag($id, 'Family'); - $this->assertFalse(in_array($id, $tagMgr->idsForTag('Family'))); + $this->assertFalse(in_array($id, $tagMgr->getIdsForTag('Family'))); } - $this->assertEquals(1, count($tagMgr->tags())); - $this->assertEquals(0, count($tagMgr->idsForTag('Family'))); + $this->assertEquals(1, count($tagMgr->getTags())); + $this->assertEquals(0, count($tagMgr->getIdsForTag('Family'))); + } + + public function testFavorite() { + $tagMgr = new OC\Tags($this->user); + $tagMgr->loadTagsFor($this->objectType); + $this->assertTrue($tagMgr->addToFavorites(1)); + $this->assertTrue($tagMgr->removeFromFavorites(1)); } }