2017-03-31 19:27:59 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\Test\Database;
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2017-03-31 19:27:59 +00:00
|
|
|
use Phake;
|
|
|
|
|
|
|
|
trait SeriesFolder {
|
2017-06-18 16:48:29 +00:00
|
|
|
protected $data = [
|
|
|
|
'arsse_users' => [
|
|
|
|
'columns' => [
|
|
|
|
'id' => 'str',
|
|
|
|
'password' => 'str',
|
|
|
|
'name' => 'str',
|
2017-06-18 16:24:19 +00:00
|
|
|
],
|
2017-06-18 16:48:29 +00:00
|
|
|
'rows' => [
|
2017-08-18 02:36:15 +00:00
|
|
|
["jane.doe@example.com", "", "Jane Doe"],
|
|
|
|
["john.doe@example.com", "", "John Doe"],
|
2017-06-18 16:24:19 +00:00
|
|
|
],
|
2017-06-18 16:48:29 +00:00
|
|
|
],
|
|
|
|
'arsse_folders' => [
|
|
|
|
'columns' => [
|
|
|
|
'id' => "int",
|
|
|
|
'owner' => "str",
|
|
|
|
'parent' => "int",
|
|
|
|
'name' => "str",
|
|
|
|
],
|
|
|
|
/* Layout translates to:
|
|
|
|
Jane
|
|
|
|
Politics
|
|
|
|
John
|
|
|
|
Technology
|
|
|
|
Software
|
|
|
|
Politics
|
|
|
|
Rocketry
|
|
|
|
Politics
|
|
|
|
*/
|
|
|
|
'rows' => [
|
|
|
|
[1, "john.doe@example.com", null, "Technology"],
|
|
|
|
[2, "john.doe@example.com", 1, "Software"],
|
|
|
|
[3, "john.doe@example.com", 1, "Rocketry"],
|
|
|
|
[4, "jane.doe@example.com", null, "Politics"],
|
|
|
|
[5, "john.doe@example.com", null, "Politics"],
|
|
|
|
[6, "john.doe@example.com", 2, "Politics"],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
2017-03-31 19:27:59 +00:00
|
|
|
|
|
|
|
function testAddARootFolder() {
|
|
|
|
$user = "john.doe@example.com";
|
2017-05-11 01:21:23 +00:00
|
|
|
$folderID = $this->nextID("arsse_folders");
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($folderID, Arsse::$db->folderAdd($user, ['name' => "Entertainment"]));
|
|
|
|
Phake::verify(Arsse::$user)->authorize($user, "folderAdd");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
2017-05-11 01:21:23 +00:00
|
|
|
$state['arsse_folders']['rows'][] = [$folderID, $user, null, "Entertainment"];
|
2017-03-31 19:27:59 +00:00
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddADuplicateRootFolder() {
|
|
|
|
$this->assertException("constraintViolation", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => "Politics"]);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddANestedFolder() {
|
|
|
|
$user = "john.doe@example.com";
|
2017-05-11 01:21:23 +00:00
|
|
|
$folderID = $this->nextID("arsse_folders");
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($folderID, Arsse::$db->folderAdd($user, ['name' => "GNOME", 'parent' => 2]));
|
|
|
|
Phake::verify(Arsse::$user)->authorize($user, "folderAdd");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
2017-05-11 01:21:23 +00:00
|
|
|
$state['arsse_folders']['rows'][] = [$folderID, $user, 2, "GNOME"];
|
2017-03-31 19:27:59 +00:00
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddANestedFolderToAMissingParent() {
|
|
|
|
$this->assertException("idMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => "Sociology", 'parent' => 2112]);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddANestedFolderForTheWrongOwner() {
|
|
|
|
$this->assertException("idMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => "Sociology", 'parent' => 4]); // folder ID 4 belongs to Jane
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddAFolderWithAMissingName() {
|
|
|
|
$this->assertException("missing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", []);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddAFolderWithABlankName() {
|
|
|
|
$this->assertException("missing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => ""]);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddAFolderWithAWhitespaceName() {
|
|
|
|
$this->assertException("whitespace", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => " "]);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAddAFolderWithoutAuthority() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(false);
|
2017-03-31 19:27:59 +00:00
|
|
|
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderAdd("john.doe@example.com", ['name' => "Sociology"]);
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|
2017-03-31 21:42:28 +00:00
|
|
|
|
|
|
|
function testListRootFolders() {
|
|
|
|
$exp = [
|
|
|
|
['id' => 5, 'name' => "Politics", 'parent' => null],
|
|
|
|
['id' => 1, 'name' => "Technology", 'parent' => null],
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", null, false)->getAll());
|
2017-03-31 21:42:28 +00:00
|
|
|
$exp = [
|
|
|
|
['id' => 4, 'name' => "Politics", 'parent' => null],
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("jane.doe@example.com", null, false)->getAll());
|
2017-03-31 21:42:28 +00:00
|
|
|
$exp = [];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("admin@example.net", null, false)->getAll());
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderList");
|
|
|
|
Phake::verify(Arsse::$user)->authorize("jane.doe@example.com", "folderList");
|
|
|
|
Phake::verify(Arsse::$user)->authorize("admin@example.net", "folderList");
|
2017-03-31 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", null, true)->getAll());
|
2017-03-31 21:42:28 +00:00
|
|
|
$exp = [
|
|
|
|
['id' => 6, 'name' => "Politics", 'parent' => 2],
|
|
|
|
['id' => 3, 'name' => "Rocketry", 'parent' => 1],
|
|
|
|
['id' => 2, 'name' => "Software", 'parent' => 1],
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", 1, true)->getAll());
|
2017-03-31 21:42:28 +00:00
|
|
|
$exp = [];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertSame($exp, Arsse::$db->folderList("jane.doe@example.com", 4, true)->getAll());
|
|
|
|
Phake::verify(Arsse::$user, Phake::times(2))->authorize("john.doe@example.com", "folderList");
|
|
|
|
Phake::verify(Arsse::$user)->authorize("jane.doe@example.com", "folderList");
|
2017-03-31 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testListFoldersOfAMissingParent() {
|
|
|
|
$this->assertException("idMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderList("john.doe@example.com", 2112);
|
2017-03-31 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testListFoldersOfTheWrongOwner() {
|
|
|
|
$this->assertException("idMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderList("john.doe@example.com", 4); // folder ID 4 belongs to Jane
|
2017-03-31 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testListFoldersWithoutAuthority() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(false);
|
2017-03-31 21:42:28 +00:00
|
|
|
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderList("john.doe@example.com");
|
2017-03-31 21:42:28 +00:00
|
|
|
}
|
2017-03-31 22:48:24 +00:00
|
|
|
|
|
|
|
function testRemoveAFolder() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertTrue(Arsse::$db->folderRemove("john.doe@example.com", 6));
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderRemove");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
2017-03-31 22:48:24 +00:00
|
|
|
array_pop($state['arsse_folders']['rows']);
|
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveAFolderTree() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertTrue(Arsse::$db->folderRemove("john.doe@example.com", 1));
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderRemove");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
2017-03-31 22:48:24 +00:00
|
|
|
foreach([0,1,2,5] as $index) {
|
|
|
|
unset($state['arsse_folders']['rows'][$index]);
|
|
|
|
}
|
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveAMissingFolder() {
|
2017-05-21 14:10:36 +00:00
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderRemove("john.doe@example.com", 2112);
|
2017-03-31 22:48:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 14:27:26 +00:00
|
|
|
function testRemoveAFolderOfTheWrongOwner() {
|
2017-05-21 14:10:36 +00:00
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderRemove("john.doe@example.com", 4); // folder ID 4 belongs to Jane
|
2017-03-31 22:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveAFolderWithoutAuthority() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(false);
|
2017-03-31 22:48:24 +00:00
|
|
|
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderRemove("john.doe@example.com", 1);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetThePropertiesOfAFolder() {
|
|
|
|
$exp = [
|
|
|
|
'id' => 6,
|
|
|
|
'name' => "Politics",
|
|
|
|
'parent' => 2,
|
|
|
|
];
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertArraySubset($exp, Arsse::$db->folderPropertiesGet("john.doe@example.com", 6));
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderPropertiesGet");
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetThePropertiesOfAMissingFolder() {
|
2017-05-21 14:10:36 +00:00
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesGet("john.doe@example.com", 2112);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetThePropertiesOfAFolderOfTheWrongOwner() {
|
2017-05-21 14:10:36 +00:00
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesGet("john.doe@example.com", 4); // folder ID 4 belongs to Jane
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetThePropertiesOfAFolderWithoutAuthority() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(false);
|
2017-04-01 14:27:26 +00:00
|
|
|
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesGet("john.doe@example.com", 1);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRenameAFolder() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertTrue(Arsse::$db->folderPropertiesSet("john.doe@example.com", 6, ['name' => "Opinion"]));
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderPropertiesSet");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
|
|
|
$state['arsse_folders']['rows'][5][3] = "Opinion";
|
2017-04-01 14:27:26 +00:00
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
2017-07-23 03:08:08 +00:00
|
|
|
function testRenameAFolderToTheEmptyString() {
|
|
|
|
$this->assertException("missing", "Db", "ExceptionInput");
|
|
|
|
$this->assertTrue(Arsse::$db->folderPropertiesSet("john.doe@example.com", 6, ['name' => ""]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRenameAFolderToWhitespaceOnly() {
|
|
|
|
$this->assertException("whitespace", "Db", "ExceptionInput");
|
|
|
|
$this->assertTrue(Arsse::$db->folderPropertiesSet("john.doe@example.com", 6, ['name' => " "]));
|
|
|
|
}
|
|
|
|
|
2017-04-01 14:27:26 +00:00
|
|
|
function testMoveAFolder() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$this->assertTrue(Arsse::$db->folderPropertiesSet("john.doe@example.com", 6, ['parent' => 5]));
|
|
|
|
Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderPropertiesSet");
|
2017-04-01 18:49:31 +00:00
|
|
|
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
|
2017-04-01 14:27:26 +00:00
|
|
|
$state['arsse_folders']['rows'][5][2] = 5; // parent should have changed
|
|
|
|
$this->compareExpectations($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMoveAFolderToItsDescendant() {
|
|
|
|
$this->assertException("circularDependence", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 1, ['parent' => 3]);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testMoveAFolderToItself() {
|
|
|
|
$this->assertException("circularDependence", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 1, ['parent' => 1]);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testMoveAFolderToAMissingParent() {
|
|
|
|
$this->assertException("idMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 1, ['parent' => 2112]);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCauseAFolderCollision() {
|
|
|
|
$this->assertException("constraintViolation", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 6, ['parent' => null]);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSetThePropertiesOfAMissingFolder() {
|
2017-05-21 14:10:36 +00:00
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 2112, ['parent' => null]);
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 14:10:36 +00:00
|
|
|
function testSetThePropertiesOfAFolderForTheWrongOwner() {
|
|
|
|
$this->assertException("subjectMissing", "Db", "ExceptionInput");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 4, ['parent' => null]); // folder ID 4 belongs to Jane
|
2017-04-01 14:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSetThePropertiesOfAFolderWithoutAuthority() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(false);
|
2017-04-01 14:27:26 +00:00
|
|
|
$this->assertException("notAuthorized", "User", "ExceptionAuthz");
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db->folderPropertiesSet("john.doe@example.com", 1, ['parent' => null]);
|
2017-03-31 22:48:24 +00:00
|
|
|
}
|
2017-03-31 19:27:59 +00:00
|
|
|
}
|