Files
Plenario/tests/TestCase/Command/PollsCommandsTest.php
T
gnilebein e3f74e375f
CI / Deploy / PHP 8.5 (push) Successful in 1m7s
CI / Deploy / PHP 8.4 (push) Successful in 1m11s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Revert "Browse and delete polls from a numbered list in the polls menu"
This reverts commit 7b83cf2f2c.
2026-11-16 13:52:02 +01:00

232 lines
8.7 KiB
PHP

<?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 testMenuQuits(): void
{
$this->exec('polls', ['0']);
$this->assertExitSuccess();
$this->assertOutputContains('Umfragen verwalten');
}
public function testMenuListsAndOpensPollWithLink(): void
{
[$poll, $token] = $this->createPoll('Menü-Umfrage', 'menu@example.org');
$this->exec('polls', ['1', '4', (string)$poll->id, '2', '0', '0']);
$this->assertExitSuccess();
$this->assertOutputContains('menu@example.org');
$this->assertOutputContains('https://plenario.test/admin/' . $token);
$this->exec('polls', ['4', 'ZZZZZZ', '0']);
$this->assertExitSuccess();
$this->assertErrorContains('nicht gefunden');
}
public function testMenuDeleteNeedsConfirmation(): void
{
[$poll] = $this->createPoll();
$this->exec('polls', ['4', (string)$poll->id, '3', 'n', '0', '0']);
$this->assertSame(1, $this->pollsTable()->find()->where(['id' => $poll->id])->count());
$this->exec('polls', ['4', (string)$poll->id, '3', 'y', '0']);
$this->assertSame(0, $this->pollsTable()->find()->where(['id' => $poll->id])->count());
}
public function testMenuCleanupAsksBeforeDeleting(): void
{
[$old] = $this->createPoll();
$this->age($old->id, 100);
$this->exec('polls', ['6', 'n', '0']);
$this->assertSame(1, $this->pollsTable()->find()->count());
$this->exec('polls', ['6', 'y', '0']);
$this->assertSame(0, $this->pollsTable()->find()->count());
$this->exec('polls', ['6', '0']);
$this->assertOutputContains('Nichts zu tun.');
}
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());
}
}