assertBBCode($expected, $src); } /** * Test the `[observer]` BBCode tags. * * @dataProvider bbcode_observer_provider */ public function test_bbcode_observer(string $src, bool $logged_in, string $lang, string $expected): void { if ($logged_in) { \App::$observer = [ 'xchan_addr' => '', 'xchan_name' => '', 'xchan_connurl' => '', 'xchan_photo_l' => '', // port required in xchan url due to bug in get_rpost_path 'xchan_url' => 'https://example.com:666', ]; } else { \App::$observer = null; } \App::$language = $lang; $this->assertBBCode($expected, $src); } /** * Test parsing the `[channel]` tag. */ public function test_bbcode_channel(): void { $src = '[channel=1]This is only for channels[/channel][channel=0]This is for everyone else[/channel]'; // Verify that the right part is shown to users _not_ in a channel \App::$channel = null; $this->assertBBCode('This is for everyone else', $src); // Now verify that the right part is shown to users _in_ a channel \App::$channel = [42]; $this->assertBBCode('This is only for channels', $src); } /** * Test converting html to BBCode. * * @dataProvider html2bbcode_provider */ public function test_html2bbcode(string $src, string $expected): void { $this->assertEquals($expected, html2bbcode($src)); } /** * Helper method to validate BBCode conversions. * * @param string $expected The expected result of the conversion. * @param string $src The BBCode to be converted. */ private function assertBBCode(string $expected, string $src): void { // Note! We turn off trying to create oembeds, as that will trigger a // network request when running the test. $this->assertEquals($expected, bbcode($src, ['tryoembed' => false])); } /** * Dataprovider for test_parsing_bbcode_to_html. * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ private function bbcode_to_html_provider(): array { return [ 'code block' => [ "[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]", '
testvar = "this is a test"
echo "the message is $testvar"
',
],
'code block with surroundin linebreaks \n' => [
"some text\n[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]\nsome more text",
'some texttestvar = "this is a test"
echo "the message is $testvar"
some more text',
],
'list with linebreaks \n' => [
"some text\n[list]\n[*] item1\n[*] item2\n[/list]\nsome more text",
'some textA paragraph over\nmultiple lines\nshould be unwrapped
", 'A paragraph over multiple lines should be unwrapped' ], 'image with alt text' => [ '', '[img=https://example.com/image.jpg]Alt text[/img]' ], 'code block' => [ "some\ncode
",
"[code]some\ncode[/code]"
],
'code block with indentation' => [
"some\n indented\ncode
",
"[code]some\n indented\ncode[/code]"
],
'paragraph with a mention and some text' => [
'@profile some content
', '[url=https://example.org/@profile]@profile[/url] some content' ], 'nested tags with ampersand and new line' => [ "\nfoo & bar", '[b] [i]foo & bar[/i][/b]' ], 'html reshares from streams' => [ '', '[url=https://example.com][img=https://example.com/image.jpg]image/photo[/img][/url] shared something' . "\n" . 'something' ] ]; } }