Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.33% covered (danger)
8.33%
14 / 168
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functions_make_clickable_email_test
8.33% covered (danger)
8.33%
14 / 168
60.00% covered (warning)
60.00%
3 / 5
33.73
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 data_test_make_clickable_email_positive
0.00% covered (danger)
0.00%
0 / 114
0.00% covered (danger)
0.00%
0 / 1
2
 data_test_make_clickable_email_negative
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
2
 test_email_matching_positive
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_email_matching_negative
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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_email_test extends phpbb_test_case
15{
16    protected function setUp(): void
17    {
18        parent::setUp();
19
20        global $user, $request, $symfony_request, $config;
21        $config = new \phpbb\config\config([
22            'force_server_vars' => 0,
23            'server_name' => 'testhost',
24        ]);
25        $user = new phpbb_mock_user();
26        $request = new phpbb_mock_request();
27        $symfony_request = new \phpbb\symfony_request($request);
28    }
29
30    /**
31    * 'e' tag for email addresses html
32    **/
33    public static function data_test_make_clickable_email_positive()
34    {
35        return array(
36            array(
37                'nobody@phpbb.com',
38                '<!-- e --><a href="mailto:nobody@phpbb.com">nobody@phpbb.com</a><!-- e -->'
39            ),
40            array(
41                'Nobody@sub.phpbb.com',
42                '<!-- e --><a href="mailto:Nobody@sub.phpbb.com">Nobody@sub.phpbb.com</a><!-- e -->'
43            ),
44            array(
45                'alice.bob@foo.phpbb.com',
46                '<!-- e --><a href="mailto:alice.bob@foo.phpbb.com">alice.bob@foo.phpbb.com</a><!-- e -->'
47            ),
48            array(
49                'alice-foo@bar.phpbb.com',
50                '<!-- e --><a href="mailto:alice-foo@bar.phpbb.com">alice-foo@bar.phpbb.com</a><!-- e -->'
51            ),
52            array(
53                'alice_foo@bar.phpbb.com',
54                '<!-- e --><a href="mailto:alice_foo@bar.phpbb.com">alice_foo@bar.phpbb.com</a><!-- e -->'
55            ),
56            array(
57                'alice+tag@foo.phpbb.com',
58                '<!-- e --><a href="mailto:alice+tag@foo.phpbb.com">alice+tag@foo.phpbb.com</a><!-- e -->'
59            ),
60            array(
61                'alice&amp;tag@foo.phpbb.com',
62                '<!-- e --><a href="mailto:alice&amp;tag@foo.phpbb.com">alice&amp;tag@foo.phpbb.com</a><!-- e -->'
63            ),
64            array(
65                'alice@phpbb.australia',
66                '<!-- e --><a href="mailto:alice@phpbb.australia">alice@phpbb.australia</a><!-- e -->'
67            ),
68
69            // Test shortened text for email > 55 characters long
70            // Email text should be turned into: first 39 chars + ' ... ' + last 10 chars
71            array(
72                'alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong',
73                '<!-- e --><a href="mailto:alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong">alice@phpbb.topZlevelZdomainZnamesZcanZ ... ctersZlong</a><!-- e -->'
74            ),
75            array(
76                'l3tt3rsAndNumb3rs@domain.com',
77                '<!-- e --><a href="mailto:l3tt3rsAndNumb3rs@domain.com">l3tt3rsAndNumb3rs@domain.com</a><!-- e -->'
78            ),
79            array(
80                'has-dash@domain.com',
81                '<!-- e --><a href="mailto:has-dash@domain.com">has-dash@domain.com</a><!-- e -->'
82            ),
83            array(
84                'hasApostrophe.o\'leary@domain.org',
85                '<!-- e --><a href="mailto:hasApostrophe.o\'leary@domain.org">hasApostrophe.o\'leary@domain.org</a><!-- e -->'
86            ),
87            array(
88                'uncommonTLD@domain.museum',
89                '<!-- e --><a href="mailto:uncommonTLD@domain.museum">uncommonTLD@domain.museum</a><!-- e -->'
90            ),
91            array(
92                'uncommonTLD@domain.travel',
93                '<!-- e --><a href="mailto:uncommonTLD@domain.travel">uncommonTLD@domain.travel</a><!-- e -->'
94            ),
95            array(
96                'uncommonTLD@domain.mobi',
97                '<!-- e --><a href="mailto:uncommonTLD@domain.mobi">uncommonTLD@domain.mobi</a><!-- e -->'
98            ),
99            array(
100                'countryCodeTLD@domain.uk',
101                '<!-- e --><a href="mailto:countryCodeTLD@domain.uk">countryCodeTLD@domain.uk</a><!-- e -->'
102            ),
103            array(
104                'countryCodeTLD@domain.rw',
105                '<!-- e --><a href="mailto:countryCodeTLD@domain.rw">countryCodeTLD@domain.rw</a><!-- e -->'
106            ),
107            array(
108                'numbersInDomain@911.com',
109                '<!-- e --><a href="mailto:numbersInDomain@911.com">numbersInDomain@911.com</a><!-- e -->'
110            ),
111            array(
112                'underscore_inLocal@domain.net',
113                '<!-- e --><a href="mailto:underscore_inLocal@domain.net">underscore_inLocal@domain.net</a><!-- e -->'
114            ),
115            array(
116                'IPInsteadOfDomain@127.0.0.1',
117                '<!-- e --><a href="mailto:IPInsteadOfDomain@127.0.0.1">IPInsteadOfDomain@127.0.0.1</a><!-- e -->'
118            ),
119            array(
120                'IPAndPort@127.0.0.1:25',
121                '<!-- e --><a href="mailto:IPAndPort@127.0.0.1:25">IPAndPort@127.0.0.1:25</a><!-- e -->'
122            ),
123            array(
124                'subdomain@sub.domain.com',
125                '<!-- e --><a href="mailto:subdomain@sub.domain.com">subdomain@sub.domain.com</a><!-- e -->'
126            ),
127            array(
128                'local@dash-inDomain.com',
129                '<!-- e --><a href="mailto:local@dash-inDomain.com">local@dash-inDomain.com</a><!-- e -->'
130            ),
131            array(
132                'dot.inLocal@foo.com',
133                '<!-- e --><a href="mailto:dot.inLocal@foo.com">dot.inLocal@foo.com</a><!-- e -->'
134            ),
135            array(
136                'a@singleLetterLocal.org',
137                '<!-- e --><a href="mailto:a@singleLetterLocal.org">a@singleLetterLocal.org</a><!-- e -->'
138            ),
139            array(
140                'singleLetterDomain@x.org',
141                '<!-- e --><a href="mailto:singleLetterDomain@x.org">singleLetterDomain@x.org</a><!-- e -->'
142            ),
143            array(
144                '&amp;*=?^+{}\'~@validCharsInLocal.net',
145                '<!-- e --><a href="mailto:&amp;*=?^+{}\'~@validCharsInLocal.net">&amp;*=?^+{}\'~@validCharsInLocal.net</a><!-- e -->'
146            ),
147            array(
148                'foor@bar.newTLD',
149                '<!-- e --><a href="mailto:foor@bar.newTLD">foor@bar.newTLD</a><!-- e -->'
150            ),
151        );
152    }
153
154    public static function data_test_make_clickable_email_negative()
155    {
156        return array(
157            array('foo.example.com'),            // @ is missing
158            array('.foo.example.com'),            // . as first character
159            array('Foo.@example.com'),            // . is last in local part
160            array('foo..123@example.com'),        // . doubled
161            array('a@b@c@example.com'),            // @ doubled
162
163            // Emails with invalid characters
164            // (only 'valid' pieces having localparts prepended with one of the \n \t ( > chars should parsed if any)
165            array('()[]\;:,<>@example.com'),    // invalid characters
166            array('abc(def@example.com', 'abc(<!-- e --><a href="mailto:def@example.com">def@example.com</a><!-- e -->'),        // invalid character (
167            array('abc)def@example.com'),        // invalid character )
168            array('abc[def@example.com'),        // invalid character [
169            array('abc]def@example.com'),        // invalid character ]
170            array('abc\def@example.com'),        // invalid character \
171            array('abc;def@example.com'),        // invalid character ;
172            array('abc:def@example.com'),        // invalid character :
173            array('abc,def@example.com'),        // invalid character ,
174            array('abc<def@example.com'),        // invalid character <
175            array('abc>def@example.com', 'abc><!-- e --><a href="mailto:def@example.com">def@example.com</a><!-- e -->'),        // invalid character >
176
177            // http://fightingforalostcause.net/misc/2006/compare-email-regex.php
178            array('missingDomain@.com'),
179            array('@missingLocal.org'),
180            array('missingatSign.net'),
181            array('missingDot@com'),
182            array('two@@signs.com'),
183            // Trailing colon is ignored
184            array('colonButNoPort@127.0.0.1:', '<!-- e --><a href="mailto:colonButNoPort@127.0.0.1">colonButNoPort@127.0.0.1</a><!-- e -->:'),
185
186            array(''),
187            // Trailing part after the 3rd dot is ignored
188            array('someone-else@127.0.0.1.26', '<!-- e --><a href="mailto:someone-else@127.0.0.1">someone-else@127.0.0.1</a><!-- e -->.26'),
189
190            array('.localStartsWithDot@domain.com'),
191            array('localEndsWithDot.@domain.com'),
192            array('two..consecutiveDots@domain.com'),
193            array('domainStartsWithDash@-domain.com'),
194            array('domainEndsWithDash@domain-.com'),
195            array('numbersInTLD@domain.c0m'),
196            array('missingTLD@domain.'),
197            array('! "#$%(),/;<>[]`|@invalidCharsInLocal.org'),
198            array('invalidCharsInDomain@! "#$%(),/;<>_[]`|.org'),
199            array('local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org'),
200            // The domain zone name part after the 63rd char is ignored
201            array(
202                'alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlongZ',
203                '<!-- e --><a href="mailto:alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong">alice@phpbb.topZlevelZdomainZnamesZcanZ ... ctersZlong</a><!-- e -->Z'
204            ),
205        );
206    }
207
208    /**
209     * @dataProvider data_test_make_clickable_email_positive
210     */
211    public function test_email_matching_positive($email, $expected)
212    {
213        global $config, $user, $request, $symfony_request;
214        $this->assertSame($expected, make_clickable($email));
215    }
216
217    /**
218     * @dataProvider data_test_make_clickable_email_negative
219     */
220    public function test_email_matching_negative($email, $expected = null)
221    {
222        global $config, $user, $request, $symfony_request;
223        $expected = ($expected) ?: $email;
224        $this->assertSame($expected, make_clickable($email));
225    }
226}