|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'Parser.php'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Test cases testing the HTMLSafe visitor, which escapes all html characters in the source text |
| 7 | + * |
| 8 | + * @author astax-t |
| 9 | + */ |
| 10 | +class HTMLSafeTest extends PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Asserts that the given bbcode string produces the given html string |
| 14 | + * when parsed with the default bbcodes. |
| 15 | + */ |
| 16 | + public function assertProduces($bbcode, $html) |
| 17 | + { |
| 18 | + $parser = new \JBBCode\Parser(); |
| 19 | + $parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet()); |
| 20 | + $parser->parse($bbcode); |
| 21 | + |
| 22 | + $htmlsafer = new JBBCode\visitors\HTMLSafeVisitor(); |
| 23 | + $parser->accept($htmlsafer); |
| 24 | + |
| 25 | + $this->assertEquals($html, $parser->getAsHtml()); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Tests escaping quotes and ampersands in simple text |
| 30 | + */ |
| 31 | + public function testQuoteAndAmp() |
| 32 | + { |
| 33 | + $this->assertProduces('te"xt te&xt', 'te"xt te&xt'); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Tests escaping quotes and ampersands inside a BBCode tag |
| 38 | + */ |
| 39 | + public function testQuoteAndAmpInTag() |
| 40 | + { |
| 41 | + $this->assertProduces('[b]te"xt te&xt[/b]', '<strong>te"xt te&xt</strong>'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests escaping HTML tags |
| 46 | + */ |
| 47 | + public function testHtmlTag() |
| 48 | + { |
| 49 | + $this->assertProduces('<b>not bold</b>', '<b>not bold</b>'); |
| 50 | + $this->assertProduces('[b]<b>bold</b>[/b] <hr>', '<strong><b>bold</b></strong> <hr>'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests escaping ampersands in URL using [url]...[/url] |
| 55 | + */ |
| 56 | + public function testUrlParam() |
| 57 | + { |
| 58 | + $this->assertProduces('text [url]http://example.com/?a=b&c=d[/url] more text', 'text <a href="http://example.com/?a=b&c=d">http://example.com/?a=b&c=d</a> more text'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Tests escaping ampersands in URL using [url=...] tag |
| 63 | + */ |
| 64 | + public function testUrlOption() |
| 65 | + { |
| 66 | + $this->assertProduces('text [url=http://example.com/?a=b&c=d]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&c=d">this is a "link"</a>'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Tests escaping ampersands in URL using [url=...] tag when URL is in quotes |
| 71 | + */ |
| 72 | + public function testUrlOptionQuotes() |
| 73 | + { |
| 74 | + $this->assertProduces('text [url="http://example.com/?a=b&c=d"]this is a "link"[/url]', 'text <a href="http://example.com/?a=b&c=d">this is a "link"</a>'); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments