From 4c6b3372ba38341a61ec2fbf133ca186202f4cfb Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Tue, 4 Feb 2025 06:52:09 -0500 Subject: [PATCH] Update GoogleAnalytics helper to use AssetManager; improve tests. --- .../View/Helper/Root/GoogleAnalytics.php | 6 ++-- .../src/VuFindTest/Feature/ViewTrait.php | 35 +++++++++++++++---- .../VuFindTest/View/Helper/Root/IconTest.php | 2 -- .../Helper/Root/RecordDataFormatterTest.php | 1 - .../View/Helper/AssetManagerFactory.php | 2 +- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php index 4a0b1dc959e..dc31b1eb421 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php @@ -107,11 +107,11 @@ public function __invoke($customUrl = false) if (!$this->key) { return ''; } - $inlineScript = $this->getView()->plugin('inlinescript'); + $assetManager = $this->getView()->plugin('assetManager'); $url = 'https://www.googletagmanager.com/gtag/js?id=' . urlencode($this->key); $code = $this->getRawJavascript($customUrl); return - $inlineScript(HeadScript::FILE, $url, 'SET', ['async' => true]) . "\n" - . $inlineScript(HeadScript::SCRIPT, $code, 'SET'); + $assetManager->outputInlineScriptFile($url, attrs: ['async' => true]) . "\n" + . $assetManager->outputInlineScript($code); } } diff --git a/module/VuFind/src/VuFindTest/Feature/ViewTrait.php b/module/VuFind/src/VuFindTest/Feature/ViewTrait.php index 2358f13148c..1f52c71094e 100644 --- a/module/VuFind/src/VuFindTest/Feature/ViewTrait.php +++ b/module/VuFind/src/VuFindTest/Feature/ViewTrait.php @@ -29,7 +29,11 @@ namespace VuFindTest\Feature; +use Laminas\View\Renderer\PhpRenderer; use VuFind\View\Helper\Root\SearchMemory; +use VuFindTest\Container\MockContainer; +use VuFindTheme\View\Helper\AssetManager; +use VuFindTheme\View\Helper\AssetManagerFactory; /** * Trait for tests involving Laminas Views. @@ -42,13 +46,29 @@ */ trait ViewTrait { + /** + * Get a working AssetManager helper. + * + * @param PhpRenderer $renderer View for helper + * + * @return AssetManager + */ + protected function getAssetManager(PhpRenderer $renderer): AssetManager + { + $container = new MockContainer($this); + $factory = new AssetManagerFactory(); + $helper = $factory($container, AssetManager::class); + $helper->setView($renderer); + return $helper; + } + /** * Get a working renderer. * * @param array $plugins Custom VuFind plug-ins to register * @param string $theme Theme directory to load from * - * @return \Laminas\View\Renderer\PhpRenderer + * @return PhpRenderer */ protected function getPhpRenderer($plugins = [], $theme = 'bootstrap5') { @@ -63,13 +83,14 @@ protected function getPhpRenderer($plugins = [], $theme = 'bootstrap5') $this->getPathForTheme($theme), ] ); - $renderer = new \Laminas\View\Renderer\PhpRenderer(); + $renderer = new PhpRenderer(); $renderer->setResolver($resolver); - if (!empty($plugins)) { - $pluginManager = $renderer->getHelperPluginManager(); - foreach ($plugins as $key => $value) { - $pluginManager->setService($key, $value); - } + $pluginManager = $renderer->getHelperPluginManager(); + if (!isset($plugins['assetManager'])) { + $plugins['assetManager'] = $this->getAssetManager($renderer); + } + foreach ($plugins as $key => $value) { + $pluginManager->setService($key, $value); } return $renderer; } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/IconTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/IconTest.php index 89be1f39a60..c5593bfa2df 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/IconTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/IconTest.php @@ -34,7 +34,6 @@ use Laminas\View\Helper\EscapeHtmlAttr; use VuFind\Escaper\Escaper; use VuFind\View\Helper\Root\Icon; -use VuFindTheme\View\Helper\AssetManager; use VuFindTheme\View\Helper\ImageLink; /** @@ -138,7 +137,6 @@ protected function getIconHelper( ); $plugins = array_merge( [ - 'assetManager' => $this->createMock(AssetManager::class), 'escapeHtmlAttr' => new EscapeHtmlAttr($escaper), ], $plugins diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php index f2071e9c0b7..bf40fb8ff5d 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/RecordDataFormatterTest.php @@ -89,7 +89,6 @@ protected function getViewHelpers($container): array }); $record->setDbServiceManager($serviceManager); return [ - 'assetManager' => $this->createMock(\VuFindTheme\View\Helper\AssetManager::class), 'auth' => new \VuFind\View\Helper\Root\Auth( $this->createMock(\VuFind\Auth\Manager::class), $this->createMock(\VuFind\Auth\ILSAuthenticator::class) diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/AssetManagerFactory.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/AssetManagerFactory.php index 9a91a5d852a..6cc40db9b4b 100644 --- a/module/VuFindTheme/src/VuFindTheme/View/Helper/AssetManagerFactory.php +++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/AssetManagerFactory.php @@ -102,7 +102,7 @@ public function __invoke( $configManager = $container->get(\VuFind\Config\PluginManager::class); $nonceGenerator = $container->get(\VuFind\Security\NonceGenerator::class); $nonce = $nonceGenerator->getNonce(); - $config = $configManager->get('config')->toArray(); + $config = $configManager->get('config')?->toArray() ?? []; return new $requestedName( $container->get(\VuFindTheme\ThemeInfo::class), $this->getPipelineConfig($config),