diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml new file mode 100644 index 000000000..013f6da11 --- /dev/null +++ b/config/packages/cache.yaml @@ -0,0 +1,17 @@ +framework: + cache: + pools: + # Bloc « Dernières vidéos » de la home : une heure suffit, une vidéo est + # publiée quelques jours après l'événement, pas à la minute près. + cache.latest_videos: + adapter: cache.app + default_lifetime: 3600 + +when@test: + framework: + cache: + pools: + # Pas de cache persistant en test : chaque scénario Behat recharge + # la base et peut changer la date courante. + cache.latest_videos: + adapter: cache.adapter.array diff --git a/db/seeds/TalksWithVideo.php b/db/seeds/TalksWithVideo.php new file mode 100644 index 000000000..2de3e8044 --- /dev/null +++ b/db/seeds/TalksWithVideo.php @@ -0,0 +1,88 @@ +table('afup_forum') + ->insert([ + 'id' => self::ID_EVENT, + 'titre' => 'Rencontres vidéo 2019', + 'path' => 'rencontres-video-2019', + 'nb_places' => 100, + 'date_debut' => '2019-05-23', + 'date_fin' => '2019-05-24', + 'annee' => 2019, + ]) + ->save(); + + $notEndedYet = new \DateTime('+10 days 10:00:00')->format('Y-m-d H:i:s'); + + $talks = [ + 100 => ['titre' => 'PHP 8 en production', 'fin' => '2019-05-24 10:00:00'], + 101 => ['titre' => 'Les fibers expliquées', 'fin' => '2019-05-24 17:00:00'], + 102 => ['titre' => 'Sécuriser une API', 'fin' => '2019-05-24 11:00:00'], + 103 => ['titre' => 'Domain-Driven Design au quotidien', 'fin' => '2019-05-24 15:00:00'], + 104 => ['titre' => 'Le plus ancien talk en vidéo', 'fin' => '2019-05-23 10:00:00'], + 105 => ['titre' => 'Une clinique filmée', 'fin' => '2019-05-24 18:00:00', 'genre' => 6], + 106 => ['titre' => 'Un talk sans vidéo', 'fin' => '2019-05-24 18:00:00', 'youtube_id' => ''], + 107 => ['titre' => 'Un talk non planifié', 'fin' => '2019-05-24 18:00:00', 'plannifie' => 0], + 108 => ['titre' => 'Un talk au créneau pas terminé', 'fin' => $notEndedYet], + ]; + + $sessions = []; + $speakers = []; + $plannings = []; + foreach ($talks as $sessionId => $talk) { + $sessions[] = [ + 'session_id' => $sessionId, + 'id_forum' => self::ID_EVENT, + 'date_soumission' => '2019-01-15', + 'titre' => $talk['titre'], + 'abstract' => 'Résumé du talk « ' . $talk['titre'] . ' ».', + 'genre' => $talk['genre'] ?? 1, + 'skill' => 0, + 'plannifie' => $talk['plannifie'] ?? 1, + 'youtube_id' => $talk['youtube_id'] ?? 'video' . $sessionId, + 'language_code' => 'fr', + ]; + $speakers[] = [ + 'session_id' => $sessionId, + 'conferencier_id' => Conferenciers::ID_CONFERENCIER, + ]; + $end = new \DateTime($talk['fin']); + $plannings[] = [ + 'id' => $sessionId, + 'id_session' => $sessionId, + 'debut' => (clone $end)->modify('-40 minutes')->format('U'), + 'fin' => $end->format('U'), + 'id_forum' => self::ID_EVENT, + 'keynote' => 0, + ]; + } + + $this->table('afup_sessions')->insert($sessions)->save(); + $this->table('afup_conferenciers_sessions')->insert($speakers)->save(); + $this->table('afup_forum_planning')->insert($plannings)->save(); + } +} diff --git a/sources/AppBundle/Controller/Website/Global/HomeAction.php b/sources/AppBundle/Controller/Website/Global/HomeAction.php index c0bb5ff62..7448cb5b2 100644 --- a/sources/AppBundle/Controller/Website/Global/HomeAction.php +++ b/sources/AppBundle/Controller/Website/Global/HomeAction.php @@ -6,6 +6,7 @@ use AppBundle\Event\Entity\Repository\MeetupRepository; use AppBundle\Event\Model\Repository\EventRepository; +use AppBundle\Event\Talk\LatestVideos; use AppBundle\Site\ArticleImageStorage; use AppBundle\Site\Entity\Repository\ArticleRepository; use AppBundle\Twig\ViewRenderer; @@ -20,6 +21,7 @@ public function __construct( private readonly MeetupRepository $meetupRepository, private readonly EventRepository $eventRepository, private readonly ArticleImageStorage $articleImageStorage, + private readonly LatestVideos $latestVideos, ) {} public function __invoke(): Response @@ -32,6 +34,7 @@ public function __invoke(): Response 'meetups' => $meetups, 'lastArticleImageUrl' => $this->articleImageStorage->getUrl($articles[0]) ?? 'images/article-default-image.svg', 'currentEvent' => $this->eventRepository->getMostRecentEvent(), + 'latestVideos' => $this->latestVideos->get(), ]); } } diff --git a/sources/AppBundle/Event/Talk/LatestVideo.php b/sources/AppBundle/Event/Talk/LatestVideo.php new file mode 100644 index 000000000..8b1efb123 --- /dev/null +++ b/sources/AppBundle/Event/Talk/LatestVideo.php @@ -0,0 +1,25 @@ +youtubeId) . '/hqdefault.jpg'; + } +} diff --git a/sources/AppBundle/Event/Talk/LatestVideoRepository.php b/sources/AppBundle/Event/Talk/LatestVideoRepository.php new file mode 100644 index 000000000..61bf3a2e1 --- /dev/null +++ b/sources/AppBundle/Event/Talk/LatestVideoRepository.php @@ -0,0 +1,64 @@ + + */ + public function getLatest(\DateTimeInterface $now, int $limit): array + { + /** @var list $rows */ + $rows = $this->connection->fetchAllAssociative( + 'SELECT talk.session_id, talk.titre, talk.youtube_id, event.titre AS event_title + FROM afup_sessions AS talk + INNER JOIN ( + SELECT id_session, MAX(fin) AS fin + FROM afup_forum_planning + GROUP BY id_session + ) AS planning ON planning.id_session = talk.session_id + INNER JOIN afup_forum AS event ON event.id = talk.id_forum + WHERE talk.plannifie = 1 + AND talk.genre NOT IN (:clinic, :speakerIntroductions) + AND LENGTH(talk.youtube_id) > 0 + AND (talk.date_publication IS NULL OR talk.date_publication < :now) + AND planning.fin <= :nowTimestamp + ORDER BY planning.fin DESC, talk.session_id DESC + LIMIT :limit', + [ + 'clinic' => Talk::TYPE_CLINIC, + 'speakerIntroductions' => Talk::TYPE_SPEAKER_INTRODUCTIONS, + 'now' => $now->format('Y-m-d H:i:s'), + 'nowTimestamp' => $now->getTimestamp(), + 'limit' => $limit, + ], + ['limit' => ParameterType::INTEGER], + ); + + return array_map( + static fn(array $row): LatestVideo => new LatestVideo( + (int) $row['session_id'], + // Slug construit par le modèle, pour rester identique à celui de la page talk + (new Talk())->setTitle($row['titre'])->getSlug(), + $row['titre'], + $row['youtube_id'], + $row['event_title'], + ), + $rows, + ); + } +} diff --git a/sources/AppBundle/Event/Talk/LatestVideos.php b/sources/AppBundle/Event/Talk/LatestVideos.php new file mode 100644 index 000000000..a8e5e2952 --- /dev/null +++ b/sources/AppBundle/Event/Talk/LatestVideos.php @@ -0,0 +1,41 @@ + + */ + public function get(): array + { + $cacheItem = $this->cache->getItem(self::CACHE_KEY); + + if (!$cacheItem->isHit()) { + $cacheItem->set($this->latestVideoRepository->getLatest($this->clock->now(), self::LIMIT)); + $this->cache->save($cacheItem); + } + + /** @var list $videos */ + $videos = $cacheItem->get(); + + return $videos; + } +} diff --git a/templates/site/home.html.twig b/templates/site/home.html.twig index f166f9134..4030f7864 100644 --- a/templates/site/home.html.twig +++ b/templates/site/home.html.twig @@ -173,6 +173,41 @@ + {% if latestVideos is not empty %} +
+

