From 0f0e23445ab00c49a09d3167ca220ac314722cfd Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Wed, 24 May 2017 23:47:03 +0200 Subject: :hammer::white_check_mark: Add html2markdown unit tests. A tiny refactoring to make HTML 2 markdown conversion testable. Add some unit tests to check the behavior of the now used HTML2Markdown library. There are some differences compared to the old pixel418/markdownify library. --- tests/unit/includes/MarkdownTest.php | 149 +++++++++++++++++++++++++++++++++++ tests/unit/includes/TextTest.php | 18 +++++ 2 files changed, 167 insertions(+) create mode 100644 tests/unit/includes/MarkdownTest.php (limited to 'tests/unit/includes') diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php new file mode 100644 index 000000000..3026c633a --- /dev/null +++ b/tests/unit/includes/MarkdownTest.php @@ -0,0 +1,149 @@ +assertEquals($markdown, html2markdown($html)); + } + + public function html2markdownProvider() { + return [ + 'empty text' => [ + '', + '' + ], + 'space and nbsp only' => [ + '  ', + '' + ], + 'strong, b, em, i, bib' => [ + 'strong bold em italic boitalicld', + '**strong** **bold** _em_ _italic_ **bo_italic_ld**' + ], + 'empty tags' => [ + 'text1 text2 ', + 'text1 text2' + ], + 'HTML entities, lt does not work' => [ + '& gt > lt <', + '& gt > lt' + ], + 'escaped HTML entities' => [ + '& lt < gt >', + '& lt < gt >' + ], + 'our escaped HTML entities' => [ + '&_lt_; &_gt_; &_amp_;', + '&\_lt\_; &\_gt\_; &\_amp\_;' + ], + 'linebreak' => [ + "line1
line2\nline3", + "line1 \nline2 line3" + ], + 'headlines' => [ + '

header1

Header 3

', + "header1\n=======\n\n### Header 3" + ], + 'unordered list' => [ + '', + "- Item 1\n- Item 2\n- Item **3**" + ], + 'ordered list' => [ + '
  1. Item 1
  2. Item 2
  3. Item 3
', + "1. Item 1\n2. Item 2\n3. Item **3**" + ], + 'nested lists' => [ + '', + "- Item 1\n 1. Item 1a\n 2. Item **1b**\n- Item 2" + ], + 'img' => [ + 'alt text', + '![alt text](/path/to/img.png "title text")' + ], + 'link' => [ + 'link', + '[link](http://hubzilla.org "Hubzilla")' + ], + 'img link' => [ + 'alt img text', + '[![alt img text](/img/hubzilla.png "img title")](http://hubzilla.org "Hubzilla")' + ], + 'script' => [ + "", + "" + ], + 'blockquote, issue #793' => [ + '
something
blah', + "> something\n\nblah" + ], + 'code' => [ + '<p>HTML text</p>', + '`

HTML text

`' + ], + 'pre' => [ + '
   line with  spaces   
', + '` line with spaces `' + ], + 'div p' => [ + '
div

p

', + "
div
p\n\n
" + ] + ]; + } + + /*public function testHtml2markdownException() { + //$this->expectException(\InvalidArgumentException::class); + // need to stub logger() for this to work + $this->assertEquals('', html2markdown('<getFunctionMock(__NAMESPACE__, "bbcode"); + $bbc->expects($this->once())->willReturn('testbold
i
'); + + $this->assertEquals($bb1, bb2diaspora($html1)); + } +*/ +} \ No newline at end of file diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php index e2c7cbb9a..4afa2b49b 100644 --- a/tests/unit/includes/TextTest.php +++ b/tests/unit/includes/TextTest.php @@ -11,6 +11,9 @@ use Zotlabs\Tests\Unit\UnitTestCase; */ class TextTest extends UnitTestCase { + /** + * @covers ::valid_email_regex + */ public function testGoodEmail() { $this->assertTrue(valid_email_regex('ken@spaz.org')); $this->assertTrue(valid_email_regex('ken@restivo.org')); @@ -18,11 +21,17 @@ class TextTest extends UnitTestCase { $this->assertTrue(valid_email_regex('foo+nobody@hubzilla.org')); } + /** + * @covers ::valid_email_regex + */ public function testBadEmail() { $this->assertFalse(valid_email_regex('nobody!uses!these!any.more')); $this->assertFalse(valid_email_regex('foo@bar@hubzilla.org')); } + /** + * @covers ::purify_html + */ public function testPurifyHTML() { // linebreaks $htmlbr = 'first line
@@ -46,6 +55,9 @@ empty line above'; $this->assertEquals('', purify_html('')); } + /** + * @covers ::purify_html + */ public function testPurifyHTML_html() { $this->assertEquals('

ids und classes

', purify_html('

ids und classes

')); $this->assertEquals('

close missing tags

', purify_html('

close missing tags')); @@ -59,6 +71,9 @@ empty line above'; $this->assertEquals('', purify_html('')); } + /** + * @covers ::purify_html + */ public function testPurifyHTML_js() { $this->assertEquals('

', purify_html('
')); $this->assertEquals('link', purify_html('link')); @@ -66,6 +81,9 @@ empty line above'; $this->assertEquals('', purify_html('')); } + /** + * @covers ::purify_html + */ public function testPurifyHTML_css() { $this->assertEquals('

red

', purify_html('

red

')); $this->assertEquals('

invalid color

', purify_html('

invalid color

')); -- cgit v1.2.3