mirror of
https://github.com/chylex/Nextcloud-News.git
synced 2025-04-12 19:15:43 +02:00
disallow creating and renaming of folders that already exist
This commit is contained in:
parent
b6adabf245
commit
e674fe47e6
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user