BbcodeAndHtmlTest.php 15 KB

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