aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Lib/MessageFilterTest.php
blob: 0a2aea0c6cd0c05eb816c28891ca01b1108f3c9d (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
<?php
namespace Zotlabs\Tests\Unit\Lib;

use Zotlabs\Tests\Unit\UnitTestCase;
use Zotlabs\Lib\MessageFilter;

class MessageFilterTest extends UnitTestCase {

	/**
	 * Test the `evaluate` function.
	 *
	 * @dataProvider evaluate_provider
	 */
	public function test_evaluate(string $incl, string $excl, bool $result) : void {
		// This is for simpler handling of the timestamps
		date_default_timezone_set('UTC');

		$item = [
			'title' => '',
			'body' => "A grasshopper spent the summer hopping about in the sun and singing to his heart's content. One day, an ant went hurrying by, looking very hot and weary.\r\n#story #grasshopper #ant",
			'term' => [
				['ttype' => TERM_HASHTAG, 'term' => 'story'],
				['ttype' => TERM_HASHTAG, 'term' => 'grasshopper'],
				['ttype' => TERM_HASHTAG, 'term' => 'ant']
			],
			'verb' => 'Create',
			'obj_type' => 'Note',
			'obj' => [
				'type' => 'Note',
				'attributedTo' => 'https://example.com/users/test',
				'summary' => null,
				'content' => "A grasshopper spent the summer hopping about in the sun and singing to his heart's content. One day, an ant went hurrying by, looking very hot and weary.\r\n#story #grasshopper #ant",
				'sensitive' => false
			],
			'item_private' => 0,
			'item_thread_top' => 1,
			'created' => '2025-04-18 20:50:00'
		];

		$this->assertEquals($result, MessageFilter::evaluate($item, $incl, $excl));
	}

	public static function evaluate_provider() : array {
		return [
			'body contains incl' => [
				'summer',
				'',
				true
			],
			'body contains excl' => [
				'',
				'summer',
				false
			],
			'body contains word hopper (starting with a space) in excl using regex' => [
				'',
				'/ hopper/',
				true
			],
			'lang=en in incl' => [
				'lang=en',
				'',
				true
			],
			'lang=en in excl' => [
				'',
				'lang=en',
				false
			],
			'lang=de in incl' => [
				'lang=de',
				'',
				false
			],
			'lang=de in excl' => [
				'',
				'lang=de',
				true
			],
			'until=2025-04-18 20:49:00 in excl' => [
				'',
				'until=2025-04-18 20:49:00',
				true
			],
			'until=2025-04-18 20:51:00 in excl' => [
				'',
				'until=2025-04-18 20:51:00',
				false
			],
			'until=2025-04-18 20:49:00 in incl' => [
				'until=2025-04-18 20:49:00',
				'',
				false
			],
			'until=2025-04-18 20:51:00 in incl' => [
				'until=2025-04-18 20:51:00',
				'',
				true
			],
			'hashtag in incl' => [
				'#grasshopper',
				'',
				true
			],
			'hashtag in excl' => [
				'',
				'#grasshopper',
				false
			],
			'any hashtag in excl' => [
				'',
				'#*',
				false
			],
			'item.body contains substring hopper in excl' => [
				'',
				'?body ~= hopper',
				false
			],
			'item.verb == Announce in excl' => [
				'',
				'?verb == Announce',
				true
			],
			'item.verb != Announce in incl' => [
				'?verb != Announce',
				'',
				true
			],
			'combined body contains word and item.verb == Announce in excl' => [
				'',
				"summer\r\n?verb == Announce",
				false
			],
			'item.item_thread_top == 1 in excl' => [
				'',
				"?item_thread_top == 1",
				false
			],
			'combined item_private == 0 and item.item_thread_top == 1 in excl' => [
				'',
				"?item_private == 0\r\n?item_thread_top == 1",
				false
			],
			'item.item_private < 1 in excl' => [
				'',
				"?item_private < 1",
				false
			],
			'item.item_thread_top = 1 and item.item_private > 0 in excl' => [
				'',
				"?item_thread_top == 1 && ?item_private > 0 ",
				true
			],
			'item.item_thread_top = 1 and item.item_private < 1 in excl' => [
				'',
				"?item_thread_top == 1 && ?item_private < 1 ",
				false
			],
			'item.item_thread_top = 1 or item.item_private = 0 in excl' => [
				'',
				"?item_thread_top == 1 && ?item_private == 0",
				false
			],
			'item.item_private < 1 and item.item_thread_top = 1 in excl' => [
				'',
				"?item_private < 1 && ?item_thread_top == 1",
				false
			],
			'item.item_private < 1 and item.item_thread_top = 0 in excl' => [
				'',
				"?item_private < 1 && ?item_thread_top == 0",
				true
			],
			'combined item.verb = Create, item.item_private < 1 and item.item_thread_top = 0 in excl' => [
				'',
				"?verb == Create\r\n?item_private < 1 && ?item_thread_top == 1",
				false
			],
			'item.obj contains value Note in incl' => [
				'?obj {} Note',
				'',
				true
			],
			'item.obj contains key type in incl' => [
				'?obj {*} type',
				'',
				true
			],
			'obj.type = Note in incl' => [
				'?+type == Note',
				'',
				true
			],
			'obj.sensitive = true in incl' => [
				'?+sensitive',
				'',
				false
			],
			'obj.sensitive != false in incl' => [
				'?+!sensitive',
				'',
				true
			],
		];
	}
}