Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with bad theme config more gracefully. #4220

Merged
merged 3 commits into from
Jan 30, 2025
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
20 changes: 14 additions & 6 deletions module/VuFind/src/VuFind/Bootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,20 @@ protected function initUserLanguage(): void
*/
protected function initTheme(): void
{
// Attach remaining theme configuration to the dispatch event at high
// priority (TODO: use priority constant once defined by framework):
$config = $this->config->Site;
$callback = function ($event) use ($config) {
$theme = new \VuFindTheme\Initializer($config, $event);
$theme->init();
// Attach remaining theme configuration to the dispatch event at high priority:
$siteConfig = $this->config->Site;
$callback = function ($event) use ($siteConfig) {
$theme = new \VuFindTheme\Initializer($siteConfig, $event);
try {
$theme->init();
} catch (\Exception $e) {
// Try to display an error page if the theme fails to initialize:
$appConfig = $this->container->get('config');
$model = $event->getViewModel();
$model->setTemplate('error/index');
$model->display_exceptions = $appConfig['view_manager']['display_exceptions'] ?? false;
$model->exception = $e;
}
};
$this->events->attach('dispatch.error', $callback, 9000);
$this->events->attach('dispatch', $callback, 9000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BasicTest extends \VuFindTest\Integration\MinkTestCase
public function testHomePage(): void
{
$page = $this->getSearchHomePage();
$this->assertTrue(false !== strstr($page->getContent(), 'VuFind'));
$this->assertStringContainsString('VuFind', $page->getContent());
}

/**
Expand Down Expand Up @@ -142,6 +142,31 @@ public function testThemeSwitcher(): void
);
}

/**
* Test graceful handling of an invalid theme.
*
* Note that HTML validation is disabled on this test because an improperly initialized
* theme will not generate a fully-formed page; but we still want to confirm that it
* at least outputs a human-readable error message.
*
* @return void
*/
#[\VuFindTest\Attribute\HtmlValidation(false)]
public function testBadThemeConfig(): void
{
$this->changeConfigs(
[
'config' => [
'Site' => [
'theme' => 'not-a-valid-theme',
],
],
]
);
$page = $this->getSearchHomePage();
$this->assertStringContainsString('An error has occurred', $page->getContent());
}

/**
* Test lightbox jump links
*
Expand Down
2 changes: 1 addition & 1 deletion module/VuFindTheme/src/VuFindTheme/ThemeInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __invoke(

$themeInfo = new $requestedName(
realpath(APPLICATION_PATH . '/themes'),
'bootprint3'
'sandal5'
);

// As of release 1.1.0, the memory storage adapter has a flaw which can cause
Expand Down