This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/bbcode/test.php

144 lines
4.3 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class BBCodeTest extends ShimmiePHPUnitTestCase
{
2024-01-15 14:31:51 +00:00
public function testBasics(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<b>bold</b><i>italic</i>",
$this->filter("[b]bold[/b][i]italic[/i]")
);
}
2024-01-15 14:31:51 +00:00
public function testStacking(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<b>B</b><i>I</i><b>B</b>",
$this->filter("[b]B[/b][i]I[/i][b]B[/b]")
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<b>bold<i>bolditalic</i>bold</b>",
$this->filter("[b]bold[i]bolditalic[/i]bold[/b]")
);
}
2024-01-15 14:31:51 +00:00
public function testFailure(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"[b]bold[i]italic",
$this->filter("[b]bold[i]italic")
);
}
2024-01-15 14:31:51 +00:00
public function testCode(): void
{
$this->assertEquals(
2024-06-11 15:02:52 +00:00
"<pre><code>[b]bold[/b]</code></pre>",
2020-10-24 12:46:49 +00:00
$this->filter("[code][b]bold[/b][/code]")
);
}
2009-01-25 12:26:07 +00:00
2024-01-15 14:31:51 +00:00
public function testNestedList(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<ul><li>a<ul><li>a<li>b</ul><li>b</ul>",
$this->filter("[list][*]a[list][*]a[*]b[/list][*]b[/list]")
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<ul><li>a<ol><li>a<li>b</ol><li>b</ul>",
$this->filter("[ul][*]a[ol][*]a[*]b[/ol][*]b[/ul]")
);
}
2009-01-25 12:26:07 +00:00
2024-01-15 14:31:51 +00:00
public function testSpoiler(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<span style=\"background-color:#000; color:#000;\">ShishNet</span>",
$this->filter("[spoiler]ShishNet[/spoiler]")
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"FuvfuArg",
$this->strip("[spoiler]ShishNet[/spoiler]")
);
#$this->assertEquals(
# $this->filter("[spoiler]ShishNet"),
# "[spoiler]ShishNet");
}
2009-09-19 19:23:48 +00:00
2024-01-15 14:31:51 +00:00
public function testURL(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<a href=\"https://shishnet.org\">https://shishnet.org</a>",
$this->filter("[url]https://shishnet.org[/url]")
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<a href=\"https://shishnet.org\">ShishNet</a>",
$this->filter("[url=https://shishnet.org]ShishNet[/url]")
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"[url=javascript:alert(\"owned\")]click to fail[/url]",
$this->filter("[url=javascript:alert(\"owned\")]click to fail[/url]")
);
}
2024-01-15 14:31:51 +00:00
public function testEmailURL(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"<a href=\"mailto:spam@shishnet.org\">spam@shishnet.org</a>",
$this->filter("[email]spam@shishnet.org[/email]")
);
}
2009-12-30 09:13:57 +00:00
2024-01-15 14:31:51 +00:00
public function testAnchor(): void
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
'<span class="anchor">Rules <a class="alink" href="#bb-rules" name="bb-rules" title="link to this anchor"> ¶ </a></span>',
$this->filter("[anchor=rules]Rules[/anchor]")
);
}
private function filter(string $in): string
{
$bb = new BBCode();
return $bb->_format($in);
}
2009-09-19 19:23:48 +00:00
private function strip(string $in): string
{
$bb = new BBCode();
return $bb->strip($in);
}
2020-03-28 14:11:14 +00:00
2024-01-15 14:31:51 +00:00
public function testSiteLinks(): void
2020-03-28 14:11:14 +00:00
{
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="" href="/test/post/view/123">&gt;&gt;123</a>',
$this->filter("&gt;&gt;123")
);
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="#c456" href="/test/post/view/123#c456">&gt;&gt;123#c456</a>',
$this->filter("&gt;&gt;123#c456")
);
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="" href="/test/foo/bar">foo/bar</a>',
$this->filter("[url]site://foo/bar[/url]")
);
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="#c123" href="/test/foo/bar#c123">foo/bar#c123</a>',
$this->filter("[url]site://foo/bar#c123[/url]")
);
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="" href="/test/foo/bar">look at my post</a>',
$this->filter("[url=site://foo/bar]look at my post[/url]")
);
$this->assertEquals(
'<a class="shm-clink" data-clink-sel="#c123" href="/test/foo/bar#c123">look at my comment</a>',
$this->filter("[url=site://foo/bar#c123]look at my comment[/url]")
);
}
}