xref: /webtrees/tests/app/Factories/MarkdownFactoryTest.php (revision 62ff2f188c699b1144fb2ca2d6da1358d5e1a745)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use Fisharebest\Webtrees\CommonMark\XrefExtension;
23use Fisharebest\Webtrees\TestCase;
24use Fisharebest\Webtrees\Tree;
25use PHPUnit\Framework\Attributes\CoversClass;
26
27
28#[CoversClass(MarkdownFactory::class)]
29#[CoversClass(XrefExtension::class)]
30class MarkdownFactoryTest extends TestCase
31{
32    public function testAutoLinkWithoutTree(): void
33    {
34        $factory  = new MarkdownFactory();
35
36        static::assertSame(
37            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
38            $factory->autolink('FOO https://example.com BAR')
39        );
40    }
41
42    public function testAutoLinkWithTree(): void
43    {
44        $factory = new MarkdownFactory();
45        $tree    = $this->createMock(Tree::class);
46
47        static::assertSame(
48            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
49            $factory->autolink('FOO https://example.com BAR', $tree)
50        );
51    }
52
53    public function testAutoLinkWithHtml(): void
54    {
55        $factory  = new MarkdownFactory();
56
57        static::assertSame(
58            '<p>&lt;b&gt; <a href="https://example.com">https://example.com</a> &lt;/b&gt;</p>',
59            $factory->autolink('<b> https://example.com </b>')
60        );
61    }
62
63    public function testMarkdownWithoutTree(): void
64    {
65        $factory = new MarkdownFactory();
66
67        static::assertSame(
68            '<p>FOO https://example.com BAR</p>',
69            $factory->markdown('FOO https://example.com BAR')
70        );
71
72        static::assertSame(
73            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
74            $factory->markdown('FOO <https://example.com> BAR')
75        );
76    }
77
78    public function testMarkdownWithTree(): void
79    {
80        $tree    = $this->createMock(Tree::class);
81        $factory = new MarkdownFactory();
82
83        static::assertSame(
84            '<p>FOO https://example.com BAR</p>',
85            $factory->markdown('FOO https://example.com BAR', $tree)
86        );
87
88        static::assertSame(
89            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
90            $factory->markdown('FOO <https://example.com> BAR', $tree)
91        );
92    }
93
94    public function testMarkdownWithHtml(): void
95    {
96        $factory = new MarkdownFactory();
97
98        static::assertSame(
99            '<p>&lt;b&gt; <a href="https://example.com">https://example.com</a> &lt;/b&gt;</p>',
100            $factory->markdown('<b> <https://example.com> </b>')
101        );
102    }
103
104    public function testSoftLineBreaks(): void
105    {
106        $factory = new MarkdownFactory();
107
108        static::assertSame(
109            '<p>alpha<br />beta<br />gamma<br />delta</p>',
110            $factory->autolink("alpha\nbeta\ngamma  \ndelta")
111        );
112
113        static::assertSame(
114            '<p>alpha<br />beta<br />gamma<br />delta</p>',
115            $factory->markdown("alpha\nbeta\ngamma  \ndelta")
116        );
117    }
118
119    public function testMultipleParagraphs(): void
120    {
121        $factory = new MarkdownFactory();
122
123        static::assertSame(
124            '<p>alpha<br />beta</p><p>gamma<br />delta</p>',
125            $factory->autolink("alpha\nbeta\n\n\n\ngamma\ndelta")
126        );
127
128        static::assertSame(
129            '<p>alpha<br />beta</p><p>gamma<br />delta</p>',
130            $factory->markdown("alpha\nbeta\n\n\n\ngamma\ndelta")
131        );
132    }
133}
134