bookmarks/tests/HtmlImportExportTest.php

178 lines
5.4 KiB
PHP
Raw Normal View History

2019-10-23 14:37:59 +00:00
<?php
namespace OCA\Bookmarks\Tests;
use OCA\Bookmarks\Db;
2020-03-20 13:51:03 +00:00
use OCA\Bookmarks\Exception\AlreadyExistsError;
use OCA\Bookmarks\Exception\HtmlParseError;
2019-10-23 14:37:59 +00:00
use OCA\Bookmarks\Exception\UnauthorizedAccessError;
2019-10-25 16:32:14 +00:00
use OCA\Bookmarks\Exception\UrlParseError;
2020-03-20 13:51:03 +00:00
use OCA\Bookmarks\Exception\UserLimitExceededError;
2019-10-23 14:37:59 +00:00
use OCA\Bookmarks\Service;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\QueryException;
2020-10-10 15:58:36 +00:00
use OCP\IUserManager;
2019-10-23 14:37:59 +00:00
2020-03-20 13:51:03 +00:00
class HtmlImportExportTest extends TestCase {
2019-10-23 14:37:59 +00:00
/**
* @var Db\BookmarkMapper
*/
private $bookmarkMapper;
/**
* @var Db\TagMapper
*/
private $tagMapper;
/**
* @var Db\FolderMapper
*/
private $folderMapper;
/**
* @var int
*/
private $userId;
/**
* @var Service\HtmlImporter
*/
protected $htmlImporter;
2019-10-25 14:48:40 +00:00
/**
* @var \stdClass
*/
protected $htmlExporter;
2020-03-20 13:51:03 +00:00
/**
* @var \OC\User\Manager
*/
private $userManager;
/**
* @var string
*/
private $user;
/**
* @var Db\TreeMapper
*/
private $treeMapper;
2019-10-23 14:37:59 +00:00
/**
* @throws QueryException
*/
protected function setUp(): void {
parent::setUp();
2020-03-20 13:51:03 +00:00
$this->cleanUp();
2019-10-26 12:10:49 +00:00
2020-10-10 15:58:36 +00:00
$this->bookmarkMapper = \OC::$server->get(Db\BookmarkMapper::class);
$this->tagMapper = \OC::$server->get(Db\TagMapper::class);
$this->folderMapper = \OC::$server->get(Db\FolderMapper::class);
$this->treeMapper = \OC::$server->get(Db\TreeMapper::class);
$this->htmlImporter = \OC::$server->get(Service\HtmlImporter::class);
$this->htmlExporter = \OC::$server->get(Service\HtmlExporter::class);
2019-10-26 12:10:49 +00:00
2020-10-11 12:48:44 +00:00
$this->userManager = \OC::$server->get(IUserManager::class);
2019-10-26 12:10:49 +00:00
$this->user = 'test';
if (!$this->userManager->userExists($this->user)) {
$this->userManager->createUser($this->user, 'password');
}
$this->userId = $this->userManager->get($this->user)->getUID();
2019-10-23 14:37:59 +00:00
}
/**
* @dataProvider importProvider
* @param string $file
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
2020-03-20 13:51:03 +00:00
* @throws UnauthorizedAccessError
* @throws AlreadyExistsError
* @throws UserLimitExceededError
* @throws HtmlParseError
2019-10-23 14:37:59 +00:00
*/
2020-03-20 13:51:03 +00:00
public function testImportFile(string $file): void {
2019-10-23 14:37:59 +00:00
$result = $this->htmlImporter->importFile($this->userId, $file);
2020-03-20 13:51:03 +00:00
$rootFolder = $this->folderMapper->findRootFolder($this->userId);
$imported = $this->treeMapper->getChildrenOrder($rootFolder->getId());
$this->assertCount(6, $imported);
2019-10-24 16:14:28 +00:00
2020-03-20 13:51:03 +00:00
$this->assertCount(2, $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $result['imported'][0]['id']));
$this->assertCount(2, $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $result['imported'][1]['id']));
$this->assertCount(2, $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $result['imported'][2]['id']));
$this->assertCount(2, $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $result['imported'][3]['id']));
$this->assertCount(1, $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $result['imported'][4]['id']));
2019-10-24 16:14:28 +00:00
2020-03-20 13:51:03 +00:00
/**
* @var $firstBookmark Db\Bookmark
*/
2019-10-24 16:14:28 +00:00
$firstBookmark = $this->bookmarkMapper->find($result['imported'][0]['children'][0]['id']);
$this->assertSame('Title 0', $firstBookmark->getTitle());
$this->assertSame('http://url0.net/', $firstBookmark->getUrl());
2020-03-20 13:51:03 +00:00
$this->assertSame('This is a description.', $firstBookmark->getDescription());
2019-10-24 16:14:28 +00:00
$this->assertEquals(['tag0'], $this->tagMapper->findByBookmark($firstBookmark->getId()));
2020-03-20 13:51:03 +00:00
$this->assertEquals(1231231234, $firstBookmark->getAdded());
2019-10-23 14:37:59 +00:00
}
2019-10-25 14:48:40 +00:00
/**
2019-10-25 15:33:27 +00:00
* @dataProvider exportProvider
2019-10-26 12:10:49 +00:00
* @param array $bookmarks
2019-10-25 16:32:14 +00:00
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
2020-03-20 13:51:03 +00:00
* @throws AlreadyExistsError
* @throws UserLimitExceededError
2019-10-25 14:48:40 +00:00
*/
2020-03-20 13:51:03 +00:00
public function testExport(...$bookmarks): void {
$rootFolder = new Db\Folder();
$rootFolder->setTitle('Root');
$rootFolder->setUserId($this->userId);
$rootFolder = $this->folderMapper->insert($rootFolder);
2019-10-25 15:33:27 +00:00
// Set up database
2020-03-20 13:51:03 +00:00
for ($i = 0; $i < 4; $i++) {
2019-10-25 15:33:27 +00:00
$f = new Db\Folder();
$f->setTitle($i);
2019-10-25 17:46:58 +00:00
$f->setUserId($this->userId);
2019-10-25 15:33:27 +00:00
$f = $this->folderMapper->insert($f);
2020-03-20 13:51:03 +00:00
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $f->getId(), $rootFolder->getId());
2019-10-25 16:32:14 +00:00
$b = array_shift($bookmarks);
2019-10-25 15:33:27 +00:00
$b->setUserId($this->userId);
$b = $this->bookmarkMapper->insertOrUpdate($b);
2020-03-20 13:51:03 +00:00
$this->treeMapper->addToFolders(Db\TreeMapper::TYPE_BOOKMARK, $b->getId(), [$f->getId()]);
2019-10-25 15:33:27 +00:00
}
2020-03-20 13:51:03 +00:00
$exported = $this->htmlExporter->exportFolder($this->userId, $rootFolder->getId());
2019-10-25 14:48:40 +00:00
2020-03-20 13:51:03 +00:00
$rootFolders = $this->treeMapper->findChildren(Db\TreeMapper::TYPE_FOLDER, $rootFolder->getId());
2019-10-25 16:32:14 +00:00
$this->assertCount(4, $rootFolders);
2020-03-20 13:51:03 +00:00
foreach ($rootFolders as $rootFolder) {
foreach ($this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $rootFolder->getId()) as $bookmark) {
2020-03-22 17:33:05 +00:00
$this->assertContains($bookmark->getUrl(), $exported);
2019-10-25 14:48:40 +00:00
}
}
}
2020-03-20 13:51:03 +00:00
public function importProvider(): array {
2019-10-23 14:37:59 +00:00
return [
[
2020-03-20 13:51:03 +00:00
__DIR__ . '/res/import.file',
],
2019-10-23 14:37:59 +00:00
];
}
2019-10-25 15:33:27 +00:00
2020-03-20 13:51:03 +00:00
public function exportProvider(): array {
2019-10-25 15:33:27 +00:00
return [
2020-03-20 13:51:03 +00:00
array_map(function ($props) {
2019-10-25 16:32:14 +00:00
return Db\Bookmark::fromArray($props);
2022-07-11 12:58:56 +00:00
}, [
['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine'],
['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud'],
['url' => 'https://php.net/'],
['url' => 'https://de.wikipedia.org/wiki/%C3%9C'],
['url' => 'https://github.com/nextcloud/bookmarks/projects/1'],
]),
2019-10-25 15:33:27 +00:00
];
}
2019-10-23 14:37:59 +00:00
}