Skip to content
Snippets Groups Projects
Commit cf005992 authored by IMMACULATE X's avatar IMMACULATE X Committed by Youri van Koppen
Browse files

Issue #3450532 by immaculatexavier, megachriz, joachim: Fixed MarkdownFilter...

Issue #3450532 by immaculatexavier, megachriz, joachim: Fixed MarkdownFilter should pass a string to FilterProcessResult to prevent crashes when combined with subsequent filters.
parent 4a297514
Branches
Tags
1 merge request!33Update file FilterMarkdown.php
Pipeline #456075 passed with warnings
......@@ -150,7 +150,8 @@ class FilterMarkdown extends FilterBase implements ContainerFactoryPluginInterfa
// Only use the parser to process the text if it's not empty.
if (!empty($text)) {
$language = $langcode ? \Drupal::languageManager()->getLanguage($langcode) : NULL;
$text = $this->getParser()->parse($text, $language);
// Ensure that the parsed text is cast to a string.
$text = (string) $this->getParser()->parse($text, $language);
}
return new FilterProcessResult($text);
}
......
<?php
namespace Drupal\Tests\markdown\Unit;
use Drupal\Tests\UnitTestCase;
/**
* Base class for Markdown unit tests.
*/
abstract class MarkdownUnitTestBase extends UnitTestCase {
}
<?php
namespace Drupal\Tests\markdown\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\markdown\Unit\MarkdownUnitTestBase;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\markdown\PluginManager\ParserManagerInterface;
use Drupal\markdown\Plugin\Filter\FilterMarkdown;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Drupal\markdown\Render\ParsedMarkdownInterface;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\markdown\Plugin\Filter\FilterMarkdown
* @group markdown
*/
class FilterMarkdownTest extends MarkdownUnitTestBase {
/**
* The Element Info Manager service.
*
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Render\ElementInfoManagerInterface
*/
protected $elementInfo;
/**
* The Markdown Parser Plugin Manager service.
*
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\markdown\PluginManager\ParserManagerInterface
*/
protected $parserManager;
/**
* The Markdown Parser Plugin Manager service.
*
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\markdown\Plugin\Markdown\ParserInterface
*/
protected $parser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->elementInfo = $this->prophesize(ElementInfoManagerInterface::class);
$this->parserManager = $this->prophesize(ParserManagerInterface::class);
$this->parser = $this->prophesize(ParserInterface::class);
$this->parser->getPluginId()
->willReturn('foo');
$this->parser->parse(Argument::type('string'), NULL)
->willReturn($this->createMock(ParsedMarkdownInterface::class));
$this->parser->getConfiguration()
->willReturn([]);
$container = new ContainerBuilder();
$config_factory = $this->prophesize(ConfigFactoryInterface::class);
$config_factory->get('markdown.parser.foo')
->willReturn($this->createMock(ImmutableConfig::class));
$container->set('config.factory', $config_factory->reveal());
\Drupal::setContainer($container);
}
/**
* Returns a new FilterMarkdown instance to test with.
*
* @param array $configuration
* (optional) The configuration for the filter.
*
* @return \Drupal\markdown\Plugin\Filter\FilterMarkdown
* A FilterMarkdown instance.
*/
protected function getInstance(array $configuration = []): FilterMarkdown {
$plugin_definition = [
'description' => new TranslatableMarkup('Allows content to be submitted using Markdown, a simple plain-text syntax that is filtered into valid HTML.'),
'weight' => -15,
'status' => TRUE,
'settings' => [],
'id' => 'markdown',
'title' => new TranslatableMarkup('Markdown'),
'type' => FilterInterface::TYPE_MARKUP_LANGUAGE,
'class' => FilterMarkdown::class,
'provider' => 'markdown',
];
return new FilterMarkdown($configuration, 'markdown', $plugin_definition, $this->elementInfo->reveal(), $this->parserManager->reveal());
}
/**
* @covers ::process
*/
public function testProcess() {
$this->parserManager->getDefaultParser()
->willReturn($this->parser->reveal());
$this->parserManager->createInstance('foo', Argument::type('array'))
->willReturn($this->parser->reveal());
$filter = $this->getInstance();
$result = $filter->process('foo');
$this->assertInstanceof(FilterProcessResult::class, $result);
$this->assertIsString($result->getProcessedText());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment