Browse Source

Properly handle adding a folder

Also added test file for now
microsub
J. King 7 years ago
parent
commit
8c268581fb
  1. 1
      .gitignore
  2. 9
      lib/Database.php
  3. 4
      sql/SQLite3/0.sql
  4. 24
      tests/test.php

1
.gitignore

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

9
lib/Database.php

@ -341,12 +341,19 @@ class Database {
} 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
$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]);
} 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
$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();
}
}

4
sql/SQLite3/0.sql

@ -53,8 +53,8 @@ create table newssync_subscriptions(
create table newssync_folders(
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
parent integer not null default 0, -- parent folder id
root integer not null default 0, -- first-level folder (ownCloud folder)
parent integer default null, -- parent folder id
root integer default null, -- first-level folder (ownCloud folder)
name TEXT not null, -- folder name
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

24
tests/test.php

@ -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");
Loading…
Cancel
Save