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 068f3a6..8f16304 100644 --- a/src/Service/ShortcodeDocumentationProvider.php +++ b/src/Service/ShortcodeDocumentationProvider.php @@ -53,7 +53,16 @@ public static function getDocumentation(Module $module): array $translator = $context->getTranslator(); $domain = 'Modules.Everblocklight.Shortcodes'; - $docs = [ + // Groupes ajoutés par d'autres modules, affichés avant ceux d'Ever Block Light + $docs = []; + + \Hook::exec('actionBeforeEverblocklightShortcodeDocumentation', [ + 'translator' => $translator, + 'domain' => $domain, + 'docs' => &$docs, + ]); + + $docsCore = [ [ 'title' => $translator->trans('Catalog & merchandising', [], $domain), 'entries' => [ @@ -861,7 +870,16 @@ public static function getDocumentation(Module $module): array ], ], ]; + $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); + } +}