From 92c66a75592d509f99f1168ce1e3b514fee18fa3 Mon Sep 17 00:00:00 2001 From: Josef Moravec <josef.moravec@gmail.com> Date: Tue, 29 Sep 2020 16:22:38 +0200 Subject: [PATCH] Fix markdown factory not getting config from markdown.ini (#1734) --- .../src/VuFind/Service/MarkdownFactory.php | 32 ++--- .../Service/MarkdownFactoryTest.php | 135 ++++++++++++++++++ 2 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Service/MarkdownFactoryTest.php diff --git a/module/VuFind/src/VuFind/Service/MarkdownFactory.php b/module/VuFind/src/VuFind/Service/MarkdownFactory.php index 46c76d69594..4d403f207d0 100644 --- a/module/VuFind/src/VuFind/Service/MarkdownFactory.php +++ b/module/VuFind/src/VuFind/Service/MarkdownFactory.php @@ -81,31 +81,31 @@ class MarkdownFactory implements FactoryInterface ) { $markdownConfig = $container->get(\VuFind\Config\PluginManager::class) ->get('markdown'); - $markdownSection = $markdownConfig->Markdown; + $mainConfig = $markdownConfig->Markdown; $environment = Environment::createCommonMarkEnvironment(); $environment->addExtension(new GithubFlavoredMarkdownExtension()); $config = [ - 'html_input' => $config->html_input ?? 'strip', - 'allow_unsafe_links' => $config->allow_unsafe_links ?? false, - 'enable_em' => $config->enable_em ?? true, - 'enable_strong' => $config->enable_strong ?? true, - 'use_asterisk' => $config->use_asterisk ?? true, - 'use_underscore' => $config->use_underscore ?? true, - 'unordered_list_markers' => isset($config->unordered_list_markers) - && $config->unordered_list_markers instanceof \ArrayAccess - ? $config->unordered_list_markers->toArray() + 'html_input' => $mainConfig->html_input ?? 'strip', + 'allow_unsafe_links' => $mainConfig->allow_unsafe_links ?? false, + 'enable_em' => $mainConfig->enable_em ?? true, + 'enable_strong' => $mainConfig->enable_strong ?? true, + 'use_asterisk' => $mainConfig->use_asterisk ?? true, + 'use_underscore' => $mainConfig->use_underscore ?? true, + 'unordered_list_markers' => isset($mainConfig->unordered_list_markers) + && $mainConfig->unordered_list_markers instanceof \ArrayAccess + ? $mainConfig->unordered_list_markers->toArray() : ['-', '*', '+'], - 'max_nesting_level' => $config->max_nesting_level ?? \INF, + 'max_nesting_level' => $mainConfig->max_nesting_level ?? \INF, 'renderer' => [ 'block_separator' - => $config->renderer['block_separator'] ?? "\n", + => $mainConfig->renderer['block_separator'] ?? "\n", 'inner_separator' - => $config->renderer['inner_separator'] ?? "\n", - 'soft_break' => $config->renderer['soft_break'] ?? "\n", + => $mainConfig->renderer['inner_separator'] ?? "\n", + 'soft_break' => $mainConfig->renderer['soft_break'] ?? "\n", ], ]; - $extensions = isset($markdownSection->extensions) - ? array_map('trim', explode(',', $markdownSection->extensions)) : []; + $extensions = isset($mainConfig->extensions) + ? array_map('trim', explode(',', $mainConfig->extensions)) : []; foreach ($extensions as $ext) { $extClass = sprintf( diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Service/MarkdownFactoryTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Service/MarkdownFactoryTest.php new file mode 100644 index 00000000000..da3d3e39205 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Service/MarkdownFactoryTest.php @@ -0,0 +1,135 @@ +<?php + +/** + * MarkdownFactory Test Class + * + * PHP version 7 + * + * Copyright (C) Moravian Library 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Josef Moravec <moravec@mzk.cz> + * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +namespace VuFindTest\Service; + +use Laminas\Config\Config; +use VuFind\Service\MarkdownFactory; +use VuFindTest\Unit\MockContainerTest; + +/** + * MarkdownFactory Test Class + * + * @category VuFind + * @package Tests + * @author Josef Moravec <moravec@mzk.cz> + * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +class MarkdownFactoryTest extends MockContainerTest +{ + /** + * Test to ensure the markdown factory is using right config for markdown + * service + * + * @return void + * @throws \Interop\Container\Exception\ContainerException + */ + public function testConfig() + { + $defaultConfig = []; + $defaultEnvironment = [ + 'html_input' => 'strip', + 'allow_unsafe_links' => false, + 'enable_em' => true, + 'enable_strong' => true, + 'use_asterisk' => true, + 'use_underscore' => true, + 'unordered_list_markers' => ['-', '*', '+'], + 'max_nesting_level' => \INF, + 'renderer' => [ + 'block_separator' => "\n", + 'inner_separator' => "\n", + 'soft_break' => "\n", + ], + ]; + + $customConfig = [ + 'Markdown' => [ + 'html_input' => 'escape', + 'allow_unsafe_links' => true, + 'enable_em' => false, + 'enable_strong' => false, + 'use_asterisk' => false, + 'use_underscore' => false, + 'unordered_list_markers' => [';', '^'], + 'max_nesting_level' => 10, + 'renderer' => [ + 'block_separator' => "\r\n", + 'inner_separator' => "\r\n", + 'soft_break' => "\r\n", + ], + ], + ]; + $customEnvironment = [ + 'html_input' => 'escape', + 'allow_unsafe_links' => true, + 'enable_em' => false, + 'enable_strong' => false, + 'use_asterisk' => false, + 'use_underscore' => false, + 'unordered_list_markers' => [';', '^'], + 'max_nesting_level' => 10, + 'renderer' => [ + 'block_separator' => "\r\n", + 'inner_separator' => "\r\n", + 'soft_break' => "\r\n", + ], + ]; + + $result = $this->getMarkdownEnvironmentConfig($defaultConfig); + $this->assertEquals($defaultEnvironment, $result); + + $result = $this->getMarkdownEnvironmentConfig($customConfig); + $this->assertEquals($customEnvironment, $result); + } + + /** + * Return config of created markdown service environment + * + * @param $config + * + * @return mixed + * @throws \Interop\Container\Exception\ContainerException + */ + protected function getMarkdownEnvironmentConfig($config) + { + $config = new Config($config); + $configManager = $this->container->createMock( + \VuFind\Config\PluginManager::class, ['get'] + ); + $configManager->expects($this->any())->method('get') + ->will($this->returnValue($config)); + $this->container->set(\VuFind\Config\PluginManager::class, $configManager); + $markdownFactory = new MarkdownFactory(); + $markdown = $markdownFactory->__invoke( + $this->container, \League\CommonMark\MarkdownConverterInterface::class + ); + return $markdown->getEnvironment()->getConfig(); + } +} -- GitLab