userList)) {
foreach ($this->userList as $user) {
$user->remove();
}
}
}
#[DataProvider('dataTeX')]
public function testTeX(string $name, string $bbcode, string $expected): void {
$this->assertEquals($expected, \Text::full_format($bbcode), $name);
}
public static function dataTeX(): array {
return [
['text-tex-basic', '[tex]some formula[/tex]', 'some formula'],
['text-tex-outer', '[size=4][tex]some formula[/tex][/size]', 'some formula'],
['text-tex-nested', '[tex]some [b]formula[/b][/tex]', 'some [b]formula[/b]'],
];
}
#[DataProvider('dataBB')]
public function testBB(string $name, string $bbcode, string $expected): void {
$this->userList['user'] = Helper::makeUser('bb.' . randomString(6), 'text');
\Text::setViewer($this->userList['user']);
$this->assertEquals($expected, \Text::full_format($bbcode), $name);
}
public static function dataBB(): array {
$url = 'https://www.example.com/';
return [
["text-align-l", "[align=left]o[/align]", "
o
"],
["text-artist", "[artist]Frank Zappa[/artist]", "Frank Zappa"],
["text-b", "[b]o[/b]", "o"],
["text-br", "a\nb", "a
\nb"],
["text-box", "[box]jellyfish[/box]", "jellyfish
"],
["text-code", "[code]ab\ncd[/code]", "ab
\ncd"],
["text-color-name", "[color=cornsilk]pink[/color]", "pink"],
["text-color-hash", "[color=#c0ffee]black[/color]", "black"],
["text-colour", "[colour=gold]pink[/colour]", "pink"],
["text-headline", "[headline]abc[/headline]", "=abc="],
["text-hr", "[hr]", "
"],
["text-i", "[i]am[/i]", "am"],
["text-important", "[important]x[/important]", "x"],
["text-inlinesize", "[inlinesize=7]b[/inlinesize]", "b"],
["text-n", "[[n]i]am[[n]/i]", "[i]am[/i]"],
["text-pad", "[pad=2|4|6|8]text[/pad]", "text"],
["text-plain", "[plain]a[b]c[/plain]", "a[b]c"],
["text-pre", "[pre]a\nb[/pre]", "a
\nb
"],
["text-quote-n", "[quote]a[/quote]", "a
"],
["text-quote-y", "[quote=phpunit]a[/quote]", " wrote: a
"],
["text-rule", "[rule]1[/rule]", "1"],
["text-s", "[s]sss[/s]", "sss"],
["text-size", "[size=4]big[/size]", "big"],
["text-s", "[s]sss[/s]", "sss"],
["text-u", "[u]uuu[/u]", "uuu"],
["text-url", $url, "https://www.example.com/"],
["text-hide", "[hide]secret[/hide]",
"Hidden text: Showsecret
"
],
["text-spoiler", "[spoiler]surprise[/spoiler]",
"Hidden text: Showsurprise
"
],
["text-emoji", ":nod:", "
"],
["text-nest-1", "[size=3][i]z[/i][/size]", "z"],
["text-nest-2",
"[quote=user]abc [b]xyz[/b] def[/quote] [b][i][s][u]ghi[/u][/s][/i][/b]",
" wrote: abc xyz def
ghi"
],
["text-nest-3", "[quote][/quote]", "<I am test>
"],
["text-escape-1", "I'm test", "I'm test"],
["text-escape-2", "I'm test", "I'm test"],
["text-escape-3", "I'm test", "I&apos;m test"],
["text-escape-4", "", "<I am test>"],
];
}
public function testImage(): void {
$bad = "[img=https://www.example.com/a.pdf]";
$this->assertEquals($bad, $bad, "text-image-bad");
foreach (['avif', 'bmp', 'gif', 'jpg', 'jpeg', 'png', 'svg', 'tiff', 'webp'] as $ext) {
$image = "https://www.example.com/a.jpg";
$withCache = '@^
$@';
$noCache = "
";
$this->assertEquals($noCache, \Text::full_format("[img=$image]"), "text-image1-cache-implicit-$ext");
$this->assertEquals($noCache, \Text::full_format("[img]{$image}[/img]"), "text-image2-cache-implicit-$ext");
}
}
public function testCollage(): void {
$this->userList['admin'] = Helper::makeUser('collage.' . randomString(6), 'text');
$this->userList['admin']->setField('PermissionID', SYSOP)->modify();
$name = 'collage ' . randomString(6);
$collage = (new Manager\Collage())->create($this->userList['admin'], 1, $name, 'phpunit collage', 'jazz,disco');
$this->assertInstanceOf(Collage::class, $collage, 'text-create-collage');
$this->assertEquals(
"id}\">{$collage->name()}",
\Text::full_format("[collage]{$collage->id}[/collage]"),
'text-collage-bb'
);
$this->assertEquals(
"id}\">{$collage->name()}",
\Text::full_format($collage->publicLocation()),
'text-collage-url'
);
$commentMan = new Manager\Comment();
$comment = $commentMan->create($this->userList['admin'], 'collages', $collage->id, "nice collage!");
$this->assertEquals(
"url()}\">Collages Comment #{$comment->id}",
\Text::full_format($comment->publicLocation()),
'text-collage-comment-link'
);
$this->assertEquals(1, $collage->hardRemove(), 'text-remove-collage');
}
public function testForum(): void {
$this->userList['admin'] = Helper::makeUser('forum.' . randomString(6), 'text');
$this->userList['admin']->setField('PermissionID', SYSOP)->modify();
\Text::setViewer($this->userList['admin']);
$name = 'forum ' . randomString(6);
$category = (new Manager\ForumCategory())->create($name, 10003);
$forum = Helper::makeForum(
user: $this->userList['admin'],
sequence: 999,
category: $category,
name: $name,
description: 'phpunit forum test',
);
$this->assertInstanceOf(Forum::class, $forum, 'text-create-forum');
$this->assertEquals(
"id}\" class=\"tooltip\" title=\"{$forum->name()}\">{$forum->name()}",
\Text::full_format("[forum]{$forum->id}[/forum]"),
'text-forum'
);
$thread = (new Manager\ForumThread())->create(
$forum, $this->userList['admin'], "phpunit thread title", "phpunit thread body"
);
$postId = (int)DB::DB()->scalar("
SELECT min(fp.ID)
FROM forums_posts fp
INNER JOIN forums_topics ft ON (ft.ID = fp.TopicID)
");
$post = (new Manager\ForumPost())->findById($postId);
$threadId = $post?->thread()->id;
$title = $post?->thread()->title();
$this->assertMatchesRegularExpression(
"@^\Q{$title}\E$@",
\Text::full_format("[thread]{$threadId}[/thread]"),
'text-forum-thread'
);
$this->assertMatchesRegularExpression(
"@^\Q{$title}\E \(Post #{$postId}\)$@",
\Text::full_format("[thread]{$threadId}:{$postId}[/thread]"),
'text-forum-post'
);
$this->assertMatchesRegularExpression(
"@^\Q{$title}\E$@",
\Text::full_format(SITE_URL . "/forums.php?action=viewthread&threadid={$threadId}"),
'text-forum-thread-link'
);
$this->assertMatchesRegularExpression(
"@^\Q{$title}\E \(Post #{$postId}\)$@",
\Text::full_format(SITE_URL . "/forums.php?action=viewthread&threadid={$threadId}&postid={$postId}#post{$postId}"),
'text-forum-post-link'
);
$this->assertEquals(2, $thread->remove(), 'text-remove-thread');
$this->assertEquals(1, $forum->remove(), 'text-remove-forum');
$category->remove();
}
public static function dataList(): array {
return [
['text-list-a', <<onetwo
END_HTML],
['text-list-b', <<
outro
END_HTML],
['text-list-c', <<one [*]two
END_HTML],
['text-list-d', <<one [#]two
END_HTML],
['text-list-e', <<onetwo [*]
END_HTML],
['text-list-f', <<two [*]
outro
END_HTML],
['text-list-g', <<alphabetadelta
END_HTML],
['text-list-g', <<alphabetadelta
END_HTML],
['text-list-h', "[*]", <<
END_HTML],
];
}
#[DataProvider('dataList')]
public function testList(string $name, string $bbcode, string $expected): void {
$this->assertEquals($expected, \Text::full_format($bbcode), $name);
}
public function testStrip(): void {
$url = 'https://www.example.com';
$this->assertEquals(
'https://www.example.com/a.png https://www.example.com/b.png https://www.example.com here',
\Text::strip_bbcode("[img]{$url}/a.png[/img] [img={$url}/b.png] [url]{$url}[/url] [url={$url}]here[/url]"),
'text-strip-bb'
);
}
public function testTOC(): void {
\Text::$TOC = true;
$html = \Text::full_format(<<-
- BIG
- Big
- Also Big
Smaller Small
END_HTML;
$this->assertEquals($expected, \Text::parse_toc(), 'text-toc-default');
$expected = << Smaller Small
END_HTML;
$this->assertEquals($expected, \Text::parse_toc(3, true), 'text-toc-3-true');
\Text::$TOC = false;
}
public static function dataSpan(): array {
return [
["span-hr", "a[hr]b", "a b"],
["span-img", "[img]https://example.com/a.jpg[/img]", ""],
["span-img-2", "[img=https://example.com/a.jpg]", ""],
["span-pre", "[pre]a\nb[/pre]", "a b"],
["span-quote", "[quote]abcd[/quote]", "abcd"],
["span-quote-in", "[quote=who]abcd[/quote]", "abcd"],
["span-raw-url", "https://example.com", "https://example.com"],
["span-url", "[url=https://example.com/]", "https://example.com/"],
["span-url-2", "x[url=https://example.com/a.jpg]y", "xy"],
];
}
#[DataProvider('dataSpan')]
public function testSpanText(string $name, string $bbcode, string $expected): void {
$this->assertEquals($expected, \Text::span_format($bbcode), $name);
}
public function testUser(): void {
$username = 'text.' . randomString(6);
$this->userList['user'] = Helper::makeUser($username, 'text');
\Text::setViewer($this->userList['user']);
$this->assertEquals("$username", \Text::full_format("[user]{$username}[/user]"), "text-user-1");
$url = "userList['user']->id}\">@$username";
$this->assertEquals($url, \Text::full_format("@$username"), "text-user-2");
$this->assertEquals("$url.", \Text::full_format("@$username."), "text-user-3");
$this->assertEquals(
"Mature content has been blocked. You can choose to view mature content by editing your settings.",
\Text::full_format("[mature]titties[/mature]"),
"text-mature",
);
}
}