+ Dernières vidéos +

+ + + + + Toutes les vidéos + +
+ {% endif %} +
Dans les antennes diff --git a/tests/behat/features/PublicSite/Home.feature b/tests/behat/features/PublicSite/Home.feature index 45ce13746..572ebb70c 100644 --- a/tests/behat/features/PublicSite/Home.feature +++ b/tests/behat/features/PublicSite/Home.feature @@ -45,3 +45,31 @@ Feature: Site Public - Home When I am on "/home" Then the response status code should be 301 And the response header "Location" should match "#^(https?://[^/]+)?/$#" + + @reloadDbWithTestData + Scenario: Bloc des dernières vidéos + Given I am on the homepage + Then I should see "Dernières vidéos" in the "#dernieres-videos h2" element + # Les 6 talks publiés avec vidéo dont le créneau s'est terminé le plus récemment, du plus récent au plus ancien + And I should see 6 "#dernieres-videos li" elements + And I should see "REST ou GraphQL ? Exemples illustrés avec Symfony et API Platform" in the "#dernieres-videos li:nth-child(1)" element + And the "#dernieres-videos li:nth-child(1)" element should contain "href=\"/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform\"" + And the "#dernieres-videos li:nth-child(1)" element should contain "https://img.youtube.com/vi/QhAToFl_Omo/hqdefault.jpg" + And I should see "forum" in the "#dernieres-videos li:nth-child(1)" element + And I should see "Jouons tous ensemble à un petit jeu" in the "#dernieres-videos li:nth-child(2)" element + And I should see "Les fibers expliquées" in the "#dernieres-videos li:nth-child(3)" element + And the "#dernieres-videos li:nth-child(3)" element should contain "href=\"/talks/101-les-fibers-expliquees\"" + And I should see "Rencontres vidéo 2019" in the "#dernieres-videos li:nth-child(3)" element + And I should see "Domain-Driven Design au quotidien" in the "#dernieres-videos li:nth-child(4)" element + And I should see "Sécuriser une API" in the "#dernieres-videos li:nth-child(5)" element + And I should see "PHP 8 en production" in the "#dernieres-videos li:nth-child(6)" element + # Au-delà des 6 plus récents + And I should not see "Le plus ancien talk en vidéo" in the "#dernieres-videos" element + # Talks non publiés ou sans vidéo + And I should not see "Révolutionons PHP" in the "#dernieres-videos" element + And I should not see "Une clinique filmée" in the "#dernieres-videos" element + And I should not see "Un talk sans vidéo" in the "#dernieres-videos" element + And I should not see "Un talk non planifié" in the "#dernieres-videos" element + And I should not see "Un talk au créneau pas terminé" in the "#dernieres-videos" element + When I follow "Toutes les vidéos" + Then the url should match "/talks/$" diff --git a/tests/unit/AppBundle/Event/Talk/LatestVideosTest.php b/tests/unit/AppBundle/Event/Talk/LatestVideosTest.php new file mode 100644 index 000000000..f849cea20 --- /dev/null +++ b/tests/unit/AppBundle/Event/Talk/LatestVideosTest.php @@ -0,0 +1,41 @@ +createMock(Connection::class); + $connection + ->expects(self::once()) + ->method('fetchAllAssociative') + ->willReturn([[ + 'session_id' => '42', + 'titre' => 'Les fibers expliquées', + 'youtube_id' => 'abc123', + 'event_title' => 'Forum PHP 2026', + ]]); + + $latestVideos = new LatestVideos( + new LatestVideoRepository($connection), + new MockClock(new \DateTimeImmutable('2026-11-17 12:00:00')), + new ArrayAdapter(), + ); + + $expected = [new LatestVideo(42, 'les-fibers-expliquees', 'Les fibers expliquées', 'abc123', 'Forum PHP 2026')]; + self::assertEquals($expected, $latestVideos->get()); + // Le second appel est servi par le cache : la base n'est interrogée qu'une fois. + self::assertEquals($expected, $latestVideos->get()); + } +}