mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Tests for listing folders
- Fixed checking for existence of parent before listing - Changed Result->getAll() to return an empty array rather than null for an empty result set
This commit is contained in:
parent
b68cea1188
commit
458eaac5c6
4 changed files with 63 additions and 4 deletions
|
@ -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) ".
|
||||
|
|
|
@ -10,7 +10,7 @@ interface Result extends \Iterator {
|
|||
function valid();
|
||||
|
||||
function getRow();
|
||||
function getAll();
|
||||
function getAll(): array;
|
||||
function getValue();
|
||||
|
||||
function changes();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue