From ebaf91431196768eb1d80621078f350d7d1fd3b2 Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Wed, 29 Jan 2025 15:49:41 -0500 Subject: [PATCH 1/3] Deal with bad theme config more gracefully. --- module/VuFind/src/VuFind/Bootstrapper.php | 14 +++++++++++--- .../src/VuFindTheme/ThemeInfoFactory.php | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php index 963788aa4f9..bc7b8fa03ae 100644 --- a/module/VuFind/src/VuFind/Bootstrapper.php +++ b/module/VuFind/src/VuFind/Bootstrapper.php @@ -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): + // Attach remaining theme configuration to the dispatch event at high priority: $config = $this->config->Site; $callback = function ($event) use ($config) { $theme = new \VuFindTheme\Initializer($config, $event); - $theme->init(); + try { + $theme->init(); + } catch (\Exception $e) { + // Try to display an error page if the theme fails to initialize: + $config = $this->container->get('config'); + $model = $event->getViewModel(); + $model->setTemplate('error/index'); + $model->display_exceptions = $config['view_manager']['display_exceptions'] ?? false; + $model->exception = $e; + } }; $this->events->attach('dispatch.error', $callback, 9000); $this->events->attach('dispatch', $callback, 9000); diff --git a/module/VuFindTheme/src/VuFindTheme/ThemeInfoFactory.php b/module/VuFindTheme/src/VuFindTheme/ThemeInfoFactory.php index cfa87fced17..2b601311a47 100644 --- a/module/VuFindTheme/src/VuFindTheme/ThemeInfoFactory.php +++ b/module/VuFindTheme/src/VuFindTheme/ThemeInfoFactory.php @@ -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 From 3fd2558111c9b6d71213fbb5acacb548edd20ffa Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Thu, 30 Jan 2025 06:38:37 -0500 Subject: [PATCH 2/3] Improve variable names. --- module/VuFind/src/VuFind/Bootstrapper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php index bc7b8fa03ae..cd8d69495e6 100644 --- a/module/VuFind/src/VuFind/Bootstrapper.php +++ b/module/VuFind/src/VuFind/Bootstrapper.php @@ -242,17 +242,17 @@ protected function initUserLanguage(): void protected function initTheme(): void { // Attach remaining theme configuration to the dispatch event at high priority: - $config = $this->config->Site; - $callback = function ($event) use ($config) { - $theme = new \VuFindTheme\Initializer($config, $event); + $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: - $config = $this->container->get('config'); + $appConfig = $this->container->get('config'); $model = $event->getViewModel(); $model->setTemplate('error/index'); - $model->display_exceptions = $config['view_manager']['display_exceptions'] ?? false; + $model->display_exceptions = $appConfig['view_manager']['display_exceptions'] ?? false; $model->exception = $e; } }; From b4c4a766094cde6ca3e1cf3093486b3f813ab44d Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Thu, 30 Jan 2025 07:32:27 -0500 Subject: [PATCH 3/3] Add test. --- .../src/VuFindTest/Mink/BasicTest.php | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/BasicTest.php b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/BasicTest.php index 3059776f50c..e81b8a3ac49 100644 --- a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/BasicTest.php +++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/BasicTest.php @@ -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()); } /** @@ -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 *