xref: /webtrees/tests/app/Factories/MarkdownFactoryTest.php (revision 6fab17fdcb06dd3d7848b6a50977f6b02f634d8b)
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#[CoversClass(MarkdownFactory::class)]
28#[CoversClass(XrefExtension::class)]
29class MarkdownFactoryTest extends TestCase
30{
31    public function testAutoLinkWithoutTree(): void
32    {
33        $factory  = new MarkdownFactory();
34
35        static::assertSame(
36            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
37            $factory->autolink('FOO https://example.com BAR')
38        );
39    }
40
41    public function testAutoLinkWithTree(): void
42    {
43        $factory = new MarkdownFactory();
44        $tree    = $this->createMock(Tree::class);
45
46        static::assertSame(
47            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
48            $factory->autolink('FOO https://example.com BAR', $tree)
49        );
50    }
51
52    public function testAutoLinkWithHtml(): void
53    {
54        $factory  = new MarkdownFactory();
55
56        static::assertSame(
57            '<p>&lt;b&gt; <a href="https://example.com">https://example.com</a> &lt;/b&gt;</p>',
58            $factory->autolink('<b> https://example.com </b>')
59        );
60    }
61
62    public function testMarkdownWithoutTree(): void
63    {
64        $factory = new MarkdownFactory();
65
66        static::assertSame(
67            '<p>FOO https://example.com BAR</p>',
68            $factory->markdown('FOO https://example.com BAR')
69        );
70
71        static::assertSame(
72            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
73            $factory->markdown('FOO <https://example.com> BAR')
74        );
75    }
76
77    public function testMarkdownWithTree(): void
78    {
79        $tree    = $this->createMock(Tree::class);
80        $factory = new MarkdownFactory();
81
82        static::assertSame(
83            '<p>FOO https://example.com BAR</p>',
84            $factory->markdown('FOO https://example.com BAR', $tree)
85        );
86
87        static::assertSame(
88            '<p>FOO <a href="https://example.com">https://example.com</a> BAR</p>',
89            $factory->markdown('FOO <https://example.com> BAR', $tree)
90        );
91    }
92
93    public function testMarkdownWithHtml(): void
94    {
95        $factory = new MarkdownFactory();
96
97        static::assertSame(
98            '<p>&lt;b&gt; <a href="https://example.com">https://example.com</a> &lt;/b&gt;</p>',
99            $factory->markdown('<b> <https://example.com> </b>')
100        );
101    }
102
103    public function testSoftLineBreaks(): void
104    {
105        $factory = new MarkdownFactory();
106
107        static::assertSame(
108            '<p>alpha<br />beta<br />gamma<br />delta</p>',
109            $factory->autolink("alpha\nbeta\ngamma  \ndelta")
110        );
111
112        static::assertSame(
113            '<p>alpha<br />beta<br />gamma<br />delta</p>',
114            $factory->markdown("alpha\nbeta\ngamma  \ndelta")
115        );
116    }
117
118    public function testMultipleParagraphs(): void
119    {
120        $factory = new MarkdownFactory();
121
122        static::assertSame(
123            '<p>alpha<br />beta</p><p>gamma<br />delta</p>',
124            $factory->autolink("alpha\nbeta\n\n\n\ngamma\ndelta")
125        );
126
127        static::assertSame(
128            '<p>alpha<br />beta</p><p>gamma<br />delta</p>',
129            $factory->markdown("alpha\nbeta\n\n\n\ngamma\ndelta")
130        );
131    }
132}
133