Files
Plenario/tests/TestCase/Controller/YouTubeSlideTest.php
T
gnilebeinandClaude Opus 5.5 1c509e31c2
CI / Deploy / test (push) Successful in 46s
CI / Deploy / deploy (push) Skipped
Add YouTube video slide type
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>
2026-11-07 15:08:12 +01:00

185 lines
7.6 KiB
PHP

<?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"');
}
}