Skip to content

Commit 26dc4c1

Browse files
committed
fix tests and poll cloning
Signed-off-by: dartcafe <github@dartcafe.de>
1 parent 60599d5 commit 26dc4c1

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

lib/Db/Poll.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,39 @@ public function __clone() {
220220
$this->setAnonymous(0);
221221
}
222222

223+
/**
224+
* Build a fresh Poll entity pre-populated from this poll's settings.
225+
* The fresh entity's id is never set, so QBMapper::insert() relies on the
226+
* DB autoincrement to assign a fresh id.
227+
*/
228+
public function createClone(string $newOwner): self {
229+
$clone = new self();
230+
$clone->setType($this->getType());
231+
$clone->setVotingVariant($this->getVotingVariant());
232+
$clone->setTitle('Clone of ' . $this->getTitle());
233+
$clone->setDescription($this->getDescription());
234+
$clone->setOwner($newOwner);
235+
$clone->setCreated(time());
236+
$clone->setLastInteraction(0);
237+
$clone->setExpire($this->getExpire());
238+
$clone->setDeleted(0);
239+
$clone->setAccess(self::ACCESS_PRIVATE);
240+
$clone->setAnonymous(0);
241+
$clone->setAllowComment($this->getAllowComment());
242+
$clone->setAllowMaybe($this->getAllowMaybe());
243+
$clone->setAllowProposals($this->getAllowProposals());
244+
$clone->setProposalsExpire($this->getProposalsExpire());
245+
$clone->setVoteLimit($this->getVoteLimit());
246+
$clone->setOptionLimit($this->getOptionLimit());
247+
$clone->setShowResults($this->getShowResults());
248+
$clone->setAdminAccess($this->getAdminAccess());
249+
$clone->setHideBookedUp($this->getHideBookedUp());
250+
$clone->setUseNo($this->getUseNo());
251+
$clone->setMiscSettings($this->getMiscSettings());
252+
$clone->setTimezoneName($this->getTimezoneName());
253+
return $clone;
254+
}
255+
223256
/**
224257
* @return array
225258
*

lib/Service/PollService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,7 @@ public function clone(int $pollId): Poll {
411411
throw new ForbiddenException('Poll creation is disabled');
412412
}
413413

414-
$this->poll = clone $origin;
415-
416-
$this->poll->setOwner($this->userSession->getCurrentUserId());
414+
$this->poll = $origin->createClone($this->userSession->getCurrentUserId());
417415

418416
$this->poll = $this->pollMapper->insert($this->poll);
419417
$this->eventDispatcher->dispatchTyped(new PollCreatedEvent($this->poll));

tests/Unit/Service/OptionServiceTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace OCA\Polls\Tests\Unit\Service;
1010

1111
use OCA\Polls\Db\Option;
12-
use OCA\Polls\Db\OptionMapper;
1312
use OCA\Polls\Db\Poll;
1413
use OCA\Polls\Db\PollMapper;
1514
use OCA\Polls\Exceptions\InsufficientAttributesException;
@@ -21,7 +20,6 @@
2120

2221
class OptionServiceTest extends UnitTestCase {
2322
private OptionService $optionService;
24-
private OptionMapper $optionMapper;
2523
private PollMapper $pollMapper;
2624
private ISession $session;
2725

@@ -35,7 +33,6 @@ protected function setUp(): void {
3533
$this->session->set('ncPollsUserId', 'admin');
3634

3735
$this->optionService = Server::get(OptionService::class);
38-
$this->optionMapper = Server::get(OptionMapper::class);
3936
$this->pollMapper = Server::get(PollMapper::class);
4037

4138
// Text poll owned by admin, private, open
@@ -66,11 +63,11 @@ protected function tearDown(): void {
6663
// Options are deleted when their poll is deleted
6764
try {
6865
$this->pollMapper->delete($this->textPoll);
69-
} catch (\Exception $e) {
66+
} catch (\Exception) {
7067
}
7168
try {
7269
$this->pollMapper->delete($this->datePoll);
73-
} catch (\Exception $e) {
70+
} catch (\Exception) {
7471
}
7572
}
7673

@@ -163,6 +160,10 @@ public function testConfirmOptionOnExpiredPoll(): void {
163160
$this->textPoll->setExpire(time() - 3600);
164161
$this->pollMapper->update($this->textPoll);
165162

163+
// Bust OptionService poll cache: load datePoll so the next call for textPoll
164+
// causes a cache miss and re-reads the updated expire from DB
165+
$this->optionService->list($this->datePoll->getId());
166+
166167
$confirmed = $this->optionService->confirm($this->textOption->getId());
167168
$this->assertGreaterThan(0, $confirmed->getConfirmed());
168169

@@ -175,6 +176,9 @@ public function testConfirmTogglesConfirmation(): void {
175176
$this->textPoll->setExpire(time() - 3600);
176177
$this->pollMapper->update($this->textPoll);
177178

179+
// Bust OptionService poll cache so updated expire is read from DB
180+
$this->optionService->list($this->datePoll->getId());
181+
178182
$this->optionService->confirm($this->textOption->getId()); // confirm
179183
$unconfirmed = $this->optionService->confirm($this->textOption->getId()); // toggle back
180184
$this->assertSame(0, $unconfirmed->getConfirmed());

tests/Unit/Service/VoteServiceTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
namespace OCA\Polls\Tests\Unit\Service;
1010

1111
use OCA\Polls\Db\Option;
12-
use OCA\Polls\Db\OptionMapper;
1312
use OCA\Polls\Db\Poll;
1413
use OCA\Polls\Db\PollMapper;
1514
use OCA\Polls\Db\Share;
1615
use OCA\Polls\Db\ShareMapper;
1716
use OCA\Polls\Db\Vote;
1817
use OCA\Polls\Db\VoteMapper;
18+
use OCA\Polls\Model\SimpleOption;
19+
use OCA\Polls\Service\OptionService;
1920
use OCA\Polls\Service\VoteService;
2021
use OCA\Polls\Tests\Unit\UnitTestCase;
2122
use OCA\Polls\UserSession;
@@ -24,8 +25,8 @@
2425

2526
class VoteServiceTest extends UnitTestCase {
2627
private VoteService $voteService;
28+
private OptionService $optionService;
2729
private PollMapper $pollMapper;
28-
private OptionMapper $optionMapper;
2930
private ShareMapper $shareMapper;
3031
private VoteMapper $voteMapper;
3132
private ISession $session;
@@ -42,10 +43,10 @@ protected function setUp(): void {
4243
parent::setUp();
4344
$this->session = Server::get(ISession::class);
4445
$this->pollMapper = Server::get(PollMapper::class);
45-
$this->optionMapper = Server::get(OptionMapper::class);
4646
$this->shareMapper = Server::get(ShareMapper::class);
4747
$this->voteMapper = Server::get(VoteMapper::class);
4848
$this->voteService = Server::get(VoteService::class);
49+
$this->optionService = Server::get(OptionService::class);
4950
$this->userSession = Server::get(UserSession::class);
5051

5152
// Poll owned by admin, private access, voting open (useNo=0 is default)
@@ -55,12 +56,6 @@ protected function setUp(): void {
5556
$poll->setExpire(0);
5657
$this->poll = $this->pollMapper->insert($poll);
5758

58-
// Option belonging to the poll
59-
$option = $this->fm->instance('OCA\Polls\Db\Option');
60-
$option->setPollId($this->poll->getId());
61-
$option->setOwner('admin');
62-
$this->option = $this->optionMapper->insert($option);
63-
6459
// TYPE_USER share for admin — gives getCurrentUser() a User object (TYPE_USER)
6560
$ownerShare = $this->fm->instance('OCA\Polls\Db\Share');
6661
$ownerShare->setPollId($this->poll->getId());
@@ -75,7 +70,16 @@ protected function setUp(): void {
7570
$externalShare->setUserId(self::EXTERNAL_USER_ID);
7671
$this->externalShare = $this->shareMapper->insert($externalShare);
7772

73+
// Login before creating option so OptionService can check permissions
7874
$this->loginAsOwner();
75+
76+
// Option created via service so pollOptionHash is computed and stored in DB
77+
// (factory-created options bypass setPoll() and lack the hash, causing VoteMapper
78+
// join failures)
79+
$this->option = $this->optionService->add(
80+
$this->poll->getId(),
81+
(new SimpleOption())->setText('Test option')
82+
);
7983
}
8084

8185
protected function tearDown(): void {

0 commit comments

Comments
 (0)