BbcodeAndHtmlTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. // Call Zend_Markup_BbcodeAndHtmlTest::main()
  3. // if this source file is executed directly.
  4. if (!defined("PHPUnit_MAIN_METHOD")) {
  5. define("PHPUnit_MAIN_METHOD", "Zend_Markup_BbcodeAndHtmlTest::main");
  6. }
  7. require_once dirname(__FILE__) . '/../../TestHelper.php';
  8. require_once 'Zend/Markup.php';
  9. /**
  10. * Test class for Zend_Json_Server
  11. */
  12. class Zend_Markup_BbcodeAndHtmlTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Zend_Markup_Renderer_RendererAbstract instance
  16. *
  17. * @var Zend_Markup_Renderer_RendererAbstract
  18. */
  19. protected $_markup;
  20. /**
  21. * Runs the test methods of this class.
  22. *
  23. * @return void
  24. */
  25. public static function main()
  26. {
  27. require_once "PHPUnit/TextUI/TestRunner.php";
  28. $suite = new PHPUnit_Framework_TestSuite("Zend_Markup_MarkupTest");
  29. $result = PHPUnit_TextUI_TestRunner::run($suite);
  30. }
  31. /**
  32. * Sets up the fixture
  33. * This method is called before a test is executed.
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. $this->_markup = Zend_Markup::factory('bbcode', 'html');
  40. }
  41. /**
  42. * Tears down the fixture
  43. * This method is called after a test is executed.
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. unset($this->_markup);
  50. }
  51. /**
  52. * Test for basic tags
  53. *
  54. * @return void
  55. */
  56. public function testBasicTags()
  57. {
  58. $this->assertEquals('<strong>foo</strong>bar', $this->_markup->render('[b]foo[/b]bar'));
  59. $this->assertEquals('<strong>foo<em>bar</em>foo</strong>ba[r',
  60. $this->_markup->render('[b=test file="test"]foo[i hell=nice]bar[/i]foo[/b]ba[r'));
  61. }
  62. /**
  63. * Test the behaviour of complicated tags
  64. *
  65. * @return void
  66. */
  67. public function testComplicatedTags()
  68. {
  69. $this->assertEquals('<a href="http://framework.zend.com/">http://framework.zend.com/</a>',
  70. $this->_markup->render('[url]http://framework.zend.com/[/url]'));
  71. $this->assertEquals('<a href="http://framework.zend.com/">foo</a>',
  72. $this->_markup->render('[url=http://framework.zend.com/]foo[/url]'));
  73. $this->assertEquals('bar', $this->_markup->render('[url="invalid"]bar[/url]'));
  74. $this->assertEquals('<img src="http://framework.zend.com/images/logo.png" alt="logo" />',
  75. $this->_markup->render('[img]http://framework.zend.com/images/logo.png[/img]'));
  76. $this->assertEquals('<img src="http://framework.zend.com/images/logo.png" alt="Zend Framework" />',
  77. $this->_markup->render('[img alt="Zend Framework"]http://framework.zend.com/images/logo.png[/img]'));
  78. $this->assertEquals('invalid', $this->_markup->render('[img]invalid[/img]'));
  79. }
  80. /**
  81. * Test input exceptions
  82. *
  83. * @return void
  84. */
  85. public function testExceptionParserWrongInputType()
  86. {
  87. $this->setExpectedException('Zend_Markup_Parser_Exception');
  88. $this->_markup->getParser()->parse(array());
  89. }
  90. /**
  91. * Test exception
  92. *
  93. * @return void
  94. */
  95. public function testExceptionParserEmptyInput()
  96. {
  97. $this->setExpectedException('Zend_Markup_Parser_Exception');
  98. $this->_markup->getParser()->parse('');
  99. }
  100. /**
  101. * Test adding tags
  102. *
  103. * @return void
  104. */
  105. public function testAddTags()
  106. {
  107. $this->_markup->addTag('bar',
  108. Zend_Markup_Renderer_RendererAbstract::TYPE_CALLBACK | Zend_Markup_Renderer_RendererAbstract::TAG_NORMAL,
  109. array('callback' => 'markupTestCallback', 'group' => 'inline'));
  110. $this->_markup->addTag('suppp',
  111. Zend_Markup_Renderer_RendererAbstract::TYPE_REPLACE | Zend_Markup_Renderer_RendererAbstract::TAG_NORMAL,
  112. array('start' => '<sup>', 'end' => '</sup>', 'group' => 'inline'));
  113. $this->_markup->addTag('zend',
  114. Zend_Markup_Renderer_RendererAbstract::TYPE_REPLACE | Zend_Markup_Renderer_RendererAbstract::TAG_SINGLE,
  115. array('replace' => 'Zend Framework', 'group' => 'inline'));
  116. $this->_markup->addTag('line', Zend_Markup_Renderer_RendererAbstract::TYPE_ALIAS,
  117. array('name' => 'hr'));
  118. $this->assertEquals('[foo=blaat]hell<sup>test</sup>blaat[/foo]',
  119. $this->_markup->render('[bar="blaat"]hell[suppp]test[/suppp]blaat[/]'));
  120. $this->assertEquals('Zend Framework', $this->_markup->render('[zend]'));
  121. $this->assertEquals('<hr />', $this->_markup->render('[line]'));
  122. $this->assertEquals('<sup>test aap</sup>test',
  123. $this->_markup->render('[suppp]test aap[/suppp]test'));
  124. }
  125. public function testHtmlUrlTitleIsRenderedCorrectly() {
  126. $this->assertEquals('<a href="http://exampl.com" title="foo">test</a>',
  127. $this->_markup->render('[url=http://exampl.com title=foo]test[/url]'));
  128. }
  129. public function testValueLessAttributeDoesNotThrowNotice() {
  130. // Notice: Uninitialized string offset: 42
  131. // in Zend/Markup/Parser/Bbcode.php on line 316
  132. $expected = '<a href="http://example.com">Example</a>';
  133. $value = '[url=http://example.com foo]Example[/url]';
  134. $this->assertEquals($expected, $this->_markup->render($value));
  135. }
  136. public function testAttributeNotEndingValueDoesNotThrowNotice()
  137. {
  138. // Notice: Uninitialized string offset: 13
  139. // in Zend/Markup/Parser/Bbcode.php on line 337
  140. $this->_markup->render('[url=http://framework.zend.com/ title="');
  141. }
  142. public function testAttributeFollowingValueDoesNotThrowNotice()
  143. {
  144. // Notice: Uninitialized string offset: 38
  145. // in Zend/Markup/Parser/Bbcode.php on line 337
  146. $this->_markup->render('[url="http://framework.zend.com/"title');
  147. }
  148. public function testHrTagWorks() {
  149. $this->assertEquals('foo<hr />bar', $this->_markup->render('foo[hr]bar'));
  150. }
  151. public function testFunkyCombos() {
  152. $expected = '<span style="text-decoration: underline;">a[/b][hr]b'
  153. . '<strong>c</strong></span><strong>d</strong>[/u]e';
  154. $outcome = $this->_markup->render('[u]a[/b][hr]b[b]c[/u]d[/b][/u]e');
  155. $this->assertEquals($expected, $outcome);
  156. }
  157. public function testImgSrcsConstraints() {
  158. $this->assertEquals('F/\!ZLrFz',$this->_markup->render('F[img]/\!ZLrFz[/img]'));
  159. }
  160. public function testColorConstraintsAndJs() {
  161. $input = "<kokx> i think you mean? [color=\"onclick='foobar();'\"]your text[/color] DASPRiD";
  162. $expected = "&lt;kokx&gt; i think you mean? <span>your text</span> DASPRiD";
  163. $this->assertEquals($expected, $this->_markup->render($input));
  164. }
  165. public function testNeverEndingAttribute() {
  166. $input = "[color=\"green]your text[/color]";
  167. $expected = '[color=&quot;green]your text[/color]';
  168. $this->assertEquals($expected, $this->_markup->render($input));
  169. }
  170. public function testTreatmentNonTags() {
  171. $input = '[span][acronym][h1][h2][h3][h4][h5][h6][nothing]'
  172. . '[/h6][/h5][/h4][/h3][/h2][/h1][/acronym][/span]';
  173. $expected = '<span><acronym><h1><h2><h3><h4><h5><h6>[nothing]'
  174. . '</h6></h5></h4></h3></h2></h1></acronym></span>';
  175. $this->assertEquals($expected, $this->_markup->render($input));
  176. }
  177. public function testListItems() {
  178. $input = "[list][*]Foo*bar (item 1)\n[*]Item 2\n[*]Trimmed (Item 3)\n[/list]";
  179. $expected = "<ul><li>Foo*bar (item 1)</li><li>Item 2</li><li>Trimmed (Item 3)</li></ul>";
  180. $this->assertEquals($expected, $this->_markup->render($input));
  181. }
  182. public function testListTypes()
  183. {
  184. $types = array(
  185. '01' => 'decimal-leading-zero',
  186. '1' => 'decimal',
  187. 'i' => 'lower-roman',
  188. 'I' => 'upper-roman',
  189. 'a' => 'lower-alpha',
  190. 'A' => 'upper-alpha',
  191. 'alpha' => 'lower-greek'
  192. );
  193. foreach ($types as $type => $style) {
  194. $input = "[list={$type}][*]Foobar\n[*]Zend\n[/list]";
  195. $expected = "<ol style=\"list-style-type: {$style}\"><li>Foobar</li><li>Zend</li></ol>";
  196. $this->assertEquals($expected, $this->_markup->render($input));
  197. }
  198. }
  199. public function testHtmlTags() {
  200. $m = $this->_markup;
  201. $this->assertEquals('<strong>foo</strong>', $m->render('[b]foo[/b]'));
  202. $this->assertEquals('<span style="text-decoration: underline;">foo</span>',
  203. $m->render('[u]foo[/u]'));
  204. $this->assertEquals('<em>foo</em>', $m->render('[i]foo[/i]'));
  205. $this->assertEquals('<cite>foo</cite>', $m->render('[cite]foo[/cite]'));
  206. $this->assertEquals('<del>foo</del>', $m->render('[del]foo[/del]'));
  207. $this->assertEquals('<ins>foo</ins>', $m->render('[ins]foo[/ins]'));
  208. $this->assertEquals('<sub>foo</sub>', $m->render('[sub]foo[/sub]'));
  209. $this->assertEquals('<span>foo</span>', $m->render('[span]foo[/span]'));
  210. $this->assertEquals('<acronym>foo</acronym>', $m->render('[acronym]foo[/acronym]'));
  211. $this->assertEquals('<h1>F</h1>', $m->render('[h1]F[/h1]'));
  212. $this->assertEquals('<h2>R</h2>', $m->render('[h2]R[/h2]'));
  213. $this->assertEquals('<h3>E</h3>', $m->render('[h3]E[/h3]'));
  214. $this->assertEquals('<h4>E</h4>', $m->render('[h4]E[/h4]'));
  215. $this->assertEquals('<h5>A</h5>', $m->render('[h5]A[/h5]'));
  216. $this->assertEquals('<h6>Q</h6>', $m->render('[h6]Q[/h6]'));
  217. $this->assertEquals('<span style="color: red;">foo</span>', $m->render('[color=red]foo[/color]'));
  218. $this->assertEquals('<span style="color: #00FF00;">foo</span>', $m->render('[color=#00FF00]foo[/color]'));
  219. $expected = '<code><span style="color: #000000">' . "\n"
  220. . '<span style="color: #0000BB">&lt;?php<br /></span>'
  221. . "<span style=\"color: #007700\">exit;</span>\n</span>\n</code>";
  222. $this->assertEquals($expected, $m->render("[code]<?php\nexit;[/code]"));
  223. $this->assertEquals('<p>I</p>', $m->render('[p]I[/p]'));
  224. $this->assertEquals('N',
  225. $m->render('[ignore]N[/ignore]'));
  226. $this->assertEquals('<blockquote>M</blockquote>', $m->render('[quote]M[/quote]'));
  227. $this->assertEquals('<hr />foo<hr />bar[/hr]', $m->render('[hr]foo[hr]bar[/hr]'));
  228. }
  229. public function testWrongNesting()
  230. {
  231. $this->assertEquals('<strong>foo<em>bar</em></strong>',
  232. $this->_markup->render('[b]foo[i]bar[/b][/i]'));
  233. $this->assertEquals('<strong>foo<em>bar</em></strong><em>kokx</em>',
  234. $this->_markup->render('[b]foo[i]bar[/b]kokx[/i]'));
  235. }
  236. public function testHtmlAliases() {
  237. $m = $this->_markup;
  238. $this->assertEquals($m->render('[b]F[/b]'), $m->render('[bold]F[/bold]'));
  239. $this->assertEquals($m->render('[bold]R[/bold]'), $m->render('[strong]R[/strong]'));
  240. $this->assertEquals($m->render('[i]E[/i]'), $m->render('[i]E[/i]'));
  241. $this->assertEquals($m->render('[i]E[/i]'), $m->render('[italic]E[/italic]'));
  242. $this->assertEquals($m->render('[i]A[/i]'), $m->render('[emphasized]A[/emphasized]'));
  243. $this->assertEquals($m->render('[i]Q[/i]'), $m->render('[em]Q[/em]'));
  244. $this->assertEquals($m->render('[u]I[/u]'), $m->render('[underline]I[/underline]'));
  245. $this->assertEquals($m->render('[cite]N[/cite]'), $m->render('[citation]N[/citation]'));
  246. $this->assertEquals($m->render('[del]G[/del]'), $m->render('[deleted]G[/deleted]'));
  247. $this->assertEquals($m->render('[ins]M[/ins]'), $m->render('[insert]M[/insert]'));
  248. $this->assertEquals($m->render('[s]E[/s]'),$m->render('[strike]E[/strike]'));
  249. $this->assertEquals($m->render('[sub]-[/sub]'), $m->render('[subscript]-[/subscript]'));
  250. $this->assertEquals($m->render('[sup]D[/sup]'), $m->render('[superscript]D[/superscript]'));
  251. $this->assertEquals($m->render('[url]google.com[/url]'), $m->render('[a]google.com[/a]'));
  252. $this->assertEquals($m->render('[img]http://google.com/favicon.ico[/img]'),
  253. $m->render('[image]http://google.com/favicon.ico[/image]'));
  254. }
  255. public function testEmptyTagName()
  256. {
  257. $this->assertEquals('[]', $this->_markup->render('[]'));
  258. }
  259. public function testStyleAlignCombination()
  260. {
  261. $m = $this->_markup;
  262. $this->assertEquals('<h1 style="color: green;text-align: left;">Foobar</h1>',
  263. $m->render('[h1 style="color: green" align=left]Foobar[/h1]'));
  264. $this->assertEquals('<h1 style="color: green;text-align: center;">Foobar</h1>',
  265. $m->render('[h1 style="color: green;" align=center]Foobar[/h1]'));
  266. }
  267. public function testXssInAttributeValues()
  268. {
  269. $m = $this->_markup;
  270. $this->assertEquals('<strong class="&quot;&gt;xss">foobar</strong>',
  271. $m->render('[b class=\'">xss\']foobar[/b]'));
  272. }
  273. }
  274. function markupTestCallback($token, $text)
  275. {
  276. $bar = $token->getAttribute('bar');
  277. if (!empty($bar)) {
  278. $bar = '=' . $bar;
  279. }
  280. return "[foo{$bar}]" . $text . '[/foo]';
  281. }
  282. // Call Zend_Markup_BbcodeAndHtmlTest::main()
  283. // if this source file is executed directly.
  284. if (PHPUnit_MAIN_METHOD == "Zend_Markup_BbcodeAndHtmlTest::main") {
  285. Zend_Markup_BbcodeAndHtmlTest::main();
  286. }