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

Tests for file imports

This commit is contained in:
J. King 2019-05-12 16:33:19 -04:00
parent be5a1fb94f
commit c1e13e6199
2 changed files with 62 additions and 12 deletions

View file

@ -38,9 +38,9 @@ class OPML {
// start a transaction for atomic rollback // start a transaction for atomic rollback
$tr = Arsse::$db->begin(); $tr = Arsse::$db->begin();
// get current state of database // get current state of database
$foldersDb = iterator_to_array(Arsse::$db->folderList(Arsse::$user->id)); $foldersDb = iterator_to_array(Arsse::$db->folderList($user));
$feedsDb = iterator_to_array(Arsse::$db->subscriptionList(Arsse::$user->id)); $feedsDb = iterator_to_array(Arsse::$db->subscriptionList($user));
$tagsDb = iterator_to_array(Arsse::$db->tagList(Arsse::$user->id)); $tagsDb = iterator_to_array(Arsse::$db->tagList($user));
// reconcile folders // reconcile folders
$folderMap = [0 => 0]; $folderMap = [0 => 0];
foreach ($folders as $id => $f) { foreach ($folders as $id => $f) {
@ -54,7 +54,7 @@ class OPML {
} }
if (!isset($folderMap[$id])) { if (!isset($folderMap[$id])) {
// if no existing folder exists, add one // if no existing folder exists, add one
$folderMap[$id] = Arsse::$db->folderAdd(Arsse::$user->id, ['name' => $f['name'], 'parent' -> $parent]); $folderMap[$id] = Arsse::$db->folderAdd($user, ['name' => $f['name'], 'parent' -> $parent]);
} }
} }
// process newsfeed subscriptions // process newsfeed subscriptions
@ -74,11 +74,11 @@ class OPML {
} }
if (!$found) { if (!$found) {
// if no subscription exists, add one // if no subscription exists, add one
$feedMap[$f['id']] = Arsse::$db->subscriptionAdd(Arsse::$user->id, $f['url']); $feedMap[$f['id']] = Arsse::$db->subscriptionAdd($user, $f['url']);
} }
if (!$found || $replace) { if (!$found || $replace) {
// set the subscription's properties, if this is a new feed or we're doing a full replacement // set the subscription's properties, if this is a new feed or we're doing a full replacement
Arsse::$db->subscriptionPropertiesSet(Arsse::$user->id, $feedMap[$f['id']], ['title' => $title, 'folder' => $folder]); Arsse::$db->subscriptionPropertiesSet($user, $feedMap[$f['id']], ['title' => $title, 'folder' => $folder]);
// compile the set of used tags, if this is a new feed or we're doing a full replacement // compile the set of used tags, if this is a new feed or we're doing a full replacement
foreach ($f['tags'] as $t) { foreach ($f['tags'] as $t) {
if (!strlen(trim($t))) { if (!strlen(trim($t))) {
@ -106,29 +106,29 @@ class OPML {
} }
if (!$found) { if (!$found) {
// add the tag if it wasn't found // add the tag if it wasn't found
Arsse::$db->tagAdd(Arsse::$user->id, ['name' => $tag]); Arsse::$db->tagAdd($user, ['name' => $tag]);
} }
Arsse::$db->tagSubscriptionsSet(Arsse::$user->id, $tag, $subs, $mode, true); Arsse::$db->tagSubscriptionsSet($user, $tag, $subs, $mode, true);
} }
// finally, if we're performing a replacement, delete any subscriptions, folders, or tags which were not present in the import // finally, if we're performing a replacement, delete any subscriptions, folders, or tags which were not present in the import
if ($replace) { if ($replace) {
foreach (array_diff(array_column($feedsDb, "id"), $feedMap) as $id) { foreach (array_diff(array_column($feedsDb, "id"), $feedMap) as $id) {
try { try {
Arsse::$db->subscriptionRemove(Arsse::$user->id, $id); Arsse::$db->subscriptionRemove($user, $id);
} catch (InputException $e) { } catch (InputException $e) {
// ignore errors // ignore errors
} }
} }
foreach (array_diff(array_column($foldersDb, "id"), $folderMap) as $id) { foreach (array_diff(array_column($foldersDb, "id"), $folderMap) as $id) {
try { try {
Arsse::$db->folderRemove(Arsse::$user->id, $id); Arsse::$db->folderRemove($user, $id);
} catch (InputException $e) { } catch (InputException $e) {
// ignore errors // ignore errors
} }
} }
foreach (array_diff(array_column($tagsDb, "name"), array_keys($tagMap)) as $id) { foreach (array_diff(array_column($tagsDb, "name"), array_keys($tagMap)) as $id) {
try { try {
Arsse::$db->tagRemove(Arsse::$user->id, $id, true); Arsse::$db->tagRemove($user, $id, true);
} catch (InputException $e) { } catch (InputException $e) {
// ignore errors // ignore errors
} }
@ -287,7 +287,7 @@ class OPML {
return true; return true;
} }
public function imortFile(string $file, string $user, bool $flat = false, bool $replace): bool { public function importFile(string $file, string $user, bool $flat = false, bool $replace): bool {
$data = @file_get_contents($file); $data = @file_get_contents($file);
if ($data === false) { if ($data === false) {
// if it fails throw an exception // if it fails throw an exception

View file

@ -21,16 +21,20 @@ class TestOPMLFile extends \JKingWeb\Arsse\Test\AbstractTest {
// create a mock OPML processor with stubbed underlying import/export routines // create a mock OPML processor with stubbed underlying import/export routines
$this->opml = \Phake::partialMock(OPML::class); $this->opml = \Phake::partialMock(OPML::class);
\Phake::when($this->opml)->export->thenReturn("OPML_FILE"); \Phake::when($this->opml)->export->thenReturn("OPML_FILE");
\Phake::when($this->opml)->import->thenReturn(true);
$this->vfs = vfsStream::setup("root", null, [ $this->vfs = vfsStream::setup("root", null, [
'exportGoodFile' => "", 'exportGoodFile' => "",
'exportGoodDir' => [], 'exportGoodDir' => [],
'exportBadFile' => "", 'exportBadFile' => "",
'exportBadDir' => [], 'exportBadDir' => [],
'importGoodFile' => "<opml/>",
'importBadFile' => "",
]); ]);
$this->path = $this->vfs->url()."/"; $this->path = $this->vfs->url()."/";
// make the "bad" entries inaccessible // make the "bad" entries inaccessible
chmod($this->path."exportBadFile", 0000); chmod($this->path."exportBadFile", 0000);
chmod($this->path."exportBadDir", 0000); chmod($this->path."exportBadDir", 0000);
chmod($this->path."importBadFile", 0000);
} }
public function tearDown() { public function tearDown() {
@ -78,4 +82,50 @@ class TestOPMLFile extends \JKingWeb\Arsse\Test\AbstractTest {
["exportBadDir/file", "jane.doe@example.com", false, $createException], ["exportBadDir/file", "jane.doe@example.com", false, $createException],
]; ];
} }
/** @dataProvider provideFileImports */
public function testImportFromOpmlFile(string $file, string $user, bool $flat, bool $replace, $exp) {
$path = $this->path.$file;
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
$this->opml->importFile($path, $user, $flat, $replace);
} else {
$this->assertSame($exp, $this->opml->importFile($path, $user, $flat, $replace));
}
} finally {
\Phake::verify($this->opml, \Phake::times((int) ($exp === true)))->import($user, "<opml/>", $flat, $replace);
}
}
public function provideFileImports() {
$missingException = new Exception("fileMissing");
$permissionException = new Exception("fileUnreadable");
return [
["importGoodFile", "john.doe@example.com", true, true, true],
["importBadFile", "john.doe@example.com", true, true, $permissionException],
["importNonFile", "john.doe@example.com", true, true, $missingException],
["importGoodFile", "john.doe@example.com", true, false, true],
["importBadFile", "john.doe@example.com", true, false, $permissionException],
["importNonFile", "john.doe@example.com", true, false, $missingException],
["importGoodFile", "john.doe@example.com", false, true, true],
["importBadFile", "john.doe@example.com", false, true, $permissionException],
["importNonFile", "john.doe@example.com", false, true, $missingException],
["importGoodFile", "john.doe@example.com", false, false, true],
["importBadFile", "john.doe@example.com", false, false, $permissionException],
["importNonFile", "john.doe@example.com", false, false, $missingException],
["importGoodFile", "jane.doe@example.com", true, true, true],
["importBadFile", "jane.doe@example.com", true, true, $permissionException],
["importNonFile", "jane.doe@example.com", true, true, $missingException],
["importGoodFile", "jane.doe@example.com", true, false, true],
["importBadFile", "jane.doe@example.com", true, false, $permissionException],
["importNonFile", "jane.doe@example.com", true, false, $missingException],
["importGoodFile", "jane.doe@example.com", false, true, true],
["importBadFile", "jane.doe@example.com", false, true, $permissionException],
["importNonFile", "jane.doe@example.com", false, true, $missingException],
["importGoodFile", "jane.doe@example.com", false, false, true],
["importBadFile", "jane.doe@example.com", false, false, $permissionException],
["importNonFile", "jane.doe@example.com", false, false, $missingException],
];
}
} }