UrlSoftReferenceParserTest.php 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

declare(strict_types=1);

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
namespace TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference;
19
20
21
22
23
24
25
26

class UrlSoftReferenceParserTest extends AbstractSoftReferenceParserTest
{
    public function urlSoftReferenceParserTestDataProvider(): array
    {
        return [
            'Simple url matches' => [
                'https://foo-bar.baz',
27
28
29
30
                'content' => 'https://foo-bar.baz',
                'elements' => [
                    2 => [
                        'matchString' => 'https://foo-bar.baz',
31
32
                    ],
                ],
33
34
35
            ],
            'Valid characters by RFC 3986 match' => [
                'http://ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&\'()*+,;=.foo',
36
37
38
39
                'content' => 'http://ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&\'()*+,;=.foo',
                'elements' => [
                    2 => [
                        'matchString' => 'http://ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&\'()*+,;=.foo',
40
41
                    ],
                ],
42
            ],
43
44
            'URLs in content match' => [
                'Lorem ipsum https://foo-bar.baz dolor sit',
45
46
47
48
                'content' => 'Lorem ipsum https://foo-bar.baz dolor sit',
                'elements' => [
                    2 => [
                        'matchString' => 'https://foo-bar.baz',
49
50
                    ],
                ],
51
52
53
            ],
            'FTP URLs match' => [
                'ftp://foo-bar.baz',
54
55
56
57
                'content' => 'ftp://foo-bar.baz',
                'elements' => [
                    2 => [
                        'matchString' => 'ftp://foo-bar.baz',
58
59
                    ],
                ],
60
61
62
            ],
            'Full URLs match' => [
                'https://foo-bar.baz?foo=bar&baz=fizz#anchor',
63
64
65
66
                'content' => 'https://foo-bar.baz?foo=bar&baz=fizz#anchor',
                'elements' => [
                    2 => [
                        'matchString' => 'https://foo-bar.baz?foo=bar&baz=fizz#anchor',
67
68
                    ],
                ],
69
70
71
            ],
            'URL encoded URLs match' => [
                'https://foo-bar.baz?foo%3Dbar%26baz%3Dfi%20zz%23anchor',
72
73
74
75
                'content' => 'https://foo-bar.baz?foo%3Dbar%26baz%3Dfi%20zz%23anchor',
                'elements' => [
                    2 => [
                        'matchString' => 'https://foo-bar.baz?foo%3Dbar%26baz%3Dfi%20zz%23anchor',
76
77
                    ],
                ],
78
            ],
79
80
            'No space character after the last URL matches' => [
                '<p>Lorem Ipsum<br> https://foo.bar.baz/abc/def/ghi/.</p>',
81
82
83
84
                'content' => '<p>Lorem Ipsum<br> https://foo.bar.baz/abc/def/ghi/.</p>',
                'elements' => [
                    2 => [
                        'matchString' => 'https://foo.bar.baz/abc/def/ghi/.',
85
86
87
                    ],
                ],
            ],
88
89
90
            // The two cases below are handled by typolink_tag
            'URLs in anchor tag attributes do NOT match' => [
                '<a href="https://foo-bar.baz">some link</a>',
91
92
                'content' => '',
                'elements' => [],
93
94
95
            ],
            'URLs in link tag attributes do NOT match' => [
                '<link href="https://foo-bar.baz/style.css" rel="stylesheet">',
96
97
                'content' => '',
                'elements' => [],
98
            ],
99
100
101
102
103
104
105
        ];
    }

    /**
     * @test
     * @dataProvider urlSoftReferenceParserTestDataProvider
     */
106
    public function urlSoftReferenceParserTest(string $content, string $expectedContent, array $expectedElements): void
107
108
    {
        $subject = $this->getParserByKey('url');
109
110
111
        $result = $subject->parse('pages', 'url', 1, $content);
        self::assertSame($expectedContent, $result->getContent());
        self::assertSame($expectedElements, $result->getMatchedElements());
112
113
114
115
116
117
118
119
120
121
    }

    /**
     * @test
     */
    public function urlSoftReferenceParserSubstituteTest(): void
    {
        $content = 'My website is: https://www.foo-bar.baz';
        $subject = $this->getParserByKey('url');
        $subject->setParserKey('url', ['subst']);
122
123
124
125
126
        $result = $subject->parse('pages', 'url', 1, $content);
        $matchedElements = $result->getMatchedElements();
        self::assertArrayHasKey('subst', $matchedElements[2]);
        self::assertArrayHasKey('tokenID', $matchedElements[2]['subst']);
        unset($matchedElements[2]['subst']['tokenID']);
127
128
129
130
131
132

        $expected = [
            2 => [
                'matchString' => 'https://www.foo-bar.baz',
                'subst' => [
                    'type' => 'string',
133
134
135
                    'tokenValue' => 'https://www.foo-bar.baz',
                ],
            ],
136
        ];
137
        self::assertSame($expected, $matchedElements);
138
139
    }
}