aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/GetTagsTest.php
blob: 418d32c473d810707325125f53dcae0d55e4e01d (plain) (blame)
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
<?php
/**
 * This file contains the tests for get_tags and the tag handling in item.php
 *
 * @package test.util
 */

/**
 * A class which can be used as replacement for an app if
 * only get_baseurl is used.
 *
 * @author  Alexander Kampmann
 * @package test.util
 */
class MockApp {
	function get_baseurl() {
		return "baseurl";
	}
}

/**
 * The test should not rely on a database,
 * so this is a replacement for the database access method q.
 *
 * It simulates the user with uid 11 has one contact, named Mike Lastname.
 *
 * @param string $sql
 */
function q($sql) {
	$result=array(array('id'=>15,
			'attag'=>'', 'network'=>'dfrn',
			'name'=>'Mike Lastname', 'alias'=>'Mike',
			'nick'=>'Mike', 'url'=>"http://justatest.de"));

	$args=func_get_args();

	//last parameter is always (in this test) uid, so, it should be 11
	if($args[count($args)-1]!=11) {
		return;
	}


	if(3==count($args)) {
		//first call in handle_body, id only
		if($result[0]['id']==$args[1]) {
			return $result;
		}
		//second call in handle_body, name
		if($result[0]['name']===$args[1]) {
			return $result;
		}
	}
	//third call in handle_body, nick or attag
	if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
		return $result;
	}
}

/**
 * Replacement for dbesc.
 * I don't want to test dbesc here, so
 * I just return the input. It won't be a problem, because
 * the test does not use a real database.
 *
 * DON'T USE HAT FUNCTION OUTSIDE A TEST!
 *
 * @param string $str
 *
 * @return input
 */
function dbesc($str) {
	return $str;
}

/**
 * TestCase for tag handling.
 *
 * @author  alexander
 * @package test.util
 */
class GetTagsTest extends Zotlabs\Tests\Unit\UnitTestCase {
	/**
	 * The mock to use as app
	 */
	private $a;

	/**
	 * Initialize the test. That's a phpUnit function,
	 * don't change its name.
	 */
	public function setUp() : void {
		$this->a = new MockApp();
	}

	/**
	 * Test with one Person tag
	 */
	public function testGetTagsShortPerson() {
		$text="hi @Mike";

		$tags=get_tags($text);

		$str_tags='';
		foreach($tags as $tag) {
			handle_tag($text, $str_tags, 11, $tag);
		}

		//correct tags found?
		$this->assertEquals(1, count($tags));
		$this->assertTrue(in_array("@Mike", $tags));

		//correct output from handle_tag?
		//$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
		//$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);
	}

	/**
	 * Test with one Person tag.
	 * There's a minor spelling mistake...
	 */
	public function testGetTagsShortPersonSpelling() {
		$text="hi @Mike.because";

		$tags=get_tags($text);

		//correct tags found?
		$this->assertEquals(1, count($tags));
		$this->assertTrue(in_array("@Mike.because", $tags));

		$str_tags='';
		handle_tag($text, $str_tags, 11, $tags[0]);

		// (mike) - This is a tricky case.
		// we support mentions as in @mike@example.com - which contains a period.
		// This shouldn't match anything unless you have a contact named "Mike.because".
		// We may need another test for "@Mike. because" - which should return the contact
		// as we ignore trailing periods in tags.

//		$this->assertEquals("cid:15", $inform);
//		$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
//		$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url].because", $text);

		$this->assertEquals("", $str_tags);

	}

	/**
	 * Test with one hash tag.
	 */
	public function testGetTagsShortTag() {
		$text="This is a #test_case";

		$tags=get_tags($text);

		$this->assertEquals(1, count($tags));
		$this->assertTrue(in_array("#test_case", $tags));
	}

