1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Libraries\Markdown;
7
8use App\Libraries\Markdown\OsuMarkdown;
9use Tests\TestCase;
10
11class ProcessorTest extends TestCase
12{
13 /**
14 * @dataProvider htmlExamples
15 */
16 public function testHtml($name, $path)
17 {
18 [$osuMarkdown, $expectedOutput] = $this->loadOutputTest($name, $path, 'html');
19
20 $this->assertSame(
21 $this->normalizeHTML("<div class='osu-md'>{$expectedOutput}</div>"),
22 $this->normalizeHTML($osuMarkdown->html()),
23 );
24 }
25
26 /**
27 * @dataProvider indexableExamples
28 */
29 public function testIndexable($name, $path)
30 {
31 [$osuMarkdown, $expectedOutput] = $this->loadOutputTest($name, $path, 'txt');
32
33 $this->assertSame($expectedOutput, $osuMarkdown->toIndexable());
34 }
35
36 public function testTocId()
37 {
38 $parser = new OsuMarkdown('default', osuExtensionConfig: ['attributes_allowed' => ['id'], 'generate_toc' => true]);
39
40 $parsed = $parser->load('## some header {#headerid}')->toArray();
41
42 $this->assertTrue(isset($parsed['toc']['headerid']));
43 }
44
45 public function testTocImage()
46 {
47 $parser = new OsuMarkdown('default', osuExtensionConfig: ['generate_toc' => true]);
48
49 $parsed = $parser->load('##  some header')->toArray();
50
51 $this->assertTrue(isset($parsed['toc']['some-header']));
52 $this->assertSame('some header', $parsed['toc']['some-header']['title']);
53 }
54
55 public static function htmlExamples()
56 {
57 return static::fileList(__DIR__.'/html_markdown_examples', '.md');
58 }
59
60 public static function indexableExamples()
61 {
62 return static::fileList(__DIR__.'/indexable_markdown_examples', '.md');
63 }
64
65 private function loadOutputTest(string $name, string $path, string $extension)
66 {
67 $mdFilePath = "{$path}/{$name}.md";
68 $textFilePath = "{$path}/{$name}.{$extension}";
69
70 return [
71 (new OsuMarkdown(
72 'default',
73 osuExtensionConfig: [
74 'attributes_allowed' => ['flag', 'id'],
75 'custom_container_inline' => true,
76 'style_block_allowed_classes' => ['class-name'],
77 ],
78 osuMarkdownConfig: [
79 'enable_footnote' => true,
80 ],
81 ))->load(file_get_contents($mdFilePath)),
82 file_get_contents($textFilePath),
83 ];
84 }
85}