Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions sources/AppBundle/Twig/MetaDescriptionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace AppBundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* Transforme un contenu (HTML, texte avec entités) en texte brut utilisable
* dans une balise <meta name="description"> : balises retirées, entités décodées,
* espaces normalisés, troncature sur une frontière de mot.
*/
final class MetaDescriptionExtension extends AbstractExtension
{
public const int MAX_LENGTH = 155;

private const string ELLIPSIS = '…';

#[\Override]
public function getFilters(): array
{
return [
new TwigFilter('meta_description', $this->metaDescription(...)),
];
}

public function metaDescription(string|\Stringable|null $content, int $maxLength = self::MAX_LENGTH): string
{
$content = (string) $content;

// Sans ce séparateur, « <p>Un</p><p>Deux</p> » donnerait « UnDeux ».
$content = preg_replace('#<(br|/p|/div|/li|/h[1-6]|/blockquote|/pre|/tr|/td|/th)\b[^>]*>#i', '$0 ', $content);
$text = html_entity_decode(strip_tags((string) $content), ENT_QUOTES | ENT_HTML5, 'UTF-8');
$text = trim((string) preg_replace('/\s+/u', ' ', $text));

if (mb_strlen($text) <= $maxLength) {
return $text;
}

$truncated = mb_substr($text, 0, $maxLength - mb_strlen(self::ELLIPSIS) + 1);
$lastSpace = mb_strrpos($truncated, ' ');
if ($lastSpace !== false) {
$truncated = mb_substr($truncated, 0, $lastSpace);
} else {
$truncated = mb_substr($truncated, 0, -1);
}

return rtrim($truncated, ' ,;:.-–—') . self::ELLIPSIS;
}
}
7 changes: 7 additions & 0 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{% endblock %}
{# Texte de la page, converti en texte brut tronqué à 155 caractères. Un champ HTML doit passer
par |meta_description dans le bloc : sinon ses entités, échappées par Twig, resteraient visibles. #}
{% set meta_description_content %}{% block description %}{% endblock %}{% endset %}
{% set meta_description_content = meta_description_content|meta_description %}
{% if meta_description_content is not empty %}
<meta name="description" content="{{ meta_description_content }}">
{% endif %}
{% block canonical %}
{% if app.request.attributes.get('_route') %}
<link rel="canonical" href="{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}" />
Expand Down
8 changes: 8 additions & 0 deletions templates/layouts/root.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
{% block metas %}{% endblock %}

{# Texte de la page, converti en texte brut tronqué à 155 caractères. Un champ HTML doit passer
par |meta_description dans le bloc : sinon ses entités, échappées par Twig, resteraient visibles. #}
{% set meta_description_content %}{% block description %}{% endblock %}{% endset %}
{% set meta_description_content = meta_description_content|meta_description %}
{% if meta_description_content is not empty %}
<meta name="description" content="{{ meta_description_content }}">
{% endif %}

<title>{% block title %}AFUP{% endblock %}</title>

<link rel="icon" type="image/x-icon" href="{{ asset('/images/favicon.ico') }}"/>
Expand Down
20 changes: 12 additions & 8 deletions templates/site/home.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% extends 'layouts/site.html.twig' %}

{% block title %}AFUP – Association française des utilisateurs de PHP{% endblock %}

{% block description %}Association française des utilisateurs de PHP depuis 2000 : Forum PHP, AFUP Day, meetups en région, vidéos des conférences et veille technique.{% endblock %}

{% block metas %}
{{ parent() }}
<script type="application/ld+json">
Expand Down Expand Up @@ -39,9 +43,9 @@

<div class="container mx-auto p-4 sm:py-10 flex flex-col gap-10">
<section class="w-full">
<h1 class="text-afup-800 font-titre text-2xl sm:text-4xl border-b-2 border-neutre-300 pb-3 mb-5 hidden sm:block">
<h2 class="text-afup-800 font-titre text-2xl sm:text-4xl border-b-2 border-neutre-300 pb-3 mb-5 hidden sm:block">
Actualités
</h1>
</h2>

<div class="grid grid-cols-1 sm:grid-cols-3 gap-5">
<twig:Card class="col-span-2 p-0 flex-col overflow-hidden hidden sm:flex">
Expand Down Expand Up @@ -118,9 +122,9 @@
</section>

<section class="w-full">
<h1 class="text-afup-800 font-titre text-2xl sm:text-4xl border-b-2 border-neutre-300 pb-3 mb-5 hidden sm:block">
<h2 class="text-afup-800 font-titre text-2xl sm:text-4xl border-b-2 border-neutre-300 pb-3 mb-5 hidden sm:block">
{{ currentEvent.title }}
</h1>
</h2>

<div class="flex flex-col sm:grid sm:grid-cols-2">
<div class="relative isolate bg-cover bg-center grow flex items-center justify-center rounded-t-xl rounded-bl-none sm:rounded-l-xl sm:rounded-tr-none
Expand All @@ -129,9 +133,9 @@
<img src="{{ asset('images/forumphp-2026-vecto-light.svg') }}" class="min-h-30 sm:min-h-60 m-20" alt="Logo {{ currentEvent.title }}" />
</div>
<div class="rounded-b-xl rounded-tr-none sm:rounded-r-xl sm:rounded-bl-none p-4 sm:p-10 flex flex-col gap-10 bg-linear-to-b from-afup-700 to-afup-300 text-white">
<h1 class="font-titre text-4xl">
<h2 class="font-titre text-4xl">
Participez au {{ currentEvent.title }}
</h1>
</h2>
<p>
Le rendez-vous incontournable de la communauté PHP en France. Rejoignez-nous les 8 et 9 octobre 2026
pour deux jours de conférences, d'ateliers et de networking.
Expand Down Expand Up @@ -188,9 +192,9 @@
<h2 class="flex items-center gap-1 uppercase font-bold text-sm">
<twig:UX:Icon name="akar-icons:location" class="size-4" /> {{ office_name(meetup.codeAntenne) }}
</h2>
<h1 class="text-titre font-light">
<h2 class="text-titre font-light">
{{ meetup.titre }}
</h1>
</h2>
</div>
</article>
</a>
Expand Down
2 changes: 2 additions & 0 deletions templates/site/news/display.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

{% block title %}{{ article.titre }} - AFUP{% endblock %}

{% block description %}{{ (article.chapeau ?: article.contenu)|markdown_to_html|meta_description }}{% endblock %}

{% block content %}

<div class="container mx-auto sm:pt-10 flex flex-col sm:flex-row gap-5">
Expand Down
4 changes: 4 additions & 0 deletions templates/site/news/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% block title %}Les actualités de l'AFUP, page {{ current_page }}{% endblock %}

