Add poll expiry, reminder mail and polls admin CLI
Polls are deleted POLL_RETENTION_DAYS (default 90) after creation by the cron job `bin/cake polls cleanup`. POLL_REMINDER_DAYS (default 14) before that the owner gets a one-time reminder mail (expiry_reminded_at). New commands: polls list, show (admin link only with --link), delete, remind, cleanup. The expiry date is shown in the creation mail and the dashboard. Docs: README, AGENTS.md, .env.example. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4caff47a09
commit
a19cd8339e
20 files changed
+976
-1
No files matched your search
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Test\TestCase\Command;
|
||||
|
||||
use App\Test\TestCase\PlenarioTestTrait;
|
||||
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\TestSuite\TestCase;
|
||||
|
||||
/**
|
||||
* `bin/cake polls list|show|delete|remind|cleanup` and the poll expiry (counted from creation).
|
||||
*/
|
||||
class PollsCommandsTest extends TestCase
|
||||
{
|
||||
use ConsoleIntegrationTestTrait;
|
||||
use PlenarioTestTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Configure::write('Plenario.retention.days', 90);
|
||||
Configure::write('Plenario.retention.reminderDays', 14);
|
||||
Configure::write('App.fullBaseUrl', 'https://plenario.test');
|
||||
// The commands act on all polls: start without leftovers of other tests (fixtures are empty).
|
||||
$this->slidesTable()->deleteAll('1 = 1');
|
||||
$this->pollsTable()->deleteAll('1 = 1');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id Poll id
|
||||
* @param int $days Age of the poll in days
|
||||
*/
|
||||
private function age(int $id, int $days): void
|
||||
{
|
||||
$this->pollsTable()->updateAll(['created' => DateTime::now()->subDays($days)], ['id' => $id]);
|
||||
}
|
||||
|
||||
public function testExpiryCountsFromCreationNotFromAdminVisits(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$this->assertSame($poll->created->addDays(90)->format('Y-m-d'), $poll->expiresAt()->format('Y-m-d'));
|
||||
$this->assertSame($poll->created->addDays(76)->format('Y-m-d'), $poll->reminderAt()->format('Y-m-d'));
|
||||
|
||||
$this->age($poll->id, 91);
|
||||
// A visit in the admin area changes nothing.
|
||||
$this->pollsTable()->updateAll(['last_admin_access' => DateTime::now()], ['id' => $poll->id]);
|
||||
$this->assertTrue($this->pollsTable()->get($poll->id)->isExpired());
|
||||
}
|
||||
|
||||
public function testSettingsAreRespected(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$this->age($poll->id, 1000);
|
||||
|
||||
Configure::write('Plenario.retention.days', 0);
|
||||
$this->assertNull($this->pollsTable()->get($poll->id)->expiresAt());
|
||||
$this->assertSame(0, $this->pollsTable()->find('expired')->count());
|
||||
$this->assertSame(0, $this->pollsTable()->find('reminderDue')->count());
|
||||
|
||||
// The reminder lead is always shorter than the lifetime.
|
||||
Configure::write('Plenario.retention.days', 10);
|
||||
Configure::write('Plenario.retention.reminderDays', 30);
|
||||
$this->assertSame(9, $poll::reminderLeadDays());
|
||||
}
|
||||
|
||||
public function testListShowsOwnerAndExpiredFilter(): void
|
||||
{
|
||||
[$old] = $this->createPoll('Alte Umfrage', 'alt@example.org');
|
||||
$this->createPoll('Neue Umfrage', 'neu@example.org');
|
||||
$this->age($old->id, 120);
|
||||
|
||||
$this->exec('polls list');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertOutputContains('alt@example.org');
|
||||
$this->assertOutputContains('neu@example.org');
|
||||
$this->assertOutputContains('(abgelaufen)');
|
||||
|
||||
$this->exec('polls list --expired');
|
||||
$this->assertOutputContains('alt@example.org');
|
||||
$this->assertOutputNotContains('neu@example.org');
|
||||
|
||||
$this->exec('polls list --email=NEU@');
|
||||
$this->assertOutputContains('neu@example.org');
|
||||
$this->assertOutputNotContains('alt@example.org');
|
||||
}
|
||||
|
||||
public function testShowPrintsAdminLinkOnlyOnRequest(): void
|
||||
{
|
||||
[$poll, $token] = $this->createPoll('Titel', 'owner@example.org');
|
||||
|
||||
$this->exec('polls show ' . $poll->join_code);
|
||||
$this->assertExitSuccess();
|
||||
$this->assertOutputContains('owner@example.org');
|
||||
$this->assertOutputNotContains($token);
|
||||
|
||||
$this->exec('polls show ' . $poll->id . ' --link');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertOutputContains('https://plenario.test/admin/' . $token);
|
||||
|
||||
$this->exec('polls show 99999');
|
||||
$this->assertExitError();
|
||||
}
|
||||
|
||||
public function testDeleteRemovesPollWithSlides(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$this->createSlide($poll);
|
||||
|
||||
$this->exec('polls delete ' . $poll->id . ' --yes');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertSame(0, $this->pollsTable()->find()->where(['id' => $poll->id])->count());
|
||||
$this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
|
||||
|
||||
// Again: unknown poll is an error, nothing else happens.
|
||||
$this->exec('polls delete ' . $poll->id . ' --yes');
|
||||
$this->assertExitError();
|
||||
}
|
||||
|
||||
public function testReminderGoesOutOnceInTheLastDaysBeforeExpiry(): void
|
||||
{
|
||||
[$young, $youngToken] = $this->createPoll('Jung', 'jung@example.org');
|
||||
[$due, $dueToken] = $this->createPoll('Bald weg', 'bald@example.org');
|
||||
[$gone] = $this->createPoll('Abgelaufen', 'weg@example.org');
|
||||
$this->age($young->id, 75);
|
||||
$this->age($due->id, 77);
|
||||
$this->age($gone->id, 91);
|
||||
|
||||
$this->exec('polls remind --dry-run');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertOutputContains('bald@example.org');
|
||||
$this->assertOutputNotContains('jung@example.org');
|
||||
$this->assertOutputNotContains('weg@example.org');
|
||||
$this->assertNull($this->pollsTable()->get($due->id)->expiry_reminded_at);
|
||||
|
||||
$this->exec('polls remind');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertOutputContains('1 Erinnerung(en) verschickt');
|
||||
$this->assertNotNull($this->pollsTable()->get($due->id)->expiry_reminded_at);
|
||||
$this->assertNull($this->pollsTable()->get($young->id)->expiry_reminded_at);
|
||||
|
||||
// Second run: nothing more to send.
|
||||
$this->exec('polls remind');
|
||||
$this->assertOutputContains('0 Erinnerung(en) verschickt');
|
||||
|
||||
// Reminder switched off.
|
||||
Configure::write('Plenario.retention.reminderDays', 0);
|
||||
$this->age($young->id, 80);
|
||||
$this->exec('polls remind');
|
||||
$this->assertOutputContains('Erinnerungen sind aus');
|
||||
$this->assertNull($this->pollsTable()->get($young->id)->expiry_reminded_at);
|
||||
$this->assertNotEmpty($youngToken . $dueToken);
|
||||
}
|
||||
|
||||
public function testCleanupRemindsThenDeletesOnlyExpiredPollsAndIsIdempotent(): void
|
||||
{
|
||||
[$old] = $this->createPoll('Alt');
|
||||
[$soon] = $this->createPoll('Bald');
|
||||
[$fresh] = $this->createPoll('Frisch');
|
||||
$this->createSlide($old);
|
||||
$this->age($old->id, 91);
|
||||
$this->age($soon->id, 80);
|
||||
$this->age($fresh->id, 10);
|
||||
|
||||
$this->exec('polls cleanup --dry-run');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertSame(3, $this->pollsTable()->find()->count());
|
||||
$this->assertNull($this->pollsTable()->get($soon->id)->expiry_reminded_at);
|
||||
|
||||
$this->exec('polls cleanup');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertEqualsCanonicalizing(
|
||||
[$soon->id, $fresh->id],
|
||||
$this->pollsTable()->find()->all()->extract('id')->toList(),
|
||||
);
|
||||
$this->assertNotNull($this->pollsTable()->get($soon->id)->expiry_reminded_at);
|
||||
$this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $old->id])->count());
|
||||
|
||||
$this->exec('polls cleanup');
|
||||
$this->assertExitSuccess();
|
||||
$this->assertSame(2, $this->pollsTable()->find()->count());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user