From 5e1addc75e73e84254e820fc67dba057a8728db6 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 26 Nov 2020 11:14:40 +0000 Subject: revert folder renaming --- tests/unit/includes/FeedutilsTest.php | 89 ++++++++++++++++ tests/unit/includes/LanguageTest.php | 164 +++++++++++++++++++++++++++++ tests/unit/includes/MarkdownTest.php | 147 ++++++++++++++++++++++++++ tests/unit/includes/PhotodriverTest.php | 39 +++++++ tests/unit/includes/TextTest.php | 120 +++++++++++++++++++++ tests/unit/includes/dba/_files/account.yml | 9 ++ 6 files changed, 568 insertions(+) create mode 100644 tests/unit/includes/FeedutilsTest.php create mode 100644 tests/unit/includes/LanguageTest.php create mode 100644 tests/unit/includes/MarkdownTest.php create mode 100644 tests/unit/includes/PhotodriverTest.php create mode 100644 tests/unit/includes/TextTest.php create mode 100644 tests/unit/includes/dba/_files/account.yml (limited to 'tests/unit/includes') diff --git a/tests/unit/includes/FeedutilsTest.php b/tests/unit/includes/FeedutilsTest.php new file mode 100644 index 000000000..e9826a73d --- /dev/null +++ b/tests/unit/includes/FeedutilsTest.php @@ -0,0 +1,89 @@ +assertEquals('id', normalise_id('id')); + $this->assertEquals('id', normalise_id('X-ZOT:id')); + $this->assertEquals('id id2', normalise_id('X-ZOT:id X-ZOT:id2')); + $this->assertEmpty(normalise_id('')); + } + + public function test_encode_rel_links() { + // invalid params return empty array + $this->assertEquals([], encode_rel_links('string')); + $this->assertEquals([], encode_rel_links([])); + + $b = ['attribs' => ['' => [ + 'rel' => 'rel_value', + 'type' => 'type_value', + 'href' => 'href_value', + 'length' => 'length_value', + 'title' => 'title_value' + ]]]; + $blink1 = ['link1' => $b]; + $bresult[] = $b['attribs']['']; + $this->assertEquals($bresult, encode_rel_links($blink1)); + } + +/* public function test_encode_rel_links_fail() { + $a = [ 'key' => 'value']; + $this->assertFalse(encode_rel_links($a)); + //Illegal string offset 'attribs' + }*/ + + /** + * @uses ::xmlify + */ + public function test_atom_author() { + $this->assertEquals('', atom_author('', 'nick', 'name', 'uri', 72, 72, 'png', 'photourl')); + + $a = ' + uri + nick + uri + + + nick + name +'; + + $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); + } + + /** + * @uses ::xmlify + */ + public function test_atom_render_author() { + $xchan = [ + 'xchan_addr' => 'chan@hub', + 'xchan_url' => 'http://hub', + 'xchan_name' => 'Chan', + 'xchan_photo_l' => 'http://hub/img', + 'xchan_photo_mimetype' => 'mimetype' + ]; + // There is no input validation in atom_render_author + //$this->assertEquals('', atom_render_author('', $xchan)); + + $a = ' + http://activitystrea.ms/schema/1.0/person + http://hub + chan + http://hub + + + + chan + Chan +'; + + $this->assertXmlStringEqualsXmlString($a, atom_render_author('tag', $xchan)); + } +} diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php new file mode 100644 index 000000000..0ca9eacd0 --- /dev/null +++ b/tests/unit/includes/LanguageTest.php @@ -0,0 +1,164 @@ +getFunctionMock(__NAMESPACE__, 'get_config'); + //$gc->expects($this->once())->willReturn(10) + //$cg = $this->getFunctionMock('Zotlabs\Lib\Config', 'Get'); + //$cg->expects($this->once())->willReturn(10); + //$this->assertEquals($langCode, detect_language($text)); + + + // Can not unit test detect_language(), therefore test the used library + // only for now to find regressions on library updates. + $l = new Text_LanguageDetect; + // return 2-letter ISO 639-1 (en) language code + $l->setNameMode(2); + $lng = $l->detectConfidence($text); + + $this->assertEquals($langCode, $lng['language']); + $this->assertEquals($confidence, round($lng['confidence'], 6)); + } + + public function languageExamplesProvider() { + return [ + 'empty text' => [ + '', + '', + null + ], + 'English' => [ + 'English is a West Germanic language that was first spoken in early medieval England and is now a global lingua franca.[4][5] Named after the Angles, one of the Germanic tribes that migrated to England, it ultimately derives its name from the Anglia (Angeln) peninsula in the Baltic Sea. It is closely related to the Frisian languages, but its vocabulary has been significantly influenced by other Germanic languages, particularly Norse (a North Germanic language), as well as by Latin and Romance languages, especially French.', + 'en', + 0.078422 + ], + 'German' => [ + 'Deutschland ist ein Bundesstaat in Mitteleuropa. Er besteht aus 16 Ländern und ist als freiheitlich-demokratischer und sozialer Rechtsstaat verfasst. Die Bundesrepublik Deutschland stellt die jüngste Ausprägung des deutschen Nationalstaates dar. Mit rund 82,8 Millionen Einwohnern (31. Dezember 2016) zählt Deutschland zu den dicht besiedelten Flächenstaaten.', + 'de', + 0.134339 + ], + 'Norwegian' => [ + 'Kongeriket Norge er et nordisk, europeisk land og en selvstendig stat vest på Den skandinaviske halvøy. Landet er langt og smalt, og kysten strekker seg langs Nord-Atlanteren, hvor også Norges kjente fjorder befinner seg. Totalt dekker det relativt tynt befolkede landet 385 000 kvadratkilometer med litt over fem millioner innbyggere (2016).', + 'no', + 0.007076 + ] + ]; + } + + + /** + * @covers ::get_language_name + * @dataProvider getLanguageNameProvider + */ + public function testGetLanguageName($lang, $name, $trans) { + $this->assertEquals($name, get_language_name($lang)); + foreach ($trans as $k => $v) { + //echo "$k -> $v"; + $this->assertEquals($v, get_language_name($lang, $k)); + } + } + + public function getLanguageNameProvider() { + return [ + 'empty language code' => [ + '', + '', + ['de' => ''] + ], + 'invalid language code' => [ + 'zz', + 'zz', + ['de' => 'zz'] + ], + 'de' => [ + 'de', + 'German', + [ + 'de' => 'Deutsch', + 'nb' => 'tysk' + ] + ], + 'de-de' => [ + 'de-de', + 'German', + [ + 'de-de' => 'Deutsch', + 'nb' => 'Deutsch' // should be tysk, seems to be a bug upstream + ] + ], + 'en' => [ + 'en', + 'English', + [ + 'de' => 'Englisch', + 'nb' => 'engelsk' + ] + ], + 'en-gb' => [ + 'en-gb', + 'British English', + [ + 'de' => 'Englisch (Vereinigtes Königreich)', + 'nb' => 'engelsk (Storbritannia)' + ] + ], + 'en-au' => [ + 'en-au', + 'Australian English', + [ + 'de' => 'Englisch (Australien)', + 'nb' => 'engelsk (Australia)' + ] + ], + 'nb' => [ + 'nb', + 'Norwegian Bokmål', + [ + 'de' => 'Norwegisch Bokmål', + 'nb' => 'norsk bokmål' + ] + ] + ]; + } +} diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php new file mode 100644 index 000000000..2a92a58d2 --- /dev/null +++ b/tests/unit/includes/MarkdownTest.php @@ -0,0 +1,147 @@ +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 >' + ], + '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' => [ + '
  one line with spaces  
', + "```\n one line with spaces \n```" + ], + '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)); + } +*/ +} diff --git a/tests/unit/includes/PhotodriverTest.php b/tests/unit/includes/PhotodriverTest.php new file mode 100644 index 000000000..6f6ad0ffe --- /dev/null +++ b/tests/unit/includes/PhotodriverTest.php @@ -0,0 +1,39 @@ +getFunctionMock(__NAMESPACE__, 'logger'); + //$logger->expects($this->once()); + + //$ph = \photo_factory('', 'image/bmp'); + //$this->assertNull($ph); + + $this->markTestIncomplete('Need to mock logger(), otherwise not unit testable.'); + } + + public function testPhotofactoryReturnsPhotogdIfConfigIgnore_imagickIsSet() { + // php-mock can not mock global functions which is called by a global function. + // If the calling function is in a namespace it would work. + //$gc = $this->getFunctionMock(__NAMESPACE__, 'get_config'); + // simulate get_config('system', 'ignore_imagick') configured + //$gc->expects($this->once())->willReturn(1) + + //$ph = \photo_factory(file_get_contents('images/hz-16.png'), 'image/png'); + //$this->assertInstanceOf(PhotoGd::class, $ph); + + $this->markTestIncomplete('Need to mock get_config(), otherwise not unit testable.'); + } +} \ No newline at end of file diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php new file mode 100644 index 000000000..97fa64895 --- /dev/null +++ b/tests/unit/includes/TextTest.php @@ -0,0 +1,120 @@ + + one tab preserved + +empty line above'; + $this->assertEquals($htmlbr, purify_html($htmlbr)); + + // HTML5 is not supported by HTMLPurifier yet, test our own configuration + $html5elements = '
section
footer
'; + $this->assertEquals($html5elements, purify_html($html5elements)); + $this->assertEquals('', purify_html('')); + + // unsupported HTML5 elements + $this->assertEquals('Your HTML parser does not support HTML5 video.', purify_html('')); + $this->assertEquals('Your HTML parser does not support HTML5 audio.', purify_html('')); + + // preserve f6 and bootstrap additional data attributes from our own configuration + $this->assertEquals('
text
', purify_html('
text
')); + $this->assertEquals('', purify_html('')); + $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')); + $this->assertEquals('

deprecated tag
', purify_html('
deprecated tag
')); + $this->assertEquals('
illegal nesting
', purify_html('
illegal nesting
')); + $this->assertEquals('link with target', purify_html('link with target')); + $this->assertEquals('link with rel="nofollow"', purify_html('link with rel="nofollow"')); + $this->assertEquals('a b', purify_html('a b')); + $this->assertEquals('ä ä € €', purify_html('ä ä € €')); + $this->assertEquals('text', purify_html('text')); + $this->assertEquals('', purify_html('')); + } + + /** + * @covers ::purify_html + */ + public function testPurifyHTML_js() { + $this->assertEquals('
', purify_html('
')); + $this->assertEquals('link', purify_html('link')); + $this->assertEquals('', purify_html('')); + $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

')); + $this->assertEquals('

invalid style

', purify_html('

invalid style

')); + + // test our own CSS configuration + $this->assertEquals('
position removed
', purify_html('
position removed
')); + $this->assertEquals('
position preserved
', purify_html('
position preserved
', true)); + $this->assertEquals('
invalid position removed
', purify_html('
invalid position removed
', true)); + + $this->assertEquals('
position removed
', purify_html('
position removed
')); + $this->assertEquals('
position preserved
', purify_html('
position preserved
', true)); + $this->assertEquals('
invalid position removed
', purify_html('
invalid position removed
', true)); + } + + /** + * @dataProvider notagsProvider + */ + public function testNotags($string, $expected) { + $this->assertEquals($expected, notags($string)); + } + public function notagsProvider() { + return [ + 'empty string' => ['', ''], + 'simple tag' => ['', '[value]'], + 'tag pair' => ['text', '[b]text[/b]'], + 'double angle bracket' => ['< ['>', '>'] + ]; + } + + /** + * @dataProvider sanitise_aclProvider + */ + public function testSanitise_acl($string, $expected) { + sanitise_acl($string); + $this->assertEquals($expected, $string); + } + public function sanitise_aclProvider() { + return [ + 'text' => ['value', ''], + 'text with angle bracket' => ['', '<[value]>'], + 'comma separated acls' => ['value1,value2', ''] + ]; + } + + public function testUnsetSanitise_acl() { + $empty = ''; + sanitise_acl($empty); + $this->assertTrue(isset($empty)); // unset() not working? Would expect false + $this->assertEmpty($empty); + } + +} diff --git a/tests/unit/includes/dba/_files/account.yml b/tests/unit/includes/dba/_files/account.yml new file mode 100644 index 000000000..344bdb799 --- /dev/null +++ b/tests/unit/includes/dba/_files/account.yml @@ -0,0 +1,9 @@ +account: + - + account_id: 42 + account_email: "hubzilla@example.com" + account_language: "no" + - + account_id: 43 + account_email: "hubzilla@example.org" + account_language: "de" -- cgit v1.2.3