]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - tests/plugins/PluginMarkdownTest.php
More merge mess up fixes
[github/shaarli/Shaarli.git] / tests / plugins / PluginMarkdownTest.php
... / ...
CommitLineData
1<?php
2
3/**
4 * PluginMarkdownTest.php
5 */
6
7require_once 'application/Utils.php';
8require_once 'plugins/markdown/markdown.php';
9
10/**
11 * Class PluginMarkdownTest
12 * Unit test for the Markdown plugin
13 */
14class PluginMarkdownTest extends PHPUnit_Framework_TestCase
15{
16 /**
17 * Reset plugin path
18 */
19 function setUp()
20 {
21 PluginManager::$PLUGINS_PATH = 'plugins';
22 }
23
24 /**
25 * Test render_linklist hook.
26 * Only check that there is basic markdown rendering.
27 */
28 function testMarkdownLinklist()
29 {
30 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
31 $data = array(
32 'links' => array(
33 0 => array(
34 'description' => $markdown,
35 ),
36 ),
37 );
38
39 $data = hook_markdown_render_linklist($data);
40 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
41 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
42 }
43
44 /**
45 * Test render_daily hook.
46 * Only check that there is basic markdown rendering.
47 */
48 function testMarkdownDaily()
49 {
50 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
51 $data = array(
52 // Columns data
53 'cols' => array(
54 // First, second, third.
55 0 => array(
56 // nth link
57 0 => array(
58 'formatedDescription' => $markdown,
59 ),
60 ),
61 ),
62 );
63
64 $data = hook_markdown_render_daily($data);
65 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<h1>'));
66 $this->assertNotFalse(strpos($data['cols'][0][0]['formatedDescription'], '<p>'));
67 }
68
69 /**
70 * Test reverse_text2clickable().
71 */
72 function testReverseText2clickable()
73 {
74 $text = 'stuff http://hello.there/is=someone#here otherstuff';
75 $clickableText = text2clickable($text, '');
76 $reversedText = reverse_text2clickable($clickableText);
77 $this->assertEquals($text, $reversedText);
78 }
79
80 /**
81 * Test reverse_nl2br().
82 */
83 function testReverseNl2br()
84 {
85 $text = 'stuff' . PHP_EOL . 'otherstuff';
86 $processedText = nl2br($text);
87 $reversedText = reverse_nl2br($processedText);
88 $this->assertEquals($text, $reversedText);
89 }
90
91 /**
92 * Test reverse_space2nbsp().
93 */
94 function testReverseSpace2nbsp()
95 {
96 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
97 $processedText = space2nbsp($text);
98 $reversedText = reverse_space2nbsp($processedText);
99 $this->assertEquals($text, $reversedText);
100 }
101
102 /**
103 * Test sanitize_html().
104 */
105 function testSanitizeHtml()
106 {
107 $input = '< script src="js.js"/>';
108 $input .= '< script attr>alert(\'xss\');</script>';
109 $input .= '<style> * { display: none }</style>';
110 $output = escape($input);
111 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
112 $output .= '<a href="#" attr="tt">link</a>';
113 $this->assertEquals($output, sanitize_html($input));
114 // Do not touch escaped HTML.
115 $input = escape($input);
116 $this->assertEquals($input, sanitize_html($input));
117 }
118
119 /**
120 * Test the no markdown tag.
121 */
122 function testNoMarkdownTag()
123 {
124 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
125 $data = array(
126 'links' => array(array(
127 'description' => $str,
128 'tags' => NO_MD_TAG,
129 'taglist' => array(NO_MD_TAG),
130 ))
131 );
132
133 $processed = hook_markdown_render_linklist($data);
134 $this->assertEquals($str, $processed['links'][0]['description']);
135
136 $processed = hook_markdown_render_feed($data);
137 $this->assertEquals($str, $processed['links'][0]['description']);
138
139 $data = array(
140 // Columns data
141 'cols' => array(
142 // First, second, third.
143 0 => array(
144 // nth link
145 0 => array(
146 'formatedDescription' => $str,
147 'tags' => NO_MD_TAG,
148 'taglist' => array(),
149 ),
150 ),
151 ),
152 );
153
154 $data = hook_markdown_render_daily($data);
155 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
156 }
157
158 /**
159 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
160 */
161 function testNoMarkdownNotExcactlyMatching()
162 {
163 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
164 $data = array(
165 'links' => array(array(
166 'description' => $str,
167 'tags' => '.' . NO_MD_TAG,
168 'taglist' => array('.'. NO_MD_TAG),
169 ))
170 );
171
172 $data = hook_markdown_render_feed($data);
173 $this->assertContains('<em>', $data['links'][0]['description']);
174 }
175
176 /**
177 * Test hashtag links processed with markdown.
178 */
179 function testMarkdownHashtagLinks()
180 {
181 $md = file_get_contents('tests/plugins/resources/markdown.md');
182 $md = format_description($md);
183 $html = file_get_contents('tests/plugins/resources/markdown.html');
184
185 $data = process_markdown($md);
186 $this->assertEquals($html, $data);
187 }
188}