diff options
Diffstat (limited to 'tests/unit/Widget')
-rw-r--r-- | tests/unit/Widget/HelpindexTest.php | 10 | ||||
-rw-r--r-- | tests/unit/Widget/MessagesWidgetTest.php | 83 |
2 files changed, 93 insertions, 0 deletions
diff --git a/tests/unit/Widget/HelpindexTest.php b/tests/unit/Widget/HelpindexTest.php index 26aa34104..87042c559 100644 --- a/tests/unit/Widget/HelpindexTest.php +++ b/tests/unit/Widget/HelpindexTest.php @@ -8,6 +8,8 @@ * SPDX-License-Identifier: MIT */ +use PHPUnit\Framework\Attributes\Before; + /** * Test class for testing the Helpindex widget. */ @@ -15,6 +17,8 @@ class HelpindexTest extends \Zotlabs\Tests\Unit\Module\TestCase { use \phpmock\phpunit\PHPMock; + private string $output; + /** * Define the stubs to make sure they work later in the test. * @@ -27,6 +31,12 @@ class HelpindexTest extends \Zotlabs\Tests\Unit\Module\TestCase { self::defineFunctionMock('Zotlabs\Widget', 'file_get_contents'); } + #[Before] + public function setup_state(): void { + // Make sure the output is cleared before running the test + $this->output = ''; + } + public function test_loading_toc(): void { // Stub `file_get_contents` to plant our own content. $fgc_stub = $this->getFunctionMock('Zotlabs\Widget', 'file_get_contents'); diff --git a/tests/unit/Widget/MessagesWidgetTest.php b/tests/unit/Widget/MessagesWidgetTest.php new file mode 100644 index 000000000..ca025ff43 --- /dev/null +++ b/tests/unit/Widget/MessagesWidgetTest.php @@ -0,0 +1,83 @@ +<?php +/* + * SPDX-FileCopyrightText: 2025 The Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen <haraldei@anduin.net> + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Widget; + +use App; +use Zotlabs\Widget\Messages; +use Zotlabs\Tests\Unit\Module\TestCase; + +class MessagesWidgetTest extends TestCase +{ + use \phpmock\phpunit\PHPMock; + + /** + * List of file tags should be empty if there are no file tags. + */ + public function testNoFileTags(): void + { + $local_channe_stub = $this->getFunctionMock('Zotlabs\Widget', 'local_channel') + ->expects($this->any()) + ->willReturn(42); + + $feature_enabled_stub = $this->getFunctionMock('Zotlabs\Widget', 'feature_enabled') + ->expects($this->any()) + ->willReturn(true); + + $this->renderWidget(); + $this->assertOutputMatches('|<datalist\s+id="data_filetags">\s+</datalist>|'); + } + + /** + * The widget lists file tags that are defined for the channel. + */ + public function testFileTagsAreListed(): void + { + $local_channe_stub = $this->getFunctionMock('Zotlabs\Widget', 'local_channel') + ->expects($this->any()) + ->willReturn(42); + + $feature_enabled_stub = $this->getFunctionMock('Zotlabs\Widget', 'feature_enabled') + ->expects($this->any()) + ->willReturn(true); + + /* + * Add a few tags. + */ + store_item_tag(42, 1, TERM_OBJ_POST, TERM_FILE, 'test_file_tag', ''); + store_item_tag(42, 1, TERM_OBJ_POST, TERM_FILE, 'test_file_tag2', ''); + + $this->renderWidget(); + $this->assertOutputMatches('|<option\s+value="test_file_tag">|'); + $this->assertOutputMatches('|<option\s+value="test_file_tag2">|'); + } + + /** + * Initializes the app and calls the widget code. + */ + private function renderWidget(): void { + $_GET['q'] = 'hq'; + $_SERVER['REQUEST_METHOD'] = 'GET'; + + App::init(); + + $widget = new Messages(); + $this->output = $widget->widget([]); + } + + /** + * Asserts that the output matches a given regex pattern. + * + * If the pattern does not match, the test will be marked as failed. + * + * @param string $pattern The regex that should be matched. + */ + private function assertOutputMatches(string $pattern): void { + $this->assertMatchesRegularExpression($pattern, $this->output); + } +} |