Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/AbuseFilterRowsAndFiltersTestTrait.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
4 use MediaWiki\Extension\AbuseFilter\Filter\Flags;
5 use MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo;
6 use MediaWiki\Extension\AbuseFilter\Filter\Specs;
7
8 /**
9 * This trait contains some Filter <-> db_row correspondences, and can be used to avoid long test
10 * methods.
11 */
12 trait AbuseFilterRowsAndFiltersTestTrait {
13 /**
14 * @return array[]
15 */
16 public function getRowsAndFilters(): array {
17 static $ret = null;
18 if ( $ret !== null ) {
19 return $ret;
20 }
21 $rows = [
22 'no actions' => (object)[
23 'af_id' => 42,
24 'af_pattern' => 'false',
25 'af_user' => 1,
26 'af_user_text' => 'FilterTester',
27 'af_actor' => 1,
28 'af_timestamp' => '20190826000000',
29 'af_enabled' => 1,
30 'af_comments' => '',
31 'af_public_comments' => 'Mock filter',
32 'af_hidden' => Flags::FILTER_PUBLIC,
33 'af_hit_count' => 0,
34 'af_throttled' => 0,
35 'af_deleted' => 0,
36 'af_actions' => '',
37 'af_global' => 0,
38 'af_group' => 'default'
39 ],
40 'with actions' => (object)[
41 'af_id' => 163,
42 'af_pattern' => 'false',
43 'af_user' => 1,
44 'af_user_text' => 'FilterTester',
45 'af_actor' => 1,
46 'af_timestamp' => '20190826000000',
47 'af_enabled' => 1,
48 'af_comments' => '',
49 'af_public_comments' => 'Mock filter',
50 'af_hidden' => Flags::FILTER_HIDDEN,
51 'af_hit_count' => 0,
52 'af_throttled' => 1,
53 'af_deleted' => 0,
54 'af_actions' => 'disallow,blockautopromote',
55 'af_global' => 0,
56 'af_group' => 'default'
57 ]
58 ];
59
60 foreach ( $rows as $name => $row ) {
61 $actionKeys = $row->af_actions ? explode( ',', $row->af_actions ) : [];
62 $filter = new ExistingFilter(
63 new Specs(
64 $row->af_pattern,
65 $row->af_comments,
66 $row->af_public_comments,
67 $actionKeys,
68 $row->af_group
69 ),
70 new Flags(
71 (bool)$row->af_enabled,
72 (bool)$row->af_deleted,
73 (int)$row->af_hidden,
74 (bool)$row->af_global
75 ),
76 array_fill_keys( $actionKeys, [] ),
77 new LastEditInfo(
78 $row->af_user,
79 $row->af_user_text,
80 $row->af_timestamp
81 ),
82 $row->af_id,
83 $row->af_hit_count,
84 $row->af_throttled
85 );
86 $ret[$name] = [
87 'row' => $row,
88 'actions' => $this->getRowsForActions( $row->af_id, $actionKeys ),
89 'filter' => $filter
90 ];
91 }
92
93 // Add special cases that cannot be generalized above
94 $ret = array_merge( $ret, [
95 'null comments and name' => [
96 'row' => (object)[
97 'af_id' => 333,
98 'af_pattern' => 'false',
99 'af_user' => 1,
100 'af_user_text' => 'FilterTester',
101 'af_actor' => 1,
102 'af_timestamp' => '20190826000000',
103 'af_enabled' => 1,
104 'af_comments' => null,
105 'af_public_comments' => null,
106 'af_hidden' => Flags::FILTER_HIDDEN,
107 'af_hit_count' => 100,
108 'af_throttled' => 1,
109 'af_deleted' => 0,
110 'af_actions' => 'warn',
111 'af_global' => 0,
112 'af_group' => 'default'
113 ],
114 'actions' => $this->getRowsForActions( 333, [ 'warn' ] ),
115 'filter' => new ExistingFilter(
116 new Specs(
117 'false',
118 '',
119 '',
120 [ 'warn' ],
121 'default'
122 ),
123 new Flags( true, false, true, false ),
124 [ 'warn' => [] ],
125 new LastEditInfo(
126 1,
127 'FilterTester',
128 '20190826000000'
129 ),
130 333,
131 100,
132 1
133 )
134 ],
135 'no hitcount and throttled' => [
136 'row' => (object)[
137 'af_id' => 1000,
138 'af_pattern' => '"foo"',
139 'af_user' => 1,
140 'af_user_text' => 'FilterTester',
141 'af_actor' => 1,
142 'af_timestamp' => '20190826000000',
143 'af_enabled' => 0,
144 'af_comments' => 'foo',
145 'af_public_comments' => 'bar',
146 'af_hidden' => Flags::FILTER_HIDDEN,
147 'af_deleted' => 1,
148 'af_actions' => '',
149 'af_global' => 1,
150 'af_group' => 'default'
151 ],
152 'actions' => [],
153 'filter' => new ExistingFilter(
154 new Specs(
155 '"foo"',
156 'foo',
157 'bar',
158 [],
159 'default'
160 ),
161 new Flags( false, true, true, true ),
162 [],
163 new LastEditInfo(
164 1,
165 'FilterTester',
166 '20190826000000'
167 ),
168 1000
169 )
170 ],
171 'pattern to trim' => [
172 'row' => (object)[
173 'af_id' => 1000,
174 'af_pattern' => "\n\t 0 \t\n",
175 'af_user' => 1,
176 'af_user_text' => 'FilterTester',
177 'af_actor' => 1,
178 'af_timestamp' => '20190826000000',
179 'af_enabled' => 0,
180 'af_comments' => 'foo',
181 'af_public_comments' => 'bar',
182 'af_hidden' => Flags::FILTER_HIDDEN,
183 'af_hit_count' => 163,
184 'af_throttled' => 1,
185 'af_deleted' => 1,
186 'af_actions' => '',
187 'af_global' => 1,
188 'af_group' => 'default'
189 ],
190 'actions' => [],
191 'filter' => new ExistingFilter(
192 new Specs(
193 '0',
194 'foo',
195 'bar',
196 [],
197 'default'
198 ),
199 new Flags( false, true, true, true ),
200 [],
201 new LastEditInfo(
202 1,
203 'FilterTester',
204 '20190826000000'
205 ),
206 1000,
207 163,
208 1
209 )
210 ],
211 ] );
212
213 return $ret;
214 }
215
216 /**
217 * @param int $id
218 * @param string[] $actions
219 * @return stdClass[]
220 */
221 private function getRowsForActions( int $id, array $actions ): array {
222 $ret = [];
223 foreach ( $actions as $action ) {
224 $ret[] = (object)[
225 'afa_filter' => $id,
226 'afa_consequence' => $action,
227 'afa_parameters' => ''
228 ];
229 }
230 return $ret;
231 }
232 }