From 8abddeab4f541883721d912f97dec07bffdfc6b8 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sat, 14 Nov 2020 00:09:38 +0100 Subject: Remove usage of old Folder code Signed-off-by: Sean Molenaar --- CHANGELOG.md | 3 + composer.lock | 172 ++++++- lib/Controller/FeedController.php | 22 +- lib/Controller/FolderController.php | 61 ++- lib/Cron/UpdaterJob.php | 2 +- lib/Db/FolderMapper.php | 143 ------ lib/Db/FolderMapperV2.php | 14 +- lib/Hooks/UserDeleteHook.php | 4 +- lib/Service/FolderService.php | 227 ---------- lib/Service/FolderServiceV2.php | 139 +++++- lib/Service/Service.php | 4 +- lib/Service/StatusService.php | 16 +- lib/Service/UpdaterService.php | 4 +- tests/Integration/Db/ItemMapperTest.php | 32 -- tests/Integration/Fixtures/FeedFixture.php | 47 +- tests/Integration/Fixtures/Fixture.php | 3 +- tests/Integration/Fixtures/FolderFixture.php | 20 +- tests/Integration/Fixtures/ItemFixture.php | 35 +- tests/Integration/IntegrationTest.php | 25 +- tests/Unit/Controller/FeedControllerTest.php | 151 ++++--- tests/Unit/Controller/FolderApiControllerTest.php | 1 - tests/Unit/Controller/FolderControllerTest.php | 202 +++------ tests/Unit/Db/FolderMapperTest.php | 519 +++++++++------------- tests/Unit/Db/MapperTestUtility.php | 19 + tests/Unit/Service/FolderServiceTest.php | 320 ++++++------- tests/Unit/Service/StatusServiceTest.php | 65 ++- 26 files changed, 1016 insertions(+), 1234 deletions(-) delete mode 100644 lib/Db/FolderMapper.php delete mode 100644 lib/Service/FolderService.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 815059c41..a76ccf06f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. ## Unreleased +## Changed +- Remove outdated folder DB code + ## 15.1.0 ### Changed diff --git a/composer.lock b/composer.lock index d71dc2448..67a3294a9 100644 --- a/composer.lock +++ b/composer.lock @@ -57,6 +57,10 @@ "html", "readability" ], + "support": { + "issues": "https://github.com/andreskrey/readability.php/issues", + "source": "https://github.com/andreskrey/readability.php/tree/v2.1.0" + }, "time": "2019-07-22T21:42:25+00:00" }, { @@ -110,6 +114,11 @@ "finder", "icon" ], + "support": { + "issues": "https://github.com/ArthurHoaro/favicon/issues", + "source": "https://github.com/ArthurHoaro/favicon/tree/v1.3.1", + "sources": "https://github.com/ArthurHoaro/favicon" + }, "time": "2020-10-14T09:32:44+00:00" }, { @@ -173,6 +182,10 @@ "news", "rss" ], + "support": { + "issues": "https://github.com/alexdebril/feed-io/issues", + "source": "https://github.com/alexdebril/feed-io/tree/v4.7.10" + }, "funding": [ { "url": "https://github.com/alexdebril", @@ -229,6 +242,10 @@ "keywords": [ "html" ], + "support": { + "issues": "https://github.com/ezyang/htmlpurifier/issues", + "source": "https://github.com/ezyang/htmlpurifier/tree/master" + }, "time": "2020-06-29T00:56:53+00:00" }, { @@ -293,6 +310,10 @@ "uri", "url" ], + "support": { + "issues": "https://pear.php.net/bugs/search.php?cmd=display&package_name[]=Net_URL2", + "source": "https://github.com/pear/Net_URL2" + }, "time": "2017-08-25T06:16:11+00:00" }, { @@ -340,6 +361,9 @@ "psr", "psr-3" ], + "support": { + "source": "https://github.com/php-fig/log/tree/1.1.3" + }, "time": "2020-03-23T09:12:05+00:00" }, { @@ -390,6 +414,10 @@ "path", "system" ], + "support": { + "issues": "https://github.com/Riimu/Kit-PathJoin/issues", + "source": "https://github.com/Riimu/Kit-PathJoin/tree/master" + }, "time": "2017-07-09T14:41:04+00:00" }, { @@ -441,6 +469,11 @@ "phpcs", "standards" ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, "time": "2020-10-23T02:01:07+00:00" } ], @@ -494,6 +527,10 @@ "constructor", "instantiate" ], + "support": { + "issues": "https://github.com/doctrine/instantiator/issues", + "source": "https://github.com/doctrine/instantiator/tree/1.4.0" + }, "funding": [ { "url": "https://www.doctrine-project.org/sponsorship.html", @@ -512,16 +549,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.10.1", + "version": "1.10.2", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "969b211f9a51aa1f6c01d1d2aef56d3bd91598e5" + "reference": "776f831124e9c62e1a2c601ecc52e776d8bb7220" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/969b211f9a51aa1f6c01d1d2aef56d3bd91598e5", - "reference": "969b211f9a51aa1f6c01d1d2aef56d3bd91598e5", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/776f831124e9c62e1a2c601ecc52e776d8bb7220", + "reference": "776f831124e9c62e1a2c601ecc52e776d8bb7220", "shasum": "" }, "require": { @@ -556,13 +593,17 @@ "object", "object graph" ], + "support": { + "issues": "https://github.com/myclabs/DeepCopy/issues", + "source": "https://github.com/myclabs/DeepCopy/tree/1.10.2" + }, "funding": [ { "url": "https://tidelift.com/funding/github/packagist/myclabs/deep-copy", "type": "tidelift" } ], - "time": "2020-06-29T13:22:24+00:00" + "time": "2020-11-13T09:40:50+00:00" }, { "name": "phar-io/manifest", @@ -617,6 +658,10 @@ } ], "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "support": { + "issues": "https://github.com/phar-io/manifest/issues", + "source": "https://github.com/phar-io/manifest/tree/master" + }, "time": "2018-07-08T19:23:20+00:00" }, { @@ -664,6 +709,10 @@ } ], "description": "Library for handling version information and constraints", + "support": { + "issues": "https://github.com/phar-io/version/issues", + "source": "https://github.com/phar-io/version/tree/master" + }, "time": "2018-07-08T19:19:57+00:00" }, { @@ -713,6 +762,10 @@ "reflection", "static analysis" ], + "support": { + "issues": "https://github.com/phpDocumentor/ReflectionCommon/issues", + "source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x" + }, "time": "2020-06-27T09:03:43+00:00" }, { @@ -765,6 +818,10 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "support": { + "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/master" + }, "time": "2020-09-03T19:13:55+00:00" }, { @@ -810,6 +867,10 @@ } ], "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "support": { + "issues": "https://github.com/phpDocumentor/TypeResolver/issues", + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.4.0" + }, "time": "2020-09-17T18:55:26+00:00" }, { @@ -873,6 +934,10 @@ "spy", "stub" ], + "support": { + "issues": "https://github.com/phpspec/prophecy/issues", + "source": "https://github.com/phpspec/prophecy/tree/1.12.1" + }, "time": "2020-09-29T09:10:42+00:00" }, { @@ -915,6 +980,10 @@ "MIT" ], "description": "PHPStan - PHP Static Analysis Tool", + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/0.12.55" + }, "funding": [ { "url": "https://github.com/ondrejmirtes", @@ -993,6 +1062,10 @@ "testing", "xunit" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/8.0.2" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1049,6 +1122,10 @@ "filesystem", "iterator" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", + "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/3.0.5" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1108,6 +1185,10 @@ "keywords": [ "process" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-invoker/issues", + "source": "https://github.com/sebastianbergmann/php-invoker/tree/3.1.1" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1163,6 +1244,10 @@ "keywords": [ "template" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-text-template/issues", + "source": "https://github.com/sebastianbergmann/php-text-template/tree/2.0.4" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1218,6 +1303,10 @@ "keywords": [ "timer" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-timer/issues", + "source": "https://github.com/sebastianbergmann/php-timer/tree/5.0.3" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1273,6 +1362,10 @@ "keywords": [ "tokenizer" ], + "support": { + "issues": "https://github.com/sebastianbergmann/php-token-stream/issues", + "source": "https://github.com/sebastianbergmann/php-token-stream/tree/master" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1368,6 +1461,10 @@ "testing", "xunit" ], + "support": { + "issues": "https://github.com/sebastianbergmann/phpunit/issues", + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.2.6" + }, "funding": [ { "url": "https://phpunit.de/donate.html", @@ -1424,6 +1521,10 @@ ], "description": "Collection of value objects that represent the PHP code units", "homepage": "https://github.com/sebastianbergmann/code-unit", + "support": { + "issues": "https://github.com/sebastianbergmann/code-unit/issues", + "source": "https://github.com/sebastianbergmann/code-unit/tree/1.0.8" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1475,6 +1576,10 @@ ], "description": "Looks up which function or method a line of code belongs to", "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "support": { + "issues": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/issues", + "source": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/tree/2.0.3" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1545,6 +1650,10 @@ "compare", "equality" ], + "support": { + "issues": "https://github.com/sebastianbergmann/comparator/issues", + "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.6" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1607,6 +1716,10 @@ "unidiff", "unified diff" ], + "support": { + "issues": "https://github.com/sebastianbergmann/diff/issues", + "source": "https://github.com/sebastianbergmann/diff/tree/4.0.4" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1666,6 +1779,10 @@ "environment", "hhvm" ], + "support": { + "issues": "https://github.com/sebastianbergmann/environment/issues", + "source": "https://github.com/sebastianbergmann/environment/tree/5.1.3" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1739,6 +1856,10 @@ "export", "exporter" ], + "support": { + "issues": "https://github.com/sebastianbergmann/exporter/issues", + "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.3" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1799,6 +1920,10 @@ "keywords": [ "global state" ], + "support": { + "issues": "https://github.com/sebastianbergmann/global-state/issues", + "source": "https://github.com/sebastianbergmann/global-state/tree/master" + }, "time": "2020-02-07T06:11:37+00:00" }, { @@ -1846,6 +1971,10 @@ ], "description": "Traverses array structures and object graphs to enumerate all referenced objects", "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "support": { + "issues": "https://github.com/sebastianbergmann/object-enumerator/issues", + "source": "https://github.com/sebastianbergmann/object-enumerator/tree/4.0.4" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1897,6 +2026,10 @@ ], "description": "Allows reflection of object attributes, including inherited and non-public ones", "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "support": { + "issues": "https://github.com/sebastianbergmann/object-reflector/issues", + "source": "https://github.com/sebastianbergmann/object-reflector/tree/2.0.4" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -1956,6 +2089,10 @@ ], "description": "Provides functionality to recursively process PHP variables", "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "support": { + "issues": "https://github.com/sebastianbergmann/recursion-context/issues", + "source": "https://github.com/sebastianbergmann/recursion-context/tree/4.0.4" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -2007,6 +2144,10 @@ ], "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "support": { + "issues": "https://github.com/sebastianbergmann/resource-operations/issues", + "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.3" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -2059,6 +2200,10 @@ ], "description": "Collection of value objects that represent the types of the PHP type system", "homepage": "https://github.com/sebastianbergmann/type", + "support": { + "issues": "https://github.com/sebastianbergmann/type/issues", + "source": "https://github.com/sebastianbergmann/type/tree/2.3.1" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -2108,6 +2253,10 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", + "support": { + "issues": "https://github.com/sebastianbergmann/version/issues", + "source": "https://github.com/sebastianbergmann/version/tree/3.0.2" + }, "funding": [ { "url": "https://github.com/sebastianbergmann", @@ -2176,6 +2325,9 @@ "polyfill", "portable" ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.20.0" + }, "funding": [ { "url": "https://symfony.com/sponsor", @@ -2230,6 +2382,10 @@ } ], "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "support": { + "issues": "https://github.com/theseer/tokenizer/issues", + "source": "https://github.com/theseer/tokenizer/tree/master" + }, "funding": [ { "url": "https://github.com/theseer", @@ -2285,6 +2441,10 @@ "check", "validate" ], + "support": { + "issues": "https://github.com/webmozart/assert/issues", + "source": "https://github.com/webmozart/assert/tree/master" + }, "time": "2020-07-08T17:02:28+00:00" } ], @@ -2302,5 +2462,5 @@ "ext-curl": "*" }, "platform-dev": [], - "plugin-api-version": "1.1.0" + "plugin-api-version": "2.0.0" } diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 5e2ba8a96..76f777a12 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -15,6 +15,7 @@ namespace OCA\News\Controller; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FolderServiceV2; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; use OCP\IConfig; @@ -23,8 +24,9 @@ use OCP\AppFramework\Http; use OCA\News\Service\ItemService; use OCA\News\Service\FeedService; -use OCA\News\Service\FolderService; use OCA\News\Db\FeedType; +use OCP\IUser; +use OCP\IUserSession; class FeedController extends Controller { @@ -33,27 +35,29 @@ class FeedController extends Controller //TODO: Remove private $feedService; //TODO: Remove - private $folderService; - //TODO: Remove private $itemService; + /** + * @var FolderServiceV2 + */ + private $folderService; private $userId; private $settings; public function __construct( string $appName, IRequest $request, - FolderService $folderService, + FolderServiceV2 $folderService, FeedService $feedService, ItemService $itemService, IConfig $settings, - string $UserId + IUser $user ) { parent::__construct($appName, $request); - $this->feedService = $feedService; $this->folderService = $folderService; - $this->itemService = $itemService; - $this->userId = $UserId; - $this->settings = $settings; + $this->feedService = $feedService; + $this->itemService = $itemService; + $this->settings = $settings; + $this->userId = $user->getUID(); } diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index e67299c6d..662d45029 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -19,18 +19,20 @@ use \OCP\IRequest; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; -use \OCA\News\Service\FolderService; +use \OCA\News\Service\FolderServiceV2; use \OCA\News\Service\FeedService; use \OCA\News\Service\ItemService; use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\Exceptions\ServiceConflictException; -use \OCA\News\Service\Exceptions\ServiceValidationException; +use OCP\IUser; class FolderController extends Controller { - use JSONHttpErrorTrait; + use JSONHttpErrorTrait, ApiPayloadTrait; - //TODO: Remove + /** + * @var FolderServiceV2 + */ private $folderService; //TODO: Remove private $feedService; @@ -41,16 +43,16 @@ class FolderController extends Controller public function __construct( string $appName, IRequest $request, - FolderService $folderService, + FolderServiceV2 $folderService, FeedService $feedService, ItemService $itemService, - $UserId + IUser $user ) { parent::__construct($appName, $request); $this->folderService = $folderService; $this->feedService = $feedService; $this->itemService = $itemService; - $this->userId = $UserId; + $this->userId = $user->getUID(); } @@ -60,7 +62,7 @@ class FolderController extends Controller public function index() { $folders = $this->folderService->findAllForUser($this->userId); - return ['folders' => $folders]; + return ['folders' => $this->serialize($folders)]; } @@ -77,7 +79,7 @@ class FolderController extends Controller $folderId = $folderId === 0 ? null : $folderId; try { - $this->folderService->open($folderId, $open, $this->userId); + $this->folderService->open($this->userId, $folderId, $open); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -89,24 +91,17 @@ class FolderController extends Controller /** * @NoAdminRequired * - * @param string $folderName + * @param string $folderName + * @param int|null $parent * * @return array|JSONResponse */ - public function create(string $folderName) + public function create(string $folderName, ?int $parent = null) { - try { - // we need to purge deleted folders if a folder is created to - // prevent already exists exceptions - $this->folderService->purgeDeleted($this->userId, false); - $folder = $this->folderService->create($folderName, $this->userId); + $this->folderService->purgeDeleted(); + $folder = $this->folderService->create($this->userId, $folderName, $parent); - return ['folders' => [$folder]]; - } catch (ServiceConflictException $ex) { - return $this->error($ex, Http::STATUS_CONFLICT); - } catch (ServiceValidationException $ex) { - return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); - } + return ['folders' => $this->serialize($folder)]; } @@ -123,9 +118,11 @@ class FolderController extends Controller return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { - $this->folderService->markDeleted($folderId, $this->userId); + $this->folderService->markDelete($this->userId, $folderId, true); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); + } catch (ServiceConflictException $ex) { + return $this->error($ex, Http::STATUS_CONFLICT); } return []; @@ -146,17 +143,11 @@ class FolderController extends Controller return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { - $folder = $this->folderService->rename( - $folderId, - $folderName, - $this->userId - ); + $folder = $this->folderService->rename($this->userId, $folderId, $folderName); - return ['folders' => [$folder]]; + return ['folders' => $this->serialize($folder)]; } catch (ServiceConflictException $ex) { return $this->error($ex, Http::STATUS_CONFLICT); - } catch (ServiceValidationException $ex) { - return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -179,8 +170,8 @@ class FolderController extends Controller $highestItemId, $this->userId ); - - return ['feeds' => $this->feedService->findAllForUser($this->userId)]; + $feeds = $this->feedService->findAllForUser($this->userId); + return ['feeds' => $this->serialize($feeds)]; } @@ -196,9 +187,11 @@ class FolderController extends Controller $folderId = $folderId === 0 ? null : $folderId; try { - $this->folderService->unmarkDeleted($folderId, $this->userId); + $this->folderService->markDelete($this->userId, $folderId, false); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); + } catch (ServiceConflictException $ex) { + return $this->error($ex, Http::STATUS_CONFLICT); } return []; diff --git a/lib/Cron/UpdaterJob.php b/lib/Cron/UpdaterJob.php index d2dbde149..bec1c3579 100644 --- a/lib/Cron/UpdaterJob.php +++ b/lib/Cron/UpdaterJob.php @@ -60,7 +60,7 @@ class UpdaterJob extends TimedJob Application::DEFAULT_SETTINGS['useCronUpdates'] ); - if (!$uses_cron || !$this->statusService->isProperlyConfigured()) { + if (!$uses_cron || !$this->statusService->isCronProperlyConfigured()) { return; } diff --git a/lib/Db/FolderMapper.php b/lib/Db/FolderMapper.php deleted file mode 100644 index 75b749974..000000000 --- a/lib/Db/FolderMapper.php +++ /dev/null @@ -1,143 +0,0 @@ - - * @author Bernhard Posselt - * @copyright 2012 Alessandro Cosentino - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Db; - -use OCA\News\Utility\Time; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Db\MultipleObjectsReturnedException; -use OCP\IDBConnection; -use OCP\AppFramework\Db\Entity; - -/** - * Class LegacyFolderMapper - * - * @package OCA\News\Db - * @deprecated use FolderMapper - */ -class FolderMapper extends NewsMapper -{ - - const TABLE_NAME = 'news_folders'; - - public function __construct(IDBConnection $db, Time $time) - { - parent::__construct($db, $time, Folder::class); - } - - public function find(string $userId, int $id) - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; - - return $this->findEntity($sql, [$id, $userId]); - } - - - public function findAllFromUser(string $userId): array - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ? ' . - 'AND `deleted_at` = 0'; - $params = [$userId]; - - return $this->findEntities($sql, $params); - } - - - public function findByName(string $folderName, string $userId) - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `name` = ? ' . - 'AND `user_id` = ?'; - $params = [$folderName, $userId]; - - return $this->findEntities($sql, $params); - } - - - public function delete(Entity $entity): Entity - { - parent::delete($entity); - - // someone please slap me for doing this manually :P - // we needz CASCADE + FKs please - $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; - $params = [$entity->getId()]; - $stmt = $this->execute($sql, $params); - $stmt->closeCursor(); - - $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN ' . - '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - - $stmt = $this->execute($sql); - $stmt->closeCursor(); - - return $entity; - } - - - /** - * @param int $deleteOlderThan if given gets all entries with a delete date - * older than that timestamp - * @param string $userId if given returns only entries from the given user - * @return array with the database rows - */ - public function getToDelete($deleteOlderThan = null, $userId = null) - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 '; - $params = []; - - // sometimes we want to delete all entries - if ($deleteOlderThan !== null) { - $sql .= 'AND `deleted_at` < ? '; - $params[] = $deleteOlderThan; - } - - // we need to sometimes only delete feeds of a user - if ($userId !== null) { - $sql .= 'AND `user_id` = ?'; - $params[] = $userId; - } - - return $this->findEntities($sql, $params); - } - - - /** - * Deletes all folders of a user - * - * @param string $userId the name of the user - */ - public function deleteUser(string $userId) - { - $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; - $this->execute($sql, [$userId]); - } - - /** - * NO-OP - * @return array - */ - public function findAll(): array - { - return []; - } - - public function findFromUser(string $userId, int $id): Entity - { - return $this->find($id, $userId); - } -} diff --git a/lib/Db/FolderMapperV2.php b/lib/Db/FolderMapperV2.php index c2b172870..a69f8be45 100644 --- a/lib/Db/FolderMapperV2.php +++ b/lib/Db/FolderMapperV2.php @@ -43,7 +43,7 @@ class FolderMapperV2 extends NewsMapperV2 * @param string $userId The user identifier * @param array $params Filter parameters * - * @return Entity[] + * @return Folder[] */ public function findAllFromUser(string $userId, array $params = []): array { @@ -60,7 +60,7 @@ class FolderMapperV2 extends NewsMapperV2 /** * Find all items * - * @return Entity[] + * @return Folder[] */ public function findAll(): array { @@ -72,6 +72,16 @@ class FolderMapperV2 extends NewsMapperV2 return $this->findEntities($builder); } + /** + * Find a single feed for a user + * + * @param string $userId The user identifier + * @param int $id The feed ID + * + * @return Folder + * @throws \OCP\AppFramework\Db\DoesNotExistException + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException + */ public function findFromUser(string $userId, int $id): Entity { $builder = $this->db->getQueryBuilder(); diff --git a/lib/Hooks/UserDeleteHook.php b/lib/Hooks/UserDeleteHook.php index 491f84fcb..128e6fa79 100644 --- a/lib/Hooks/UserDeleteHook.php +++ b/lib/Hooks/UserDeleteHook.php @@ -16,7 +16,7 @@ namespace OCA\News\Hooks; use OCA\News\AppInfo\Application; use OCA\News\Service\ItemService; use OCA\News\Service\FeedService; -use OCA\News\Service\FolderService; +use OCA\News\Service\FolderServiceV2; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\User\Events\BeforeUserDeletedEvent; @@ -39,6 +39,6 @@ class UserDeleteHook implements IEventListener // order is important! $container->get(ItemService::class)->deleteUser($userId); $container->get(FeedService::class)->deleteUser($userId); - $container->get(FolderService::class)->deleteUser($userId); + $container->get(FolderServiceV2::class)->deleteUser($userId); } } diff --git a/lib/Service/FolderService.php b/lib/Service/FolderService.php deleted file mode 100644 index db060b050..000000000 --- a/lib/Service/FolderService.php +++ /dev/null @@ -1,227 +0,0 @@ - - * @author Bernhard Posselt - * @copyright 2012 Alessandro Cosentino - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Service; - -use OCA\News\AppInfo\Application; -use OCP\IConfig; -use OCA\News\Service\Exceptions\ServiceConflictException; -use OCA\News\Service\Exceptions\ServiceException; -use OCA\News\Service\Exceptions\ServiceNotFoundException; -use OCA\News\Service\Exceptions\ServiceValidationException; -use OCP\AppFramework\Db\Entity; -use OCP\IL10N; -use OCA\News\Db\Folder; -use OCA\News\Db\FolderMapper; -use OCA\News\Utility\Time; -use Psr\Log\LoggerInterface; - -/** - * Class LegacyFolderService - * - * @package OCA\News\Service - * @deprecated use FolderServiceV2 - */ -class FolderService extends Service -{ - - private $l10n; - private $timeFactory; - private $autoPurgeMinimumInterval; - private $folderMapper; - - public function __construct( - FolderMapper $folderMapper, - IL10N $l10n, - Time $timeFactory, - IConfig $config, - LoggerInterface $logger - ) { - parent::__construct($folderMapper, $logger); - $this->l10n = $l10n; - $this->timeFactory = $timeFactory; - $this->folderMapper = $folderMapper; - $this->autoPurgeMinimumInterval = $config->getAppValue( - Application::NAME, - 'autoPurgeMinimumInterval', - Application::DEFAULT_SETTINGS['autoPurgeMinimumInterval'] - ); - } - - /** - * Finds all folders of a user - * - * @param string $userId the name of the user - * - * @return Folder[] - */ - public function findAllForUser(string $userId, array $params = []): array - { - return $this->folderMapper->findAllFromUser($userId); - } - - - private function validateFolder(string $folderName, string $userId) - { - $existingFolders - = $this->folderMapper->findByName($folderName, $userId); - if (count($existingFolders) > 0) { - throw new ServiceConflictException( - $this->l10n->t('Can not add folder: Exists already') - ); - } - - if (mb_strlen($folderName) === 0) { - throw new ServiceValidationException( - 'Folder name can not be empty' - ); - } - } - - - /** - * Creates a new folder - * - * @param string $folderName the name of the folder - * @param string $userId the name of the user for whom it should be created - * @param int $parentId the parent folder id, deprecated we don't nest - * folders - * - * @return Folder|Entity the newly created folder - * @throws ServiceValidationException if the folder has invalid parameters - * @throws ServiceConflictException if name exists already - */ - public function create(string $folderName, string $userId, ?int $parentId = null) - { - $this->validateFolder($folderName, $userId); - - $folder = new Folder(); - $folder->setName($folderName) - ->setUserId($userId) - ->setParentId($parentId) - ->setOpened(true); - - return $this->folderMapper->insert($folder); - } - - - /** - * @param int|null $folderId - * @param bool $opened - * @param string $userId - * - * @throws ServiceNotFoundException - */ - public function open(?int $folderId, bool $opened, string $userId) - { - $folder = $this->find($userId, $folderId); - $folder->setOpened($opened); - $this->folderMapper->update($folder); - } - - - /** - * Renames a folder - * - * @param int $folderId the id of the folder that should be deleted - * @param string $folderName the new name of the folder - * @param string $userId the name of the user for security reasons - * - * @return Folder the updated folder - * @throws ServiceValidationException if the folder has invalid parameters - * @throws ServiceNotFoundException if the folder does not exist - * @throws ServiceConflictException if name exists already - */ - public function rename(int $folderId, string $folderName, string $userId) - { - $this->validateFolder($folderName, $userId); - - $folder = $this->find($userId, $folderId); - $folder->setName($folderName); - - return $this->folderMapper->update($folder); - } - - - /** - * Use this to mark a folder as deleted. That way it can be un-deleted - * - * @param int $folderId the id of the folder that should be deleted - * @param string $userId the name of the user for security reasons - * - * @throws ServiceNotFoundException when folder does not exist - */ - public function markDeleted(int $folderId, string $userId) - { - $folder = $this->find($userId, $folderId); - $folder->setDeletedAt($this->timeFactory->getTime()); - $this->folderMapper->update($folder); - } - - - /** - * Use this to restore a folder - * - * @param int $folderId the id of the folder that should be restored - * @param string $userId the name of the user for security reasons - * - * @throws ServiceNotFoundException when folder does not exist - */ - public function unmarkDeleted(int $folderId, string $userId) - { - $folder = $this->find($userId, $folderId); - $folder->setDeletedAt(0); - $this->folderMapper->update($folder); - } - - - /** - * Deletes all deleted folders - * - * @param ?string $userId if given it purges only folders of that user - * @param boolean $useInterval defaults to true, if true it only purges - * entries in a given interval to give the user a chance to undo the - * deletion - */ - public function purgeDeleted(?string $userId = null, bool $useInterval = true) - { - $deleteOlderThan = null; - - if ($useInterval) { - $now = $this->timeFactory->getTime(); - $deleteOlderThan = $now - $this->autoPurgeMinimumInterval; - } - - $toDelete = $this->folderMapper->getToDelete($deleteOlderThan, $userId); - - foreach ($toDelete as $folder) { - $this->folderMapper->delete($folder); - } - } - - - /** - * Deletes all folders of a user - * - * @param string $userId the name of the user - */ - public function deleteUser(string $userId) - { - $this->folderMapper->deleteUser($userId); - } - - public function findAll(): array - { - return $this->mapper->findAll(); - } -} diff --git a/lib/Service/FolderServiceV2.php b/lib/Service/FolderServiceV2.php index ee24addfd..95761b530 100644 --- a/lib/Service/FolderServiceV2.php +++ b/lib/Service/FolderServiceV2.php @@ -13,11 +13,14 @@ namespace OCA\News\Service; -use OCA\News\Db\Feed; -use OCA\News\Db\FeedMapperV2; +use OC\AppFramework\Utility\TimeFactory; use OCA\News\Db\Folder; use OCA\News\Db\FolderMapperV2; +use OCA\News\Service\Exceptions\ServiceConflictException; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use Psr\Log\LoggerInterface; /** @@ -31,14 +34,20 @@ class FolderServiceV2 extends Service * @var FeedServiceV2 */ private $feedService; + /** + * @var FeedServiceV2 + */ + private $timeFactory; public function __construct( FolderMapperV2 $mapper, FeedServiceV2 $feedService, + TimeFactory $timeFactory, LoggerInterface $logger ) { parent::__construct($mapper, $logger); $this->feedService = $feedService; + $this->timeFactory = $timeFactory; } /** @@ -55,7 +64,31 @@ class FolderServiceV2 extends Service } /** - * @param string $userId + * Finds a folder of a user + * + * @param string $userId The name/ID of the user + * @param int|null $folderId ID of the folder + * + * @return Folder + * + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function findForUser(string $userId, ?int $folderId): Entity + { + try { + return $this->mapper->findFromUser($userId, $folderId); + } catch (DoesNotExistException $e) { + throw new ServiceNotFoundException('Folder not found'); + } catch (MultipleObjectsReturnedException $e) { + throw new ServiceConflictException('Multiple folders found'); + } + } + + /** + * Find all folders and it's feeds. + * + * @param string $userId The name/ID of the owner * * @return Folder[] */ @@ -80,33 +113,117 @@ class FolderServiceV2 extends Service return $this->mapper->findAll(); } + /** + * Create a folder + * + * @param string $userId + * @param string $name + * @param int|null $parent + * + * @return Folder + */ public function create(string $userId, string $name, ?int $parent = null): Entity { $folder = new Folder(); $folder->setUserId($userId) ->setName($name) - ->setParentId($parent); + ->setParentId($parent) + ->setOpened(true); return $this->mapper->insert($folder); } - public function delete(string $user, int $id) + /** + * Delete a feed. + * + * @param string $userId Folder owner + * @param int $folderId Folder ID + * + * @return Folder + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function delete(string $userId, int $folderId): Entity { - $entity = $this->mapper->findFromUser($user, $id); + $folder = $this->findForUser($userId, $folderId); - $this->mapper->delete($entity); + return $this->mapper->delete($folder); } + /** + * Purge all deleted folders. + */ public function purgeDeleted() { $this->mapper->purgeDeleted(); } - public function rename(string $userId, int $folderId, string $newName) + /** + * Rename a folder + * + * @param string $userId Folder owner + * @param int $folderId Folder ID + * @param string $newName New name + * + * @return Folder + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function rename(string $userId, int $folderId, string $newName): Entity { - /** @var Folder $folder */ - $folder = $this->mapper->find($userId, $folderId); + $folder = $this->findForUser($userId, $folderId); $folder->setName($newName); - $this->mapper->update($folder); + return $this->mapper->update($folder); + } + + /** + * Mark a folder as deleted + * + * @param string $userId Folder owner + * @param int $folderId Folder ID + * @param bool $mark If the mark should be added or removed + * + * @return Folder + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function markDelete(string $userId, int $folderId, bool $mark): Entity + { + $folder = $this->findForUser($userId, $folderId); + $time = $mark ? $this->timeFactory->getTime() : 0; + $folder->setDeletedAt($time); + + return $this->mapper->update($folder); + } + + /** + * Mark a folder as opened + * + * @param string $userId Folder owner + * @param int|null $folderId Folder ID + * @param bool $open If the mark should be added or removed + * + * @return Folder + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function open(string $userId, ?int $folderId, bool $open): Entity + { + $folder = $this->findForUser($userId, $folderId); + $folder->setOpened($open); + return $this->mapper->update($folder); + } + + /** + * Delete all folders of a user + * + * @param string $userId User ID/name + */ + public function deleteUser(string $userId): void + { + $folders = $this->findAllForUser($userId); + foreach ($folders as $folder) { + $this->mapper->delete($folder); + } } } diff --git a/lib/Service/Service.php b/lib/Service/Service.php index 04965af4b..970613830 100644 --- a/lib/Service/Service.php +++ b/lib/Service/Service.php @@ -29,7 +29,7 @@ use Psr\Log\LoggerInterface; abstract class Service { /** - * @var NewsMapper|NewsMapperV2 + * @var NewsMapperV2 */ protected $mapper; /** @@ -40,7 +40,7 @@ abstract class Service /** * Service constructor. * - * @param NewsMapper|NewsMapperV2 $mapper + * @param NewsMapperV2 $mapper * @param LoggerInterface $logger */ public function __construct($mapper, LoggerInterface $logger) diff --git a/lib/Service/StatusService.php b/lib/Service/StatusService.php index acfdf6be1..f46624cd0 100644 --- a/lib/Service/StatusService.php +++ b/lib/Service/StatusService.php @@ -36,9 +36,16 @@ class StatusService $this->connection = $connection; } - public function isProperlyConfigured(): bool + /** + * Check if cron is properly configured + * + * @return bool + */ + public function isCronProperlyConfigured(): bool { + //Is NC cron enabled? $cronMode = $this->settings->getAppValue('core', 'backgroundjobs_mode'); + //Expect nextcloud cron $cronOff = !$this->settings->getAppValue( Application::NAME, 'useCronUpdates', @@ -50,6 +57,11 @@ class StatusService } + /** + * Get the app status + * + * @return array + */ public function getStatus(): array { $version = $this->settings->getAppValue( @@ -60,7 +72,7 @@ class StatusService return [ 'version' => $version, 'warnings' => [ - 'improperlyConfiguredCron' => !$this->isProperlyConfigured(), + 'improperlyConfiguredCron' => !$this->isCronProperlyConfigured(), 'incorrectDbCharset' => !$this->connection->supports4ByteText() ] ]; diff --git a/lib/Service/UpdaterService.php b/lib/Service/UpdaterService.php index c18eed8d7..7f1b04ddc 100644 --- a/lib/Service/UpdaterService.php +++ b/lib/Service/UpdaterService.php @@ -18,7 +18,7 @@ class UpdaterService { /** - * @var FolderService + * @var FolderServiceV2 */ private $folderService; @@ -28,7 +28,7 @@ class UpdaterService private $feedService; /** - * @var ItemService + * @var ItemServiceV2 */ private $itemService; diff --git a/tests/Integration/Db/ItemMapperTest.php b/tests/Integration/Db/ItemMapperTest.php index 28a6c3e11..ea7f306df 100644 --- a/tests/Integration/Db/ItemMapperTest.php +++ b/tests/Integration/Db/ItemMapperTest.php @@ -150,38 +150,6 @@ class ItemMapperTest extends IntegrationTest } - public function testReadFolder() - { - $this->loadFixtures('default'); - - $folderId = $this->findFolderByName('first folder')->getId(); - $this->itemMapper->readFolder( - $folderId, PHP_INT_MAX, 10, $this->user - ); - - $items = $this->itemMapper->findAllItems( - 30, 0, 0, false, false, $this->user - ); - - $this->assertEquals(1, count($items)); - - $item = $this->findItemByTitle('a title1'); - $item = $this->itemMapper->find($this->user, $item->getId()); - - $this->assertEquals(10, $item->getLastModified()); - - $item = $this->findItemByTitle('a title3'); - $item = $this->itemMapper->find($this->user, $item->getId()); - - $this->assertEquals(10, $item->getLastModified()); - - $item = $this->findItemByTitle('a title9'); - $item = $this->itemMapper->find($this->user, $item->getId()); - - $this->assertTrue($item->isUnread()); - } - - public function testReadFeed() { $this->loadFixtures('default'); diff --git a/tests/Integration/Fixtures/FeedFixture.php b/tests/Integration/Fixtures/FeedFixture.php index 77abd3ee0..0c1e31af9 100644 --- a/tests/Integration/Fixtures/FeedFixture.php +++ b/tests/Integration/Fixtures/FeedFixture.php @@ -12,7 +12,6 @@ namespace OCA\News\Tests\Integration\Fixtures; - use OCA\News\Db\Feed; class FeedFixture extends Feed @@ -20,31 +19,33 @@ class FeedFixture extends Feed use Fixture; - public function __construct(array $defaults=[]) + public function __construct(array $defaults = []) { + parent::__construct(); $defaults = array_merge( [ - 'userId' => 'test', - 'urlHash' => 'urlHash', - 'url' => 'http://the.url.com', - 'title' => 'title', - 'faviconLink' => 'http://feed.com/favicon.ico', - 'added' => 3000, - 'folderId' => null, - 'link' => 'http://feed.com/rss', - 'preventUpdate' => false, - 'deletedAt' => 0, - 'articlesPerUpdate' => 40, - 'httpLastModified' => 10, - 'httpEtag' => '', - 'location' => 'http://feed.com/rss', - 'ordering' => 0, - 'fullTextEnabled' => false, - 'pinned' => false, - 'updateMode' => 0, - 'updateErrorCount' => 0, - 'lastUpdateError' => '', - ], $defaults + 'userId' => 'test', + 'urlHash' => 'urlHash', + 'url' => 'http://the.url.com', + 'title' => 'title', + 'faviconLink' => 'http://feed.com/favicon.ico', + 'added' => 3000, + 'folderId' => null, + 'link' => 'http://feed.com/rss', + 'preventUpdate' => false, + 'deletedAt' => 0, + 'articlesPerUpdate' => 40, + 'httpLastModified' => 10, + 'httpEtag' => '', + 'location' => 'http://feed.com/rss', + 'ordering' => 0, + 'fullTextEnabled' => false, + 'pinned' => false, + 'updateMode' => 0, + 'updateErrorCount' => 0, + 'lastUpdateError' => '', + ], + $defaults ); unset($defaults['items']); $this->fillDefaults($defaults); diff --git a/tests/Integration/Fixtures/Fixture.php b/tests/Integration/Fixtures/Fixture.php index 1874c8102..366c93228 100644 --- a/tests/Integration/Fixtures/Fixture.php +++ b/tests/Integration/Fixtures/Fixture.php @@ -15,8 +15,7 @@ namespace OCA\News\Tests\Integration\Fixtures; trait Fixture { - - protected function fillDefaults(array $defaults=[]) + protected function fillDefaults(array $defaults = []) { foreach ($defaults as $key => $value) { $method = 'set' . ucfirst($key); diff --git a/tests/Integration/Fixtures/FolderFixture.php b/tests/Integration/Fixtures/FolderFixture.php index 09c03e5de..ffd6042c0 100644 --- a/tests/Integration/Fixtures/FolderFixture.php +++ b/tests/Integration/Fixtures/FolderFixture.php @@ -12,24 +12,26 @@ namespace OCA\News\Tests\Integration\Fixtures; - use OCA\News\Db\Folder; class FolderFixture extends Folder { use Fixture; - public function __construct(array $defaults=[]) + public function __construct(array $defaults = []) { + parent::__construct(); + $defaults = array_merge( [ - 'parentId' => null, - 'name' => 'folder', - 'userId' => 'test', - 'opened' => true, - 'deletedAt' => 0, - 'lastModified' => 9 - ], $defaults + 'parentId' => null, + 'name' => 'folder', + 'userId' => 'test', + 'opened' => true, + 'deletedAt' => 0, + 'lastModified' => 9 + ], + $defaults ); unset($defaults['feeds']); $this->fillDefaults($defaults); diff --git a/tests/Integration/Fixtures/ItemFixture.php b/tests/Integration/Fixtures/ItemFixture.php index e84b4250b..978b12b55 100644 --- a/tests/Integration/Fixtures/ItemFixture.php +++ b/tests/Integration/Fixtures/ItemFixture.php @@ -12,32 +12,33 @@ namespace OCA\News\Tests\Integration\Fixtures; - use OCA\News\Db\Item; class ItemFixture extends Item { use Fixture; - public function __construct(array $defaults=[]) + public function __construct(array $defaults = []) { + parent::__construct(); $defaults = array_merge( [ - 'url' => 'http://google.de', - 'title' => 'title', - 'author' => 'my author', - 'pubDate' => 2323, - 'body' => 'this is a body', - 'enclosureMime' => 'video/mpeg', - 'enclosureLink' => 'http://google.de/web.webm', - 'mediaThumbnail' => 'https://i3.ytimg.com/vi/Zgge1O9wdPY/hqdefault.jpg', - 'mediaDescription' => 'The best video ever', - 'feedId' => 0, - 'unread' => true, - 'starred' => false, - 'lastModified' => 113, - 'rtl' => false, - ], $defaults + 'url' => 'http://google.de', + 'title' => 'title', + 'author' => 'my author', + 'pubDate' => 2323, + 'body' => 'this is a body', + 'enclosureMime' => 'video/mpeg', + 'enclosureLink' => 'http://google.de/web.webm', + 'mediaThumbnail' => 'https://i3.ytimg.com/vi/Zgge1O9wdPY/hqdefault.jpg', + 'mediaDescription' => 'The best video ever', + 'feedId' => 0, + 'unread' => true, + 'starred' => false, + 'lastModified' => 113, + 'rtl' => false, + ], + $defaults ); if (!array_key_exists('guid', $defaults)) { diff --git a/tests/Integration/IntegrationTest.php b/tests/Integration/IntegrationTest.php index 6ecc3404c..bc309b339 100644 --- a/tests/Integration/IntegrationTest.php +++ b/tests/Integration/IntegrationTest.php @@ -12,6 +12,7 @@ namespace OCA\News\Tests\Integration; +use OCA\News\Db\FolderMapperV2; use OCA\News\Db\Item; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\IAppContainer; @@ -44,7 +45,7 @@ abstract class IntegrationTest extends \Test\TestCase protected $feedMapper; /** - * @var FolderMapper + * @var FolderMapperV2 */ protected $folderMapper; @@ -64,7 +65,7 @@ abstract class IntegrationTest extends \Test\TestCase // set up database layers $this->itemMapper = $this->container->get(ItemMapper::class); $this->feedMapper = $this->container->get(FeedMapper::class); - $this->folderMapper = $this->container->get(FolderMapper::class); + $this->folderMapper = $this->container->get(FolderMapperV2::class); } protected function findItemByTitle($title) @@ -92,16 +93,6 @@ abstract class IntegrationTest extends \Test\TestCase return $result; } - protected function findFolderByName($name) - { - return $this->folderMapper->where( - [ - 'userId' => $this->user, - 'name' => $name - ] - )[0]; - } - protected function findFeedByTitle($title) { return $this->feedMapper->where( @@ -115,7 +106,7 @@ abstract class IntegrationTest extends \Test\TestCase /** * @param string $name loads fixtures from a given file */ - protected function loadFixtures($name) + protected function loadFixtures(string $name) { $fixtures = include __DIR__ . '/Fixtures/data/' . $name . '.php'; if (array_key_exists('folders', $fixtures)) { @@ -184,7 +175,7 @@ abstract class IntegrationTest extends \Test\TestCase */ protected function setupUser($user, $password) { - $userManager = $this->container->query(IUserManager::class); + $userManager = $this->container->get(IUserManager::class); $userManager->createUser($user, $password); $this->loginAsUser($user); @@ -197,7 +188,7 @@ abstract class IntegrationTest extends \Test\TestCase */ protected function tearDownUser($user) { - $userManager = $this->container->query(IUserManager::class); + $userManager = $this->container->get(IUserManager::class); if ($userManager->userExists($user)) { $userManager->get($user)->delete(); @@ -211,7 +202,7 @@ abstract class IntegrationTest extends \Test\TestCase * * @param string $user */ - protected function clearUserNewsDatabase($user) + protected function clearUserNewsDatabase(string $user) { $sql = [ 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` IN @@ -220,7 +211,7 @@ abstract class IntegrationTest extends \Test\TestCase 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?' ]; - $db = $this->container->query(IDBConnection::class); + $db = $this->container->get(IDBConnection::class); foreach ($sql as $query) { $db->prepare($query)->execute([$user]); } diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php index f5c51b93e..fb5d0b5b5 100644 --- a/tests/Unit/Controller/FeedControllerTest.php +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -15,7 +15,7 @@ namespace OCA\News\Tests\Unit\Controller; use OCA\News\Controller\FeedController; use OCA\News\Service\FeedService; -use OCA\News\Service\FolderService; +use OCA\News\Service\FolderServiceV2; use OCA\News\Service\ItemService; use OCP\AppFramework\Http; @@ -26,21 +26,49 @@ use OCA\News\Service\Exceptions\ServiceConflictException; use OCP\IConfig; use OCP\IRequest; +use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class FeedControllerTest extends TestCase { - + /** + * @var string + */ private $appName; - private $feedService; - private $request; - private $controller; + private $exampleResult; + private $uid; + + /** + * @var MockObject|FolderServiceV2 + */ private $folderService; + /** + * TODO: Remove + * @var MockObject|FeedService + */ + private $feedService; + /** + * TODO: Remove + * @var MockObject|ItemService + */ private $itemService; + + /** + * @var MockObject|IConfig + */ private $settings; - private $exampleResult; + + /** + * @var MockObject|IUser + */ private $user; + /** + * @var FeedController + */ + private $class; + /** * Gets run before each test @@ -48,7 +76,13 @@ class FeedControllerTest extends TestCase public function setUp(): void { $this->appName = 'news'; - $this->user = 'jack'; + $this->uid = 'jack'; + $this->user = $this->getMockBuilder(IUser::class) + ->getMock(); + + $this->user->expects($this->once()) + ->method('getUID') + ->willReturn($this->uid); $this->settings = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -61,14 +95,15 @@ class FeedControllerTest extends TestCase ->disableOriginalConstructor() ->getMock(); $this->folderService = $this - ->getMockBuilder(FolderService::class) + ->getMockBuilder(FolderServiceV2::class) ->disableOriginalConstructor() ->getMock(); - $this->request = $this->getMockBuilder(IRequest::class) + $request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); - $this->controller = new FeedController( - $this->appName, $this->request, + $this->class = new FeedController( + $this->appName, + $request, $this->folderService, $this->feedService, $this->itemService, @@ -94,18 +129,18 @@ class FeedControllerTest extends TestCase ]; $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user)) + ->with($this->uid) ->will($this->returnValue($result['feeds'])); $this->itemService->expects($this->once()) ->method('getNewestItemId') -