diff options
Diffstat (limited to 'tests/unit/includes')
-rw-r--r-- | tests/unit/includes/AccountTest.php | 2 | ||||
-rw-r--r-- | tests/unit/includes/AuthTest.php | 81 | ||||
-rw-r--r-- | tests/unit/includes/BBCodeTest.php | 68 | ||||
-rw-r--r-- | tests/unit/includes/DatetimeTest.php | 53 | ||||
-rw-r--r-- | tests/unit/includes/FeedutilsTest.php | 66 | ||||
-rw-r--r-- | tests/unit/includes/ItemsTest.php | 132 | ||||
-rw-r--r-- | tests/unit/includes/LanguageTest.php | 50 | ||||
-rw-r--r-- | tests/unit/includes/MarkdownTest.php | 39 | ||||
-rw-r--r-- | tests/unit/includes/NetworkTest.php | 70 | ||||
-rw-r--r-- | tests/unit/includes/TextTest.php | 4 | ||||
-rw-r--r-- | tests/unit/includes/dba/DbaPdoTest.php | 140 | ||||
-rw-r--r-- | tests/unit/includes/dba/TransactionTest.php | 6 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/account.yml | 19 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/config.yml | 4 | ||||
-rw-r--r-- | tests/unit/includes/dba/_files/register.yml | 20 |
15 files changed, 673 insertions, 81 deletions
diff --git a/tests/unit/includes/AccountTest.php b/tests/unit/includes/AccountTest.php index af5bcd3c1..3978f9d04 100644 --- a/tests/unit/includes/AccountTest.php +++ b/tests/unit/includes/AccountTest.php @@ -19,7 +19,7 @@ class AccountTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertEquals($expected, check_account_email($email)); } - function check_account_email_provider() : array { + public static function check_account_email_provider() : array { return [ // Empty and valid emails return the same result ['', ['error' => false, 'message' => '']], diff --git a/tests/unit/includes/AuthTest.php b/tests/unit/includes/AuthTest.php new file mode 100644 index 000000000..fa9726fe8 --- /dev/null +++ b/tests/unit/includes/AuthTest.php @@ -0,0 +1,81 @@ +<?php +/** + * Tests for the authentication code used in Hubzilla. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Includes; + +use App; +use Zotlabs\Lib\AConfig; +use Zotlabs\Tests\Unit\UnitTestCase; +use PHPUnit\Framework\Attributes\{DataProvider, RunTestsInSeparateProcesses}; + +/** + * Test class containing the test for the Hubzilla authentication code. + * + * Since the main authentication code is executed in the global scope on + * inclusion of the `includes/auth.php` file, we need to run each test in a + * separate process to make sure we can excersize the code as we need. + */ +#[RunTestsInSeparateProcesses] +class AuthTest extends UnitTestCase { + + /** + * Check that mfa status is not checked for certain modules. + * + * This causes issues with things like WebDAV and CardDAV, as there's + * currently no way for these modules to signal that a TOTP code is needed + * back to the connecting client. + */ + #[DataProvider('modules_excluded_from_mfa')] + public function test_mfa_is_not_checked_for_excluded_modules(string $module, array $args): void { + $account_id = $this->fixtures['account']['0']['account_id']; + + $_SESSION = [ + 'authenticated' => true, + 'account_id' => $account_id, + + // Trick the code to not warn that $_SESSION['uid'] is not set, + // but also not trigger the code that tries to change to the + // given channel. *Remove when code is fixed!* + 'uid' => 0, + ]; + + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + + App::$session = $this->create_session_stub(); + App::$module = $module; + App::$argv = $args; + App::$argc = count($args); + + // Enable multi factor authentication for this account + AConfig::Set($account_id, 'system', 'mfa_enabled', true); + + require 'include/auth.php'; + + $this->assertEquals(1, $_SESSION['authenticated']); + } + + /** + * Data provider for testing modules excluded from mfa + * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + */ + public static function modules_excluded_from_mfa(): array { + return [ + ['totp_check', []], + ['cdav', []], + ['cdav', ['calendar']], + ['cdav', ['addressbook']], + ['dav', []], + ]; + } + + private function create_session_stub(): \Zotlabs\Web\Session { + return $this->createStub('Zotlabs\Web\Session'); + } +} diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index a8aa8ee2b..136fc6e0e 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -101,7 +101,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function bbcode_to_html_provider(): array { + public static function bbcode_to_html_provider(): array { return [ 'code block' => [ "[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]", @@ -134,7 +134,27 @@ class BBCodeTest extends UnitTestCase { 'list with linebreaks \r\n in text' => [ "some text\r\n[list]\r\n[*] item1\r\nsome text[*] item2\r\nsome text[/list]\r\nsome more text", 'some text<br /><ul class="listbullet"><li> item1<br />some text<li> item2<br />some text</ul>some more text' - ] + ], + 'del tag' => [ + 'some [s]strike through[/s] text', + 'some <del>strike through</del> text' + ], + 'naked url is converted to link' => [ + 'example url: https://example.com', + 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>' + ], + 'naked url followed by newline' => [ + "https://www.example.com\nhave a great day.", + '<a href="https://www.example.com" target="_blank" rel="nofollow noopener">https://www.example.com</a><br />have a great day.', + ], + 'inline naked url' => [ + "This is a link https://example.com/some/path more info.", + 'This is a link <a href="https://example.com/some/path" target="_blank" rel="nofollow noopener">https://example.com/some/path</a> more info.', + ], + 'naked url within code block is not converted to link' => [ + "[code]\nhttp://example.com\n[/code]", + "<pre><code>http://example.com</code></pre>" + ], ]; } @@ -149,7 +169,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function bbcode_observer_provider(): array { + public static function bbcode_observer_provider(): array { return [ 'authenticated observer' => [ '[observer=1]This should be visible[/observer][observer=0]but not this[/observer]', @@ -201,7 +221,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function html2bbcode_provider(): array { + public static function html2bbcode_provider(): array { return [ 'paragraph over multiple lines' => [ "<p>A paragraph over\nmultiple lines\nshould be unwrapped</p>", @@ -219,6 +239,10 @@ class BBCodeTest extends UnitTestCase { "<pre><code>some\n indented\ncode</code></pre>", "[code]some\n indented\ncode[/code]" ], + 'code block with URL' => [ + '<pre><code>\nproxy_pass http://example.com\n</code></pre>', + '[code]\nproxy_pass http://example.com\n[/code]' + ], 'paragraph with a mention and some text' => [ '<p><span class="h-card" translate="no"><a href="https://example.org/@profile" class="u-url mention">@<span>profile</span></a></span> some content</p>', '[url=https://example.org/@profile]@profile[/url] some content' @@ -230,6 +254,42 @@ class BBCodeTest extends UnitTestCase { 'html reshares from streams' => [ '<div><div><a href="https://example.com"><img src="https://example.com/image.jpg" alt="image/photo"></a> shared something</div>something</div>', '[url=https://example.com][img=https://example.com/image.jpg]image/photo[/img][/url] shared something' . "\n" . 'something' + ], + 'list' => [ + '<ul><li>list 1</li><li>list 2</li><li>list 3</li></ul>', + '[list][*]list 1[*]list 2[*]list 3[/list]' + ], + 'list with paragraph' => [ + '<ul><li><p>list 1</p></li><li><p>list 2</p></li><li><p>list 3</p></li></ul>', + '[list][*]list 1[*]list 2[*]list 3[/list]' + ], + 'nested list' => [ + '<ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li></ul></ul>', + '[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list]' + ], + 'double nested list' => [ + '<ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li><ul><li>list 1</li><li>list 2</li><li>list 3</li></ul></ul></ul>', + '[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list][/list]' + ], + 'list without closing li' => [ + '<ul><li>list 1<li>list 2<li>list 3</ul>', + '[list][*]list 1[*]list 2[*]list 3[/list]' + ], + 'nested list without closing li' => [ + '<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3</ul></ul>', + '[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list]' + ], + 'double nested list without closing li' => [ + '<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3<ul><li>list 1<li>list 2<li>list 3</ul></ul></ul>', + '[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[list][*]list 1[*]list 2[*]list 3[/list][/list][/list]' + ], + 'del tag' => [ + 'some <del>strike through</del> text', + 'some [s]strike through[/s] text' + ], + 'table' => [ + '<table><tr><td>row1, col1</td><td>row1, col2</td></tr><tr><td>row2, col1</td><td>row2, col2</td></tr></table>', + '[table][tr][td]row1, col1[/td][td]row1, col2[/td][/tr][tr][td]row2, col1[/td][td]row2, col2[/td][/tr][/table]' ] ]; } diff --git a/tests/unit/includes/DatetimeTest.php b/tests/unit/includes/DatetimeTest.php new file mode 100644 index 000000000..f8c480449 --- /dev/null +++ b/tests/unit/includes/DatetimeTest.php @@ -0,0 +1,53 @@ +<?php +/** + * tests function from include/datetime.php + * + * @package test.util + */ + +use Zotlabs\Tests\Unit\UnitTestCase; + +class DatetimeTest extends UnitTestCase { + + // Test when the timestamp is in the past + public function test_relative_time_past() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2023-12-05 10:30:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('1 year ago', $result); + } + + // Test when the timestamp is in the future + public function test_relative_time_future() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-09 12:00:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('in 2 days', $result); + } + + // Test for "now" case (timestamp exactly equal to current time) + public function test_relative_time_now() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 00:00:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('now', $result); + } + + // Test for future time with smaller units (e.g., minutes) + public function test_relative_time_future_minutes() { + $now = new DateTime('2024-12-07 10:30:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 10:35:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('in 5 minutes', $result); + } + + // Test for past time with smaller units (e.g., seconds) + public function test_relative_time_past_seconds() { + $now = new DateTime('2024-12-07 10:30:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 10:29:58'); + $result = relative_time($timestamp, $now); + $this->assertEquals('2 seconds ago', $result); + } +} + + diff --git a/tests/unit/includes/FeedutilsTest.php b/tests/unit/includes/FeedutilsTest.php index bda0bf425..05bff06a3 100644 --- a/tests/unit/includes/FeedutilsTest.php +++ b/tests/unit/includes/FeedutilsTest.php @@ -47,7 +47,7 @@ class FeedutilsTest extends UnitTestCase { public function test_atom_author() { $this->assertEquals('', atom_author('', 'nick', 'name', 'uri', 72, 72, 'png', 'photourl')); - $a = '<tag> + $expected = '<tag> <id>uri</id> <name>nick</name> <uri>uri</uri> @@ -57,7 +57,10 @@ class FeedutilsTest extends UnitTestCase { <poco:displayName>name</poco:displayName> </tag>'; - $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); + $this->assertAtomAuthorMatches( + $expected, + atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl') + ); } /** @@ -86,6 +89,63 @@ class FeedutilsTest extends UnitTestCase { <poco:displayName>Chan</poco:displayName> </tag>'; - $this->assertXmlStringEqualsXmlString($a, atom_render_author('tag', $xchan)); + $this->assertAtomAuthorMatches($a, atom_render_author('tag', $xchan)); + } + + /** + * Helper method to assert that the generated author tag matches + * what we expect. + * + * Calling `assertXmlStringEqualsXmlString` directly on the fragments + * does not work anymore in PHPUnit >= 10.x, as t will throw an XMLException + * because of undefined namespaces. + * + * To overcome that we wrap the generated tags in the proper template, + * and compare the fully generated XML from the template instead. + * + * @param string $expected The expected author XML fragment. + * @param string $actual The actually generated authr XML fragment. + */ + private function assertAtomAuthorMatches(string $expected, string $actual): void { + + // Make sure the template engine is initialized before we try to render + // the template. + // + // This may be problematic, as it will compile the template into the same + // directory as the site. Assuming that nobody is crazy enough to run the + // test suite in a production server, it should probably be fine for now. + $smarty = new \Zotlabs\Render\SmartyTemplate(); + \App::register_template_engine(get_class($smarty)); + + $feed_template = get_markup_template('atom_feed.tpl'); + $expected_xml = replace_macros($feed_template, [ + '$version' => 42, + '$generator' => 'Hubzilla test', + '$generator_uri' => 'https://hubzilla.test', + '$feed_id' => 'test_channel', + '$feed_title' => 'Test channel', + '$feed_updated' => 'Sometime', + '$author' => $expected, + '$owner' => $expected, + '$profile_page' => xmlify('https://hubzilla.test/channel/test'), + ]); + + $expected_xml .= '</feed>'; + + $actual_xml = replace_macros($feed_template, [ + '$version' => 42, + '$generator' => 'Hubzilla test', + '$generator_uri' => 'https://hubzilla.test', + '$feed_id' => 'test_channel', + '$feed_title' => 'Test channel', + '$feed_updated' => 'Sometime', + '$author' => $actual, + '$owner' => $actual, + '$profile_page' => xmlify('https://hubzilla.test/channel/test'), + ]); + + $actual_xml .= '</feed>'; + + $this->assertXmlStringEqualsXmlString($expected_xml, $actual_xml); } } diff --git a/tests/unit/includes/ItemsTest.php b/tests/unit/includes/ItemsTest.php new file mode 100644 index 000000000..1c2fb6725 --- /dev/null +++ b/tests/unit/includes/ItemsTest.php @@ -0,0 +1,132 @@ +<?php +/** + * tests function from include/items.php + * + * @package test.util + */ + +use Zotlabs\Tests\Unit\UnitTestCase; + +class ItemsTest extends UnitTestCase { + /** + * Data provider for item_forwardable function. + * + * @return array + */ + public static function itemForwardableDataProvider() + { + return [ + // Test case: item is unpublished + [ + [ + 'item_unpublished' => 1, + 'item_delayed' => 0, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => '', + 'author' => ['xchan_network' => ''] + ], + false // Expected result + ], + // Test case: item is delayed + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 1, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => '', + 'author' => ['xchan_network' => ''] + ], + false + ], + // Test case: item is blocked + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 0, + 'item_blocked' => 1, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => '', + 'author' => ['xchan_network' => ''] + ], + false + ], + // Test case: verb is 'Follow' (forbidden verb) + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 0, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Follow', + 'postopts' => '', + 'author' => ['xchan_network' => ''] + ], + false + ], + // Test case: postopts contains 'nodeliver' + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 0, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => 'nodeliver', + 'author' => ['xchan_network' => ''] + ], + false + ], + // Test case: actor's network is 'rss' (restricted network) + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 0, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => '', + 'author' => ['xchan_network' => 'rss'] + ], + false + ], + // Test case: no conditions met (should forward) + [ + [ + 'item_unpublished' => 0, + 'item_delayed' => 0, + 'item_blocked' => 0, + 'item_hidden' => 0, + 'item_restrict' => 0, + 'verb' => 'Create', + 'postopts' => '', + 'author' => ['xchan_network' => 'other'] + ], + true + ] + ]; + } + + /** + * Test item_forwardable with various data. + * + * @dataProvider itemForwardableDataProvider + */ + public function testItemForwardable($item, $expected) + { + $this->assertSame($expected, item_forwardable($item)); + } + +} + + diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php index 9525c783d..8f62e71e2 100644 --- a/tests/unit/includes/LanguageTest.php +++ b/tests/unit/includes/LanguageTest.php @@ -33,54 +33,6 @@ use Text_LanguageDetect; * @author Klaus Weidenbach */ class LanguageTest extends UnitTestCase { - //use PHPMock; - - /** - * @dataProvider languageExamplesProvider - * @coversNothing - */ - public function testDetectLanguage($text, $langCode, $confidence) { - - // 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'); - //$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 [ - '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 @@ -94,7 +46,7 @@ class LanguageTest extends UnitTestCase { } } - public function getLanguageNameProvider() { + public static function getLanguageNameProvider() { return [ 'empty language code' => [ '', diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php index 960c15139..55dbb4445 100644 --- a/tests/unit/includes/MarkdownTest.php +++ b/tests/unit/includes/MarkdownTest.php @@ -36,10 +36,10 @@ class MarkdownTest extends UnitTestCase { * @dataProvider markdown_to_bbcode_provider */ public function test_markdown_to_bbcode(string $expected, string $src): void { - $this->assertEquals($expected, markdown_to_bb($src)); + $this->assertEquals($expected, markdown_to_bb($src, true, ['preserve_lf' => true])); } - private function markdown_to_bbcode_provider(): array { + public static function markdown_to_bbcode_provider(): array { return [ 'empty text' => [ '', @@ -54,11 +54,14 @@ class MarkdownTest extends UnitTestCase { 'This is a test of **bold text**, *italic text* and ***bold and italic text***' ], 'multiline text' => [ - 'This text is text wrapped over multiple lines.', + // This is not as expected in markdown, but may be needed + // for compatibility with bbcode behaviour. + "This text is\ntext wrapped\nover multiple\nlines.", "This text is\ntext wrapped\nover multiple\nlines." ], 'text with hard linebreak' => [ - "Line one\nLine two", + // An extra line break is inserted here... + "Line one\n\nLine two", "Line one \nLine two" ], 'paragraphs' => [ @@ -78,21 +81,39 @@ class MarkdownTest extends UnitTestCase { '`some code`' ], 'inline code with wrapped text' => [ - '[code]some code unwrapped[/code]', + // Not sure if the newline should be preseved here? + "[code]some code\nunwrapped[/code]", "`some code\n unwrapped`" ], 'code block no language' => [ - "[code]some code\nover multiple lines[/code]", + "[code]some code\nover multiple lines\n[/code]", "```\nsome code\nover multiple lines\n```" ], 'code block no language indented' => [ - "[code]some code\n over multiple lines\n with indentation[/code]", + // For some reason one space char is eaten on indented lines. + "[code]some code\n over multiple lines\n with indentation\n[/code]", "```\nsome code\n over multiple lines\n with indentation\n```" ], 'code block with language' => [ - "[code=php]<?php\necho phpinfo();[/code]", + "[code=php]<?php\necho phpinfo();\n[/code]", "```php\n<?php\necho phpinfo();\n```" ], + 'code block with URL' => [ + "[code]an example url https://example.com\n[/code]", + "```\nan example url https://example.com\n```" + ], + 'bbcode code block with URL' => [ + "[code]\nproxy_pass http://example.com;\n[/code]", + "[code]\nproxy_pass http://example.com;\n[/code]" + ], + 'naked url followed by newline' => [ + "https://example.com\nhave a great day.", + "https://example.com\nhave a great day.", + ], + 'inline naked url' => [ + 'This is a link https://example.com/some/path more info.', + 'This is a link https://example.com/some/path more info.', + ], ]; } @@ -104,7 +125,7 @@ class MarkdownTest extends UnitTestCase { $this->assertEquals($markdown, html2markdown($html)); } - public function html2markdownProvider(): array { + public static function html2markdownProvider(): array { return [ 'empty text' => [ '', diff --git a/tests/unit/includes/NetworkTest.php b/tests/unit/includes/NetworkTest.php index 9fb00e9d3..0d99fc9c3 100644 --- a/tests/unit/includes/NetworkTest.php +++ b/tests/unit/includes/NetworkTest.php @@ -7,12 +7,6 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { - public function setUp() : void { - parent::setUp(); - - \App::set_baseurl("https://mytest.org"); - } - /** * @dataProvider localUrlTestProvider */ @@ -20,10 +14,10 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertEquals($expected, is_local_url($url)); } - public function localUrlTestProvider() : array { + public static function localUrlTestProvider() : array { return [ [ '/some/path', true ], - [ 'https://mytest.org/some/path', true ], + [ 'https://hubzilla.test/some/path', true ], [ 'https://other.site/some/path', false ], ]; } @@ -47,7 +41,7 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertTrue(validate_email($email)); } - function validate_email_provider() : array { + public static function validate_email_provider() : array { return [ // First some invalid email addresses ['', false], @@ -66,7 +60,63 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { ['some.email@example.cancerresearch', true], // And internationalized TLD's - ['some.email@example.شبكة', true] + ['some.email@example.شبكة', true], + + // Allow plus/minus addressing + ['address+tag@example.com', true], + ['address-tag@example.com', true], ]; } + + /** + * Test the unparse_url function. + * + */ + public function test_unparse_url_full() + { + $parsed_url = [ + 'scheme' => 'https', + 'host' => 'www.example.com', + 'port' => '8080', + 'user' => 'username', + 'pass' => 'password', + 'path' => '/path', + 'query' => 'param=value', + 'fragment' => 'section' + ]; + + $expected = 'https://username:password@www.example.com:8080/path?param=value#section'; + $this->assertEquals($expected, unparse_url($parsed_url)); + } + + public function test_unparse_url_partial() + { + $parsed_url = [ + 'scheme' => 'http', + 'host' => 'example.com', + 'path' => '/index.php' + ]; + + $expected = 'http://example.com/index.php'; + $this->assertEquals($expected, unparse_url($parsed_url)); + } + + public function test_unparse_url_custom() + { + $parsed_url = [ + 'scheme' => 'https', + 'host' => 'www.example.com', + 'port' => '443', + 'path' => '/api' + ]; + + $parts = ['scheme', 'host']; + $expected = 'https://www.example.com'; + $this->assertEquals($expected, unparse_url($parsed_url, $parts)); + } + + public function test_unparse_url_empty() + { + $this->assertEquals('', unparse_url([])); + } } diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php index b76b15dcf..1e80d71d8 100644 --- a/tests/unit/includes/TextTest.php +++ b/tests/unit/includes/TextTest.php @@ -85,7 +85,7 @@ empty line above'; public function testNotags($string, $expected) { $this->assertEquals($expected, notags($string)); } - public function notagsProvider() { + public static function notagsProvider() { return [ 'empty string' => ['', ''], 'simple tag' => ['<value>', '[value]'], @@ -102,7 +102,7 @@ empty line above'; sanitise_acl($string); $this->assertEquals($expected, $string); } - public function sanitise_aclProvider() { + public static function sanitise_aclProvider() { return [ 'text' => ['value', '<value>'], 'text with angle bracket' => ['<value>', '<[value]>'], diff --git a/tests/unit/includes/dba/DbaPdoTest.php b/tests/unit/includes/dba/DbaPdoTest.php new file mode 100644 index 000000000..8a1a2b197 --- /dev/null +++ b/tests/unit/includes/dba/DbaPdoTest.php @@ -0,0 +1,140 @@ +<?php +/** + * Tests for `includes/dba_pdo.php`. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\includes; + +use DBA; +use PDO; +use PDOStatement; +use PHPUnit\Framework\Attributes\DataProvider; +use Zotlabs\Tests\Unit\UnitTestCase; + +class DbaPdoTest extends UnitTestCase +{ + public function testInsertingRowWithRturningClauseReturnsInsertedRow(): void + { + // MySQL does not support the `returning` clause, so we skip the test + // for that DB backend. + $this->skipIfMySQL(); + + // Let's manually insert a row in the config table. + // This is just because it's a conventient table to test + // against + $res = q(<<<SQL + INSERT INTO config (cat, k, v) + VALUES ('test', 'a key', 'A value') + RETURNING * + SQL); + + $this->assertIsArray($res); + $this->assertIsArray($res[0]); + $this->assertTrue($res[0]['id'] > 0); + $this->assertEquals('test', $res[0]['cat']); + $this->assertEquals('a key', $res[0]['k']); + $this->assertEquals('A value', $res[0]['v']); + } + + #[DataProvider('insertRowProvider')] + public function testInsertRow(string $table, array $data, string $id): void + { + $res = DBA::$dba->insert($table, $data, $id); + + $this->assertIsArray($res); + + // Make sure the result contains the expected id + $this->assertArrayHasKey($id, $res); + + foreach ($data as $key => $value) { + $this->assertEquals($value, $res[$key]); + } + } + + #[DataProvider('insertRowProvider')] + public function testInsertShouldReturnFalseIfInsertFails( + string $table, + array $data, + string $id + ): void + { + $res1 = DBA::$dba->insert($table, $data, $id); + $this->assertIsArray($res1); + + // Inserting the same row again should fail. + $res2 = DBA::$dba->insert($table, $data, $id); + $this->assertFalse($res2); + } + + /** + * Dataprovider for testInertRow. + * + * @return array An array of [ $table, $data, $id ] elements. + */ + public static function insertRowProvider(): array + { + return [ + 'table with numeric primary id' => [ + 'config', + [ 'cat' => 'test', 'k' => 'a key', 'v' => 'A value' ], + 'id', + ], + 'table with text primary id' => [ + 'cache', + [ 'k' => 'some key', 'v' => 'cached value', 'updated' => date('Y-m-d H:i:s')], + 'k', + ], + ]; + } + + public function testUpdateRow(): void + { + // Let's fetch a row from the config table + $res = q("SELECT * FROM config WHERE cat = 'system' AND k = 'baseurl'"); + + $this->assertIsArray($res); + $this->assertIsArray($res[0]); + + $row = $res[0]; + + // Update the baseurl + $updated = DBA::$dba->update( + 'config', + [ 'v' => 'https://some.other_site.test/' ], + 'id', + $row['id'] + ); + + $this->assertTrue($updated); + + // Verify that the record was updated + $updated_res = q("SELECT * FROM config WHERE cat = 'system' AND k = 'baseurl'"); + $this->assertIsArray($updated_res); + + $updated_row = $updated_res[0]; + + $this->assertIsArray($updated_row); + $this->assertEquals($row['id'], $updated_row['id']); + $this->assertEquals('system', $updated_row['cat']); + $this->assertEquals('baseurl', $updated_row['k']); + $this->assertEquals('https://some.other_site.test/', $updated_row['v']); + } + + /** + * Mark the test as skipped if the current db is MySQL. + */ + private function skipIfMySQL(): void { + $driver = DBA::$dba->db->getAttribute(PDO::ATTR_DRIVER_NAME); + $version = DBA::$dba->db->getAttribute(PDO::ATTR_SERVER_VERSION); + + if ($driver === 'mysql' && stripos($version, 'mariadb') === false) { + $this->markTestSkipped("RETURNING clause not supported for {$driver}"); + } + + } +} diff --git a/tests/unit/includes/dba/TransactionTest.php b/tests/unit/includes/dba/TransactionTest.php index 99e3f459d..0b986c6aa 100644 --- a/tests/unit/includes/dba/TransactionTest.php +++ b/tests/unit/includes/dba/TransactionTest.php @@ -24,8 +24,8 @@ require_once 'tests/fakes/fake_dba.php'; require_once 'include/dba/dba_transaction.php'; -use \PHPUnit\Framework\TestCase; -use \Zotlabs\Tests\Fakes\FakeDba; +use PHPUnit\Framework\TestCase; +use Zotlabs\Tests\Fakes\FakeDba; /** * Test database transactions. @@ -39,7 +39,7 @@ class DbaTransactionTest extends TestCase { private $pdo_stub; public function setUp(): void { - $this->pdo_stub = $this->createStub(PDO::class); + $this->pdo_stub = $this->createMock(PDO::class); } diff --git a/tests/unit/includes/dba/_files/account.yml b/tests/unit/includes/dba/_files/account.yml index 344bdb799..9c3d00ec8 100644 --- a/tests/unit/includes/dba/_files/account.yml +++ b/tests/unit/includes/dba/_files/account.yml @@ -3,7 +3,26 @@ account: account_id: 42 account_email: "hubzilla@example.com" account_language: "no" + account_level: 5 + account_flags: 0 - account_id: 43 account_email: "hubzilla@example.org" account_language: "de" + account_level: 5 + account_flags: 1 + - + account_id: 44 + account_email: "blocked@example.org" + account_level: 5 + account_flags: 2 + - + account_id: 45 + account_email: "pending@example.org" + account_level: 5 + account_flags: 0x10 + - + account_id: 46 + account_email: "unverified@example.org" + account_level: 5 + account_flags: 0x11 diff --git a/tests/unit/includes/dba/_files/config.yml b/tests/unit/includes/dba/_files/config.yml index e93486857..ac3c8acb0 100644 --- a/tests/unit/includes/dba/_files/config.yml +++ b/tests/unit/includes/dba/_files/config.yml @@ -2,6 +2,10 @@ config: - cat: system + k: baseurl + v: https://hubzilla.test + - + cat: system k: do_not_check_dns v: true - diff --git a/tests/unit/includes/dba/_files/register.yml b/tests/unit/includes/dba/_files/register.yml new file mode 100644 index 000000000..2ef1a5365 --- /dev/null +++ b/tests/unit/includes/dba/_files/register.yml @@ -0,0 +1,20 @@ +--- +register: + - + reg_vital: 1 + reg_flags: 0x10 + reg_did2: 'verified@example.com' + reg_email: 'verified@example.com' + reg_hash: '123' + reg_uid: 45 + reg_pass: 'verify' + reg_stuff: '' + - + reg_vital: 1 + reg_flags: 0x11 + reg_did2: 'unverified@example.com' + reg_email: 'unverified@example.com' + reg_hash: '666' + reg_uid: 46 + reg_pass: 'verify' + reg_stuff: '' |