Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Service/OptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ private function countProposals(array $options): int {
* @psalm-return array<array-key, Option>
*/
public function shift(int $pollId, int $step, string $unit): array {
$this->getPoll($pollId);
$this->getPoll($pollId)
->request(Poll::PERMISSION_POLL_EDIT);

if ($this->poll->getType() !== Poll::TYPE_DATE) {
throw new InvalidPollTypeException('Shifting is only available in date polls');
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Service/OptionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Polls\Db\Option;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Exceptions\ForbiddenException;
use OCA\Polls\Exceptions\InsufficientAttributesException;
use OCA\Polls\Model\SimpleOption;
use OCA\Polls\Service\OptionService;
Expand All @@ -26,6 +27,7 @@ class OptionServiceTest extends UnitTestCase {

private Poll $textPoll;
private Poll $datePoll;
private Poll $foreignDatePoll;
private Option $textOption;

protected function setUp(): void {
Expand All @@ -52,6 +54,14 @@ protected function setUp(): void {
$datePoll->setExpire(0);
$this->datePoll = $this->pollMapper->insert($datePoll);

// Date poll owned by another user, private, open, not shared with admin
$foreignDatePoll = $this->fm->instance('OCA\\Polls\\Db\\Poll');
$foreignDatePoll->setOwner('foreignUser');
$foreignDatePoll->setType(Poll::TYPE_DATE);
$foreignDatePoll->setAccess(Poll::ACCESS_PRIVATE);
$foreignDatePoll->setExpire(0);
$this->foreignDatePoll = $this->pollMapper->insert($foreignDatePoll);

// Pre-existing text option for update/delete/confirm/reorder tests
$this->textOption = $this->optionService->add(
$this->textPoll->getId(),
Expand All @@ -70,6 +80,10 @@ protected function tearDown(): void {
$this->pollMapper->delete($this->datePoll);
} catch (\Exception) {
}
try {
$this->pollMapper->delete($this->foreignDatePoll);
} catch (\Exception) {
}
}

// --- add ---
Expand Down Expand Up @@ -210,4 +224,24 @@ public function testReorderChangesOptionOrder(): void {
$this->assertSame(1, $orderById[$second->getId()]);
$this->assertSame(2, $orderById[$this->textOption->getId()]);
}

// --- shift ---

public function testShiftMovesDateOptions(): void {
$timestamp = strtotime('2027-06-15 12:00:00 UTC');
$this->optionService->add(
$this->datePoll->getId(),
(new SimpleOption())->setDateTime($timestamp)
);

$shifted = $this->optionService->shift($this->datePoll->getId(), 1, 'day');

$this->assertCount(1, $shifted);
$this->assertSame(strtotime('+1 day', $timestamp), reset($shifted)->getTimestamp());
}

public function testShiftDeniedWithoutEditPermission(): void {
$this->expectException(ForbiddenException::class);
$this->optionService->shift($this->foreignDatePoll->getId(), 1, 'day');
}
}
Loading