assertEquals($expected, Text::full_format($bbcode), $name);
}
public 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]'],
];
}
// for phpunit v10: #[DataProvider('dataBB')]
/**
* @dataProvider dataBB
*/
public function testBB(string $name, string $bbcode, string $expected): void {
global $Viewer;
$Viewer = (new Gazelle\Manager\User)->find('@user');
Text::setViewer($Viewer);
$this->assertEquals($expected, Text::full_format($bbcode), $name);
}
public static function dataBB(): array {
$url = 'https://www.example.com/';
$user = (new Gazelle\Manager\User)->find('@user');
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-mature", "[mature]titties[/mature]",
"Mature content has been blocked. You can choose to view mature content by editing your id()}\">settings."
],
["text-spoiler", "[spoiler]surprise[/spoiler]",
"Hidden text: Showsurprise
"
],
["text-user-1", "[user]user[/user]", "user"],
["text-user-2", "@user", "id()}\">@user"],
["text-user-3", "@user.", "id()}\">@user."],
["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 {
$image = 'https://www.example.com/a.jpg';
$withCache = '@^
$@';
$noCache = "
";
$this->assertEquals($noCache, Text::full_format("[img=$image]"), 'text-image1-cache-implicit');
// $this->assertEquals($noCache, Text::full_format("[img=$image]", cache: false), 'text-image1-cache-false');
$this->assertEquals($noCache, Text::full_format("[img]{$image}[/img]"), 'text-image2-cache-implicit');
// $this->assertEquals($noCache, Text::full_format("[img]{$image}[/img]", cache: false), 'text-image2-cache-false');
// $this->assertMatchesRegularExpression($withCache, Text::full_format("[img=$image]", cache: true), 'text-image1-cache-true');
// $this->assertMatchesRegularExpression($withCache, Text::full_format("[img]{$image}[/img]", cache: true), 'text-image2-cache-true');
}
public function testCollage(): void {
$admin = (new Gazelle\Manager\User)->find('@admin');
$name = 'collage ' . randomString(6);
$collage = (new Gazelle\Manager\Collage)->create($admin, 1, $name, 'phpunit collage', 'jazz,disco', new Gazelle\Log);
$this->assertInstanceOf(Gazelle\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 Gazelle\Manager\Comment;
$comment = $commentMan->create($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->remove(), 'text-remove-collage');
}
public function testForum(): void {
$user = (new Gazelle\Manager\User)->find('@admin');
Text::setViewer($user);
$name = 'forum ' . randomString(6);
$forum = (new Gazelle\Manager\Forum)->create(
user: $user,
sequence: 999,
categoryId: 1,
name: $name,
description: 'phpunit forum test',
minClassRead: 100,
minClassWrite: 100,
minClassCreate: 100,
autoLock: false,
autoLockWeeks: 52,
);
$this->assertInstanceOf(Gazelle\Forum::class, $forum, 'text-create-forum');
$this->assertEquals(
"id()}\">{$forum->name()}",
Text::full_format("[forum]{$forum->id()}[/forum]"),
'text-forum'
);
$thread = (new Gazelle\Manager\ForumThread)->create(
$forum, $user->id(), "phpunit thread title", "phpunit thread body"
);
$postId = (int)Gazelle\DB::DB()->scalar("
SELECT min(fp.ID)
FROM forums_posts fp
INNER JOIN forums_topics ft ON (ft.ID = fp.TopicID)
");
$post = (new Gazelle\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(1, $forum->remove(), 'text-remove-forum');
}
public 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],
];
}
// for phpunit v10: #[DataProvider('dataList')]
/**
* @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 function testTorrent(): void {
$id = (int)Gazelle\DB::DB()->scalar("
SELECT min(t.ID)
FROM torrents t
INNER JOIN torrents_group tg ON (tg.ID = t.GroupID)
INNER JOIN category c ON (c.category_id = tg.CategoryID)
WHERE c.name = ?
", 'Music'
);
$torrent = (new Gazelle\Manager\Torrent)->findById($id);
$torrentId = $torrent->id();
$tgroupId = $torrent->group()->id();
$torrentRegexp = "^.* – .* \[\d+ .*?\]";
$this->assertMatchesRegularExpression("@{$torrentRegexp} .*$@",
Text::full_format("[pl]{$torrentId}[/pl]"),
'text-pl'
);
// FIXME: we generate torrent urls in two different ways
$torrentRegexp = "^.* – .* \[\d+ .*?\]";
$this->assertMatchesRegularExpression("@{$torrentRegexp}@",
Text::full_format(SITE_URL . "/{$torrent->location()}"),
"text-torrent-url tg={$tgroupId} t={$torrentId}"
);
$tgroupRegexp = ".*? – .*? \[\d+ \S+\]";
$this->assertMatchesRegularExpression("@{$tgroupRegexp}@",
Text::full_format("[torrent]{$tgroupId}[/torrent]"),
'text-tgroup-id'
);
$this->assertMatchesRegularExpression("@{$tgroupRegexp}@",
Text::full_format(SITE_URL . "/{$torrent->group()->location()}"),
'text-tgroup-url'
);
}
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"],
];
}
// for phpunit v10: #[DataProvider('dataSpan')]
/**
* @dataProvider dataSpan
*/
public function testSpanText(string $name, string $bbcode, string $expected): void {
$this->assertEquals($expected, Text::span_format($bbcode), $name);
}
}