{% block description %}Les actualités de l'AFUP : annonces du Forum PHP et de l'AFUP Day, vie de l'association, des antennes locales et de la communauté PHP.{% endblock %}

{% block canonical %}
{% if current_page == 1 %}
<link rel="canonical" href="{{ url('news_list') }}" />
Expand All @@ -14,6 +16,8 @@

<div class="container mx-auto px-4 py-6 sm:px-0 sm:py-10 flex flex-row sm:flex-row gap-5">
<div class="flex flex-col gap-10 sm:gap-12 flex-grow">
<h1 class="font-titre text-afup-800 text-2xl sm:text-4xl">Actualités de l'AFUP</h1>

{% for article in articles %}
<article>
<h2 class="text-afup-800 text-xl sm:text-2xl">
Expand Down
2 changes: 2 additions & 0 deletions templates/site/talks/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% block title %}{{ title }}{% endblock %}

{% block description %}Les vidéos des conférences de l'AFUP (Forum PHP, AFUP Day, PHP Tour), à rechercher par sujet, speaker ou événement.{% endblock %}

{% block canonical %}
<link rel="canonical" href="{{ canonical }}" />
{% endblock %}
Expand Down
2 changes: 2 additions & 0 deletions templates/site/talks/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% block title %}{{ talk.title }} - {{ event.title }}{% endblock %}

{% block description %}{{ talk.description|meta_description }}{% endblock %}

{% block metas %}
{{ parent() }}
<meta name="twitter:card" content="summary">
Expand Down
35 changes: 35 additions & 0 deletions tests/behat/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,41 @@ public function allLinksShouldHaveRel(string $href, string $rel): void
}
}

#[Then('the page should have a meta description')]
public function assertMetaDescriptionIsNotEmpty(): void
{
if ($this->getMetaDescription() === '') {
throw new ExpectationException('The meta description is empty', $this->minkContext->getSession()->getDriver());
}
}

#[Then('the meta description should be :expected')]
public function assertMetaDescriptionEquals(string $expected): void
{
$actual = $this->getMetaDescription();

if ($actual !== $expected) {
throw new ExpectationException(
sprintf('The meta description is "%s", "%s" expected', $actual, $expected),
$this->minkContext->getSession()->getDriver(),
);
}
}

private function getMetaDescription(): string
{
$metas = $this->minkContext->getSession()->getPage()->findAll('css', 'meta[name="description"]');

if (count($metas) !== 1) {
throw new ExpectationException(
sprintf('Exactly one meta description expected, %d found', count($metas)),
$this->minkContext->getSession()->getDriver(),
);
}

return trim((string) $metas[0]->getAttribute('content'));
}

#[Then('/^(?:|I )click on link with (class|id) "(?P<text>(?:[^"]|\\")*)"$/')]
public function clickOnLink(string $type, string $text): void
{
Expand Down
10 changes: 10 additions & 0 deletions tests/behat/features/PublicSite/Home.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ 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: Title, meta description et titre unique de la home
Given the current date is "2026-01-01"
And I am on the homepage
Then the response should contain "<title>AFUP – Association française des utilisateurs de PHP</title>"
And the page should have a meta description
And the meta description should be "Association française des utilisateurs de PHP depuis 2000 : Forum PHP, AFUP Day, meetups en région, vidéos des conférences et veille technique."
And I should see 1 "h1" elements
And the "h1" element should contain "AFUP : Association Française"
14 changes: 14 additions & 0 deletions tests/behat/features/PublicSite/News.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ Feature: Site Public - News
"""
<a href="https://afup.org">commodi delectus</a>
"""

@reloadDbWithTestData
Scenario: Titre et meta description de la liste des actualités
Given I am on "/news/"
Then I should see 1 "h1" elements
And the "h1" element should contain "Actualités de l'AFUP"
And the page should have a meta description

@reloadDbWithTestData
Scenario: Meta description d'un article tirée de son chapeau, sans Markdown
Given I am on "/news/"
When I follow "Lire l'article: Un article en Markdown"
Then the page should have a meta description
And the meta description should be "Un peu de text avec de la mise en forme"
11 changes: 11 additions & 0 deletions tests/behat/features/PublicSite/Talks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ Feature: Site Public - Talks
When I go to "/talks/6-revolutionons-php"
Then the response status code should be 200
And the response should not contain a JSON-LD "VideoObject"

@reloadDbWithTestData
Scenario: Meta description d'un talk tirée de son résumé, sans balises et tronquée
When I go to "/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform"
Then the page should have a meta description
And the meta description should be "GraphQL est une alternative aux architectures REST pour la réalisation d’API web. Le langage promu par Facebook a des avantages indéniab les…"

@reloadDbWithTestData
Scenario: Meta description de la liste des vidéos
When I go to "/talks/"
Then the page should have a meta description
71 changes: 71 additions & 0 deletions tests/unit/AppBundle/Twig/MetaDescriptionExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace AppBundle\Tests\Twig;

use AppBundle\Twig\MetaDescriptionExtension;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class MetaDescriptionExtensionTest extends TestCase
{
#[DataProvider('contentProvider')]
public function testMetaDescription(?string $content, string $expected): void
{
self::assertSame($expected, new MetaDescriptionExtension()->metaDescription($content));
}

public static function contentProvider(): \Generator
{
yield 'contenu vide' => [null, ''];
yield 'texte court inchangé' => ['Une conférence sur PHP.', 'Une conférence sur PHP.'];
yield 'balises retirées' => ['<p>Une <strong>conférence</strong> sur <a href="https://php.net">PHP</a>.</p>', 'Une conférence sur PHP.'];
yield 'blocs séparés par une espace' => ["<h2>Titre</h2><p>Premier.</p><ul><li>Un</li><li>Deux</li></ul>Fin<br>ligne", 'Titre Premier. Un Deux Fin ligne'];
yield 'entités décodées' => ['L&#039;AFUP &amp; la communauté &laquo;&nbsp;PHP&nbsp;&raquo; &eacute;t&eacute;', "L'AFUP & la communauté « PHP » été"];
yield 'espaces normalisés' => [" Un\n\n texte\t indenté ", 'Un texte indenté'];
}

public function testTruncatesOnWordBoundary(): void
{
$content = str_repeat('mot ', 35) . 'conférence finale';

$result = new MetaDescriptionExtension()->metaDescription($content);

self::assertLessThanOrEqual(MetaDescriptionExtension::MAX_LENGTH, mb_strlen($result));
self::assertSame(str_repeat('mot ', 35) . 'conférence…', $result);
}

public function testExactMaxLengthIsKept(): void
{
$content = str_repeat('a', 150) . ' éèàù';

self::assertSame($content, new MetaDescriptionExtension()->metaDescription($content));
}

public function testDoesNotKeepTrailingPunctuationBeforeEllipsis(): void
{
$content = str_repeat('a', 145) . ' fin, ' . str_repeat('b', 20);

self::assertSame(str_repeat('a', 145) . ' fin…', new MetaDescriptionExtension()->metaDescription($content));
}

public function testTruncatesSingleLongWord(): void
{
$result = new MetaDescriptionExtension()->metaDescription(str_repeat('a', 200));

self::assertSame(str_repeat('a', 154) . '…', $result);
}

public function testCustomLength(): void
{
self::assertSame('Une…', new MetaDescriptionExtension()->metaDescription('Une conférence', 10));
}

public function testTagsAreStrippedBeforeMeasuring(): void
{
$content = '<p class="' . str_repeat('x', 300) . '">Texte court</p>';

self::assertSame('Texte court', new MetaDescriptionExtension()->metaDescription($content));
}
}
Loading