2015-11-17 21:01:11 +01:00
|
|
|
<?php
|
2017-03-03 23:06:12 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2015-11-17 21:01:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PluginMarkdownTest.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once 'application/Utils.php';
|
|
|
|
require_once 'plugins/markdown/markdown.php';
|
|
|
|
|
|
|
|
/**
|
2016-11-13 16:51:21 +01:00
|
|
|
* Class PluginMarkdownTest
|
|
|
|
* Unit test for the Markdown plugin
|
2015-11-17 21:01:11 +01:00
|
|
|
*/
|
|
|
|
class PluginMarkdownTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2017-02-27 19:45:55 +01:00
|
|
|
/**
|
|
|
|
* @var ConfigManager instance.
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2015-11-17 21:01:11 +01:00
|
|
|
/**
|
|
|
|
* Reset plugin path
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function setUp()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = 'plugins';
|
2017-02-27 19:45:55 +01:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
2017-05-25 14:52:42 +02:00
|
|
|
$this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
|
2015-11-17 21:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test render_linklist hook.
|
|
|
|
* Only check that there is basic markdown rendering.
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testMarkdownLinklist()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
$markdown = '# My title' . PHP_EOL . 'Very interesting content.';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(
|
|
|
|
0 => array(
|
|
|
|
'description' => $markdown,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$data = hook_markdown_render_linklist($data, $this->conf);
|
2015-11-17 21:01:11 +01:00
|
|
|
$this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
|
|
|
|
$this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
|
2018-08-13 10:42:27 +02:00
|
|
|
|
|
|
|
$this->assertEquals($markdown, $data['links'][0]['description_src']);
|
2015-11-17 21:01:11 +01:00
|
|
|
}
|
|
|
|
|
2018-05-19 12:55:43 +02:00
|
|
|
/**
|
|
|
|
* Test render_feed hook.
|
|
|
|
*/
|
|
|
|
public function testMarkdownFeed()
|
|
|
|
{
|
|
|
|
$markdown = '# My title' . PHP_EOL . 'Very interesting content.';
|
|
|
|
$markdown .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(
|
|
|
|
0 => array(
|
|
|
|
'description' => $markdown,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = hook_markdown_render_feed($data, $this->conf);
|
|
|
|
$this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
|
|
|
|
$this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'— <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
|
|
|
|
$data['links'][0]['description']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:01:11 +01:00
|
|
|
/**
|
|
|
|
* Test render_daily hook.
|
|
|
|
* Only check that there is basic markdown rendering.
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testMarkdownDaily()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
$markdown = '# My title' . PHP_EOL . 'Very interesting content.';
|
|
|
|
$data = array(
|
|
|
|
// Columns data
|
2018-02-01 13:16:58 +01:00
|
|
|
'linksToDisplay' => array(
|
|
|
|
// nth link
|
2015-11-17 21:01:11 +01:00
|
|
|
0 => array(
|
2018-02-01 13:16:58 +01:00
|
|
|
'formatedDescription' => $markdown,
|
2015-11-17 21:01:11 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$data = hook_markdown_render_daily($data, $this->conf);
|
2018-02-01 13:16:58 +01:00
|
|
|
$this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
|
|
|
|
$this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
|
2015-11-17 21:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reverse_text2clickable().
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testReverseText2clickable()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
$text = 'stuff http://hello.there/is=someone#here otherstuff';
|
|
|
|
$clickableText = text2clickable($text, '');
|
|
|
|
$reversedText = reverse_text2clickable($clickableText);
|
|
|
|
$this->assertEquals($text, $reversedText);
|
|
|
|
}
|
|
|
|
|
2018-08-14 12:26:51 +02:00
|
|
|
/**
|
|
|
|
* Test reverse_text2clickable().
|
|
|
|
*/
|
|
|
|
public function testReverseText2clickableHashtags()
|
|
|
|
{
|
|
|
|
$text = file_get_contents('tests/plugins/resources/hashtags.raw');
|
|
|
|
$md = file_get_contents('tests/plugins/resources/hashtags.md');
|
|
|
|
$clickableText = hashtag_autolink($text);
|
|
|
|
$reversedText = reverse_text2clickable($clickableText);
|
|
|
|
$this->assertEquals($md, $reversedText);
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:01:11 +01:00
|
|
|
/**
|
|
|
|
* Test reverse_nl2br().
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testReverseNl2br()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
$text = 'stuff' . PHP_EOL . 'otherstuff';
|
|
|
|
$processedText = nl2br($text);
|
|
|
|
$reversedText = reverse_nl2br($processedText);
|
|
|
|
$this->assertEquals($text, $reversedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reverse_space2nbsp().
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testReverseSpace2nbsp()
|
2015-11-17 21:01:11 +01:00
|
|
|
{
|
|
|
|
$text = ' stuff' . PHP_EOL . ' otherstuff and another';
|
|
|
|
$processedText = space2nbsp($text);
|
|
|
|
$reversedText = reverse_space2nbsp($processedText);
|
|
|
|
$this->assertEquals($text, $reversedText);
|
|
|
|
}
|
|
|
|
|
2018-05-19 12:55:43 +02:00
|
|
|
public function testReverseFeedPermalink()
|
|
|
|
{
|
|
|
|
$text = 'Description... ';
|
|
|
|
$text .= '— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
|
|
|
|
$expected = 'Description... — [Permalien](http://domain.tld/?0oc_VQ)';
|
|
|
|
$processedText = reverse_feed_permalink($text);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $processedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReverseLastFeedPermalink()
|
|
|
|
{
|
|
|
|
$text = 'Description... ';
|
|
|
|
$text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
|
|
|
|
$expected = $text;
|
|
|
|
$text .= '<br>— <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
|
|
|
|
$expected .= '<br>— [Permalien](http://domain.tld/?0oc_VQ)';
|
|
|
|
$processedText = reverse_feed_permalink($text);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $processedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReverseNoFeedPermalink()
|
|
|
|
{
|
|
|
|
$text = 'Hello! Where are you from?';
|
|
|
|
$expected = $text;
|
|
|
|
$processedText = reverse_feed_permalink($text);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $processedText);
|
|
|
|
}
|
|
|
|
|
2015-11-17 21:01:11 +01:00
|
|
|
/**
|
2016-02-19 19:37:13 +01:00
|
|
|
* Test sanitize_html().
|
2015-11-17 21:01:11 +01:00
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testSanitizeHtml()
|
2016-03-21 18:46:34 +01:00
|
|
|
{
|
2016-02-19 19:37:13 +01:00
|
|
|
$input = '< script src="js.js"/>';
|
|
|
|
$input .= '< script attr>alert(\'xss\');</script>';
|
|
|
|
$input .= '<style> * { display: none }</style>';
|
|
|
|
$output = escape($input);
|
|
|
|
$input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
|
|
|
|
$output .= '<a href="#" attr="tt">link</a>';
|
2017-02-27 19:45:55 +01:00
|
|
|
$input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
|
|
|
|
$output .= '<a href="#" attr="tt">link</a>';
|
2016-02-19 19:37:13 +01:00
|
|
|
$this->assertEquals($output, sanitize_html($input));
|
|
|
|
// Do not touch escaped HTML.
|
|
|
|
$input = escape($input);
|
|
|
|
$this->assertEquals($input, sanitize_html($input));
|
2015-11-17 21:01:11 +01:00
|
|
|
}
|
2016-03-21 18:46:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the no markdown tag.
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testNoMarkdownTag()
|
2016-03-21 18:46:34 +01:00
|
|
|
{
|
|
|
|
$str = 'All _work_ and `no play` makes Jack a *dull* boy.';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(array(
|
|
|
|
'description' => $str,
|
2016-05-30 18:51:00 +02:00
|
|
|
'tags' => NO_MD_TAG,
|
|
|
|
'taglist' => array(NO_MD_TAG),
|
2016-03-21 18:46:34 +01:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$processed = hook_markdown_render_linklist($data, $this->conf);
|
2016-11-13 16:51:21 +01:00
|
|
|
$this->assertEquals($str, $processed['links'][0]['description']);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$processed = hook_markdown_render_feed($data, $this->conf);
|
2016-11-13 16:51:21 +01:00
|
|
|
$this->assertEquals($str, $processed['links'][0]['description']);
|
2016-03-21 18:46:34 +01:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
// Columns data
|
2018-02-01 13:16:58 +01:00
|
|
|
'linksToDisplay' => array(
|
|
|
|
// nth link
|
2016-03-21 18:46:34 +01:00
|
|
|
0 => array(
|
2018-02-01 13:16:58 +01:00
|
|
|
'formatedDescription' => $str,
|
|
|
|
'tags' => NO_MD_TAG,
|
|
|
|
'taglist' => array(),
|
2016-03-21 18:46:34 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$data = hook_markdown_render_daily($data, $this->conf);
|
2018-02-01 13:16:58 +01:00
|
|
|
$this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
|
2016-03-21 18:46:34 +01:00
|
|
|
}
|
2016-10-21 12:38:38 +02:00
|
|
|
|
2016-11-13 16:51:21 +01:00
|
|
|
/**
|
|
|
|
* Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
|
|
|
|
*/
|
2017-01-05 19:33:06 +01:00
|
|
|
public function testNoMarkdownNotExcactlyMatching()
|
2016-11-13 16:51:21 +01:00
|
|
|
{
|
|
|
|
$str = 'All _work_ and `no play` makes Jack a *dull* boy.';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(array(
|
|
|
|
'description' => $str,
|
|
|
|
'tags' => '.' . NO_MD_TAG,
|
|
|
|
'taglist' => array('.'. NO_MD_TAG),
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2017-02-27 19:45:55 +01:00
|
|
|
$data = hook_markdown_render_feed($data, $this->conf);
|
2016-11-13 16:51:21 +01:00
|
|
|
$this->assertContains('<em>', $data['links'][0]['description']);
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:38:38 +02:00
|
|
|
/**
|
2017-05-25 14:52:42 +02:00
|
|
|
* Make sure that the generated HTML match the reference HTML file.
|
2016-10-21 12:38:38 +02:00
|
|
|
*/
|
2017-05-25 14:52:42 +02:00
|
|
|
public function testMarkdownGlobalProcessDescription()
|
2016-10-21 12:38:38 +02:00
|
|
|
{
|
|
|
|
$md = file_get_contents('tests/plugins/resources/markdown.md');
|
|
|
|
$md = format_description($md);
|
|
|
|
$html = file_get_contents('tests/plugins/resources/markdown.html');
|
|
|
|
|
2017-05-25 14:52:42 +02:00
|
|
|
$data = process_markdown(
|
|
|
|
$md,
|
|
|
|
$this->conf->get('security.markdown_escape', true),
|
|
|
|
$this->conf->get('security.allowed_protocols')
|
|
|
|
);
|
2018-08-14 12:26:51 +02:00
|
|
|
$this->assertEquals($html, $data . PHP_EOL);
|
2016-10-21 12:38:38 +02:00
|
|
|
}
|
2017-02-27 19:45:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that the HTML tags are escaped.
|
|
|
|
*/
|
|
|
|
public function testMarkdownWithHtmlEscape()
|
|
|
|
{
|
|
|
|
$md = '**strong** <strong>strong</strong>';
|
|
|
|
$html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(
|
|
|
|
0 => array(
|
|
|
|
'description' => $md,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$data = hook_markdown_render_linklist($data, $this->conf);
|
|
|
|
$this->assertEquals($html, $data['links'][0]['description']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure that the HTML tags aren't escaped with the setting set to false.
|
|
|
|
*/
|
|
|
|
public function testMarkdownWithHtmlNoEscape()
|
|
|
|
{
|
|
|
|
$this->conf->set('security.markdown_escape', false);
|
|
|
|
$md = '**strong** <strong>strong</strong>';
|
|
|
|
$html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
|
|
|
|
$data = array(
|
|
|
|
'links' => array(
|
|
|
|
0 => array(
|
|
|
|
'description' => $md,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$data = hook_markdown_render_linklist($data, $this->conf);
|
|
|
|
$this->assertEquals($html, $data['links'][0]['description']);
|
|
|
|
}
|
2015-11-17 21:01:11 +01:00
|
|
|
}
|