diff options
author | Mario <mario@mariovavti.com> | 2024-05-15 08:28:19 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-05-15 08:28:19 +0000 |
commit | a10402a7883efd4886ad17c8133c10237f443181 (patch) | |
tree | eca3f5a3ba34d40d1e7d0766a59d3494c9db47ad /tests | |
parent | 2145207ad2feedde9a1fe73807ef6365664aad79 (diff) | |
parent | 5da0cc138f4330fd07e9c117b3f4261dc1bcbc19 (diff) | |
download | volse-hubzilla-a10402a7883efd4886ad17c8133c10237f443181.tar.gz volse-hubzilla-a10402a7883efd4886ad17c8133c10237f443181.tar.bz2 volse-hubzilla-a10402a7883efd4886ad17c8133c10237f443181.zip |
Merge branch 'refactor-module-rbmark' into 'dev'
Refactor and cleanup Rbmark module + add tests
See merge request hubzilla/core!2126
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/Module/HelpTest.php | 9 | ||||
-rw-r--r-- | tests/unit/Module/RbmarkTest.php | 80 | ||||
-rw-r--r-- | tests/unit/Module/TestCase.php | 20 |
3 files changed, 99 insertions, 10 deletions
diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index 0f1610db5..3fb7687d3 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -176,13 +176,4 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->assertPageContains('<h3>This is the included file.</h3>'); } - - /** - * Helper to simplify asserting contents in the rendered page. - * - * @param string $needle The expected string to find. - */ - private function assertPageContains(string $needle): void { - $this->assertStringContainsString($needle, \App::$page['content']); - } } diff --git a/tests/unit/Module/RbmarkTest.php b/tests/unit/Module/RbmarkTest.php new file mode 100644 index 000000000..594e7369b --- /dev/null +++ b/tests/unit/Module/RbmarkTest.php @@ -0,0 +1,80 @@ +<?php +/** + * Unit/integration tests for the Rbmark module. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * + * SPDX-License-Identifier: MIT + */ + +class RbmarkTest extends \Zotlabs\Tests\Unit\Module\TestCase { + public function test_unauthenticated_get_request_return_login_form(): void { + $lc_stub = $this->getFunctionMock('Zotlabs\Module', 'local_channel'); + $lc_stub + ->expects($this->once()) + ->willReturn(false); + + $this->get('rbmark', ['url' => 'https://bookmarked.url']); + + $this->assertPageContains('value="login" />'); + + // also check that the original query is saved in the session + $this->assertEquals('https://bookmarked.url', $_SESSION['bookmark']['url']); + $this->assertEquals('rbmark', $_SESSION['bookmark']['q']); + } + + public function test_authenticated_get_request_returns_save_bookmark_form(): void { + $lc_stub = $this->getFunctionMock('Zotlabs\Module', 'local_channel'); + $lc_stub + ->expects($this->once()) + ->willReturn(42); + + $this->get('rbmark', [ + 'url' => 'https://bookmarked.url', + 'title' => 'My bookmark', + ]); + + $this->assertPageContains('<form action="rbmark" method="post"'); + $this->assertPageContains('URL of bookmark'); + $this->assertPageContains('value="https://bookmarked.url"'); + $this->assertPageContains('value="My bookmark"'); + } + + public function test_that_params_are_escaped_in_save_bookmark_form(): void { + $lc_stub = $this->getFunctionMock('Zotlabs\Module', 'local_channel'); + $lc_stub + ->expects($this->once()) + ->willReturn(42); + + $this->get('rbmark', [ + 'url' => 'https://bookmarked.url" onload="alert(/boom/)', + 'title' => 'My bookmark"><script alert(/boom/);</script>', + ]); + + $this->assertPageContains('value="https://bookmarked.url" onload="alert(/boom/)'); + $this->assertPageContains('value="My bookmark"><script alert(/boom/);</script>'); + } + + public function test_that_existing_bookmark_folders_are_listed(): void { + $lc_stub = $this->getFunctionMock('Zotlabs\Module', 'local_channel'); + $lc_stub + ->expects($this->once()) + ->willReturn(42); + + $menu_id = menu_create([ + 'menu_name' => 'My bookmarks', + 'menu_desc' => 'A collection of my bookmarks', + 'menu_flags' => MENU_BOOKMARK, + 'menu_channel_id' => 42, + ]); + + $this->get('rbmark', [ + 'url' => 'https://bookmarked.url', + 'title' => 'My bookmark', + ]); + + $this->assertPageContains( + "<option value=\"{$menu_id}\" >My bookmarks</option>" + ); + } +} diff --git a/tests/unit/Module/TestCase.php b/tests/unit/Module/TestCase.php index aa09e0596..5ad213e81 100644 --- a/tests/unit/Module/TestCase.php +++ b/tests/unit/Module/TestCase.php @@ -12,10 +12,19 @@ class TestCase extends \Zotlabs\Tests\Unit\UnitTestCase { * * @param string $uri The URI to request. Typically this will be the module * name, followed by any req args separated by slashes. + * + * @param array $query Assciative array of query args, with the parameters + * as keys. */ - protected function get(string $uri): void { + protected function get(string $uri, array $query = []): void { $_GET['q'] = $uri; + + if (!empty($query)) { + $_GET = array_merge($_GET, $query); + } + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_REQUEST = $_GET; \App::init(); \App::$page['content'] = ''; @@ -25,6 +34,15 @@ class TestCase extends \Zotlabs\Tests\Unit\UnitTestCase { } /** + * Helper to simplify asserting contents in the rendered page. + * + * @param string $needle The expected string to find. + */ + protected function assertPageContains(string $needle): void { + $this->assertStringContainsString($needle, \App::$page['content']); + } + + /** * Stub out the `killme` function. * * Usefule for modules that call this function directly. |