1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +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:
J. King 2017-03-31 17:42:28 -04:00
parent b68cea1188
commit 458eaac5c6
4 changed files with 63 additions and 4 deletions

View file

@ -362,9 +362,15 @@ class Database {
if (!$this->userExists($user)) { if (!$this->userExists($user)) {
throw new User\Exception("doesNotExist", ["user" => $user, "action" => __FUNCTION__]); 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 we're not returning a recursive list we can use a simpler query
if(!$recursive) { 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 { } else {
return $this->db->prepare( 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) ". "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) ".

View file

@ -10,7 +10,7 @@ interface Result extends \Iterator {
function valid(); function valid();
function getRow(); function getRow();
function getAll(); function getAll(): array;
function getValue(); function getValue();
function changes(); function changes();

View file

@ -26,12 +26,11 @@ class Result implements \JKingWeb\Arsse\Db\Result {
return ($this->valid() ? $this->cur : null); return ($this->valid() ? $this->cur : null);
} }
public function getAll() { public function getAll(): array {
$out = []; $out = [];
foreach($this as $row) { foreach($this as $row) {
$out [] = $row; $out [] = $row;
} }
if(sizeof($out) < 1) return null;
return $out; return $out;
} }

View file

@ -99,4 +99,58 @@ trait SeriesFolder {
$this->assertException("notAuthorized", "User", "ExceptionAuthz"); $this->assertException("notAuthorized", "User", "ExceptionAuthz");
Data::$db->folderAdd("john.doe@example.com", ['name' => "Sociology"]); 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");
}
} }