aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functions/make_clickable_email_test.php
blob: 4c802d04879479eccaf403fccfd345f50dabd67d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';

class phpbb_functions_make_clickable_email_test extends phpbb_test_case
{
	protected function setUp()
	{
		parent::setUp();

		global $config, $user, $request;
		$user = new phpbb_mock_user();
		$request = new phpbb_mock_request();
	}

	/**
	* 'e' tag for email addresses html
	**/
	public function data_test_make_clickable_email_positive()
	{
		return array(
			array(
				'nobody@phpbb.com',
				'<!-- e --><a href="mailto:nobody@phpbb.com">nobody@phpbb.com</a><!-- e -->'
			),
			array(
				'Nobody@sub.phpbb.com',
				'<!-- e --><a href="mailto:Nobody@sub.phpbb.com">Nobody@sub.phpbb.com</a><!-- e -->'
			),
			array(
				'alice.bob@foo.phpbb.com',
				'<!-- e --><a href="mailto:alice.bob@foo.phpbb.com">alice.bob@foo.phpbb.com</a><!-- e -->'
			),
			array(
				'alice-foo@bar.phpbb.com',
				'<!-- e --><a href="mailto:alice-foo@bar.phpbb.com">alice-foo@bar.phpbb.com</a><!-- e -->'
			),
			array(
				'alice_foo@bar.phpbb.com',
				'<!-- e --><a href="mailto:alice_foo@bar.phpbb.com">alice_foo@bar.phpbb.com</a><!-- e -->'
			),
			array(
				'alice+tag@foo.phpbb.com',
				'<!-- e --><a href="mailto:alice+tag@foo.phpbb.com">alice+tag@foo.phpbb.com</a><!-- e -->'
			),
			array(
				'alice&amp;tag@foo.phpbb.com',
				'<!-- e --><a href="mailto:alice&amp;tag@foo.phpbb.com">alice&amp;tag@foo.phpbb.com</a><!-- e -->'
			),
			array(
				'alice@phpbb.australia',
				'<!-- e --><a href="mailto:alice@phpbb.australia">alice@phpbb.australia</a><!-- e -->'
			),

			// Test shortened text for email > 55 characters long
			// Email text should be turned into: first 39 chars + ' ... ' + last 10 chars
			array(
				'alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong',
				'<!-- e --><a href="mailto:alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong">alice@phpbb.topZlevelZdomainZnamesZcanZ ... ctersZlong</a><!-- e -->'
			),
			array(
				'l3tt3rsAndNumb3rs@domain.com',
				'<!-- e --><a href="mailto:l3tt3rsAndNumb3rs@domain.com">l3tt3rsAndNumb3rs@domain.com</a><!-- e -->'
			),
			array(
				'has-dash@domain.com',
				'<!-- e --><a href="mailto:has-dash@domain.com">has-dash@domain.com</a><!-- e -->'
			),
			array(
				'hasApostrophe.o\'leary@domain.org',
				'<!-- e --><a href="mailto:hasApostrophe.o\'leary@domain.org">hasApostrophe.o\'leary@domain.org</a><!-- e -->'
			),
			array(
				'uncommonTLD@domain.museum',
				'<!-- e --><a href="mailto:uncommonTLD@domain.museum">uncommonTLD@domain.museum</a><!-- e -->'
			),
			array(
				'uncommonTLD@domain.travel',
				'<!-- e --><a href="mailto:uncommonTLD@domain.travel">uncommonTLD@domain.travel</a><!-- e -->'
			),
			array(
				'uncommonTLD@domain.mobi',
				'<!-- e --><a href="mailto:uncommonTLD@domain.mobi">uncommonTLD@domain.mobi</a><!-- e -->'
			),
			array(
				'countryCodeTLD@domain.uk',
				'<!-- e --><a href="mailto:countryCodeTLD@domain.uk">countryCodeTLD@domain.uk</a><!-- e -->'
			),
			array(
				'countryCodeTLD@domain.rw',
				'<!-- e --><a href="mailto:countryCodeTLD@domain.rw">countryCodeTLD@domain.rw</a><!-- e -->'
			),
			array(
				'numbersInDomain@911.com',
				'<!-- e --><a href="mailto:numbersInDomain@911.com">numbersInDomain@911.com</a><!-- e -->'
			),
			array(
				'underscore_inLocal@domain.net',
				'<!-- e --><a href="mailto:underscore_inLocal@domain.net">underscore_inLocal@domain.net</a><!-- e -->'
			),
			array(
				'IPInsteadOfDomain@127.0.0.1',
				'<!-- e --><a href="mailto:IPInsteadOfDomain@127.0.0.1">IPInsteadOfDomain@127.0.0.1</a><!-- e -->'
			),
			array(
				'IPAndPort@127.0.0.1:25',
				'<!-- e --><a href="mailto:IPAndPort@127.0.0.1:25">IPAndPort@127.0.0.1:25</a><!-- e -->'
			),
			array(
				'subdomain@sub.domain.com',
				'<!-- e --><a href="mailto:subdomain@sub.domain.com">subdomain@sub.domain.com</a><!-- e -->'
			),
			array(
				'local@dash-inDomain.com',
				'<!-- e --><a href="mailto:local@dash-inDomain.com">local@dash-inDomain.com</a><!-- e -->'
			),
			array(
				'dot.inLocal@foo.com',
				'<!-- e --><a href="mailto:dot.inLocal@foo.com">dot.inLocal@foo.com</a><!-- e -->'
			),
			array(
				'a@singleLetterLocal.org',
				'<!-- e --><a href="mailto:a@singleLetterLocal.org">a@singleLetterLocal.org</a><!-- e -->'
			),
			array(
				'singleLetterDomain@x.org',
				'<!-- e --><a href="mailto:singleLetterDomain@x.org">singleLetterDomain@x.org</a><!-- e -->'
			),
			array(
				'&amp;*=?^+{}\'~@validCharsInLocal.net',
				'<!-- e --><a href="mailto:&amp;*=?^+{}\'~@validCharsInLocal.net">&amp;*=?^+{}\'~@validCharsInLocal.net</a><!-- e -->'
			),
			array(
				'foor@bar.newTLD',
				'<!-- e --><a href="mailto:foor@bar.newTLD">foor@bar.newTLD</a><!-- e -->'
			),
		);
	}

