1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00
parsedown/tests/ParsedownTest.php

202 lines
5.1 KiB
PHP
Raw Normal View History

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
use PHPUnit\Framework\TestCase;
2018-04-17 16:44:38 +03:00
use Erusev\Parsedown\Parsedown;
class ParsedownTest extends TestCase
2014-09-26 03:04:25 +04:00
{
final function __construct($name = null, array $data = array(), $dataName = '')
{
$this->dirs = $this->initDirs();
$this->Parsedown = $this->initParsedown();
parent::__construct($name, $data, $dataName);
}
private $dirs;
protected $Parsedown;
2014-09-26 03:04:25 +04:00
/**
* @return array
*/
protected function initDirs()
{
$dirs []= dirname(__FILE__).'/data/';
return $dirs;
}
/**
* @return Parsedown
*/
protected function initParsedown()
{
$Parsedown = new TestParsedown();
2014-09-26 03:04:25 +04:00
return $Parsedown;
}
/**
* @dataProvider data
* @param $test
* @param $dir
*/
function test_($test, $dir)
{
$markdown = file_get_contents($dir . $test . '.md');
$expectedMarkup = file_get_contents($dir . $test . '.html');
$expectedMarkup = str_replace("\r\n", "\n", $expectedMarkup);
$expectedMarkup = str_replace("\r", "\n", $expectedMarkup);
$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);
}
function testRawHtml()
{
$markdown = "```php\nfoobar\n```";
$expectedMarkup = '<pre><code class="language-php"><p>foobar</p></code></pre>';
$expectedSafeMarkup = '<pre><code class="language-php">&lt;p&gt;foobar&lt;/p&gt;</code></pre>';
2018-04-17 16:44:38 +03:00
$unsafeExtension = new SampleExtensions\UnsafeExtension;
$actualMarkup = $unsafeExtension->text($markdown);
$this->assertEquals($expectedMarkup, $actualMarkup);
$unsafeExtension->setSafeMode(true);
$actualSafeMarkup = $unsafeExtension->text($markdown);
$this->assertEquals($expectedSafeMarkup, $actualSafeMarkup);
}
function testTrustDelegatedRawHtml()
{
$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;
$actualMarkup = $unsafeExtension->text($markdown);
$this->assertEquals($expectedMarkup, $actualMarkup);
$unsafeExtension->setSafeMode(true);
$actualSafeMarkup = $unsafeExtension->text($markdown);
$this->assertEquals($expectedSafeMarkup, $actualSafeMarkup);
}
2014-09-26 03:04:25 +04:00
function data()
{
$data = array();
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
foreach ($Folder as $File)
{
/** @var $File DirectoryIterator */
if ( ! $File->isFile())
{
continue;
}
$filename = $File->getFilename();
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($extension !== 'md')
{
continue;
}
$basename = $File->getBasename('.md');
if (file_exists($dir . $basename . '.html'))
{
$data []= array($basename, $dir);
}
}
}
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>&lt;div&gt;<em>content</em>&lt;/div&gt;</p>
2014-09-26 03:04:25 +04:00
<p>sparse:</p>
2014-12-03 00:53:19 +03:00
<p>&lt;div&gt;
2018-03-19 01:33:26 +03:00
&lt;div class="inner"&gt;
2014-09-26 03:04:25 +04:00
<em>content</em>
2014-12-03 00:53:19 +03:00
&lt;/div&gt;
&lt;/div&gt;</p>
2014-09-26 03:04:25 +04:00
<p>paragraph</p>
2018-03-19 01:33:26 +03:00
<p>&lt;style type="text/css"&gt;
2014-12-15 01:52:03 +03:00
p {
color: red;
}
&lt;/style&gt;</p>
2014-10-10 22:07:41 +04:00
<p>comment</p>
2014-12-03 00:53:19 +03:00
<p>&lt;!-- html comment --&gt;</p>
2014-09-26 03:04:25 +04:00
EXPECTED_HTML;
$parsedownWithNoMarkup = new TestParsedown();
2014-09-26 03:04:25 +04:00
$parsedownWithNoMarkup->setMarkupEscaped(true);
$this->assertEquals($expectedHtml, $parsedownWithNoMarkup->text($markdownWithHtml));
}
public function testLateStaticBinding()
{
$parsedown = Parsedown::instance();
2018-04-17 17:05:39 +03:00
$this->assertInstanceOf('Erusev\Parsedown\Parsedown', $parsedown);
// 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);
$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);
$sameInstanceAgain = TestParsedown::instance('test late static binding');
$this->assertSame($testParsedown, $sameInstanceAgain);
}
2014-09-26 03:04:25 +04:00
}