aboutsummaryrefslogtreecommitdiff
path: root/mod/thewire/tests/regex.php
blob: c73e06bdc4570ed69d8b58d9ee570f6b4153e84f (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
 * Regular expression tests for the wire
 */
class TheWireRegexTest extends ElggCoreUnitTest {

	/**
	 * Called before each test object.
	 */
	public function __construct() {
		$this->ia = elgg_set_ignore_access(TRUE);
		parent::__construct();

		// all __construct() code should come after here
	}

	/**
	 * Called before each test method.
	 */
	public function setUp() {

	}

	/**
	 * Called after each test method.
	 */
	public function tearDown() {
		// do not allow SimpleTest to interpret Elgg notices as exceptions
		$this->swallowErrors();
	}

	/**
	 * Called after each test object.
	 */
	public function __destruct() {
		elgg_set_ignore_access($this->ia);
		// all __destruct() code should go above here
		parent::__destruct();
	}

	/**
	 * Get the link for a user's wire page
	 *
	 * @param string $username Username
	 * @return string
	 */
	protected function getUserWireLink($username) {
		$url = "thewire/owner/$username";
		$url = elgg_normalize_url($url);
		return "<a href=\"$url\">@$username</a>";
	}

	/**
	 * Get the link for a hashtag page
	 *
	 * @param string $tag Tag string
	 * @return string
	 */
	protected function getHashtagLink($tag) {
		$url = "thewire/tag/$tag";
		$url = elgg_normalize_url($url);
		return "<a href=\"$url\">#$tag</a>";
	}

	/**
	 * Get a link for an email address mailto
	 *
	 * @param string $address Email address
	 * @return string
	 */
	protected function getEmailLink($address) {
		return "<a href=\"mailto:$address\">$address</a>";
	}

	/**
	 * Get the html for a link
	 *
	 * @param string $address URL
	 * @return string
	 */
	protected function getLink($address) {
		return parse_urls($address);
	}

	/**
	 * Usernames: @user
	 */
	public function testReplaceUsernames() {
		// beginning of text
		$text = "@user test";
		$expected = $this->getUserWireLink('user') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// after space
		$text = "test @user test";
		$expected = "test " . $this->getUserWireLink('user') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by comma
		$text = "test @user, test";
		$expected = "test " . $this->getUserWireLink('user') . ", test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);
		
		// preceded by comma
		$text = "test ,@user test";
		$expected = "test ," . $this->getUserWireLink('user') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// include digit
		$text = "@3user test";
		$expected = $this->getUserWireLink('3user') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// include underscore
		$text = "@user_name test";
		$expected = $this->getUserWireLink('user_name') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// parentheses
		$text = "test (@user) test";
		$expected = "test (" . $this->getUserWireLink('user') . ") test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// utf8 characters
		$text = "@tyúkanyó";
		$expected = $this->getUserWireLink('tyúkanyó');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);
	}

	/**
	 * Hashtags: #tag
	 */
	public function testReplaceHashtags() {
		// tag at beginning
		$text = "#tag test";
		$expected = $this->getHashtagLink('tag') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// tag not at beginning
		$text = "test #tag test";
		$expected = "test " . $this->getHashtagLink('tag') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by comma
		$text = "test #tag, test";
		$expected = "test " . $this->getHashtagLink('tag') . ", test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// preceded by comma
		$text = "test,#tag test";
		$expected = "test," . $this->getHashtagLink('tag') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by period
		$text = "test #tag. test";
		$expected = "test " . $this->getHashtagLink('tag') . ". test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// parentheses
		$text = "test (#tag) test";
		$expected = "test (" . $this->getHashtagLink('tag') . ") test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// include number
		$text = "test #tag2000 test";
		$expected = "test " . $this->getHashtagLink('tag2000') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// cannot be just a number
		$text = "test #1 test";
		$expected = $text;
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);
}

	/**
	 * Email: johndoe@gmail.com
	 */
	public function testReplaceEmailAddress() {
		// email at beginning of text
		$text = "test@test.com test";
		$expected = $this->getEmailLink('test@test.com') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// after space
		$text = "test test@test.com test";
		$expected = "test " . $this->getEmailLink('test@test.com') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by comma
		$text = "test test@test.com, test";
		$expected = "test " . $this->getEmailLink('test@test.com') . ", test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// preceded by comma
		$text = "test,test@test.com test";
		$expected = "test," . $this->getEmailLink('test@test.com') . " test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by period
		$text = "test test@test.com. test";
		$expected = "test " . $this->getEmailLink('test@test.com') . ". test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// parentheses
		$text = "test (test@test.com) test";
		$expected = "test (" . $this->getEmailLink('test@test.com') . ") test";
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// includes digits
		$text = "user1@domain1.com";
		$expected = $this->getEmailLink('user1@domain1.com');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// includes underscore
		$text = "user_name@domain.com";
		$expected = $this->getEmailLink('user_name@domain.com');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// includes period
		$text = "user.name@domain.com";
		$expected = $this->getEmailLink('user.name@domain.com');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// includes subdomains
		$text = "user.name@domain.com.uk";
		$expected = $this->getEmailLink('user.name@domain.com.uk');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);
	}

	/**
	 * Links: http://www.example.org/
	 */
	public function testReplaceLinks() {
		// beginning of text
		$text = "http://www.test.org";
		$expected = $this->getLink('http://www.test.org');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// not at beginning of text
		$text = "test http://www.test.org";
		$expected = 'test ' . $this->getLink('http://www.test.org');
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by comma
		$text = "test http://www.test.org, test";
		$expected = 'test ' . $this->getLink('http://www.test.org') . ', test';
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// preceeded by comma
		$text = "test,http://www.test.org test";
		$expected = 'test,' . $this->getLink('http://www.test.org') . ' test';
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// followed by period
		$text = "test http://www.test.org. test";
		$expected = 'test ' . $this->getLink('http://www.test.org') . '. test';
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// surrounded by parentheses
		$text = "test (http://www.test.org) test";
		$expected = 'test (' . $this->getLink('http://www.test.org') . ') test';
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);

		// no http://
		$text = "test www.test.org test";
		$expected = 'test ' . $this->getLink('www.test.org') . ' test';
		$result = thewire_filter($text);
		$this->assertEqual($result, $expected);
	}

}