1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Properly handle adding a folder

Also added test file for now
This commit is contained in:
J. King 2017-03-09 22:41:11 -05:00
parent 18c8076a0f
commit 8c268581fb
4 changed files with 34 additions and 4 deletions

1
.gitignore vendored
View file

@ -3,7 +3,6 @@ vendor
#temp files #temp files
cache/* cache/*
test.php
newssync.db* newssync.db*
# Windows image file caches # Windows image file caches

View file

@ -341,12 +341,19 @@ class Database {
} else { } else {
// if a parent is specified, make sure it exists and belongs to the user; get its root (first-level) folder if it's a nested folder // if a parent is specified, make sure it exists and belongs to the user; get its root (first-level) folder if it's a nested folder
$p = $this->db->prepare("SELECT id,root from newssync_folders where owner is ? and id is ?", "str", "int")->run($user, $parent)->getRow(); $p = $this->db->prepare("SELECT id,root from newssync_folders where owner is ? and id is ?", "str", "int")->run($user, $parent)->getRow();
if($p===null) { if(!$p) {
throw new Db\ExceptionInput("idMissing", ["action" => __FUNCTION__, "field" => "parent", 'id' => $parent]); throw new Db\ExceptionInput("idMissing", ["action" => __FUNCTION__, "field" => "parent", 'id' => $parent]);
} else { } else {
// if the parent does not have a root specified (because it is a first-level folder) use the parent ID as the root ID // if the parent does not have a root specified (because it is a first-level folder) use the parent ID as the root ID
$root = $p['root']===null ? $parent : $p['root']; $root = $p['root']===null ? $parent : $p['root'];
} }
} }
// check if a folder by the same name already exists, because nulls are wonky in SQL
// FIXME: How should folder name be compared? Should a Unicode normalization be applied before comparison and insertion?
if($this->db->prepare("SELECT count(*) from newssync_folders where owner is ? and parent is ? and name is ?", "str", "int", "str")->run($user, $parent, $data['name'])->getValue() > 0) {
throw new Db\ExceptionInput("constraintViolation"); // FIXME: There needs to be a practical message here
}
// actually perform the insert (!)
return $this->db->prepare("INSERT INTO newssync_folders(owner,parent,root,name) values(?,?,?,?)", "str", "int", "int", "str")->run($user, $parent, $root, $data['name'])->lastId();
} }
} }

View file

@ -53,8 +53,8 @@ create table newssync_subscriptions(
create table newssync_folders( create table newssync_folders(
id integer primary key not null, -- sequence number id integer primary key not null, -- sequence number
owner TEXT not null references newssync_users(id) on delete cascade on update cascade, -- owner of folder owner TEXT not null references newssync_users(id) on delete cascade on update cascade, -- owner of folder
parent integer not null default 0, -- parent folder id parent integer default null, -- parent folder id
root integer not null default 0, -- first-level folder (ownCloud folder) root integer default null, -- first-level folder (ownCloud folder)
name TEXT not null, -- folder name name TEXT not null, -- folder name
modified datetime not null default CURRENT_TIMESTAMP, -- modified datetime not null default CURRENT_TIMESTAMP, --
unique(owner,name,parent) -- cannot have multiple folders with the same name under the same parent for the same owner unique(owner,name,parent) -- cannot have multiple folders with the same name under the same parent for the same owner

24
tests/test.php Normal file
View file

@ -0,0 +1,24 @@
<?php
namespace JKingWeb\NewsSync;
const INSTALL = true;
require_once "../bootstrap.php";
$user = "john.doe@example.com";
$pass = "secret";
$_SERVER['PHP_AUTH_USER'] = $user;
$_SERVER['PHP_AUTH_PW'] = $pass;
$conf = new Conf();
$conf->dbSQLite3File = ":memory:";
$conf->userAuthPreferHTTP = true;
$data = new RuntimeData($conf);
$data->db->schemaUpdate();
$data->user->add($user, $pass);
$data->user->auth();
$data->user->authorizationEnabled(false);
$data->user->rightsSet($user, User\Driver::RIGHTS_GLOBAL_ADMIN);
$data->user->authorizationEnabled(true);
$data->db->folderAdd($user, ['name' => 'ook']);
$data->db->subscriptionAdd($user, "http://www.tbray.org/ongoing/ongoing.atom");