mirror of
https://github.com/flarum/core.git
synced 2025-08-01 14:10:37 +02:00
Refactor and improve formatter extender (#2098)
- Deprecated all events involved with Formatter - Refactor ->configure() method on extender not to use events - Add extender methods for ->render() and ->parse() - Add integration tests
This commit is contained in:
committed by
GitHub
parent
9149489405
commit
5dddc2e663
145
framework/core/tests/integration/extenders/FormatterTest.php
Normal file
145
framework/core/tests/integration/extenders/FormatterTest.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Formatter\Formatter;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
|
||||
class FormatterTest extends TestCase
|
||||
{
|
||||
protected function getFormatter()
|
||||
{
|
||||
$formatter = $this->app()->getContainer()->make(Formatter::class);
|
||||
$formatter->flush();
|
||||
|
||||
return $formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_config_doesnt_work_by_default()
|
||||
{
|
||||
$formatter = $this->getFormatter();
|
||||
|
||||
$this->assertEquals('<t>[B]something[/B]</t>', $formatter->parse('[B]something[/B]'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_config_works_if_added_with_closure()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->configure(function ($config) {
|
||||
$config->BBCodes->addFromRepository('B');
|
||||
}));
|
||||
|
||||
$formatter = $this->getFormatter();
|
||||
|
||||
$this->assertEquals('<b>something</b>', $formatter->render($formatter->parse('[B]something[/B]')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_config_works_if_added_with_invokable_class()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->configure(InvokableConfig::class));
|
||||
|
||||
$formatter = $this->getFormatter();
|
||||
|
||||
$this->assertEquals('<b>something</b>', $formatter->render($formatter->parse('[B]something[/B]')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_parsing_doesnt_work_by_default()
|
||||
{
|
||||
$this->assertEquals('<t>Text<a></t>', $this->getFormatter()->parse('Text<a>'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_parsing_works_if_added_with_closure()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->parse(function ($parser, $context, $text) {
|
||||
return 'ReplacedText<a>';
|
||||
}));
|
||||
|
||||
$this->assertEquals('<t>ReplacedText<a></t>', $this->getFormatter()->parse('Text<a>'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_parsing_works_if_added_with_invokable_class()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->parse(InvokableParsing::class));
|
||||
|
||||
$this->assertEquals('<t>ReplacedText<a></t>', $this->getFormatter()->parse('Text<a>'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_rendering_doesnt_work_by_default()
|
||||
{
|
||||
$this->assertEquals('Text', $this->getFormatter()->render('<p>Text</p>'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_rendering_works_if_added_with_closure()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->render(function ($renderer, $context, $xml, $request) {
|
||||
return '<html>ReplacedText</html>';
|
||||
}));
|
||||
|
||||
$this->assertEquals('ReplacedText', $this->getFormatter()->render('<html>Text</html>'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_formatter_rendering_works_if_added_with_invokable_class()
|
||||
{
|
||||
$this->extend((new Extend\Formatter)->render(InvokableRendering::class));
|
||||
|
||||
$this->assertEquals('ReplacedText', $this->getFormatter()->render('<html>Text</html>'));
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableConfig
|
||||
{
|
||||
public function __invoke($config)
|
||||
{
|
||||
$config->BBCodes->addFromRepository('B');
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableParsing
|
||||
{
|
||||
public function __invoke($parser, $context, $text)
|
||||
{
|
||||
return 'ReplacedText<a>';
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableRendering
|
||||
{
|
||||
public function __invoke($renderer, $context, $xml, $request)
|
||||
{
|
||||
return '<html>ReplacedText</html>';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user