Skip to content
Merged
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
1 change: 0 additions & 1 deletion assets/seo/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SeoSnippet {
title: document.querySelector('#seofields-title'),
description: document.querySelector('#seofields-description'),
keywords: document.querySelector('#seofields-keywords'),
shortlink: document.querySelector('#seofields-shortlink'),
canonical: document.querySelector('#seofields-canonical'),
robots: document.querySelector('#seofields-robots'),
og: document.querySelector('#seofields-og'),
Expand Down
27 changes: 0 additions & 27 deletions templates/seo.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
title: '',
description: '',
keywords: '',
shortlink: '',
canonical: '',
robots: '',
og: ''
Expand All @@ -26,7 +25,6 @@
{{ block('seo_description') }}
{{ block('seo_keywords') }}
{{ block('seo_snippet') }}
{{ block('seo_shortlink') }}
{{ block('seo_canonical') }}
{{ block('seo_robots') }}
{{ block('seo_og') }}
Expand Down Expand Up @@ -147,31 +145,6 @@
</div>
{% endblock %}

{% block seo_shortlink %}
<div id="field-text-{{ name }}-shortlink" class="form-group in-compound is-normal">
<label for="field-{{ name }}-shortlink" class="editor--label">
{{ __("Shortlink") }} ({{ __("alias") }}):
</label>

<div>
<input
name="{{ name }}[shortlink]"
class="form-control"
id="seofields-shortlink"
maxlength="255"
type="text"
value="{{ seovalues.shortlink|default('')|trim }}"
/>
</div>

<div id="field-{{ name }}-shortlink_postfix" class="form--helper">
<p>
{{ __("Use this to create an alias or shortlink to this record. Be sure to make it an absolute link") }}.
</p>
</div>
</div>
{% endblock %}

{% block seo_canonical %}
<div id="field-text-{{ name }}-canonical" class="form-group in-compound is-normal">
<label for="field-{{ name }}-canonical" class="editor--label">
Expand Down
85 changes: 85 additions & 0 deletions tests/Twig/SeoFieldScriptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Appolo\BoltSeo\Tests\Twig;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;

/**
* assets/seo/seo.js looks up the SEO inputs by id (`#seofields-title`, …) and adds
* an event listener to each one without a null check. If templates/seo.html.twig
* stops rendering one of them, the script throws while it starts, and the inputs
* after it in the list stop saving. This renders the whole field, with the
* keywords field both disabled (a hidden input) and enabled, and checks that every
* id the script asks for is there exactly once.
*/
class SeoFieldScriptTest extends TestCase
{
private static function renderField(int $keywordsLength): string
{
$twig = new Environment(new ChainLoader([
new FilesystemLoader(\dirname(__DIR__, 2) . '/templates'),
new ArrayLoader([
'@bolt/_partials/fields/_base.html.twig' => '{% block field %}{% endblock %}',
'@bolt/_partials/fields/_label.html.twig' => '',
]),
]));

$twig->addTest(new TwigTest('json', static fn (): bool => true));
$twig->addFilter(new TwigFilter('json_decode', static fn (string $json): mixed => json_decode($json, true)));
$twig->addFunction(new TwigFunction('__', static fn (string $text): string => $text));
$twig->addFunction(new TwigFunction('seoGetConfig', static fn (): array => ['keywords_length' => $keywordsLength]));
$twig->addFunction(new TwigFunction('seoFieldDefinition', static fn (): null => null));
$twig->addFunction(new TwigFunction('seoFieldValue', static fn (): string => ''));
$twig->addFunction(new TwigFunction('canonical', static fn (): string => ''));

return $twig->load('seo.html.twig')->renderBlock('field', [
'name' => 'fields[seo]',
'id' => 'field-seo',
'value' => '',
'record' => ['definition' => ['singular_slug' => 'page', 'record_route' => 'record']],
]);
}

/**
* @return list<string>
*/
private static function scriptInputIds(): array
{
$script = (string) file_get_contents(\dirname(__DIR__, 2) . '/assets/seo/seo.js');
preg_match_all("/querySelector\\('#(seofields-[a-z]+)'\\)/", $script, $matches);

return $matches[1];
}

/**
* @return iterable<string, array{int}>
*/
public static function keywordsLengths(): iterable
{
yield 'keywords disabled' => [0];
yield 'keywords enabled' => [255];
}

#[DataProvider('keywordsLengths')]
public function testEveryInputTheScriptUsesIsRendered(int $keywordsLength): void
{
$html = self::renderField($keywordsLength);

$ids = self::scriptInputIds();
self::assertNotEmpty($ids);

foreach ($ids as $id) {
self::assertSame(1, mb_substr_count($html, 'id="' . $id . '"'), "seo.html.twig should render #{$id} exactly once");
}
}
}
18 changes: 0 additions & 18 deletions translations/messages.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>vychozi-titulek</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Zkrácený odkaz</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Kanonický odkaz</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Použijte k vytvoření aliasu nebo zkráceného odkazu na tento záznam. Ujistěte se, že se jedná o absolutní odkaz</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>standardtitel</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Kurzlink</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>Alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Canonical-Link</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Verwenden Sie dies, um einen Alias oder Kurzlink zu diesem Datensatz zu erstellen. Achten Sie darauf, einen absoluten Link zu verwenden</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>default-title</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Shortlink</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Canonical Link</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>titulo-predeterminado</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Enlace corto</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Enlace canónico</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Úselo para crear un alias o enlace corto a este registro. Asegúrese de que sea un enlace absoluto</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>titre-par-defaut</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Lien court</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Lien canonique</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Utilisez-le pour créer un alias ou un lien court vers cet enregistrement. Assurez-vous qu'il soit un lien absolu</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.hu.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>alapertelmezett-cim</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Rövid link</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Kanonikus link</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Használja ezt aliasz vagy rövid link létrehozására ehhez a rekordhoz. Ügyeljen arra, hogy abszolút link legyen</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>titolo-predefinito</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Link breve</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Link canonico</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Usalo per creare un alias o un link breve a questo record. Assicurati che sia un link assoluto</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
18 changes: 0 additions & 18 deletions translations/messages.nl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,12 @@
<target>standaardtitel</target>
</segment>
</unit>
<unit id="ORnQHm." name="Shortlink">
<segment>
<source>Shortlink</source>
<target>Verkorte link</target>
</segment>
</unit>
<unit id="GgpqNso" name="alias">
<segment>
<source>alias</source>
<target>alias</target>
</segment>
</unit>
<unit id="..3kd27" name="Canonical Link">
<segment>
<source>Canonical Link</source>
<target>Canonieke link</target>
</segment>
</unit>
<unit id="V.BIy_i" name="Use this to create an alias or shortlink to this record. Be sure to make it an absolute link">
<segment>
<source>Use this to create an alias or shortlink to this record. Be sure to make it an absolute link</source>
<target>Gebruik dit om een alias of verkorte link naar dit record te maken. Zorg ervoor dat het een absolute link is</target>
</segment>
</unit>
<unit id="FwLQ5cl" name="Use with caution, and only if you know what you're doing">
<segment>
<source>Use with caution, and only if you know what you're doing</source>
Expand Down
Loading
Loading