Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
9.38% covered (danger)
9.38%
15 / 160
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functions_make_clickable_test
9.38% covered (danger)
9.38%
15 / 160
50.00% covered (danger)
50.00%
4 / 8
55.63
0.00% covered (danger)
0.00%
0 / 1
 data_test_make_clickable_url_positive
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
2
 data_test_make_clickable_url_idn
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
2
 data_test_make_clickable_local_url_idn
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 data_test_make_clickable_custom_classes
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
2
 setUp
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 test_urls_matching_positive
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_local_urls_matching_idn
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_make_clickable_custom_classes
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14class phpbb_functions_make_clickable_test extends phpbb_test_case
15{
16    /**
17    * Tags:
18    * 'm' - full URL like xxxx://aaaaa.bbb.cccc.
19    * 'l' - local relative board URL like http://domain.tld/path/to/board/index.php
20    * 'w' - URL without http/https protocol like www.xxxx.yyyy[/zzzz] aka 'lazy' URLs
21    * 'e' - email@domain type address
22    *
23    * Classes:
24    * "postlink-local" for 'l' URLs
25    * "postlink" for the rest of URLs
26    * empty for email addresses
27    **/
28    public static function data_test_make_clickable_url_positive()
29    {
30        return [
31            [
32                'http://www.phpbb.com/community/',
33                '<!-- m --><a class="postlink" href="http://www.phpbb.com/community/">http://www.phpbb.com/community/</a><!-- m -->'
34            ],
35            [
36                'http://www.phpbb.com/path/file.ext#section',
37                '<!-- m --><a class="postlink" href="http://www.phpbb.com/path/file.ext#section">http://www.phpbb.com/path/file.ext#section</a><!-- m -->'
38            ],
39            [
40                'ftp://ftp.phpbb.com/',
41                '<!-- m --><a class="postlink" href="ftp://ftp.phpbb.com/">ftp://ftp.phpbb.com/</a><!-- m -->'
42            ],
43            [
44                'sip://bantu@phpbb.com',
45                '<!-- m --><a class="postlink" href="sip://bantu@phpbb.com">sip://bantu@phpbb.com</a><!-- m -->'
46            ],
47            [
48                'www.phpbb.com/community/',
49                '<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->'
50            ],
51            [
52                'http://testhost/viewtopic.php?t=1',
53                '<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
54            ],
55            [
56                'javascript://testhost/viewtopic.php?t=1',
57                'javascript://testhost/viewtopic.php?t=1'
58            ],
59            [
60                "java\nscri\npt://testhost/viewtopic.php?t=1",
61                "java\nscri\n<!-- m --><a class=\"postlink\" href=\"pt://testhost/viewtopic.php?t=1\">pt://testhost/viewtopic.php?t=1</a><!-- m -->"
62            ],
63            [
64                'email@domain.com',
65                '<!-- e --><a href="mailto:email@domain.com">email@domain.com</a><!-- e -->'
66            ],
67            // Test appending punctuation mark to the URL
68            [
69                'http://testhost/viewtopic.php?t=1!',
70                '<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!'
71            ],
72            [
73                'www.phpbb.com/community/?',
74                '<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->?'
75            ],
76            // Test shortened text for URL > 55 characters long
77            // URL text should be turned into: first 39 chars + ' ... ' + last 10 chars
78            [
79                'http://www.phpbb.com/community/path/to/long/url/file.ext#section',
80                '<!-- m --><a class="postlink" href="http://www.phpbb.com/community/path/to/long/url/file.ext#section">http://www.phpbb.com/community/path/to/ ... xt#section</a><!-- m -->'
81            ],
82        ];
83    }
84
85    public static function data_test_make_clickable_url_idn()
86    {
87        return [
88            [
89                'http://www.täst.de/community/',
90                '<!-- m --><a class="postlink" href="http://www.täst.de/community/">http://www.täst.de/community/</a><!-- m -->'
91            ],
92            [
93                'http://www.täst.de/path/file.ext#section',
94                '<!-- m --><a class="postlink" href="http://www.täst.de/path/file.ext#section">http://www.täst.de/path/file.ext#section</a><!-- m -->'
95            ],
96            [
97                'ftp://ftp.täst.de/',
98                '<!-- m --><a class="postlink" href="ftp://ftp.täst.de/">ftp://ftp.täst.de/</a><!-- m -->'
99            ],
100            [
101                'javascript://täst.de/',
102                'javascript://täst.de/'
103            ],
104            [
105                'sip://bantu@täst.de',
106                '<!-- m --><a class="postlink" href="sip://bantu@täst.de">sip://bantu@täst.de</a><!-- m -->'
107            ],
108            [
109                'www.täst.de/community/',
110                '<!-- w --><a class="postlink" href="http://www.täst.de/community/">www.täst.de/community/</a><!-- w -->'
111            ],
112            // Test appending punctuation mark to the URL
113            [
114                'http://домен.рф/viewtopic.php?t=1!',
115                '<!-- m --><a class="postlink" href="http://домен.рф/viewtopic.php?t=1">http://домен.рф/viewtopic.php?t=1</a><!-- m -->!'
116            ],
117            [
118                'www.домен.рф/сообщество/?',
119                '<!-- w --><a class="postlink" href="http://www.домен.рф/сообщество/">www.домен.рф/сообщество/</a><!-- w -->?'
120            ],
121            // Test shortened text for URL > 55 characters long
122            // URL text should be turned into: first 39 chars + ' ... ' + last 10 chars
123            [
124                'http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section',
125                '<!-- m --><a class="postlink" href="http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section">http://www.домен.рф/сообщество/путь/по/ ... xt#section</a><!-- m -->'
126            ],
127
128            // IDN with invalid characters shouldn't be parsed correctly (only 'valid' part)
129            [
130                'http://www.täst╫.de',
131                '<!-- m --><a class="postlink" href="http://www.täst">http://www.täst</a><!-- m -->╫.de'
132            ],
133            // IDN in emails is unsupported yet
134            ['почта@домен.рф', 'почта@домен.рф'],
135        ];
136    }
137
138    public static function data_test_make_clickable_local_url_idn()
139    {
140        return [
141            [
142                'http://www.домен.рф/viewtopic.php?t=1',
143                '<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
144            ],
145            // Test appending punctuation mark to the URL
146            [
147                'http://www.домен.рф/viewtopic.php?t=1!',
148                '<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!'
149            ],
150            [
151                'http://www.домен.рф/сообщество/?',
152                '<!-- l --><a class="postlink-local" href="http://www.домен.рф/сообщество/">сообщество/</a><!-- l -->?'
153            ],
154        ];
155    }
156
157    public static function data_test_make_clickable_custom_classes()
158    {
159        return [
160            [
161                'http://www.домен.рф/viewtopic.php?t=1',
162                'http://www.домен.рф',
163                'class1',
164                '<!-- l --><a class="class1-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
165            ],
166            [
167                'http://www.домен.рф/viewtopic.php?t=1!',
168                false,
169                'class2',
170                '<!-- m --><a class="class2" href="http://www.домен.рф/viewtopic.php?t=1">http://www.домен.рф/viewtopic.php?t=1</a><!-- m -->!'
171            ],
172            [
173                'http://www.домен.рф/сообщество/?',
174                false,
175                'class3',
176                '<!-- m --><a class="class3" href="http://www.домен.рф/сообщество/">http://www.домен.рф/сообщество/</a><!-- m -->?'
177            ],
178            [
179                'www.phpbb.com/community/',
180                false,
181                'class2',
182                '<!-- w --><a class="class2" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->'
183            ],
184            [
185                'http://testhost/viewtopic.php?t=1',
186                false,
187                'class1',
188                '<!-- l --><a class="class1-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
189            ],
190            [
191                'email@domain.com',
192                false,
193                'class-email',
194                '<!-- e --><a href="mailto:email@domain.com">email@domain.com</a><!-- e -->'
195            ],
196        ];
197    }
198
199    protected function setUp(): void
200    {
201        parent::setUp();
202
203        global $user, $request, $symfony_request, $config;
204        $config = new \phpbb\config\config([
205            'force_server_vars' => 0,
206            'server_name' => 'testhost',
207        ]);
208        $user = new phpbb_mock_user();
209        $request = new phpbb_mock_request();
210        $symfony_request = new \phpbb\symfony_request($request);
211    }
212
213    /**
214     * @dataProvider data_test_make_clickable_url_positive
215     * @dataProvider data_test_make_clickable_url_idn
216     */
217    public function test_urls_matching_positive($url, $expected)
218    {
219        global $user, $request, $symfony_request, $config;
220        $this->assertSame($expected, make_clickable($url));
221    }
222
223    /**
224     * @dataProvider data_test_make_clickable_local_url_idn
225     */
226    public function test_local_urls_matching_idn($url, $expected)
227    {
228        global $user, $request, $symfony_request, $config;
229        $this->assertSame($expected, make_clickable($url, "http://www.домен.рф"));
230    }
231
232    /**
233     * @dataProvider data_test_make_clickable_custom_classes
234     */
235    public function test_make_clickable_custom_classes($url, $server_url, $class, $expected)
236    {
237        global $user, $request, $symfony_request, $config;
238        $this->assertSame($expected, make_clickable($url, $server_url, $class));
239    }
240}