Every run creates a new poll with one slide per interactive type and shows the first (choice) slide live with voting open. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
285 lines
11 KiB
PHP
285 lines
11 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\EmailTrait;
|
|
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 EmailTrait;
|
|
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 testSeedCreatesNewPollWithLiveFirstSlideEveryTime(): void
|
|
{
|
|
$this->exec('polls seed --json');
|
|
$this->assertExitSuccess();
|
|
$first = json_decode(implode('', $this->_out->messages()), true);
|
|
$this->exec('polls seed --json');
|
|
$second = json_decode(implode('', $this->_out->messages()), true);
|
|
|
|
$this->assertNotSame($first['id'], $second['id']);
|
|
$this->assertNotSame($first['joinCode'], $second['joinCode']);
|
|
$this->assertSame(7, $second['slides']);
|
|
|
|
$poll = $this->pollsTable()->get($second['id']);
|
|
$slide = $this->slidesTable()->get($poll->current_slide_id);
|
|
$this->assertSame('choice', $slide->type);
|
|
$this->assertTrue($slide->isVotingOpen());
|
|
}
|
|
|
|
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 testListAndShowAsJson(): void
|
|
{
|
|
[$old, $token] = $this->createPoll('Alt <b>&</b>', 'alt@example.org');
|
|
$this->createPoll('Neu', 'neu@example.org');
|
|
$this->age($old->id, 120);
|
|
|
|
$this->exec('polls list --json');
|
|
$this->assertExitSuccess();
|
|
$rows = json_decode(implode("
|
|
", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR);
|
|
$this->assertCount(2, $rows);
|
|
$this->assertSame('Alt <b>&</b>', $rows[0]['title']);
|
|
$this->assertSame('alt@example.org', $rows[0]['email']);
|
|
$this->assertTrue($rows[0]['expired']);
|
|
$this->assertFalse($rows[1]['expired']);
|
|
$this->assertNull($rows[1]['reminderSentAt']);
|
|
$this->assertArrayNotHasKey('adminUrl', $rows[0]);
|
|
$this->assertStringNotContainsString($token, implode('', $this->_out->messages()));
|
|
|
|
$this->exec('polls list --json --expired');
|
|
$this->assertCount(1, json_decode(implode("
|
|
", $this->_out->messages()), true));
|
|
|
|
$this->exec('polls list --json --email=gibtsnicht');
|
|
$this->assertSame([], json_decode(implode("
|
|
", $this->_out->messages()), true));
|
|
|
|
$this->exec('polls show ' . $old->id . ' --json');
|
|
$data = json_decode(implode("
|
|
", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR);
|
|
$this->assertSame($old->id, $data['id']);
|
|
$this->assertNull($data['adminUrl']);
|
|
$this->assertStringNotContainsString($token, implode('', $this->_out->messages()));
|
|
|
|
$this->exec('polls show ' . $old->id . ' --json --link');
|
|
$data = json_decode(implode("
|
|
", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR);
|
|
$this->assertSame('https://plenario.test/admin/' . $token, $data['adminUrl']);
|
|
}
|
|
|
|
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 testRemindCanBeForcedForOnePoll(): void
|
|
{
|
|
[$young] = $this->createPoll('Frisch', 'frisch@example.org');
|
|
[$other] = $this->createPoll('Anderer', 'anderer@example.org');
|
|
|
|
// Not in the reminder window at all, yet it is sent on request, to this poll only.
|
|
$this->exec('polls remind ' . $young->join_code);
|
|
$this->assertExitSuccess();
|
|
$this->assertOutputContains('erzwungen');
|
|
$this->assertMailCount(1);
|
|
$this->assertMailSentTo('frisch@example.org');
|
|
$this->assertNull($this->pollsTable()->get($young->id)->expiry_reminded_at, 'a forced reminder does not mark the poll');
|
|
|
|
// Sendable again (also after a regular reminder), still not marked.
|
|
$this->pollsTable()->updateAll(['expiry_reminded_at' => DateTime::now()], ['id' => $young->id]);
|
|
$this->exec('polls remind ' . $young->id);
|
|
$this->assertExitSuccess();
|
|
$this->assertMailCount(2);
|
|
|
|
// Dry run sends nothing.
|
|
$this->exec('polls remind ' . $other->id . ' --dry-run');
|
|
$this->assertOutputContains('Würde erinnern');
|
|
$this->assertMailCount(2);
|
|
|
|
// Works with reminders switched off, fails without an expiry date and for unknown polls.
|
|
Configure::write('Plenario.retention.reminderDays', 0);
|
|
$this->exec('polls remind ' . $other->id);
|
|
$this->assertExitSuccess();
|
|
$this->assertMailCount(3);
|
|
|
|
$this->exec('polls remind 99999');
|
|
$this->assertExitError();
|
|
$this->assertErrorContains('nicht gefunden');
|
|
|
|
Configure::write('Plenario.retention.days', 0);
|
|
$this->exec('polls remind ' . $other->id);
|
|
$this->assertExitError();
|
|
$this->assertMailCount(3);
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|