	public function data_test_make_clickable_email_negative()
	{
		return array(
			array('foo.example.com'),			// @ is missing
			array('.foo.example.com'),			// . as first character
			array('Foo.@example.com'),			// . is last in local part
			array('foo..123@example.com'),		// . doubled
			array('a@b@c@example.com'),			// @ doubled

			// Emails with invalid characters
			// (only 'valid' pieces having localparts prepended with one of the \n \t ( > chars should parsed if any)
			array('()[]\;:,<>@example.com'),	// invalid characters
			array('abc(def@example.com', 'abc(<!-- e --><a href="mailto:def@example.com">def@example.com</a><!-- e -->'),		// invalid character (
			array('abc)def@example.com'),		// invalid character )
			array('abc[def@example.com'),		// invalid character [
			array('abc]def@example.com'),		// invalid character ]
			array('abc\def@example.com'),		// invalid character \
			array('abc;def@example.com'),		// invalid character ;
			array('abc:def@example.com'),		// invalid character :
			array('abc,def@example.com'),		// invalid character ,
			array('abc<def@example.com'),		// invalid character <
			array('abc>def@example.com', 'abc><!-- e --><a href="mailto:def@example.com">def@example.com</a><!-- e -->'),		// invalid character >
			
			// http://fightingforalostcause.net/misc/2006/compare-email-regex.php
			array('missingDomain@.com'),
			array('@missingLocal.org'),
			array('missingatSign.net'),
			array('missingDot@com'),
			array('two@@signs.com'),
			// Trailing colon is ignored
			array('colonButNoPort@127.0.0.1:', '<!-- e --><a href="mailto:colonButNoPort@127.0.0.1">colonButNoPort@127.0.0.1</a><!-- e -->:'),

			array(''),
			// Trailing part after the 3rd dot is ignored
			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'),

			array('.localStartsWithDot@domain.com'),
			array('localEndsWithDot.@domain.com'),
			array('two..consecutiveDots@domain.com'),
			array('domainStartsWithDash@-domain.com'),
			array('domainEndsWithDash@domain-.com'),
			array('numbersInTLD@domain.c0m'),
			array('missingTLD@domain.'),
			array('! "#$%(),/;<>[]`|@invalidCharsInLocal.org'),
			array('invalidCharsInDomain@! "#$%(),/;<>_[]`|.org'),
			array('local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org'),
			// The domain zone name part after the 63rd char is ignored
			array(
				'alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlongZ',
				'<!-- e --><a href="mailto:alice@phpbb.topZlevelZdomainZnamesZcanZbeZupZtoZsixtyZthreeZcharactersZlong">alice@phpbb.topZlevelZdomainZnamesZcanZ ... ctersZlong</a><!-- e -->Z'
			),
		);
	}

	/**
	 * @dataProvider data_test_make_clickable_email_positive
	 */
	public function test_email_matching_positive($email, $expected)
	{
		$this->assertSame($expected, make_clickable($email));
	}

	/**
	 * @dataProvider data_test_make_clickable_email_negative
	 */
	public function test_email_matching_negative($email, $expected = null)
	{
		$expected = ($expected) ?: $email;
		$this->assertSame($expected, make_clickable($email));
	}
}