Add YouTube video slide type
CI / Deploy / test (push) Successful in 46s
CI / Deploy / deploy (push) Skipped

Admins paste a YouTube link (watch, youtu.be, shorts, live, embed).
The link is checked via oEmbed on save and with a "check link" button
(not found / not embeddable is rejected, unreachable saves with a
warning). Start time can be entered or is taken from the link's
timecode; autoplay is optional. The presenter embeds the video via
youtube-nocookie.com; participants get the link plus a share button
(Web Share API, falls back to copying).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 15:08:12 +01:00
1 parent 93aeae1884
commit 1c509e31c2
14 files changed
+972 -24

No files matched your search

@@ -0,0 +1,184 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Http\TestSuite\HttpClientTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* YouTube slides: creating (incl. link check), presenter embed, participant link.
*/
class YouTubeSlideTest extends TestCase
{
use HttpClientTrait;
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 mockOembed(int $code, string $body = ''): void
{
// Registering a mock also makes unexpected requests fail instead of hitting the network.
$this->mockClientGet('https://www.youtube.com/*', $this->newClientResponse($code, ['Content-Type: application/json'], $body));
}
public function testAddSlideUsesTimecodeOfLinkAndVideoTitle(): void
{
[$poll, $token] = $this->createPoll();
$this->mockOembed(200, '{"title":"Never Gonna Give You Up"}');
$this->post("/admin/$token/slides/add?type=youtube", [
'type' => 'youtube',
'question' => '',
'settings' => ['url' => 'https://youtu.be/dQw4w9WgXcQ?t=43', 'start' => '', 'autoplay' => '1'],
]);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertSame('Never Gonna Give You Up', $slide->question);
$this->assertSame(['url' => 'https://youtu.be/dQw4w9WgXcQ?t=43', 'video_id' => 'dQw4w9WgXcQ', 'start' => 43, 'autoplay' => true], $slide->settings);
$this->assertFalse($slide->isInteractive());
}
public function testEnteredStartWins(): void
{
[$poll, $token] = $this->createPoll();
$this->mockOembed(200, '{"title":"Video"}');
$this->post("/admin/$token/slides/add?type=youtube", [
'type' => 'youtube',
'question' => 'Mein Titel',
'settings' => ['url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=43s', 'start' => '1:30'],
]);
$this->assertRedirect();
$slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail();
$this->assertSame('Mein Titel', $slide->question);
$this->assertSame(90, $slide->setting('start'));
$this->assertFalse($slide->setting('autoplay'));
}
public function testInvalidLinkAndStartAreRejected(): void
{
[$poll, $token] = $this->createPoll();
$this->post("/admin/$token/slides/add?type=youtube", [
'type' => 'youtube',
'question' => 'Video',
'settings' => ['url' => 'https://vimeo.com/123', 'start' => '1:99'],
]);
$this->assertResponseOk();
$this->assertResponseContains('kein gültiger YouTube-Link');
$this->assertResponseContains('Startzeit');
$this->assertResponseContains('value="1:99"');
$this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
}
public function testVideoThatMayNotBeEmbeddedIsRejected(): void
{
[$poll, $token] = $this->createPoll();
$this->mockOembed(401);
$this->post("/admin/$token/slides/add?type=youtube", [
'type' => 'youtube',
'question' => 'Video',
'settings' => ['url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'],
]);
$this->assertResponseOk();
$this->assertResponseContains('darf nicht eingebettet werden');
$this->assertSame(0, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
}
public function testUnreachableYouTubeSavesWithWarning(): void
{
[$poll, $token] = $this->createPoll();
$this->mockOembed(503);
$this->post("/admin/$token/slides/add?type=youtube", [
'type' => 'youtube',
'question' => 'Video',
'settings' => ['url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'],
]);
$this->assertRedirect();
$this->assertFlashMessageAt(0, 'YouTube war gerade nicht erreichbar, das Video konnte nicht geprüft werden. Bitte teste das Video vor deiner Präsentation.');
$this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count());
}
public function testCheckEndpoint(): void
{
[, $token] = $this->createPoll();
$this->mockOembed(200, '{"title":"Never Gonna Give You Up"}');
$this->post("/admin/$token/slides/youtube-check", ['url' => 'https://youtu.be/dQw4w9WgXcQ?t=90', 'start' => '']);
$this->assertResponseOk();
$this->assertHeader('Referrer-Policy', 'no-referrer');
$json = json_decode((string)$this->_response->getBody(), true);
$this->assertTrue($json['ok']);
$this->assertSame('Never Gonna Give You Up', $json['title']);
$this->assertSame('1:30', $json['linkStart']);
$this->assertStringContainsString('youtube-nocookie.com/embed/dQw4w9WgXcQ', $json['embedUrl']);
$this->assertStringContainsString('start=90', $json['embedUrl']);
$this->post("/admin/$token/slides/youtube-check", ['url' => 'kein link']);
$json = json_decode((string)$this->_response->getBody(), true);
$this->assertFalse($json['ok']);
}
public function testPresenterGetsEmbedAndParticipantGetsShareLink(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_YOUTUBE, [
'question' => 'Ein <Video>',
'settings' => ['url' => 'https://youtu.be/dQw4w9WgXcQ', 'video_id' => 'dQw4w9WgXcQ', 'start' => 90, 'autoplay' => true],
]);
$poll = $this->goLive($poll, $slide);
$this->get("/admin/$token/state");
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertFalse($state['slide']['isInteractive']);
$this->assertSame(
'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?rel=0&playsinline=1&start=90&autoplay=1',
$state['slide']['video']['embedUrl'],
);
$this->get('/p/' . $poll->join_code);
$this->assertResponseOk();
$this->assertResponseContains('Ein &lt;Video&gt;');
$this->assertResponseContains('href="https://www.youtube.com/watch?v=dQw4w9WgXcQ&amp;t=90s"');
$this->assertResponseContains('data-share');
$this->assertResponseNotContains('<iframe');
$this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id, 'text' => 'x']);
$this->assertSame(0, $this->slidesTable()->Responses->find()->where(['slide_id' => $slide->id])->count());
}
public function testEditFormShowsPreviewAndKeepsLinkTimecodeEmpty(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_YOUTUBE, [
'settings' => ['url' => 'https://youtu.be/dQw4w9WgXcQ?t=43', 'video_id' => 'dQw4w9WgXcQ', 'start' => 43, 'autoplay' => false],
]);
$this->get("/admin/$token/slides/{$slide->id}/edit");
$this->assertResponseOk();
$this->assertResponseContains('referrerpolicy="strict-origin"');
$this->assertResponseContains('id="youtube-start" name="settings[start]" class="input-short" maxlength="10"');
$this->assertResponseContains('value=""');
$this->assertResponseContains('placeholder="0:43"');
}
}
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Service;
use App\Model\Entity\Slide;
use App\Model\Table\SlidesTable;
use App\Service\YouTubeService;
use Cake\Http\TestSuite\HttpClientTrait;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* Link parsing, timecodes and the oEmbed check of YouTube slides.
*/
class YouTubeServiceTest extends TestCase
{
use HttpClientTrait;
/**
* @return array<string, array{0: string, 1: string|null, 2: int|null}>
*/
public static function urlProvider(): array
{
return [
'watch' => ['https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'dQw4w9WgXcQ', null],
'watch with t' => ['https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=43s', 'dQw4w9WgXcQ', 43],
'watch t first, playlist' => ['https://youtube.com/watch?t=1m30s&v=dQw4w9WgXcQ&list=PL123', 'dQw4w9WgXcQ', 90],
'short link' => ['https://youtu.be/dQw4w9WgXcQ?t=90', 'dQw4w9WgXcQ', 90],
'short link with si' => ['https://youtu.be/dQw4w9WgXcQ?si=AbC123', 'dQw4w9WgXcQ', null],
'without scheme' => ['youtu.be/dQw4w9WgXcQ', 'dQw4w9WgXcQ', null],
'mobile' => ['https://m.youtube.com/watch?v=dQw4w9WgXcQ#t=1h2m3s', 'dQw4w9WgXcQ', 3723],
'shorts' => ['https://www.youtube.com/shorts/dQw4w9WgXcQ', 'dQw4w9WgXcQ', null],
'live' => ['https://www.youtube.com/live/dQw4w9WgXcQ?feature=share', 'dQw4w9WgXcQ', null],
'embed with start' => ['https://www.youtube.com/embed/dQw4w9WgXcQ?start=12', 'dQw4w9WgXcQ', 12],
'nocookie' => ['https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ', 'dQw4w9WgXcQ', null],
'bare id' => ['dQw4w9WgXcQ', 'dQw4w9WgXcQ', null],
'other host' => ['https://vimeo.com/123456789', null, null],
'fake host' => ['https://youtube.com.evil.example/watch?v=dQw4w9WgXcQ', null, null],
'channel' => ['https://www.youtube.com/@somechannel', null, null],
'id too short' => ['https://youtu.be/abc', null, null],
'empty' => ['', null, null],
];
}
#[DataProvider('urlProvider')]
public function testParseUrl(string $url, ?string $id, ?int $start): void
{
$parsed = YouTubeService::parseUrl($url);
if ($id === null) {
$this->assertNull($parsed);
return;
}
$this->assertSame(['videoId' => $id, 'start' => $start], $parsed);
}
public function testParseStartInput(): void
{
$this->assertSame(90, YouTubeService::parseStartInput('1:30'));
$this->assertSame(3723, YouTubeService::parseStartInput('1:02:03'));
$this->assertSame(120, YouTubeService::parseStartInput('2'), 'a plain number means minutes');
$this->assertSame(90, YouTubeService::parseStartInput('1m30s'));
$this->assertSame(0, YouTubeService::parseStartInput('0'));
$this->assertNull(YouTubeService::parseStartInput('1:75'));
$this->assertNull(YouTubeService::parseStartInput('abc'));
$this->assertSame(YouTubeService::MAX_START, YouTubeService::parseStartInput('99999'));
}
public function testFormatAndUrls(): void
{
$this->assertSame('0:00', YouTubeService::formatTime(0));
$this->assertSame('1:30', YouTubeService::formatTime(90));
$this->assertSame('1:02:03', YouTubeService::formatTime(3723));
$this->assertSame('https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=90s', YouTubeService::watchUrl('dQw4w9WgXcQ', 90));
$this->assertSame('https://www.youtube.com/watch?v=dQw4w9WgXcQ', YouTubeService::watchUrl('dQw4w9WgXcQ'));
$this->assertSame(
'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?rel=0&playsinline=1&start=90&autoplay=1',
YouTubeService::embedUrl('dQw4w9WgXcQ', 90, true),
);
}
public function testSanitizeSettingsPrefersEnteredStartOverLinkTimecode(): void
{
$url = 'https://youtu.be/dQw4w9WgXcQ?t=43';
$fromLink = SlidesTable::sanitizeSettings(Slide::TYPE_YOUTUBE, ['url' => $url, 'start' => '', 'autoplay' => '1', 'x' => 1]);
$this->assertSame(['url' => $url, 'video_id' => 'dQw4w9WgXcQ', 'start' => 43, 'autoplay' => true], $fromLink);
$entered = SlidesTable::sanitizeSettings(Slide::TYPE_YOUTUBE, ['url' => $url, 'start' => '2:00']);
$this->assertSame(120, $entered['start']);
$this->assertFalse($entered['autoplay']);
$invalid = SlidesTable::sanitizeSettings(Slide::TYPE_YOUTUBE, ['url' => 'https://example.org']);
$this->assertSame('', $invalid['video_id']);
}
public function testCheckStatuses(): void
{
$service = new YouTubeService();
$this->mockClientGet('https://www.youtube.com/*', $this->newClientResponse(200, ['Content-Type: application/json'], '{"title":"Never Gonna Give You Up"}'));
$this->assertSame(['status' => YouTubeService::STATUS_OK, 'title' => 'Never Gonna Give You Up'], $service->check('dQw4w9WgXcQ'));
$this->cleanupMockResponses();
$this->mockClientGet('https://www.youtube.com/*', $this->newClientResponse(401, [], 'Unauthorized'));
$this->assertSame(YouTubeService::STATUS_NOT_EMBEDDABLE, $service->check('dQw4w9WgXcQ')['status']);
$this->cleanupMockResponses();
$this->mockClientGet('https://www.youtube.com/*', $this->newClientResponse(404, [], 'Not Found'));
$this->assertSame(YouTubeService::STATUS_NOT_FOUND, $service->check('dQw4w9WgXcQ')['status']);
$this->cleanupMockResponses();
$this->mockClientGet('https://www.youtube.com/*', $this->newClientResponse(503, [], ''));
$this->assertSame(YouTubeService::STATUS_UNREACHABLE, $service->check('dQw4w9WgXcQ')['status']);
}
}