2014-09-26 03:04:25 +04:00
|
|
|
<?php
|
2018-04-17 16:44:38 +03:00
|
|
|
|
|
|
|
namespace Erusev\Parsedown\Tests;
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2019-01-27 00:48:05 +03:00
|
|
|
use Erusev\Parsedown\Components\Blocks\Markup as BlockMarkup;
|
|
|
|
use Erusev\Parsedown\Components\Inlines\Markup as InlineMarkup;
|
|
|
|
use Erusev\Parsedown\Configurables\BlockTypes;
|
2019-01-26 00:20:58 +03:00
|
|
|
use Erusev\Parsedown\Configurables\Breaks;
|
2020-05-05 00:02:20 +03:00
|
|
|
use Erusev\Parsedown\Configurables\HeaderSlug;
|
2019-01-27 00:48:05 +03:00
|
|
|
use Erusev\Parsedown\Configurables\InlineTypes;
|
2019-01-20 05:56:19 +03:00
|
|
|
use Erusev\Parsedown\Configurables\SafeMode;
|
|
|
|
use Erusev\Parsedown\Configurables\StrictMode;
|
2018-04-17 16:44:38 +03:00
|
|
|
use Erusev\Parsedown\Parsedown;
|
2019-01-20 05:56:19 +03:00
|
|
|
use Erusev\Parsedown\State;
|
2018-12-04 19:24:25 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-11-11 05:56:03 +03:00
|
|
|
|
|
|
|
class ParsedownTest extends TestCase
|
2014-09-26 03:04:25 +04:00
|
|
|
{
|
2019-02-11 01:38:34 +03:00
|
|
|
/**
|
|
|
|
* @param string|null $name
|
|
|
|
* @param array $data
|
|
|
|
* @param string $dataName
|
|
|
|
*/
|
2018-12-04 19:24:25 +03:00
|
|
|
final public function __construct($name = null, array $data = [], $dataName = '')
|
2014-09-26 03:04:25 +04:00
|
|
|
{
|
|
|
|
$this->dirs = $this->initDirs();
|
|
|
|
|
|
|
|
parent::__construct($name, $data, $dataName);
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
/** @var string[] */
|
2017-06-19 01:00:00 +03:00
|
|
|
private $dirs;
|
2014-09-26 03:04:25 +04:00
|
|
|
|
|
|
|
/**
|
2019-02-11 01:38:34 +03:00
|
|
|
* @return string[]
|
2014-09-26 03:04:25 +04:00
|
|
|
*/
|
|
|
|
protected function initDirs()
|
|
|
|
{
|
2019-02-11 01:38:34 +03:00
|
|
|
return [\dirname(__FILE__).'/data/'];
|
2014-09-26 03:04:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider data
|
2019-02-11 01:38:34 +03:00
|
|
|
* @param string $test
|
|
|
|
* @param string $dir
|
|
|
|
* @return void
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
2014-09-26 03:04:25 +04:00
|
|
|
*/
|
2018-12-04 19:24:25 +03:00
|
|
|
public function test_($test, $dir)
|
2014-09-26 03:04:25 +04:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$markdown = \file_get_contents($dir . $test . '.md');
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$expectedMarkup = \file_get_contents($dir . $test . '.html');
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$expectedMarkup = \str_replace("\r\n", "\n", $expectedMarkup);
|
|
|
|
$expectedMarkup = \str_replace("\r", "\n", $expectedMarkup);
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2019-01-20 05:56:19 +03:00
|
|
|
$Parsedown = new Parsedown(new State([
|
|
|
|
new SafeMode(\substr($test, 0, 3) === 'xss'),
|
|
|
|
new StrictMode(\substr($test, 0, 6) === 'strict'),
|
2019-01-26 00:20:58 +03:00
|
|
|
new Breaks(\substr($test, 0, 14) === 'breaks_enabled'),
|
2020-05-05 00:02:20 +03:00
|
|
|
new HeaderSlug(\substr($test, 0, 4) === 'slug'),
|
2019-01-20 05:56:19 +03:00
|
|
|
]));
|
2018-03-15 22:46:03 +03:00
|
|
|
|
2020-01-19 18:26:48 +03:00
|
|
|
$actualMarkup = $Parsedown->toHtml($markdown);
|
2018-03-15 22:46:03 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expectedMarkup, $actualMarkup);
|
|
|
|
}
|
|
|
|
|
2019-06-16 23:33:55 +03:00
|
|
|
/** @return array<int, array{0:string, 1:string}> */
|
2018-12-04 19:24:25 +03:00
|
|
|
public function data()
|
2014-09-26 03:04:25 +04:00
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$data = [];
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($this->dirs as $dir) {
|
2018-04-17 16:44:38 +03:00
|
|
|
$Folder = new \DirectoryIterator($dir);
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
foreach ($Folder as $File) {
|
2014-09-26 03:04:25 +04:00
|
|
|
/** @var $File DirectoryIterator */
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (! $File->isFile()) {
|
2014-09-26 03:04:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = $File->getFilename();
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$extension = \pathinfo($filename, \PATHINFO_EXTENSION);
|
2014-09-26 03:04:25 +04:00
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if ($extension !== 'md') {
|
2014-09-26 03:04:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$basename = $File->getBasename('.md');
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
if (\file_exists($dir . $basename . '.html')) {
|
|
|
|
$data []= [$basename, $dir];
|
2014-09-26 03:04:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:38:34 +03:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*/
|
2019-01-27 00:48:05 +03:00
|
|
|
public function test_no_markup()
|
|
|
|
{
|
|
|
|
$markdownWithHtml = <<<MARKDOWN_WITH_MARKUP
|
|
|
|
<div>_content_</div>
|
|
|
|
|
|
|
|
sparse:
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<div class="inner">
|
|
|
|
_content_
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
paragraph
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
comment
|
|
|
|
|
|
|
|
<!-- html comment -->
|
|
|
|
MARKDOWN_WITH_MARKUP;
|
|
|
|
|
|
|
|
$expectedHtml = <<<EXPECTED_HTML
|
|
|
|
<p><div><em>content</em></div></p>
|
|
|
|
<p>sparse:</p>
|
|
|
|
<p><div>
|
2019-01-27 21:02:32 +03:00
|
|
|
<div class="inner">
|
2019-01-27 00:48:05 +03:00
|
|
|
<em>content</em>
|
|
|
|
</div>
|
|
|
|
</div></p>
|
|
|
|
<p>paragraph</p>
|
2019-01-27 21:02:32 +03:00
|
|
|
<p><style type="text/css">
|
2019-01-27 00:48:05 +03:00
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
</style></p>
|
|
|
|
<p>comment</p>
|
|
|
|
<p><!-- html comment --></p>
|
|
|
|
EXPECTED_HTML;
|
|
|
|
|
|
|
|
$parsedownWithNoMarkup = new Parsedown(new State([
|
2019-01-27 22:38:07 +03:00
|
|
|
BlockTypes::initial()->removing([BlockMarkup::class]),
|
2019-01-27 00:48:05 +03:00
|
|
|
InlineTypes::initial()->removing([InlineMarkup::class]),
|
|
|
|
]));
|
|
|
|
|
2020-01-19 18:26:48 +03:00
|
|
|
$this->assertEquals($expectedHtml, $parsedownWithNoMarkup->toHtml($markdownWithHtml));
|
2019-01-27 00:48:05 +03:00
|
|
|
}
|
2014-09-26 03:04:25 +04:00
|
|
|
}
|