diff --git a/lib/Database.php b/lib/Database.php index 9bc44afc..e69ef832 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -362,9 +362,15 @@ class Database { if (!$this->userExists($user)) { throw new User\Exception("doesNotExist", ["user" => $user, "action" => __FUNCTION__]); } + // check to make sure the parent exists, if one is specified + if(!is_null($parent)) { + if(!$this->db->prepare("SELECT count(*) from arsse_folders where owner is ? and id is ?", "str", "int")->run($user, $parent)->getValue()) { + throw new Db\ExceptionInput("idMissing", ["action" => __FUNCTION__, "field" => "parent", 'id' => $parent]); + } + } // if we're not returning a recursive list we can use a simpler query if(!$recursive) { - return $this->db->preparre("SELECT id,name,parent from arsse_folders where owner is ? and parent is ?", "str", "int")->run($user, $parent); + return $this->db->prepare("SELECT id,name,parent from arsse_folders where owner is ? and parent is ?", "str", "int")->run($user, $parent); } else { return $this->db->prepare( "WITH RECURSIVE folders(id) as (SELECT id from arsse_folders where owner is ? and parent is ? union select arsse_folders.id from arsse_folders join folders on arsse_folders.parent=folders.id) ". diff --git a/lib/Db/Result.php b/lib/Db/Result.php index ae7382ce..dca03e04 100644 --- a/lib/Db/Result.php +++ b/lib/Db/Result.php @@ -10,7 +10,7 @@ interface Result extends \Iterator { function valid(); function getRow(); - function getAll(); + function getAll(): array; function getValue(); function changes(); diff --git a/lib/Db/SQLite3/Result.php b/lib/Db/SQLite3/Result.php index 8cd90da6..a5432d12 100644 --- a/lib/Db/SQLite3/Result.php +++ b/lib/Db/SQLite3/Result.php @@ -26,12 +26,11 @@ class Result implements \JKingWeb\Arsse\Db\Result { return ($this->valid() ? $this->cur : null); } - public function getAll() { + public function getAll(): array { $out = []; foreach($this as $row) { $out [] = $row; } - if(sizeof($out) < 1) return null; return $out; } diff --git a/tests/lib/Database/SeriesFolder.php b/tests/lib/Database/SeriesFolder.php index d7cea89d..8e392fdb 100644 --- a/tests/lib/Database/SeriesFolder.php +++ b/tests/lib/Database/SeriesFolder.php @@ -99,4 +99,58 @@ trait SeriesFolder { $this->assertException("notAuthorized", "User", "ExceptionAuthz"); Data::$db->folderAdd("john.doe@example.com", ['name' => "Sociology"]); } + + function testListRootFolders() { + $exp = [ + ['id' => 5, 'name' => "Politics", 'parent' => null], + ['id' => 1, 'name' => "Technology", 'parent' => null], + ]; + $this->assertSame($exp, Data::$db->folderList("john.doe@example.com", null, false)->getAll()); + $exp = [ + ['id' => 4, 'name' => "Politics", 'parent' => null], + ]; + $this->assertSame($exp, Data::$db->folderList("jane.doe@example.com", null, false)->getAll()); + $exp = []; + $this->assertSame($exp, Data::$db->folderList("admin@example.net", null, false)->getAll()); + } + + function testListFoldersRecursively() { + $exp = [ + ['id' => 5, 'name' => "Politics", 'parent' => null], + ['id' => 6, 'name' => "Politics", 'parent' => 2], + ['id' => 3, 'name' => "Rocketry", 'parent' => 1], + ['id' => 2, 'name' => "Software", 'parent' => 1], + ['id' => 1, 'name' => "Technology", 'parent' => null], + ]; + $this->assertSame($exp, Data::$db->folderList("john.doe@example.com", null, true)->getAll()); + $exp = [ + ['id' => 6, 'name' => "Politics", 'parent' => 2], + ['id' => 3, 'name' => "Rocketry", 'parent' => 1], + ['id' => 2, 'name' => "Software", 'parent' => 1], + ]; + $this->assertSame($exp, Data::$db->folderList("john.doe@example.com", 1, true)->getAll()); + $exp = []; + $this->assertSame($exp, Data::$db->folderList("jane.doe@example.com", 4, true)->getAll()); + } + + function testListFoldersOfAMissingParent() { + $this->assertException("idMissing", "Db", "ExceptionInput"); + Data::$db->folderList("john.doe@example.com", 2112); + } + + function testListFoldersOfTheWrongOwner() { + $this->assertException("idMissing", "Db", "ExceptionInput"); + Data::$db->folderList("john.doe@example.com", 4); // folder ID 4 belongs to Jane + } + + function testListFoldersForAMissingUser() { + $this->assertException("doesNotExist", "User"); + Data::$db->folderList("john.doe@example.org"); + } + + function testListFoldersWithoutAuthority() { + Phake::when(Data::$user)->authorize->thenReturn(false); + $this->assertException("notAuthorized", "User", "ExceptionAuthz"); + Data::$db->folderList("john.doe@example.com"); + } } \ No newline at end of file