From e674fe47e6a44d693314af4149022d51597b42ab Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 21 Mar 2013 23:29:54 +0100 Subject: disallow creating and renaming of folders that already exist --- bl/folderbl.php | 18 ++++++++++++++---- db/foldermapper.php | 12 +++++++++++- tests/bl/FolderBlTest.php | 36 +++++++++++++++++++++++++++++++++++- tests/db/FolderMapperTest.php | 17 ++++++++++++++++- 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/bl/folderbl.php b/bl/folderbl.php index 8a75d5de7..19c31657c 100644 --- a/bl/folderbl.php +++ b/bl/folderbl.php @@ -40,11 +40,20 @@ class FolderBl extends Bl { return $this->mapper->findAllFromUser($userId); } + private function allowNoNameTwice($folderName, $userId){ + $existingFolders = $this->mapper->findByName($folderName, $userId); + if(count($existingFolders) > 0){ + throw new BLException('Error: Folder with name ' . $folderName . + ' exists already!'); + } + } + + public function create($folderName, $userId, $parentId=0) { + $this->allowNoNameTwice($folderName, $userId); - public function create($name, $parentId) { - // TODO: throw error when already existing $folder = new Folder(); - $folder->setName($name); + $folder->setName($folderName); + $folder->setUserId($userId); $folder->setParentId($parentId); return $this->mapper->insert($folder); } @@ -58,7 +67,8 @@ class FolderBl extends Bl { public function rename($folderId, $folderName, $userId){ - // TODO: throw error when already existing + $this->allowNoNameTwice($folderName, $userId); + $folder = $this->find($folderId, $userId); $folder->setName($folderName); $this->mapper->update($folder); diff --git a/db/foldermapper.php b/db/foldermapper.php index 9cfa905cb..041b3ada4 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -63,9 +63,19 @@ class FolderMapper extends NewsMapper { public function findAllFromUser($userId){ $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . - 'AND `user_id` = ?'; + 'WHERE `user_id` = ?'; $params = array($userId); return $this->findAllRows($sql, $params); } + + + public function findByName($folderName, $userId){ + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `name` = ?' . + 'AND `user_id` = ?'; + $params = array($folderName, $userId); + + return $this->findAllRows($sql, $params); + } } \ No newline at end of file diff --git a/tests/bl/FolderBlTest.php b/tests/bl/FolderBlTest.php index 6b771e02b..c9fe50802 100644 --- a/tests/bl/FolderBlTest.php +++ b/tests/bl/FolderBlTest.php @@ -65,18 +65,35 @@ class FolderBlTest extends \OCA\AppFramework\Utility\TestUtility { $folder = new Folder(); $folder->setName('hey'); $folder->setParentId(5); + $folder->setUserId('john'); $this->folderMapper->expects($this->once()) ->method('insert') ->with($this->equalTo($folder)) ->will($this->returnValue($folder)); - $result = $this->folderBl->create('hey', 5); + $result = $this->folderBl->create('hey', 'john', 5); $this->assertEquals($folder, $result); } + public function testCreateThrowsExWhenFolderNameExists(){ + $folderName = 'hihi'; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue($rows)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $result = $this->folderBl->create($folderName, 'john', 3); + } + + public function testOpen(){ $folder = new Folder(); @@ -114,4 +131,21 @@ class FolderBlTest extends \OCA\AppFramework\Utility\TestUtility { $this->assertEquals('bogus', $folder->getName()); } + + public function testRenameThrowsExWhenFolderNameExists(){ + $folderName = 'hihi'; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue($rows)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $result = $this->folderBl->rename(3, $folderName, 'john'); + } + + } diff --git a/tests/db/FolderMapperTest.php b/tests/db/FolderMapperTest.php index 469ea2701..924bd7068 100644 --- a/tests/db/FolderMapperTest.php +++ b/tests/db/FolderMapperTest.php @@ -107,7 +107,7 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { array('id' => $this->folders[1]->getId()) ); $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . - 'AND `user_id` = ?'; + 'WHERE `user_id` = ?'; $this->setMapperResult($sql, array($userId), $rows); @@ -116,4 +116,19 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } + public function testFindByName(){ + $userId = 'john'; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `user_id` = ?'; + + $this->setMapperResult($sql, array($userId), $rows); + + $result = $this->folderMapper->findAllFromUser($userId); + $this->assertEquals($this->folders, $result); + } + } \ No newline at end of file -- cgit v1.2.3