1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2025-01-03 14:32:40 +00:00

Tests for adding folders

This commit is contained in:
J. King 2017-03-31 15:27:59 -04:00
parent 82e4838162
commit b68cea1188
5 changed files with 143 additions and 43 deletions

View file

@ -323,7 +323,7 @@ class Database {
throw new User\Exception("doesNotExist", ["user" => $user, "action" => __FUNCTION__]); throw new User\Exception("doesNotExist", ["user" => $user, "action" => __FUNCTION__]);
} }
// if the desired folder name is missing or invalid, throw an exception // if the desired folder name is missing or invalid, throw an exception
if(!array_key_exists("name", $data)) { if(!array_key_exists("name", $data) || $data['name']=="") {
throw new Db\ExceptionInput("missing", ["action" => __FUNCTION__, "field" => "name"]); throw new Db\ExceptionInput("missing", ["action" => __FUNCTION__, "field" => "name"]);
} else if(!strlen(trim($data['name']))) { } else if(!strlen(trim($data['name']))) {
throw new Db\ExceptionInput("whitespace", ["action" => __FUNCTION__, "field" => "name"]); throw new Db\ExceptionInput("whitespace", ["action" => __FUNCTION__, "field" => "name"]);

View file

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse\Test\Database;
use JKingWeb\Arsse\Data;
use JKingWeb\Arsse\User\Driver as UserDriver;
use Phake;
trait SeriesFolder {
function setUpSeries() {
$data = [
'arsse_folders' => [
'columns' => [
'id' => "int",
'owner' => "str",
'parent' => "int",
'root' => "int",
'name' => "str",
],
/* Layout translates to:
Jane
Politics
John
Technology
Software
Politics
Rocketry
Politics
*/
'rows' => [
[1, "john.doe@example.com", null, null, "Technology"],
[2, "john.doe@example.com", 1, 1, "Software"],
[3, "john.doe@example.com", 1, 1, "Rocketry"],
[4, "jane.doe@example.com", null, null, "Politics"],
[5, "john.doe@example.com", null, null, "Politics"],
[6, "john.doe@example.com", 2, 1, "Politics"],
]
]
];
// merge folder table with base user table
$this->data = array_merge($this->data, $data);
$this->primeDatabase($this->data);
}
function testAddARootFolder() {
$user = "john.doe@example.com";
$this->assertSame(7, Data::$db->folderAdd($user, ['name' => "Entertainment"]));
Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'root', 'name']]);
$state['arsse_folders']['rows'][] = [7, $user, null, null, "Entertainment"];
$this->compareExpectations($state);
}
function testAddADuplicateRootFolder() {
$this->assertException("constraintViolation", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", ['name' => "Politics"]);
}
function testAddANestedFolder() {
$user = "john.doe@example.com";
$this->assertSame(7, Data::$db->folderAdd($user, ['name' => "GNOME", 'parent' => 2]));
Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'root', 'name']]);
$state['arsse_folders']['rows'][] = [7, $user, 2, 1, "GNOME"];
$this->compareExpectations($state);
}
function testAddANestedFolderToAMissingParent() {
$this->assertException("idMissing", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", ['name' => "Sociology", 'parent' => 2112]);
}
function testAddANestedFolderForTheWrongOwner() {
$this->assertException("idMissing", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", ['name' => "Sociology", 'parent' => 4]); // folder ID 4 belongs to Jane
}
function testAddAFolderForAMissingUser() {
$this->assertException("doesNotExist", "User");
Data::$db->folderAdd("john.doe@example.org", ['name' => "Sociology"]);
}
function testAddAFolderWithAMissingName() {
$this->assertException("missing", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", []);
}
function testAddAFolderWithABlankName() {
$this->assertException("missing", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", ['name' => ""]);
}
function testAddAFolderWithAWhitespaceName() {
$this->assertException("whitespace", "Db", "ExceptionInput");
Data::$db->folderAdd("john.doe@example.com", ['name' => " "]);
}
function testAddAFolderWithoutAuthority() {
Phake::when(Data::$user)->authorize->thenReturn(false);
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
Data::$db->folderAdd("john.doe@example.com", ['name' => "Sociology"]);
}
}

View file

@ -71,7 +71,9 @@ trait Setup {
$cols = implode(",", array_keys($info['columns'])); $cols = implode(",", array_keys($info['columns']));
foreach($this->drv->prepare("SELECT $cols from $table")->run() as $num => $row) { foreach($this->drv->prepare("SELECT $cols from $table")->run() as $num => $row) {
$row = array_values($row); $row = array_values($row);
$this->assertSame($expected[$table]['rows'][$num], $row, "Row ".($num+1)." of table $table does not match expectations at array index $num."); $this->assertGreaterThan(0, sizeof($info['rows']), "Expectations contain fewer rows than the database table $table");
$exp = array_shift($info['rows']);
$this->assertSame($exp, $row, "Row ".($num+1)." of table $table does not match expectations at array index $num.");
} }
} }
return true; return true;

View file

@ -10,40 +10,36 @@
beStrictAboutTestSize="true" beStrictAboutTestSize="true"
stopOnError="true"> stopOnError="true">
<testsuites>
<testsuite name="Exceptions"> <testsuite name="Exceptions">
<file>Exception/TestException.php</file> <file>Exception/TestException.php</file>
</testsuite> </testsuite>
<testsuite name="Localization"> <testsuite name="Localization">
<file>Lang/TestLang.php</file> <file>Lang/TestLang.php</file>
<file>Lang/TestLangComplex.php</file> <file>Lang/TestLangComplex.php</file>
<file>Lang/TestLangErrors.php</file> <file>Lang/TestLangErrors.php</file>
</testsuite> </testsuite>
<testsuite name="Configuration loading and saving"> <testsuite name="Configuration loading and saving">
<file>Conf/TestConf.php</file> <file>Conf/TestConf.php</file>
</testsuite> </testsuite>
<testsuite name="User management"> <testsuite name="User management">
<file>User/TestUserMockInternal.php</file> <file>User/TestUserMockInternal.php</file>
<file>User/TestUserMockExternal.php</file> <file>User/TestUserMockExternal.php</file>
<file>User/TestUserInternalDriver.php</file> <file>User/TestUserInternalDriver.php</file>
<file>User/TestAuthorization.php</file> <file>User/TestAuthorization.php</file>
</testsuite> </testsuite>
<testsuite name="SQLite3 database driver"> <testsuite name="SQLite3 database driver">
<file>Db/SQLite3/TestDbResultSQLite3.php</file> <file>Db/SQLite3/TestDbResultSQLite3.php</file>
<file>Db/SQLite3/TestDbStatementSQLite3.php</file> <file>Db/SQLite3/TestDbStatementSQLite3.php</file>
<file>Db/SQLite3/TestDbDriverSQLite3.php</file> <file>Db/SQLite3/TestDbDriverSQLite3.php</file>
<file>Db/SQLite3/TestDbUpdateSQLite3.php</file> <file>Db/SQLite3/TestDbUpdateSQLite3.php</file>
</testsuite> </testsuite>
<testsuite name="SQLite3 database functions"> <testsuite name="SQLite3 database functions">
<file>Db/SQLite3/Database/TestDatabaseUserSQLite3.php</file> <file>Db/SQLite3/Database/TestDatabaseUserSQLite3.php</file>
<file>Db/SQLite3/Database/TestDatabaseFolderSQLite3.php</file>
</testsuite> </testsuite>
<testsuite name="NextCloud News API"> <testsuite name="NextCloud News API">
<file>REST/NextCloudNews/TestNCNVersionDiscovery.php</file> <file>REST/NextCloudNews/TestNCNVersionDiscovery.php</file>
</testsuite> </testsuite>
</testsuites>
</phpunit> </phpunit>