	/**
	 * Test with a person and a hash tag
	 */
	public function testGetTagsShortTagAndPerson() {
		$text="hi @Mike This is a #test_case";

		$tags=get_tags($text);

		$this->assertEquals(2, count($tags));
		$this->assertTrue(in_array("@Mike", $tags));
		$this->assertTrue(in_array("#test_case", $tags));

		$str_tags='';
		foreach($tags as $tag) {
			handle_tag($text, $str_tags, 11, $tag);
		}

		//$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url],#[url=baseurl/search?tag=test%20case]test case[/url]", $str_tags);
		//$this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url] This is a #[url=baseurl/search?tag=test%20case]test case[/url]", $text);

	}

	/**
	 * Test with a person, a hash tag and some special chars.
	 */
	public function testGetTagsShortTagAndPersonSpecialChars() {
		$text="hi @Mike, This is a #test_case.";

		$tags=get_tags($text);

		$this->assertEquals(2, count($tags));
		$this->assertTrue(in_array("@Mike", $tags));
		$this->assertTrue(in_array("#test_case", $tags));
	}

	/**
	 * Test with a person tag and text behind it.
	 */
	public function testGetTagsPersonOnly() {
		$text="@Test I saw the Theme Dev group was created.";

		$tags=get_tags($text);

		$this->assertEquals(1, count($tags));
		$this->assertTrue(in_array("@Test", $tags));
	}

	/**
	 * Test a tag with an id in it
	 */
	public function testIdTag() {
		$text="Test with @mike+15 id tag";

		$tags=get_tags($text);

		$this->assertEquals(1, count($tags));
		$this->assertTrue(in_array("@mike+15", $tags));

		$str_tags='';
		foreach($tags as $tag) {
			handle_tag($text, $str_tags, 11, $tag);
		}

		//$this->assertEquals("Test with @[url=http://justatest.de]Mike Lastname[/url] id tag", $text);
		//$this->assertEquals("@[url=http://justatest.de]Mike Lastname[/url]", $str_tags);
	}

	/**
	 * Test with two persons and one special tag.
	 */
	public function testGetTags2Persons1TagSpecialChars() {
		$text="hi @Mike, I'm just writing #test_cases, so"
			. " so @somebody@friendica.com may change #things.";

		$tags=get_tags($text);

		$this->assertEquals(4, count($tags));
		$this->assertTrue(in_array("@Mike", $tags));
		$this->assertTrue(in_array("#test_cases", $tags));
		$this->assertTrue(in_array("@somebody@friendica.com", $tags));
		$this->assertTrue(in_array("#things", $tags));
	}

	/**
	 * Test with a long text.
	 */
	public function testGetTags() {
		$text="hi @Mike, I'm just writing #test_cases, "
			. " so @somebody@friendica.com may change #things. Of course I "
			. "look for a lot of #pitfalls, like #tags at the end of a sentence "
			. "@comment. I hope noone forgets about @fullstops.because that might"
			. " break #things. @Mike@campino@friendica.eu is also #nice, isn't it? "
			. "Now, add a @first_last tag. ";

		$tags=get_tags($text);

		$this->assertTrue(in_array("@Mike", $tags));
		$this->assertTrue(in_array("#test_cases", $tags));
		$this->assertTrue(in_array("@somebody@friendica.com", $tags));
		$this->assertTrue(in_array("#things", $tags));
		$this->assertTrue(in_array("#pitfalls", $tags));
		$this->assertTrue(in_array("#tags", $tags));
		$this->assertTrue(in_array("@comment", $tags));
		$this->assertTrue(in_array("@fullstops.because", $tags));
		$this->assertTrue(in_array("#things", $tags));
		$this->assertTrue(in_array("@Mike", $tags));
		$this->assertTrue(in_array("#nice", $tags));
		$this->assertTrue(in_array("@first_last", $tags));

		//right now, none of the is matched (unsupported)
//		$this->assertFalse(in_array("@Mike@campino@friendica.eu", $tags));
//		$this->assertTrue(in_array("@campino@friendica.eu", $tags));
//		$this->assertTrue(in_array("@campino@friendica.eu is", $tags));
	}

	/**
	 * Test with an empty string
	 */
	public function testGetTagsEmpty() {
		$tags=get_tags("");
		$this->assertEquals(0, count($tags));
	}
}