Merge pull request 'Add --json to polls list and polls show' (#87) from feature/polls-json into main
Reviewed-on: #87
This commit was merged in pull request #87.
This commit is contained in:
commit
4f03414d7a
6 files changed
+115
-2
No files matched your search
+6
-2
@@ -122,8 +122,8 @@ Zum Verwalten per Hand und für Skripte/Cron:
|
|||||||
|
|
||||||
| Befehl | Zweck |
|
| Befehl | Zweck |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung |
|
| `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title] [--json]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung |
|
||||||
| `bin/cake polls show <id\|code> [--link]` | Details; mit `--link` auch der geheime Admin-Link (nur auf Wunsch) |
|
| `bin/cake polls show <id\|code> [--link] [--json]` | Details; mit `--link` auch der geheime Admin-Link (nur auf Wunsch) |
|
||||||
| `bin/cake polls delete <id\|code> [--yes]` | Eine Umfrage samt Folien, Antworten und Bildern löschen (fragt nach) |
|
| `bin/cake polls delete <id\|code> [--yes]` | Eine Umfrage samt Folien, Antworten und Bildern löschen (fragt nach) |
|
||||||
| `bin/cake polls remind [--dry-run]` | Erinnerungsmails für bald ablaufende Umfragen (je Umfrage einmal) |
|
| `bin/cake polls remind [--dry-run]` | Erinnerungsmails für bald ablaufende Umfragen (je Umfrage einmal) |
|
||||||
| `bin/cake polls cleanup [--dry-run]` | Der Cronjob: erst erinnern, dann abgelaufene Umfragen löschen, danach verwaiste Bilddateien |
|
| `bin/cake polls cleanup [--dry-run]` | Der Cronjob: erst erinnern, dann abgelaufene Umfragen löschen, danach verwaiste Bilddateien |
|
||||||
@@ -134,6 +134,10 @@ einmalig eine Mail mit Ablaufdatum und Admin-Link, um die Ergebnisse als PDF zu
|
|||||||
der Anlege-Mail und im Dashboard (Karte „Sicherheit“). Es wird berechnet, eine Änderung der Einstellung gilt also sofort
|
der Anlege-Mail und im Dashboard (Karte „Sicherheit“). Es wird berechnet, eine Änderung der Einstellung gilt also sofort
|
||||||
für alle Umfragen.
|
für alle Umfragen.
|
||||||
|
|
||||||
|
Mit `--json` geben `polls list` und `polls show` statt der Tabelle bzw. des Texts reines JSON aus (Datumswerte als
|
||||||
|
ISO 8601, ohne weitere Ausgabe), z. B. für `jq`: `bin/cake polls list --json --expired | jq '.[].email'`. Der Admin-Link
|
||||||
|
steht auch dort nur mit `--link` (`adminUrl`, sonst `null`).
|
||||||
|
|
||||||
### Cronjob einrichten
|
### Cronjob einrichten
|
||||||
|
|
||||||
Ein einziger täglicher Cronjob erledigt Erinnern, Löschen und Bild-Aufräumen (`polls cleanup`). Ohne ihn läuft keine
|
Ein einziger täglicher Cronjob erledigt Erinnern, Löschen und Bild-Aufräumen (`polls cleanup`). Ohne ihn läuft keine
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ Aufräumen und Erinnerungen, CI/CD, Release-Pakete, Container.
|
|||||||
Folien/Bildern, danach `images_cleanup`; `--dry-run`; Cron-Einrichtung für Server und Docker steht in [deployment.md](deployment.md) unter
|
Folien/Bildern, danach `images_cleanup`; `--dry-run`; Cron-Einrichtung für Server und Docker steht in [deployment.md](deployment.md) unter
|
||||||
„Cronjob einrichten“, im Container läuft kein cron). `last_admin_access` fließt nicht mehr ein. `polls show --link`
|
„Cronjob einrichten“, im Container läuft kein cron). `last_admin_access` fließt nicht mehr ein. `polls show --link`
|
||||||
ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`).
|
ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`).
|
||||||
|
`polls list|show --json` gibt nur JSON aus (`PollsCommand::pollData()`/`printJson()`, Ausgabemodus RAW, sonst würden `<…>` in Titeln als
|
||||||
|
Markup verschluckt; nie das Token, `adminUrl` nur mit `--link`).
|
||||||
Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen).
|
Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen).
|
||||||
|
|
||||||
## Deployment
|
## Deployment
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Model\Entity\Poll;
|
|||||||
use App\Model\Table\PollsTable;
|
use App\Model\Table\PollsTable;
|
||||||
use Cake\Command\Command;
|
use Cake\Command\Command;
|
||||||
use Cake\Console\ConsoleIo;
|
use Cake\Console\ConsoleIo;
|
||||||
|
use Cake\Console\ConsoleOutput;
|
||||||
use Cake\I18n\DateTime;
|
use Cake\I18n\DateTime;
|
||||||
use Cake\Routing\Router;
|
use Cake\Routing\Router;
|
||||||
|
|
||||||
@@ -26,6 +27,43 @@ abstract class PollsCommand extends Command
|
|||||||
return $polls;
|
return $polls;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Machine-readable form of a poll (for `--json`). Never contains the admin token.
|
||||||
|
*
|
||||||
|
* @param \App\Model\Entity\Poll $poll Poll
|
||||||
|
* @return array<string, mixed> Dates as ISO 8601 (server time zone), null where empty
|
||||||
|
*/
|
||||||
|
protected function pollData(Poll $poll): array
|
||||||
|
{
|
||||||
|
$date = fn(?DateTime $d): ?string => $d?->format(DATE_ATOM);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $poll->id,
|
||||||
|
'title' => $poll->title,
|
||||||
|
'email' => $poll->email,
|
||||||
|
'joinCode' => $poll->join_code,
|
||||||
|
'joinUrl' => $this->joinLink($poll),
|
||||||
|
'created' => $date($poll->created),
|
||||||
|
'lastAdminAccess' => $date($poll->last_admin_access),
|
||||||
|
'expiresAt' => $date($poll->expiresAt()),
|
||||||
|
'expired' => $poll->isExpired(),
|
||||||
|
'reminderSentAt' => $date($poll->expiry_reminded_at),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints data as JSON only: nothing else on stdout, no markup interpretation (titles may contain "<").
|
||||||
|
*
|
||||||
|
* @param \Cake\Console\ConsoleIo $io Console
|
||||||
|
* @param mixed $data JSON serialisable data
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function printJson(ConsoleIo $io, mixed $data): void
|
||||||
|
{
|
||||||
|
$io->setOutputAs(ConsoleOutput::RAW);
|
||||||
|
$io->out((string)json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
|
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ class PollsListCommand extends PollsCommand
|
|||||||
->addOption('email', [
|
->addOption('email', [
|
||||||
'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'),
|
'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'),
|
||||||
])
|
])
|
||||||
|
->addOption('json', [
|
||||||
|
'help' => __('Ausgabe als JSON statt als Tabelle (für Skripte).'),
|
||||||
|
'boolean' => true,
|
||||||
|
])
|
||||||
->addOption('sort', [
|
->addOption('sort', [
|
||||||
'help' => __('Sortierung (aufsteigend).'),
|
'help' => __('Sortierung (aufsteigend).'),
|
||||||
'default' => 'created',
|
'default' => 'created',
|
||||||
@@ -75,6 +79,12 @@ class PollsListCommand extends PollsCommand
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($args->getOption('json')) {
|
||||||
|
$this->printJson($io, array_map($this->pollData(...), $polls));
|
||||||
|
|
||||||
|
return static::CODE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
if ($polls === []) {
|
if ($polls === []) {
|
||||||
$io->out(__('Keine Umfragen gefunden.'));
|
$io->out(__('Keine Umfragen gefunden.'));
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ class PollsShowCommand extends PollsCommand
|
|||||||
'help' => __('ID oder Beitrittscode der Umfrage.'),
|
'help' => __('ID oder Beitrittscode der Umfrage.'),
|
||||||
'required' => true,
|
'required' => true,
|
||||||
])
|
])
|
||||||
|
->addOption('json', [
|
||||||
|
'help' => __('Ausgabe als JSON statt als Text (für Skripte).'),
|
||||||
|
'boolean' => true,
|
||||||
|
])
|
||||||
->addOption('link', [
|
->addOption('link', [
|
||||||
'help' => __('Auch den Admin-Link ausgeben (enthält das geheime Token!).'),
|
'help' => __('Auch den Admin-Link ausgeben (enthält das geheime Token!).'),
|
||||||
'boolean' => true,
|
'boolean' => true,
|
||||||
@@ -55,6 +59,21 @@ class PollsShowCommand extends PollsCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
$slides = $this->fetchTable('Slides')->find()->where(['poll_id' => $poll->id])->count();
|
$slides = $this->fetchTable('Slides')->find()->where(['poll_id' => $poll->id])->count();
|
||||||
|
if ($args->getOption('json')) {
|
||||||
|
$data = $this->pollData($poll) + ['slides' => $slides, 'adminUrl' => null];
|
||||||
|
if ($args->getOption('link')) {
|
||||||
|
$data['adminUrl'] = $this->adminLink($poll);
|
||||||
|
if ($data['adminUrl'] === null) {
|
||||||
|
$io->err(__('Der Admin-Link lässt sich nicht entschlüsseln (SECURITY_SALT geändert?).'));
|
||||||
|
|
||||||
|
return static::CODE_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->printJson($io, $data);
|
||||||
|
|
||||||
|
return static::CODE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
$io->out([
|
$io->out([
|
||||||
__('ID: {0}', $poll->id),
|
__('ID: {0}', $poll->id),
|
||||||
__('Titel: {0}', $poll->title),
|
__('Titel: {0}', $poll->title),
|
||||||
|
|||||||
@@ -103,6 +103,46 @@ class PollsCommandsTest extends TestCase
|
|||||||
$this->assertExitError();
|
$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
|
public function testDeleteRemovesPollWithSlides(): void
|
||||||
{
|
{
|
||||||
[$poll] = $this->createPoll();
|
[$poll] = $this->createPoll();
|
||||||
|
|||||||
Reference in new issue
Block a user