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
|
|
|
|
2018-04-17 16:44:38 +03:00
|
|
|
use Erusev\Parsedown\Parsedown;
|
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
|
|
|
{
|
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();
|
|
|
|
$this->Parsedown = $this->initParsedown();
|
|
|
|
|
|
|
|
parent::__construct($name, $data, $dataName);
|
|
|
|
}
|
|
|
|
|
2017-06-19 01:00:00 +03:00
|
|
|
private $dirs;
|
|
|
|
protected $Parsedown;
|
2014-09-26 03:04:25 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function initDirs()
|
|
|
|
{
|
2018-12-04 19:24:25 +03:00
|
|
|
$dirs []= \dirname(__FILE__).'/data/';
|
2014-09-26 03:04:25 +04:00
|
|
|
|
|
|
|
return $dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Parsedown
|
|
|
|
*/
|
|
|
|
protected function initParsedown()
|
|
|
|
{
|
2016-09-05 22:10:23 +03:00
|
|
|
$Parsedown = new TestParsedown();
|
2014-09-26 03:04:25 +04:00
|
|
|
|
|
|
|
return $Parsedown;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider data
|
|
|
|
* @param $test
|
|
|
|
* @param $dir
|
|
|
|
*/
|
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
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
$this->Parsedown->setSafeMode(\substr($test, 0, 3) === 'xss');
|
|
|
|
$this->Parsedown->setStrictMode(\substr($test, 0, 6) === 'strict');
|
2017-05-01 05:33:49 +03:00
|
|
|
|
2014-09-26 03:04:25 +04:00
|
|
|
$actualMarkup = $this->Parsedown->text($markdown);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedMarkup, $actualMarkup);
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function testRawHtml()
|
2018-03-15 13:42:29 +03:00
|
|
|
{
|
|
|
|
$markdown = "```php\nfoobar\n```";
|
|
|
|
$expectedMarkup = '<pre><code class="language-php"><p>foobar</p></code></pre>';
|
2018-03-15 14:09:55 +03:00
|
|
|
$expectedSafeMarkup = '<pre><code class="language-php"><p>foobar</p></code></pre>';
|
2018-03-15 13:42:29 +03:00
|
|
|
|
2018-04-17 16:44:38 +03:00
|
|
|
$unsafeExtension = new SampleExtensions\UnsafeExtension;
|
2018-03-15 13:42:29 +03:00
|
|
|
$actualMarkup = $unsafeExtension->text($markdown);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedMarkup, $actualMarkup);
|
2018-03-15 14:09:55 +03:00
|
|
|
|
|
|
|
$unsafeExtension->setSafeMode(true);
|
|
|
|
$actualSafeMarkup = $unsafeExtension->text($markdown);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedSafeMarkup, $actualSafeMarkup);
|
2018-03-15 13:42:29 +03:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:24:25 +03:00
|
|
|
public function testTrustDelegatedRawHtml()
|
2018-03-15 22:46:03 +03:00
|
|
|
{
|
|
|
|
$markdown = "```php\nfoobar\n```";
|
|
|
|
$expectedMarkup = '<pre><code class="language-php"><p>foobar</p></code></pre>';
|
|
|
|
$expectedSafeMarkup = $expectedMarkup;
|
|
|
|
|
2018-04-17 16:44:38 +03:00
|
|
|
$unsafeExtension = new SampleExtensions\TrustDelegatedExtension;
|
2018-03-15 22:46:03 +03:00
|
|
|
$actualMarkup = $unsafeExtension->text($markdown);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedMarkup, $actualMarkup);
|
|
|
|
|
|
|
|
$unsafeExtension->setSafeMode(true);
|
|
|
|
$actualSafeMarkup = $unsafeExtension->text($markdown);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedSafeMarkup, $actualSafeMarkup);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2014-10-10 22:07:41 +04:00
|
|
|
|
|
|
|
comment
|
|
|
|
|
|
|
|
<!-- html comment -->
|
2014-09-26 03:04:25 +04:00
|
|
|
MARKDOWN_WITH_MARKUP;
|
|
|
|
|
|
|
|
$expectedHtml = <<<EXPECTED_HTML
|
2014-12-03 00:53:19 +03:00
|
|
|
<p><div><em>content</em></div></p>
|
2014-09-26 03:04:25 +04:00
|
|
|
<p>sparse:</p>
|
2014-12-03 00:53:19 +03:00
|
|
|
<p><div>
|
2018-03-19 01:33:26 +03:00
|
|
|
<div class="inner">
|
2014-09-26 03:04:25 +04:00
|
|
|
<em>content</em>
|
2014-12-03 00:53:19 +03:00
|
|
|
</div>
|
|
|
|
</div></p>
|
2014-09-26 03:04:25 +04:00
|
|
|
<p>paragraph</p>
|
2018-03-19 01:33:26 +03:00
|
|
|
<p><style type="text/css">
|
2014-12-15 01:52:03 +03:00
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
</style></p>
|
2014-10-10 22:07:41 +04:00
|
|
|
<p>comment</p>
|
2014-12-03 00:53:19 +03:00
|
|
|
<p><!-- html comment --></p>
|
2014-09-26 03:04:25 +04:00
|
|
|
EXPECTED_HTML;
|
2016-09-05 22:10:23 +03:00
|
|
|
|
|
|
|
$parsedownWithNoMarkup = new TestParsedown();
|
2014-09-26 03:04:25 +04:00
|
|
|
$parsedownWithNoMarkup->setMarkupEscaped(true);
|
|
|
|
$this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
|
|
|
|
}
|
2015-07-20 17:30:15 +03:00
|
|
|
|
|
|
|
public function testLateStaticBinding()
|
|
|
|
{
|
|
|
|
$parsedown = Parsedown::instance();
|
2018-04-17 17:05:39 +03:00
|
|
|
$this->assertInstanceOf('Erusev\Parsedown\Parsedown', $parsedown);
|
2015-07-20 17:30:15 +03:00
|
|
|
|
|
|
|
// After instance is already called on Parsedown
|
|
|
|
// subsequent calls with the same arguments return the same instance
|
|
|
|
$sameParsedown = TestParsedown::instance();
|
2018-04-17 17:05:39 +03:00
|
|
|
$this->assertInstanceOf('Erusev\Parsedown\Parsedown', $sameParsedown);
|
2015-07-20 17:30:15 +03:00
|
|
|
$this->assertSame($parsedown, $sameParsedown);
|
|
|
|
|
|
|
|
$testParsedown = TestParsedown::instance('test late static binding');
|
2018-04-17 17:05:39 +03:00
|
|
|
$this->assertInstanceOf('Erusev\Parsedown\Parsedown', $testParsedown);
|
2015-07-20 17:30:15 +03:00
|
|
|
|
|
|
|
$sameInstanceAgain = TestParsedown::instance('test late static binding');
|
|
|
|
$this->assertSame($testParsedown, $sameInstanceAgain);
|
|
|
|
}
|
2014-09-26 03:04:25 +04:00
|
|
|
}
|