]> git.mxchange.org Git - friendica.git/blob - tests/src/Moderation/Factory/ReportTest.php
Posts per author/server on the community pages (#13764)
[friendica.git] / tests / src / Moderation / Factory / ReportTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Moderation\Factory;
23
24 use Friendica\Moderation\Collection;
25 use Friendica\Moderation\Factory;
26 use Friendica\Moderation\Entity;
27 use Friendica\Test\MockedTest;
28 use Friendica\Util\Clock\FrozenClock;
29 use Friendica\Util\DateTimeFormat;
30 use Psr\Clock\ClockInterface;
31 use Psr\Log\NullLogger;
32
33 class ReportTest extends MockedTest
34 {
35         public function dataCreateFromTableRow(): array
36         {
37                 $clock = new FrozenClock();
38
39                 // We need to strip the microseconds part to match database stored timestamps
40                 $nowSeconds = $clock->now()->setTime(
41                         $clock->now()->format('H'),
42                         $clock->now()->format('i'),
43                         $clock->now()->format('s')
44                 );
45
46                 return [
47                         'default' => [
48                                 'clock' => $clock,
49                                 'row' => [
50                                         'id'              => 11,
51                                         'reporter-id'     => 12,
52                                         'uid'             => null,
53                                         'cid'             => 13,
54                                         'gsid'            => 14,
55                                         'comment'         => '',
56                                         'forward'         => false,
57                                         'category-id'     => Entity\Report::CATEGORY_SPAM,
58                                         'public-remarks'  => '',
59                                         'private-remarks' => '',
60                                         'last-editor-uid' => null,
61                                         'assigned-uid'    => null,
62                                         'status'          => Entity\Report::STATUS_OPEN,
63                                         'resolution'      => null,
64                                         'created'         => $nowSeconds->format(DateTimeFormat::MYSQL),
65                                         'edited'          => null,
66                                 ],
67                                 'posts' => new Collection\Report\Posts(),
68                                 'rules' => new Collection\Report\Rules(),
69                                 'assertion'  => new Entity\Report(
70                                         12,
71                                         13,
72                                         14,
73                                         $nowSeconds,
74                                         Entity\Report::CATEGORY_SPAM,
75                                         null,
76                                         '',
77                                         false,
78                                         new Collection\Report\Posts(),
79                                         new Collection\Report\Rules(),
80                                         '',
81                                         '',
82                                         null,
83                                         Entity\Report::STATUS_OPEN,
84                                         null,
85                                         null,
86                                         null,
87                                         11
88                                 ),
89                         ],
90                         'full' => [
91                                 'clock' => $clock,
92                                 'row' => [
93                                         'id'              => 11,
94                                         'reporter-id'     => 42,
95                                         'uid'             => 12,
96                                         'cid'             => 13,
97                                         'gsid'            => 14,
98                                         'comment'         => 'Report',
99                                         'forward'         => true,
100                                         'category-id'     => Entity\Report::CATEGORY_VIOLATION,
101                                         'public-remarks'  => 'Public remarks',
102                                         'private-remarks' => 'Private remarks',
103                                         'last-editor-uid' => 15,
104                                         'assigned-uid'    => 16,
105                                         'status'          => Entity\Report::STATUS_CLOSED,
106                                         'resolution'      => Entity\Report::RESOLUTION_ACCEPTED,
107                                         'created'         => '2021-10-12 12:23:00',
108                                         'edited'          => '2021-12-10 21:08:00',
109                                 ],
110                                 'posts' => new Collection\Report\Posts([
111                                         new Entity\Report\Post(89),
112                                         new Entity\Report\Post(90),
113                                 ]),
114                                 'rules' => new Collection\Report\Rules([
115                                         new Entity\Report\Rule(1, 'No hate speech'),
116                                         new Entity\Report\Rule(3, 'No commercial promotion'),
117                                 ]),
118                                 'assertion'  => new Entity\Report(
119                                         42,
120                                         13,
121                                         14,
122                                         new \DateTimeImmutable('2021-10-12 12:23:00', new \DateTimeZone('UTC')),
123                                         Entity\Report::CATEGORY_VIOLATION,
124                                         12,
125                                         'Report',
126                                         true,
127                                         new Collection\Report\Posts([
128                                                 new Entity\Report\Post(89),
129                                                 new Entity\Report\Post(90),
130                                         ]),
131                                         new Collection\Report\Rules([
132                                                 new Entity\Report\Rule(1, 'No hate speech'),
133                                                 new Entity\Report\Rule(3, 'No commercial promotion'),
134                                         ]),
135                                         'Public remarks',
136                                         'Private remarks',
137                                         new \DateTimeImmutable('2021-12-10 21:08:00', new \DateTimeZone('UTC')),
138                                         Entity\Report::STATUS_CLOSED,
139                                         Entity\Report::RESOLUTION_ACCEPTED,
140                                         16,
141                                         15,
142                                         11
143                                 ),
144                         ],
145                 ];
146         }
147
148         /**
149          * @dataProvider dataCreateFromTableRow
150          */
151         public function testCreateFromTableRow(ClockInterface $clock, array $row, Collection\Report\Posts $posts, Collection\Report\Rules $rules, Entity\Report $assertion)
152         {
153                 $factory = new Factory\Report(new NullLogger(), $clock);
154
155                 $this->assertEquals($factory->createFromTableRow($row, $posts, $rules), $assertion);
156         }
157
158         public function dataCreateFromReportsRequest(): array
159         {
160                 $clock = new FrozenClock();
161
162                 return [
163                         'default' => [
164                                 'clock'      => $clock,
165                                 'rules'      => [],
166                                 'reporterId' => 12,
167                                 'cid'        => 13,
168                                 'gsid'       => 14,
169                                 'comment'    => '',
170                                 'category'   => 'spam',
171                                 'forward'    => false,
172                                 'postUriIds' => [],
173                                 'ruleIds'    => [],
174                                 'uid'        => null,
175                                 'assertion'  => new Entity\Report(
176                                         12,
177                                         13,
178                                         14,
179                                         $clock->now(),
180                                         Entity\Report::CATEGORY_SPAM,
181                                 ),
182                         ],
183                         'full' => [
184                                 'clock'      => $clock,
185                                 'rules'      => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
186                                 'reporterId' => 12,
187                                 'cid'        => 13,
188                                 'gsid'       => 14,
189                                 'comment'    => 'Report',
190                                 'category'   => 'violation',
191                                 'forward'    => true,
192                                 'postUriIds' => [89, 90],
193                                 'ruleIds'    => [1, 3],
194                                 'uid'        => 42,
195                                 'assertion'  => new Entity\Report(
196                                         12,
197                                         13,
198                                         14,
199                                         $clock->now(),
200                                         Entity\Report::CATEGORY_VIOLATION,
201                                         42,
202                                         'Report',
203                                         true,
204                                         new Collection\Report\Posts([
205                                                 new Entity\Report\Post(89),
206                                                 new Entity\Report\Post(90)
207                                         ]),
208                                         new Collection\Report\Rules([
209                                                 new Entity\Report\Rule(1, 'Rule 1'),
210                                                 new Entity\Report\Rule(3, 'Rule 3'),
211                                         ]),
212                                 ),
213                         ],
214                         'forced-violation' => [
215                                 'clock'      => $clock,
216                                 'rules'      => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
217                                 'reporterId' => 12,
218                                 'cid'        => 13,
219                                 'gsid'       => 14,
220                                 'comment'    => 'Report',
221                                 'category'   => 'other',
222                                 'forward'    => false,
223                                 'postUriIds' => [],
224                                 'ruleIds'    => [2, 3],
225                                 'uid'        => null,
226                                 'assertion'  => new Entity\Report(
227                                         12,
228                                         13,
229                                         14,
230                                         $clock->now(),
231                                         Entity\Report::CATEGORY_VIOLATION,
232                                         null,
233                                         'Report',
234                                         false,
235                                         new Collection\Report\Posts(),
236                                         new Collection\Report\Rules([
237                                                 new Entity\Report\Rule(2, 'Rule 2'),
238                                                 new Entity\Report\Rule(3, 'Rule 3'),
239                                         ]),
240                                 ),
241                         ],
242                         'unknown-category' => [
243                                 'clock'      => $clock,
244                                 'rules'      => ['', 'Rule 1', 'Rule 2', 'Rule 3'],
245                                 'reporterId' => 12,
246                                 'cid'        => 13,
247                                 'gsid'       => 14,
248                                 'comment'    => '',
249                                 'category'   => 'unknown',
250                                 'forward'    => false,
251                                 'postUriIds' => [],
252                                 'ruleIds'    => [],
253                                 'uid'        => null,
254                                 'assertion'  => new Entity\Report(
255                                         12,
256                                         13,
257                                         14,
258                                         $clock->now(),
259                                         Entity\Report::CATEGORY_OTHER,
260                                 ),
261                         ],
262                 ];
263         }
264
265         /**
266          * @dataProvider dataCreateFromReportsRequest
267          */
268         public function testCreateFromReportsRequest(ClockInterface $clock, array $rules, int $reporterId, int $cid, int $gsid, string $comment, string $category, bool $forward, array $postUriIds, array $ruleIds, int $uid = null, Entity\Report $assertion)
269         {
270                 $factory = new Factory\Report(new NullLogger(), $clock);
271
272                 $this->assertEquals($factory->createFromReportsRequest($rules, $reporterId, $cid, $gsid, $comment, $category, $forward, $postUriIds, $ruleIds, $uid), $assertion);
273         }
274 }