Skip to content

Commit

Permalink
Update GoogleAnalytics helper to use AssetManager; improve tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Feb 4, 2025
1 parent 7e3c158 commit 4c6b337
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
35 changes: 28 additions & 7 deletions module/VuFind/src/VuFindTest/Feature/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
{
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -138,7 +137,6 @@ protected function getIconHelper(
);
$plugins = array_merge(
[
'assetManager' => $this->createMock(AssetManager::class),
'escapeHtmlAttr' => new EscapeHtmlAttr($escaper),
],
$plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 4c6b337

Please sign in to comment.