From 62aefadc27002f9d47f391b15520a82d5c3d6ecc Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 May 2024 11:30:29 +0200 Subject: Module\Rpost: Add basic test and fix session access. Just a basic test to ensure that the module `get()` method behaves somewhat reasonable when no query params are given. Had to make a small change to the Rpost module itself. Since the `$_SESSION` superglobal may not always be set (and is not in the test), use `isset` instead of `array_key_exists` to check if we have saved query params in the session. In general, isset is safer than array_key_exists if there's a chance that the array itself may not exist. --- tests/unit/Module/RpostTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/unit/Module/RpostTest.php (limited to 'tests/unit') diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php new file mode 100644 index 000000000..796901520 --- /dev/null +++ b/tests/unit/Module/RpostTest.php @@ -0,0 +1,31 @@ +getFunctionMock('Zotlabs\Module', 'local_channel') + ->expects($this->any()) + ->willReturn(42); + + // Set basic access controls to keep AccessList happy. + \App::$channel = [ + 'channel_allow_cid' => null, + 'channel_allow_gid' => null, + 'channel_deny_cid' => null, + 'channel_deny_gid' => null, + ]; + + $this->get('rpost'); + + $this->assertPageContains('
assertPageContains(' Date: Sat, 18 May 2024 20:58:09 +0200 Subject: tests: Configure system.baseurl for tests. --- tests/unit/includes/NetworkTest.php | 8 +------- tests/unit/includes/dba/_files/config.yml | 4 ++++ 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/includes/NetworkTest.php b/tests/unit/includes/NetworkTest.php index 5bf175953..a41075f25 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 */ @@ -23,7 +17,7 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { 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 ], ]; } 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 @@ -1,5 +1,9 @@ --- config: + - + cat: system + k: baseurl + v: https://hubzilla.test - cat: system k: do_not_check_dns -- cgit v1.2.3 From 93a45be1816b33651fb2138c87aae083cfd480b9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 May 2024 20:59:08 +0200 Subject: tests: Set query string in Module\TestCase::get method. --- tests/unit/Module/TestCase.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/unit') diff --git a/tests/unit/Module/TestCase.php b/tests/unit/Module/TestCase.php index e7051e001..f2e19f265 100644 --- a/tests/unit/Module/TestCase.php +++ b/tests/unit/Module/TestCase.php @@ -25,6 +25,7 @@ class TestCase extends \Zotlabs\Tests\Unit\UnitTestCase { $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + $_SERVER['QUERY_STRING'] = "q={$uri}"; $_REQUEST = $_GET; \App::init(); -- cgit v1.2.3 From 7c688de9cd9bfbccab60d77396f0f879acd4a52d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 May 2024 21:00:28 +0200 Subject: tests: Add comment to Tests\Unit\Module\RpostTest. --- tests/unit/Module/RpostTest.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests/unit') diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php index 796901520..115894026 100644 --- a/tests/unit/Module/RpostTest.php +++ b/tests/unit/Module/RpostTest.php @@ -9,6 +9,10 @@ */ class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { + + /** + * Basic test of a get request with no args as an authenticated user. + */ public function test_get_with_no_args(): void { // Mock `local_chanel()` to emulate a valid logged in channel $lc_mock = $this->getFunctionMock('Zotlabs\Module', 'local_channel') -- cgit v1.2.3 From 76a92ac2e1ed2d22ad960739e56e5832872cfe23 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 May 2024 21:08:30 +0200 Subject: tests: Module\Rpost shows login form if not authenticated. --- tests/unit/Module/RpostTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'tests/unit') diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php index 115894026..2f277e66d 100644 --- a/tests/unit/Module/RpostTest.php +++ b/tests/unit/Module/RpostTest.php @@ -32,4 +32,17 @@ class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->assertPageContains('assertPageContains('getFunctionMock('Zotlabs\Module', 'local_channel') + ->expects($this->any()) + ->willReturn(false); + + $this->get('rpost'); + + $this->assertPageContains(' Date: Sun, 19 May 2024 11:41:45 +0200 Subject: tests: More tests for Module\Rpost. Also refactor the tests a bit to avoid duplicatng code. --- tests/unit/Module/RpostTest.php | 58 +++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php index 2f277e66d..106f06fde 100644 --- a/tests/unit/Module/RpostTest.php +++ b/tests/unit/Module/RpostTest.php @@ -14,20 +14,7 @@ class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { * Basic test of a get request with no args as an authenticated user. */ public function test_get_with_no_args(): void { - // Mock `local_chanel()` to emulate a valid logged in channel - $lc_mock = $this->getFunctionMock('Zotlabs\Module', 'local_channel') - ->expects($this->any()) - ->willReturn(42); - - // Set basic access controls to keep AccessList happy. - \App::$channel = [ - 'channel_allow_cid' => null, - 'channel_allow_gid' => null, - 'channel_deny_cid' => null, - 'channel_deny_gid' => null, - ]; - - $this->get('rpost'); + $this->get_authenticated(); $this->assertPageContains('assertPageContains('assertPageContains('get_authenticated([ + 'title' => 'This is my title', + 'body' => 'The body of the post', + 'source' => 'The temple of the Dagon', + ]); + + $this->assertPageContains('value="This is my title"'); + $this->assertPageContains('>The body of the post'); + $this->assertPageContains('value="The temple of the Dagon"'); + } + + public function test_convert_body_from_html_to_bbcode(): void { + $this->get_authenticated([ + 'body' => "

Awesome page

\r\n

Awesome content!

", + 'type' => 'html', + ]); + + $this->assertPageContains(">[h1]Awesome page[/h1]\n\nAwesome content!"); + } + + /** + * Private helper method to perform an authenticated GET request. + * + * @param array $query An associative array of query parameters. + */ + private function get_authenticated(array $query = []): void { + // Mock `local_chanel()` to emulate a valid logged in channel + $lc_mock = $this->getFunctionMock('Zotlabs\Module', 'local_channel') + ->expects($this->any()) + ->willReturn(42); + + // Set basic access controls to keep AccessList happy. + \App::$channel = [ + 'channel_allow_cid' => null, + 'channel_allow_gid' => null, + 'channel_deny_cid' => null, + 'channel_deny_gid' => null, + ]; + + $this->get('rpost', $query); + } } -- cgit v1.2.3 From fb1c66fbc9705961454d86770511642b97b41c1a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jun 2024 17:08:29 +0200 Subject: Fix warnings exposed by tests. Mainly missing variables for templates, and channel entries. --- tests/unit/Module/RpostTest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php index 106f06fde..ad94f2f06 100644 --- a/tests/unit/Module/RpostTest.php +++ b/tests/unit/Module/RpostTest.php @@ -67,10 +67,13 @@ class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { // Set basic access controls to keep AccessList happy. \App::$channel = [ - 'channel_allow_cid' => null, - 'channel_allow_gid' => null, - 'channel_deny_cid' => null, - 'channel_deny_gid' => null, + 'channel_id' => 42, + 'channel_location' => null, + 'channel_address' => '', + 'channel_allow_cid' => '', + 'channel_allow_gid' => '', + 'channel_deny_cid' => '', + 'channel_deny_gid' => '', ]; $this->get('rpost', $query); -- cgit v1.2.3