diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 5e298d24a..f8f94e7f5 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,6 +28,13 @@ use OCA\Polls\AppConstants; use OCA\Polls\Dashboard\PollWidget; +use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\LogMapper; +use OCA\Polls\Db\OptionMapper; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Db\UserMapper; +use OCA\Polls\Db\VoteMapper; use OCA\Polls\Event\CommentAddEvent; use OCA\Polls\Event\CommentDeleteEvent; use OCA\Polls\Event\CommentEvent; @@ -64,6 +71,7 @@ use OCA\Polls\Listener\UserDeletedListener; use OCA\Polls\Listener\VoteListener; use OCA\Polls\Middleware\RequestAttributesMiddleware; +use OCA\Polls\Model\Settings\AppSettings; use OCA\Polls\Notification\Notifier; use OCA\Polls\Provider\SearchProvider; use OCP\AppFramework\App; @@ -71,7 +79,15 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Group\Events\GroupDeletedEvent; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\User\Events\UserDeletedEvent; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; /** * @psalm-api @@ -90,6 +106,7 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { include_once __DIR__ . '/../../vendor/autoload.php'; + $this->registerServices($context); $context->registerMiddleWare(RequestAttributesMiddleware::class); $context->registerNotifierService(Notifier::class); @@ -132,4 +149,66 @@ public function register(IRegistrationContext $context): void { $context->registerSearchProvider(SearchProvider::class); $context->registerDashboardWidget(PollWidget::class); } + + /** + * Register some Services + */ + private function registerServices(IRegistrationContext $context) { + $context->registerService(UserMapper::class, function (ContainerInterface $c): UserMapper { + return new UserMapper( + $c->get(IDBConnection::class), + $c->get(ISession::class), + $c->get(IUserSession::class), + $c->get(IUserManager::class), + $c->get(LoggerInterface::class), + ); + }); + + $context->registerService(AppSettings::class, function (ContainerInterface $c): AppSettings { + return new AppSettings( + $c->get(IConfig::class), + $c->get(IGroupManager::class), + $c->get(IUserSession::class), + ); + }); + + $context->registerService(PollMapper::class, function (ContainerInterface $c): PollMapper { + return new PollMapper( + $c->get(IDBConnection::class), + $c->get(UserMapper::class) + ); + }); + + $context->registerService(CommentMapper::class, function (ContainerInterface $c): CommentMapper { + return new CommentMapper( + $c->get(IDBConnection::class), + ); + }); + + $context->registerService(VoteMapper::class, function (ContainerInterface $c): VoteMapper { + return new VoteMapper( + $c->get(IDBConnection::class), + $c->get(LoggerInterface::class), + ); + }); + + $context->registerService(OptionMapper::class, function (ContainerInterface $c): OptionMapper { + return new OptionMapper( + $c->get(IDBConnection::class), + $c->get(UserMapper::class), + ); + }); + + $context->registerService(SubscriptionMapper::class, function (ContainerInterface $c): SubscriptionMapper { + return new SubscriptionMapper( + $c->get(IDBConnection::class), + ); + }); + + $context->registerService(LogMapper::class, function (ContainerInterface $c): LogMapper { + return new LogMapper( + $c->get(IDBConnection::class), + ); + }); + } } diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index df6a90168..eb73a379b 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -34,6 +34,7 @@ use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\Server; /** * @psalm-api @@ -56,7 +57,7 @@ public function __construct( #[NoAdminRequired] public function list(): JSONResponse { return $this->response(function () { - $appSettings = new AppSettings; + $appSettings = Server::get(AppSettings::class); return [ 'list' => $this->pollService->list(), 'pollCreationAllowed' => $appSettings->getPollCreationAllowed(), diff --git a/lib/Cron/JanitorCron.php b/lib/Cron/JanitorCron.php index 5c94403cf..b922a9569 100644 --- a/lib/Cron/JanitorCron.php +++ b/lib/Cron/JanitorCron.php @@ -34,6 +34,7 @@ use OCA\Polls\Model\Settings\AppSettings; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\Server; /** * @psalm-api @@ -52,7 +53,7 @@ public function __construct( ) { parent::__construct($time); parent::setInterval(86400); // run once a day - $this->appSettings = new AppSettings; + $this->appSettings = Server::get(AppSettings::class); } /** diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 6580d93fd..053eabbd6 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -136,6 +136,7 @@ class Poll extends EntityWithUser implements JsonSerializable { protected bool $hasOrphanedVotes = false; protected int $maxDate = 0; protected int $minDate = 0; + protected int $currentUserVotes = 0; public function __construct() { $this->addType('created', 'int'); @@ -153,6 +154,7 @@ public function __construct() { $this->addType('lastInteraction', 'int'); $this->addType('maxDate', 'int'); $this->addType('minDate', 'int'); + $this->addType('currentUserVotes', 'int'); $this->urlGenerator = Container::queryClass(IURLGenerator::class); $this->userMapper = Container::queryClass(UserMapper::class); $this->voteMapper = Container::queryClass(VoteMapper::class); @@ -190,6 +192,7 @@ public function jsonSerialize(): array { 'summary' => [ 'orphanedVotes' => count($this->voteMapper->findOrphanedByPollandUser($this->id, $this->userMapper->getCurrentUserCached()->getId())), 'yesByCurrentUser' => count($this->voteMapper->getYesVotesByParticipant($this->getPollId(), $this->userMapper->getCurrentUserCached()->getId())), + 'countVotes' => $this->getCurrentUserCountVotes(), ], ]; } @@ -275,6 +278,10 @@ public function getProposalsExpired(): bool { ); } + public function getCurrentUserCountVotes(): int { + return $this->currentUserVotes; + } + public function getDescription(): string { return $this->description ?? ''; } diff --git a/lib/Db/PollMapper.php b/lib/Db/PollMapper.php index 93718d24c..de08bca37 100644 --- a/lib/Db/PollMapper.php +++ b/lib/Db/PollMapper.php @@ -40,7 +40,10 @@ class PollMapper extends QBMapper { /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct(IDBConnection $db) { + public function __construct( + IDBConnection $db, + private UserMapper $userMapper, + ) { parent::__construct($db, Poll::TABLE, Poll::class); } @@ -167,6 +170,7 @@ public function deleteByUserId(string $userId): void { * Build the enhanced query with joined tables */ protected function buildQuery(): IQueryBuilder { + $currentUserId = $this->userMapper->getCurrentUser()->getId(); $qb = $this->db->getQueryBuilder(); $qb->select(self::TABLE . '.*') @@ -174,6 +178,7 @@ protected function buildQuery(): IQueryBuilder { // ->groupBy(self::TABLE . '.id') ->from($this->getTableName(), self::TABLE); $this->joinOptionsForMaxDate($qb, self::TABLE); + $this->joinCurrentUserVotes($qb, self::TABLE, $currentUserId); $qb->groupBy(self::TABLE . '.id'); return $qb; } @@ -201,4 +206,26 @@ protected function joinOptionsForMaxDate(IQueryBuilder &$qb, string $fromAlias): ); } + /** + * Joins options to evaluate min and max option date for date polls + * if text poll or no options are set, + * the min value is the current time, + * the max value is null + */ + protected function joinCurrentUserVotes(IQueryBuilder &$qb, string $fromAlias, $currentUserId): void { + $joinAlias = 'user_vote'; + // force value into a MIN function to avoid grouping errors + $qb->selectAlias($qb->func()->count($joinAlias . '.vote_answer'), 'current_user_votes'); + + $qb->leftJoin( + $fromAlias, + Vote::TABLE, + $joinAlias, + $qb->expr()->andX( + $qb->expr()->eq($joinAlias . '.poll_id', $fromAlias . '.id'), + $qb->expr()->eq($joinAlias . '.user_id', $qb->createNamedParameter($currentUserId, IQueryBuilder::PARAM_STR)), + ) + ); + } + } diff --git a/lib/Db/Share.php b/lib/Db/Share.php index 48afaf2f1..eb5f3bc57 100644 --- a/lib/Db/Share.php +++ b/lib/Db/Share.php @@ -27,9 +27,9 @@ use JsonSerializable; use OCA\Polls\AppConstants; -use OCA\Polls\Helper\Container; use OCA\Polls\Model\Settings\AppSettings; use OCP\IURLGenerator; +use OCP\Server; /** * @psalm-suppress UnusedProperty @@ -146,8 +146,8 @@ public function __construct() { $this->addType('locked', 'int'); $this->addType('reminderSent', 'int'); $this->addType('deleted', 'int'); - $this->urlGenerator = Container::queryClass(IURLGenerator::class); - $this->appSettings = new AppSettings; + $this->urlGenerator = Server::get(IURLGenerator::class); + $this->appSettings = Server::get(AppSettings::class); } /** diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index 549816d4d..e950982be 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -125,19 +125,6 @@ public function findParticipantsByPoll(int $pollId): array { return $this->findEntities($qb); } - - /** - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @return Vote[] - * @psalm-return array - */ - public function findParticipantsVotes(int $pollId, string $userId): array { - $qb = $this->buildQuery(); - $qb->andWhere($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq(self::TABLE . '.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); - return $this->findEntities($qb); - } - public function deleteByPollAndUserId(int $pollId, string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) diff --git a/lib/Helper/Container.php b/lib/Helper/Container.php index 0a1d37a29..0faa8b2da 100644 --- a/lib/Helper/Container.php +++ b/lib/Helper/Container.php @@ -31,34 +31,27 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCP\App\IAppManager; -use OCP\AppFramework\App; use OCP\IL10N; use OCP\L10N\IFactory; -use Psr\Container\ContainerInterface; +use OCP\Server; abstract class Container { - public static function getContainer(): ContainerInterface { - $app = new App(AppConstants::APP_ID); - return $app->getContainer(); - } - public static function queryClass(string $class): mixed { - return self::getContainer()->get($class); + return Server::get($class); } public static function queryPoll(int $pollId): Poll { - return self::queryClass(PollMapper::class)->find($pollId); + return Server::get(PollMapper::class)->find($pollId); } public static function findShare(int $pollId, string $userId): Share { - return self::queryClass(ShareMapper::class) - ->findByPollAndUser($pollId, $userId); + return Server::get(ShareMapper::class)->findByPollAndUser($pollId, $userId); } public static function getL10N(?string $lang = null): IL10N { - return self::queryClass(IFactory::class)->get(AppConstants::APP_ID, $lang); + return Server::get(IFactory::class)->get(AppConstants::APP_ID, $lang); } public static function isAppEnabled(string $app): bool { - return self::queryClass(IAppManager::class)->isEnabledForUser($app); + return Server::get(IAppManager::class)->isEnabledForUser($app); } } diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index cc8784f75..414125edc 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -33,7 +33,6 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\VoteMapper; use OCA\Polls\Exceptions\ForbiddenException; use OCA\Polls\Exceptions\InsufficientAttributesException; use OCA\Polls\Exceptions\InvalidPollIdException; @@ -79,12 +78,10 @@ public function __construct( private ISession $session, private ShareMapper $shareMapper, private UserMapper $userMapper, - private VoteMapper $voteMapper, private Poll $poll, private Share $share, ) { $this->pollId = null; - $this->appSettings = new AppSettings; } /** @@ -323,9 +320,7 @@ private function getIsOpenPoll(): bool { * @return bool Returns true, if the current user is already a particitipant of the current poll. */ private function getIsParticipant(): bool { - return count( - $this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId()) - ) > 0; + return $this->getPoll()->getCurrentUserCountVotes() > 0; } /** diff --git a/lib/Model/Settings/AppSettings.php b/lib/Model/Settings/AppSettings.php index d0451c763..6fce8d364 100644 --- a/lib/Model/Settings/AppSettings.php +++ b/lib/Model/Settings/AppSettings.php @@ -27,7 +27,6 @@ use JsonSerializable; use OCA\Polls\AppConstants; -use OCA\Polls\Helper\Container; use OCA\Polls\Model\Group\Group; use OCP\IConfig; use OCP\IGroupManager; @@ -64,16 +63,15 @@ class AppSettings implements JsonSerializable { public const SETTING_UPDATE_TYPE_PERIODIC_POLLING = 'periodicPolling'; public const SETTING_UPDATE_TYPE_DEFAULT = self::SETTING_UPDATE_TYPE_NO_POLLING; - private IConfig $config; - private IGroupManager $groupManager; - private IUserSession $session; private string $userId = ''; + + public function __construct( + private IConfig $config, + private IGroupManager $groupManager, + private IUserSession $session, - public function __construct() { - $this->config = Container::queryClass(IConfig::class); - $this->session = Container::queryClass(IUserSession::class); - $this->userId = Container::queryClass(IUserSession::class)->getUser()?->getUId() ?? ''; - $this->groupManager = Container::queryClass(IGroupManager::class); + ) { + $this->userId = $this->session->getUser()?->getUId() ?? ''; } // Getters diff --git a/lib/Model/UserBase.php b/lib/Model/UserBase.php index d4692e353..7eb1a8cbf 100644 --- a/lib/Model/UserBase.php +++ b/lib/Model/UserBase.php @@ -44,6 +44,7 @@ use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserSession; +use OCP\Server; use OCP\Share\IShare; class UserBase implements JsonSerializable { @@ -89,11 +90,11 @@ public function __construct( ) { $this->icon = 'icon-share'; $this->l10n = Container::getL10N(); - $this->groupManager = Container::queryClass(IGroupManager::class); - $this->timeZone = Container::queryClass(IDateTimeZone::class); - $this->userMapper = Container::queryClass(UserMapper::class); - $this->userSession = Container::queryClass(IUserSession::class); - $this->appSettings = Container::queryClass(AppSettings::class); + $this->groupManager = Server::get(IGroupManager::class); + $this->timeZone = Server::get(IDateTimeZone::class); + $this->userMapper = Server::get(UserMapper::class); + $this->userSession = Server::get(IUserSession::class); + $this->appSettings = Server::get(AppSettings::class); } public function getId(): string { diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index f27cc2558..2df736761 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -28,13 +28,11 @@ use OCA\Polls\Model\Settings\AppSettings; class SettingsService { - private AppSettings $appSettings; - + /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct() { - $this->appSettings = new AppSettings; + public function __construct(private AppSettings $appSettings) { } /** diff --git a/lib/Service/WatchService.php b/lib/Service/WatchService.php index a60af2cdb..c4518efdd 100644 --- a/lib/Service/WatchService.php +++ b/lib/Service/WatchService.php @@ -36,9 +36,7 @@ use OCP\ISession; class WatchService { - private AppSettings $appSettings; - private Watch $watch; - + /** * @psalm-suppress PossiblyUnusedMethod */ @@ -46,9 +44,9 @@ public function __construct( private ISession $session, private WatchMapper $watchMapper, private Acl $acl, + private AppSettings $appSettings, + private Watch $watch, ) { - $this->appSettings = new AppSettings; - $this->watch = new Watch; } /** diff --git a/package-lock.json b/package-lock.json index 3cdd558ff..2a0d27772 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9101,9 +9101,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "funding": [ { "type": "individual", diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index cca3fade1..9fcc3af8e 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -24,19 +24,20 @@ namespace OCA\Polls\Tests\Unit\Db; use League\FactoryMuffin\Faker\Facade as Faker; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCP\Server; class CommentMapperTest extends UnitTestCase { - private IDBConnection $con; private CommentMapper $commentMapper; private PollMapper $pollMapper; + /** @var Poll[] $polls */ private array $polls = []; + /** @var Comment[] $comments */ private array $comments = []; /** @@ -44,9 +45,8 @@ class CommentMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->commentMapper = new CommentMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->commentMapper = Server::get(CommentMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index 4223fddb7..bd7b9f55a 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -28,11 +28,9 @@ use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Tests\Unit\UnitTestCase; -use OCP\IDBConnection; use OCP\Server; class LogMapperTest extends UnitTestCase { - private IDBConnection $con; private LogMapper $logMapper; private PollMapper $pollMapper; /** @var Log[] $logs*/ @@ -45,9 +43,8 @@ class LogMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logMapper = new LogMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->logMapper = Server::get(LogMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index 1738d9092..bb0478667 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -26,7 +26,6 @@ namespace OCA\Polls\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; @@ -35,24 +34,14 @@ use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; -use OCA\Polls\Db\UserMapper; -use OCP\IGroupManager; use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class OptionMapperTest extends UnitTestCase { - private IDBConnection $con; private ISession $session; - private IUserManager $userManager; - private IUserSession $userSession; - private LoggerInterface $logger; private OptionMapper $optionMapper; private PollMapper $pollMapper; private VoteMapper $voteMapper; - private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Option[] $options */ @@ -65,18 +54,12 @@ class OptionMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logger = Server::get(LoggerInterface::class); $this->session = Server::get(ISession::class); - $this->userManager = Server::get(IUserManager::class); - $this->userSession = Server::get(IUserSession::class); $this->session->set('ncPollsUserId', 'TestUser'); - - $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); - $this->voteMapper = new VoteMapper($this->con, $this->logger); - $this->optionMapper = new OptionMapper($this->con, $this->userMapper); - $this->pollMapper = new PollMapper($this->con); + $this->voteMapper = Server::get(VoteMapper::class); + $this->optionMapper = Server::get(OptionMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index 868ba26ae..d1e5b4dee 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -26,14 +26,12 @@ namespace OCA\Polls\Tests\Unit\Db; use League\FactoryMuffin\Faker\Facade as Faker; -use OCP\IDBConnection; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\Server; class PollMapperTest extends UnitTestCase { - private IDBConnection $con; private PollMapper $pollMapper; /** @var Poll[] $polls*/ private array $polls = []; @@ -43,8 +41,7 @@ class PollMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll'), diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 95916c330..8d89cc596 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -23,7 +23,6 @@ namespace OCA\Polls\Tests\Unit\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; @@ -32,12 +31,11 @@ use OCP\Server; class SubscriptionMapperTest extends UnitTestCase { - private IDBConnection $con; private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; /** @var Poll[] $polls */ private array $polls = []; - /** @var Poll[] $polls */ + /** @var Subscription[] $subscriptions */ private array $subscriptions = []; private array $users = []; @@ -46,9 +44,8 @@ class SubscriptionMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->subscriptionMapper = new SubscriptionMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->subscriptionMapper = Server::get(SubscriptionMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index 433f176bd..f8a50c363 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -26,7 +26,6 @@ namespace OCA\Polls\Tests\Unit\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; @@ -35,22 +34,13 @@ use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; -use OCA\Polls\Db\UserMapper; use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class VoteMapperTest extends UnitTestCase { - private IDBConnection $con; private ISession $session; - private IUserManager $userManager; - private IUserSession $userSession; - private LoggerInterface $logger; private OptionMapper $optionMapper; private PollMapper $pollMapper; - private UserMapper $userMapper; private VoteMapper $voteMapper; /** @var Poll[] $polls */ private array $polls = []; @@ -64,17 +54,12 @@ class VoteMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logger = Server::get(LoggerInterface::class); $this->session = Server::get(ISession::class); - $this->userManager = Server::get(IUserManager::class); - $this->userSession = Server::get(IUserSession::class); $this->session->set('ncPollsUserId', 'TestUser'); - $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); - $this->pollMapper = new PollMapper($this->con); - $this->voteMapper = new VoteMapper($this->con, $this->logger); - $this->optionMapper = new OptionMapper($this->con, $this->userMapper); + $this->pollMapper = Server::get(PollMapper::class); + $this->voteMapper = Server::get(VoteMapper::class); + $this->optionMapper = Server::get(OptionMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') @@ -134,15 +119,6 @@ public function testParticipantsByPoll() { } } - /** - * testParticipantsByPoll - */ - public function testFindParticipantsVotes() { - foreach ($this->votes as $vote) { - $this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getPollId(), $vote->getUserId())) > 0); - } - } - /** * testUpdate */