Add countdown audio per presentation
One audio file per poll (MP3, M4A, OGG, WAV) is uploaded in the admin settings and looped in the presenter view while a quiz runs. Format is detected by magic bytes, files are served with a fixed content type, copied with the poll and swept by images_cleanup. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cad09163d9
commit
2f7a401522
16 files changed
+648
-11
No files matched your search
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Test\TestCase\Controller;
|
||||
|
||||
use App\Command\ImagesCleanupCommand;
|
||||
use App\Service\AudioService;
|
||||
use App\Service\PollStateService;
|
||||
use App\Test\TestCase\ImageTestTrait;
|
||||
use App\Test\TestCase\PlenarioTestTrait;
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\TestSuite\StubConsoleOutput;
|
||||
use Cake\TestSuite\IntegrationTestTrait;
|
||||
use Cake\TestSuite\TestCase;
|
||||
use Laminas\Diactoros\UploadedFile;
|
||||
|
||||
/**
|
||||
* Countdown music of a presentation: upload, replace, remove, delivery, state and copying.
|
||||
*/
|
||||
class PollAudioTest extends TestCase
|
||||
{
|
||||
use ImageTestTrait;
|
||||
use IntegrationTestTrait;
|
||||
use PlenarioTestTrait;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->enableCsrfToken();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->clearImages();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a small file with the given leading bytes.
|
||||
*
|
||||
* @param string $head First bytes
|
||||
* @return string Path
|
||||
*/
|
||||
protected function makeAudio(string $head = "ID3\x03\x00\x00\x00\x00\x00\x00"): string
|
||||
{
|
||||
$path = tempnam(sys_get_temp_dir(), 'plen');
|
||||
file_put_contents($path, $head . str_repeat("\x00", 2000));
|
||||
$this->imageFiles[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
protected function upload(string $token, ?string $path, array $data = []): void
|
||||
{
|
||||
$this->_request['files'] = $path === null ? [] : [
|
||||
'audio' => new UploadedFile($path, (int)filesize($path), UPLOAD_ERR_OK, 'musik.mp3', 'audio/mpeg'),
|
||||
];
|
||||
try {
|
||||
$this->post("/admin/$token/audio", $data);
|
||||
} finally {
|
||||
$this->_request['files'] = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function testUploadServeReplaceAndRemove(): void
|
||||
{
|
||||
[$poll, $token] = $this->createPoll();
|
||||
|
||||
$this->upload($token, $this->makeAudio());
|
||||
$this->assertRedirect(['_name' => 'admin', 'token' => $token, '#' => 'audio']);
|
||||
$first = (string)$this->pollsTable()->get($poll->id)->audio;
|
||||
$this->assertMatchesRegularExpression('/^[a-f0-9]{24}\.mp3$/', $first);
|
||||
$this->assertSame([$first], $this->storedImages($poll->id));
|
||||
|
||||
$this->get("/admin/$token");
|
||||
$this->assertResponseOk();
|
||||
$this->assertResponseContains('<audio controls');
|
||||
$this->assertResponseContains('Audiodatei entfernen');
|
||||
|
||||
// The presenter state carries the URL, the participant page never plays it but the file is served.
|
||||
$fresh = $this->pollsTable()->get($poll->id);
|
||||
$state = (new PollStateService())->forPresenter($fresh);
|
||||
$this->assertStringContainsString("/p/{$poll->join_code}/audio/$first", (string)$state['poll']['audioUrl']);
|
||||
|
||||
$this->get("/p/{$poll->join_code}/audio/$first");
|
||||
$this->assertResponseOk();
|
||||
$this->assertContentType('audio/mpeg');
|
||||
$this->assertHeader('X-Content-Type-Options', 'nosniff');
|
||||
|
||||
// Replacing: new random name, the old file is gone and no longer served.
|
||||
$this->upload($token, $this->makeAudio("RIFF\x00\x00\x00\x00WAVE"));
|
||||
$second = (string)$this->pollsTable()->get($poll->id)->audio;
|
||||
$this->assertMatchesRegularExpression('/^[a-f0-9]{24}\.wav$/', $second);
|
||||
$this->assertSame([$second], $this->storedImages($poll->id));
|
||||
$this->get("/p/{$poll->join_code}/audio/$first");
|
||||
$this->assertResponseCode(404);
|
||||
|
||||
// Removing is explicit and repeatable.
|
||||
foreach ([1, 2] as $ignored) {
|
||||
$this->post("/admin/$token/audio", ['remove' => '1']);
|
||||
$this->assertRedirect();
|
||||
$this->assertNull($this->pollsTable()->get($poll->id)->audio);
|
||||
$this->assertSame([], $this->storedImages($poll->id));
|
||||
}
|
||||
}
|
||||
|
||||
public function testRejectsOtherFilesAndEmptyRequests(): void
|
||||
{
|
||||
[$poll, $token] = $this->createPoll();
|
||||
|
||||
$this->upload($token, $this->makeAudio('<html><script>alert(1)</script>'));
|
||||
$this->assertRedirect();
|
||||
$this->assertNull($this->pollsTable()->get($poll->id)->audio);
|
||||
$this->assertSame([], $this->storedImages($poll->id));
|
||||
|
||||
$this->upload($token, null);
|
||||
$this->assertRedirect();
|
||||
$this->assertNull($this->pollsTable()->get($poll->id)->audio);
|
||||
}
|
||||
|
||||
public function testFileOfAnotherPollIsNotServed(): void
|
||||
{
|
||||
[$poll, $token] = $this->createPoll();
|
||||
[$other] = $this->createPoll();
|
||||
$this->upload($token, $this->makeAudio());
|
||||
$file = (string)$this->pollsTable()->get($poll->id)->audio;
|
||||
|
||||
$this->get("/p/{$other->join_code}/audio/$file");
|
||||
$this->assertResponseCode(404);
|
||||
}
|
||||
|
||||
public function testFormatDetection(): void
|
||||
{
|
||||
$this->assertSame('mp3', AudioService::detectFormat("\xFF\xFB\x90\x00" . str_repeat("\x00", 20)));
|
||||
$this->assertSame('mp3', AudioService::detectFormat("ID3\x04" . str_repeat("\x00", 20)));
|
||||
$this->assertSame('ogg', AudioService::detectFormat('OggS' . str_repeat("\x00", 20)));
|
||||
$this->assertSame('m4a', AudioService::detectFormat("\x00\x00\x00\x20ftypM4A " . str_repeat("\x00", 20)));
|
||||
$this->assertNull(AudioService::detectFormat('GIF89a' . str_repeat("\x00", 20)));
|
||||
$this->assertNull(AudioService::detectFormat("\xFF\xE1\x00\x00" . str_repeat("\x00", 20)), 'reserved layer');
|
||||
$this->assertNull(AudioService::detectFormat('short'));
|
||||
}
|
||||
|
||||
public function testCloneCopiesTheFileAndCleanupKeepsIt(): void
|
||||
{
|
||||
[$poll, $token] = $this->createPoll();
|
||||
$this->createSlide($poll, 'quiz');
|
||||
$this->upload($token, $this->makeAudio());
|
||||
$original = (string)$this->pollsTable()->get($poll->id)->audio;
|
||||
|
||||
$slides = $this->slidesTable()->listForPoll($poll->id);
|
||||
[$copy] = $this->pollsTable()->clonePoll($this->pollsTable()->get($poll->id), $slides);
|
||||
$copied = (string)$this->pollsTable()->get($copy->id)->audio;
|
||||
$this->assertNotSame($original, $copied, 'own file per poll');
|
||||
$this->assertSame([$copied], $this->storedImages($copy->id));
|
||||
$this->assertSame([$original], $this->storedImages($poll->id));
|
||||
|
||||
// The sweep for leftovers must not take the music.
|
||||
$command = new ImagesCleanupCommand();
|
||||
$command->execute(new Arguments([], ['dry-run' => false], []), new ConsoleIo(new StubConsoleOutput(), new StubConsoleOutput()));
|
||||
$this->assertSame([$original], $this->storedImages($poll->id));
|
||||
$this->assertSame([$copied], $this->storedImages($copy->id));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user