2
0

BbcodeAndHtmlTest.php 14 KB

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