From 96bc3e0b0cd856d9dd7c726203f609738572263c Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 7 Oct 2026 14:30:37 +0000 Subject: [PATCH 1/2] Add 2 hooks to add short code documentation for external modules --- .../ShortcodeDocumentationProvider.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Service/ShortcodeDocumentationProvider.php b/src/Service/ShortcodeDocumentationProvider.php index 068f3a6..65c18a9 100644 --- a/src/Service/ShortcodeDocumentationProvider.php +++ b/src/Service/ShortcodeDocumentationProvider.php @@ -41,7 +41,7 @@ class ShortcodeDocumentationProvider * * @return array> */ - public static function getDocumentation(Module $module): array + public static function getDocumentation(Module $module): array { $context = Context::getContext(); $idLang = (int) $context->language->id; @@ -53,7 +53,15 @@ public static function getDocumentation(Module $module): array $translator = $context->getTranslator(); $domain = 'Modules.Everblocklight.Shortcodes'; - $docs = [ + $docs = []; + + \Hook::exec('actionBeforeEverblocklightShortcodeDocumentation', [ + 'translator' => $translator, + 'domain' => $domain, + 'docs' => &$docs, + ]); + + $docsCore = [ [ 'title' => $translator->trans('Catalog & merchandising', [], $domain), 'entries' => [ @@ -664,6 +672,11 @@ public static function getDocumentation(Module $module): array ], ], ], + [ + 'code' => '[wordpress-posts]', + 'description' => $translator->trans('Display the latest posts retrieved from a connected WordPress site.', [], $domain), + 'parameters' => [], + ], [ 'code' => '[googlereviews place_id="YOUR_PLACE_ID" limit="6" min_rating="4"]', 'description' => $translator->trans('Showcase reviews from your Google Business profile.', [], $domain), @@ -710,6 +723,11 @@ public static function getDocumentation(Module $module): array ], ], ], + [ + 'code' => '[everinstagram]', + 'description' => $translator->trans('Display the latest Instagram media fetched by the module.', [], $domain), + 'parameters' => [], + ], [ 'code' => '[llorem]', 'description' => $translator->trans('Insert placeholder lorem ipsum text according to module settings.', [], $domain), @@ -861,9 +879,16 @@ public static function getDocumentation(Module $module): array ], ], ]; + $docs = array_merge($docsCore, $docs); static::$cache[$idLang] = $docs; + \Hook::exec('actionAfterEverblocklightShortcodeDocumentation', [ + 'translator' => $translator, + 'domain' => $domain, + 'docs' => &$docs, + ]); + return $docs; } } From 5c89c136b936ed588b96b1a35266305a7e6e6a4d Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 7 Oct 2026 16:39:36 +0200 Subject: [PATCH 2/2] Fix shortcode documentation hooks: drop removed shortcodes, cache after hooks, declare hooks - Remove [wordpress-posts] and [everinstagram] entries reintroduced by mistake (features removed in 28c9cc4; failed testNoReferenceToRemovedFeatures) - Groups added via actionBeforeEverblocklightShortcodeDocumentation are now listed before the module groups - Cache the documentation after actionAfterEverblocklightShortcodeDocumentation so later calls keep what external modules added - Declare both hooks in getCustomHooks() so they are created at install - Restore getDocumentation() indentation - Add ShortcodeDocumentationTest (+ Hook/Module/translator stubs) Co-Authored-By: Claude Opus 5.5 --- everblocklight.php | 2 + .../ShortcodeDocumentationProvider.php | 21 ++--- tests/Unit/ShortcodeDocumentationTest.php | 80 +++++++++++++++++++ tests/Unit/TestCase.php | 1 + tests/stubs/prestashop.php | 40 ++++++++++ 5 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 tests/Unit/ShortcodeDocumentationTest.php diff --git a/everblocklight.php b/everblocklight.php index c422c57..a4380be 100644 --- a/everblocklight.php +++ b/everblocklight.php @@ -259,6 +259,8 @@ private function getCustomHooks(): array ['displayAfterLocatorStore', 'display after store content on store locator', 'This hook triggers after store content on store locator'], ['displayBeforeProductMiniature', 'display before product miniature', 'This hook triggers before product miniature is rendered'], ['displayAfterProductMiniature', 'display after product miniature', 'This hook triggers after product miniature is rendered'], + ['actionBeforeEverblocklightShortcodeDocumentation', 'Before Ever Block Light shortcode documentation', 'Lets modules add shortcode documentation groups displayed before the module ones (params: translator, domain, &docs)'], + ['actionAfterEverblocklightShortcodeDocumentation', 'After Ever Block Light shortcode documentation', 'Lets modules add or alter shortcode documentation groups after the module ones (params: translator, domain, &docs)'], ]; } diff --git a/src/Service/ShortcodeDocumentationProvider.php b/src/Service/ShortcodeDocumentationProvider.php index 65c18a9..8f16304 100644 --- a/src/Service/ShortcodeDocumentationProvider.php +++ b/src/Service/ShortcodeDocumentationProvider.php @@ -41,7 +41,7 @@ class ShortcodeDocumentationProvider * * @return array> */ - public static function getDocumentation(Module $module): array + public static function getDocumentation(Module $module): array { $context = Context::getContext(); $idLang = (int) $context->language->id; @@ -53,6 +53,7 @@ public static function getDocumentation(Module $module): array $translator = $context->getTranslator(); $domain = 'Modules.Everblocklight.Shortcodes'; + // Groupes ajoutés par d'autres modules, affichés avant ceux d'Ever Block Light $docs = []; \Hook::exec('actionBeforeEverblocklightShortcodeDocumentation', [ @@ -672,11 +673,6 @@ public static function getDocumentation(Module $module): array ], ], ], - [ - 'code' => '[wordpress-posts]', - 'description' => $translator->trans('Display the latest posts retrieved from a connected WordPress site.', [], $domain), - 'parameters' => [], - ], [ 'code' => '[googlereviews place_id="YOUR_PLACE_ID" limit="6" min_rating="4"]', 'description' => $translator->trans('Showcase reviews from your Google Business profile.', [], $domain), @@ -723,11 +719,6 @@ public static function getDocumentation(Module $module): array ], ], ], - [ - 'code' => '[everinstagram]', - 'description' => $translator->trans('Display the latest Instagram media fetched by the module.', [], $domain), - 'parameters' => [], - ], [ 'code' => '[llorem]', 'description' => $translator->trans('Insert placeholder lorem ipsum text according to module settings.', [], $domain), @@ -879,16 +870,18 @@ public static function getDocumentation(Module $module): array ], ], ]; - $docs = array_merge($docsCore, $docs); - - static::$cache[$idLang] = $docs; + $docs = array_merge($docs, $docsCore); + // Derniers ajouts / modifications par d'autres modules (groupes affichés après ceux du module) \Hook::exec('actionAfterEverblocklightShortcodeDocumentation', [ 'translator' => $translator, 'domain' => $domain, 'docs' => &$docs, ]); + // Mise en cache après les deux hooks : les appels suivants renvoient la même documentation + static::$cache[$idLang] = $docs; + return $docs; } } diff --git a/tests/Unit/ShortcodeDocumentationTest.php b/tests/Unit/ShortcodeDocumentationTest.php new file mode 100644 index 0000000..5fa4a07 --- /dev/null +++ b/tests/Unit/ShortcodeDocumentationTest.php @@ -0,0 +1,80 @@ +setValue(null, []); + \Context::getContext()->language = (object) ['id' => 1]; + } + + /** + * @return array + */ + private static function codes(array $docs): array + { + $codes = []; + foreach ($docs as $group) { + foreach ($group['entries'] ?? [] as $entry) { + $codes[] = (string) ($entry['code'] ?? ''); + } + } + + return $codes; + } + + private static function titles(array $docs): array + { + return array_map(static fn (array $group): string => (string) ($group['title'] ?? ''), $docs); + } + + public function testCoreDocumentationListsModuleShortcodes(): void + { + $codes = self::codes(ShortcodeDocumentationProvider::getDocumentation(new \Module())); + + self::assertContains('[cart_total]', $codes); + self::assertContains('[alert type="success"]Content[/alert]', $codes); + foreach ($codes as $code) { + self::assertDoesNotMatchRegularExpression('/everinstagram|wordpress-posts|everfaq|everorderform/', $code, 'Shortcode retiré documenté'); + } + } + + public function testExternalModulesCanAddGroupsBeforeAndAfter(): void + { + \Hook::$listeners['actionBeforeEverblocklightShortcodeDocumentation'][] = static function (array $params): void { + $params['docs'][] = ['title' => 'Avant', 'entries' => [['code' => '[before_sc]']]]; + }; + \Hook::$listeners['actionAfterEverblocklightShortcodeDocumentation'][] = static function (array $params): void { + $params['docs'][] = ['title' => 'Après ' . $params['domain'], 'entries' => [['code' => '[after_sc]']]]; + }; + + $titles = self::titles(ShortcodeDocumentationProvider::getDocumentation(new \Module())); + + self::assertSame('Avant', $titles[0], 'Le hook Before ajoute ses groupes avant ceux du module'); + self::assertSame('Après Modules.Everblocklight.Shortcodes', end($titles), 'Le hook After ajoute ses groupes à la fin'); + } + + public function testCachedDocumentationKeepsHookAdditions(): void + { + $calls = 0; + \Hook::$listeners['actionAfterEverblocklightShortcodeDocumentation'][] = static function (array $params) use (&$calls): void { + ++$calls; + $params['docs'][] = ['title' => 'Externe', 'entries' => [['code' => '[external_sc]']]]; + }; + + $first = ShortcodeDocumentationProvider::getDocumentation(new \Module()); + $second = ShortcodeDocumentationProvider::getDocumentation(new \Module()); + + self::assertSame(1, $calls, 'Deuxième appel servi depuis le cache'); + self::assertContains('[external_sc]', self::codes($second), 'Le cache contient les ajouts du hook After'); + self::assertSame($first, $second); + } +} diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index 27923da..799c60c 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -15,6 +15,7 @@ protected function setUp(): void \Configuration::reset(); \Cache::reset(); \Context::reset(); + \Hook::reset(); } /** diff --git a/tests/stubs/prestashop.php b/tests/stubs/prestashop.php index 84093e1..0900f9b 100644 --- a/tests/stubs/prestashop.php +++ b/tests/stubs/prestashop.php @@ -89,6 +89,11 @@ class Context public $language; public $shop; + public function getTranslator(): TranslatorStub + { + return new TranslatorStub(); + } + private static ?Context $instance = null; public static function getContext(): Context @@ -105,3 +110,38 @@ public static function reset(): void self::$instance = null; } } + +class Module +{ +} + +class Hook +{ + /** @var array> */ + public static array $listeners = []; + + /** + * Les paramètres passés par référence (ex. 'docs' => &$docs) restent liés, comme dans le cœur. + */ + public static function exec($hookName, $hookArgs = [], $idModule = null, $arrayReturn = false) + { + foreach (self::$listeners[$hookName] ?? [] as $listener) { + $listener($hookArgs); + } + + return ''; + } + + public static function reset(): void + { + self::$listeners = []; + } +} + +class TranslatorStub +{ + public function trans($id, array $parameters = [], $domain = null, $locale = null): string + { + return strtr((string) $id, $parameters); + } +}