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>
This commit is contained in:
1 parent
93aeae1884
commit
1c509e31c2
14 files changed
+972
-24
No files matched your search
@@ -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']);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user