Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.40% covered (danger)
13.40%
52 / 388
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_content_visibility_delete_post_test
13.40% covered (danger)
13.40%
52 / 388
66.67% covered (warning)
66.67%
2 / 3
8.84
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete_post_data
0.00% covered (danger)
0.00%
0 / 336
0.00% covered (danger)
0.00%
0 / 1
2
 test_delete_post
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/../../phpBB/includes/functions_admin.php';
15require_once __DIR__ . '/../../phpBB/includes/functions_posting.php';
16
17class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
18{
19    public function getDataSet()
20    {
21        return $this->createXMLDataSet(__DIR__ . '/fixtures/delete_post.xml');
22    }
23
24    public static function delete_post_data()
25    {
26        $info_data = array(
27            'topic_first_post_id'    => 1,
28            'topic_last_post_id'    => 3,
29            'topic_posts_approved'        => 3,
30            'topic_posts_unapproved'    => 0,
31            'topic_posts_softdeleted'    => 0,
32            'topic_visibility'        => ITEM_APPROVED,
33            'post_time'                => 2,
34            'post_visibility'        => ITEM_APPROVED,
35            'post_postcount'        => true,
36            'poster_id'                => 1,
37            'post_reported'            => false,
38        );
39
40        return array(
41            array(
42                1, 1, 2,
43                array_merge($info_data, array(
44                    'post_time'                => 2,
45                )),
46                false, 'harddelete',
47                3, // expected next post id
48                array(
49                    array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
50                    //array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
51                    array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
52                ),
53                array(
54                    array(
55                        'topic_visibility'        => ITEM_APPROVED,
56                        'topic_first_post_id'    => 1,
57                        'topic_last_post_id'    => 3,
58                        'topic_posts_approved'        => 2,
59                        'topic_posts_unapproved'    => 0,
60                        'topic_posts_softdeleted'    => 0,
61                        'topic_delete_reason'    => '',
62                    ),
63                ),
64                array(
65                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 3),
66                ),
67                array(
68                    array('user_posts' => 3),
69                ),
70            ),
71            array(
72                1, 1, 1,
73                array_merge($info_data, array(
74                    'post_time'                => 1,
75                )),
76                false, 'harddelete',
77                2, // expected next post id
78                array(
79                    //array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
80                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
81                    array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
82                ),
83                array(
84                    array(
85                        'topic_visibility'        => ITEM_APPROVED,
86                        'topic_first_post_id'    => 2,
87                        'topic_last_post_id'    => 3,
88                        'topic_posts_approved'        => 2,
89                        'topic_posts_unapproved'    => 0,
90                        'topic_posts_softdeleted'    => 0,
91                        'topic_delete_reason'    => '',
92                    ),
93                ),
94                array(
95                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 3),
96                ),
97                array(
98                    array('user_posts' => 3),
99                ),
100            ),
101            array(
102                1, 1, 3,
103                array_merge($info_data, array(
104                    'post_time'                => 3,
105                )),
106                false, 'harddelete',
107                2, // expected next post id
108                array(
109                    array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
110                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
111                    //array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
112                ),
113                array(
114                    array(
115                        'topic_visibility'        => ITEM_APPROVED,
116                        'topic_first_post_id'    => 1,
117                        'topic_last_post_id'    => 2,
118                        'topic_posts_approved'        => 2,
119                        'topic_posts_unapproved'    => 0,
120                        'topic_posts_softdeleted'    => 0,
121                        'topic_delete_reason'    => '',
122                    ),
123                ),
124                array(
125                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 2),
126                ),
127                array(
128                    array('user_posts' => 3),
129                ),
130            ),
131            array(
132                1, 1, 2,
133                array_merge($info_data, array(
134                    'post_time'                => 2,
135                )),
136                true, 'soft delete',
137                3, // expected next post id
138                array(
139                    array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
140                    array('post_id' => 2, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'soft delete'),
141                    array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
142                ),
143                array(
144                    array(
145                        'topic_visibility'        => ITEM_APPROVED,
146                        'topic_first_post_id'    => 1,
147                        'topic_last_post_id'    => 3,
148                        'topic_posts_approved'        => 2,
149                        'topic_posts_unapproved'    => 0,
150                        'topic_posts_softdeleted'    => 1,
151                        'topic_delete_reason'    => '',
152                    ),
153                ),
154                array(
155                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 1, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 3),
156                ),
157                array(
158                    array('user_posts' => 3),
159                ),
160            ),
161            array(
162                1, 1, 1,
163                array_merge($info_data, array(
164                    'post_time'                => 1,
165                )),
166                true, 'soft delete',
167                2, // expected next post id
168                array(
169                    array('post_id' => 1, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'soft delete'),
170                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
171                    array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
172                ),
173                array(
174                    array(
175                        'topic_visibility'        => ITEM_APPROVED,
176                        'topic_first_post_id'    => 2,
177                        'topic_last_post_id'    => 3,
178                        'topic_posts_approved'        => 2,
179                        'topic_posts_unapproved'    => 0,
180                        'topic_posts_softdeleted'    => 1,
181                        'topic_delete_reason'    => '',
182                    ),
183                ),
184                array(
185                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 1, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 3),
186                ),
187                array(
188                    array('user_posts' => 3),
189                ),
190            ),
191            array(
192                1, 1, 3,
193                array_merge($info_data, array(
194                    'post_time'                => 3,
195                )),
196                true, 'soft delete',
197                3, // expected next post id
198                array(
199                    array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
200                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
201                    array('post_id' => 3, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'soft delete'),
202                ),
203                array(
204                    array(
205                        'topic_visibility'        => ITEM_APPROVED,
206                        'topic_first_post_id'    => 1,
207                        'topic_last_post_id'    => 2,
208                        'topic_posts_approved'        => 2,
209                        'topic_posts_unapproved'    => 0,
210                        'topic_posts_softdeleted'    => 1,
211                        'topic_delete_reason'    => '',
212                    ),
213                ),
214                array(
215                    array('forum_posts_approved' => 2, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 1, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 2),
216                ),
217                array(
218                    array('user_posts' => 3),
219                ),
220            ),
221
222            array(
223                2, 2, 4,
224                array(
225                    'topic_first_post_id'    => 4,
226                    'topic_last_post_id'    => 4,
227                    'topic_posts_approved'        => 1,
228                    'topic_posts_unapproved'    => 0,
229                    'topic_posts_softdeleted'    => 0,
230                    'topic_visibility'        => ITEM_APPROVED,
231                    'post_time'                => 4,
232                    'post_visibility'        => ITEM_APPROVED,
233                    'post_postcount'        => true,
234                    'poster_id'                => 1,
235                    'post_reported'            => false,
236                ),
237                false, 'harddelete',
238                false, // expected next post id
239                array(
240                ),
241                array(
242                ),
243                array(
244                    array('forum_posts_approved' => 0, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 0, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 0),
245                ),
246                array(
247                    array('user_posts' => 3),
248                ),
249            ),
250
251            array(
252                2, 2, 4,
253                array(
254                    'topic_first_post_id'    => 4,
255                    'topic_last_post_id'    => 4,
256                    'topic_posts_approved'        => 1,
257                    'topic_posts_unapproved'    => 0,
258                    'topic_posts_softdeleted'    => 0,
259                    'topic_visibility'        => ITEM_APPROVED,
260                    'post_time'                => 4,
261                    'post_visibility'        => ITEM_APPROVED,
262                    'post_postcount'        => true,
263                    'poster_id'                => 1,
264                    'post_reported'            => false,
265                ),
266                true, 'soft delete',
267                false, // expected next post id
268                array(
269                    array('post_id' => 4, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => ''),
270                ),
271                array(
272                    array(
273                        'topic_visibility'        => ITEM_DELETED,
274                        'topic_first_post_id'    => 4,
275                        'topic_last_post_id'    => 4,
276                        'topic_posts_approved'        => 0,
277                        'topic_posts_unapproved'    => 0,
278                        'topic_posts_softdeleted'    => 1,
279                        'topic_delete_reason'    => 'soft delete',
280                    ),
281                ),
282                array(
283                    array('forum_posts_approved' => 0, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 1, 'forum_topics_approved' => 0, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 1, 'forum_last_post_id' => 0),
284                ),
285                array(
286                    array('user_posts' => 3),
287                ),
288            ),
289            // Delete actual last post that is unapproved
290            array(
291                3, 3, 6,
292                array(
293                    'topic_first_post_id'    => 5,
294                    'topic_last_post_id'    => 5,
295                    'topic_posts_approved'        => 1,
296                    'topic_posts_unapproved'    => 1,
297                    'topic_posts_softdeleted'    => 0,
298                    'topic_visibility'        => ITEM_APPROVED,
299                    'post_time'                => 4,
300                    'post_visibility'        => ITEM_UNAPPROVED,
301                    'post_postcount'        => true,
302                    'poster_id'                => 1,
303                    'post_reported'            => false,
304                ),
305                false, 'harddelete',
306                5, // expected next post id
307                array(
308                    array('post_id' => 5, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
309                    // array('post_id' => 6, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
310                ),
311                array(
312                    array(
313                        'topic_visibility'        => ITEM_APPROVED,
314                        'topic_first_post_id'    => 5,
315                        'topic_last_post_id'    => 5,
316                        'topic_posts_approved'        => 1,
317                        'topic_posts_unapproved'    => 0,
318                        'topic_posts_softdeleted'    => 0,
319                        'topic_delete_reason'    => '',
320                    ),
321                ),
322                array(
323                    array('forum_posts_approved' => 1, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 5),
324                ),
325                array(
326                    array('user_posts' => 4),
327                ),
328            ),
329            // Hard delete last approved post
330            array(
331                3, 3, 5,
332                array(
333                    'topic_first_post_id'    => 5,
334                    'topic_last_post_id'    => 5,
335                    'topic_posts_approved'        => 1,
336                    'topic_posts_unapproved'    => 1,
337                    'topic_posts_softdeleted'    => 0,
338                    'topic_visibility'        => ITEM_APPROVED,
339                    'post_time'                => 4,
340                    'post_visibility'        => ITEM_APPROVED,
341                    'post_postcount'        => true,
342                    'poster_id'                => 1,
343                    'post_reported'            => false,
344                ),
345                false, 'harddelete',
346                6, // expected next post id
347                array(
348                    //array('post_id' => 5, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
349                    array('post_id' => 6, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
350                ),
351                array(
352                    array(
353                        'topic_visibility'        => ITEM_APPROVED,
354                        'topic_first_post_id'    => 6,
355                        'topic_last_post_id'    => 5, // can't be updated with no valid data
356                        'topic_posts_approved'        => 0,
357                        'topic_posts_unapproved'    => 1,
358                        'topic_posts_softdeleted'    => 0,
359                        'topic_delete_reason'    => '',
360                    ),
361                ),
362                array(
363                    array('forum_posts_approved' => 0, 'forum_posts_unapproved' => 1, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 5),
364                ),
365                array(
366                    array('user_posts' => 3),
367                ),
368            ),
369        );
370    }
371
372    /**
373    * @dataProvider delete_post_data
374    */
375    public function test_delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason, $expected_next_post_id, $expected_posts, $expected_topic, $expected_forum, $expected_user)
376    {
377        global $auth, $cache, $config, $db, $user, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx;
378
379        $config = new \phpbb\config\config(array(
380            'num_posts' => 3,
381            'num_topics' => 1,
382            'search_type' => 'foo',
383        ));
384        $cache = new phpbb_mock_cache;
385        $db = $this->new_dbal();
386        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
387
388        $storage = $this->createMock('\phpbb\storage\storage');
389
390        // Create auth mock
391        $auth = $this->createMock('\phpbb\auth\auth');
392        $auth->expects($this->any())
393            ->method('acl_get')
394            ->with($this->stringContains('_'), $this->anything())
395            ->will($this->returnValueMap(array(
396                array('m_approve', 1, true),
397            )));
398        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
399        $lang = new \phpbb\language\language($lang_loader);
400        $user = new \phpbb\user($lang, '\phpbb\datetime');
401        $user->data['user_id'] = ANONYMOUS;
402
403        $attachment_delete = new \phpbb\attachment\delete($config, $db, new \phpbb_mock_event_dispatcher(), new \phpbb\attachment\resync($db), $storage);
404
405        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
406
407        $phpbb_container = new phpbb_mock_container_builder();
408        $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager());
409        $phpbb_container->set('content.visibility', new \phpbb\content_visibility($auth, $config, $phpbb_dispatcher, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE));
410        // Works as a workaround for tests
411        $phpbb_container->set('attachment.manager', $attachment_delete);
412
413        $search_backend = $this->createMock(\phpbb\search\backend\search_backend_interface::class);
414        $search_backend_factory = $this->createMock(\phpbb\search\search_backend_factory::class);
415        $search_backend_factory->method('get_active')->willReturn($search_backend);
416        $phpbb_container->set('search.backend_factory', $search_backend_factory);
417
418        $this->assertSame($expected_next_post_id, delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason));
419        $result = $db->sql_query('SELECT post_id, post_visibility, post_delete_reason
420            FROM phpbb_posts
421            WHERE topic_id = ' . $topic_id . '
422            ORDER BY post_id ASC');
423
424        $this->assertEquals($expected_posts, $db->sql_fetchrowset($result));
425        $db->sql_freeresult($result);
426
427        $result = $db->sql_query('SELECT topic_visibility, topic_first_post_id, topic_last_post_id, topic_posts_approved, topic_posts_unapproved, topic_posts_softdeleted, topic_delete_reason
428            FROM phpbb_topics
429            WHERE topic_id = ' . $topic_id);
430
431        $this->assertEquals($expected_topic, $db->sql_fetchrowset($result));
432        $db->sql_freeresult($result);
433
434        $result = $db->sql_query('SELECT forum_posts_approved, forum_posts_unapproved, forum_posts_softdeleted, forum_topics_approved, forum_topics_unapproved, forum_topics_softdeleted, forum_last_post_id
435            FROM phpbb_forums
436            WHERE forum_id = ' . $forum_id);
437
438        $this->assertEquals($expected_forum, $db->sql_fetchrowset($result));
439        $db->sql_freeresult($result);
440
441        $sql = 'SELECT user_posts
442            FROM ' . USERS_TABLE . '
443            WHERE user_id = ' . (int) $data['poster_id'];
444        $result = $db->sql_query($sql);
445
446        $this->assertEquals($expected_user, $db->sql_fetchrowset($result));
447        $db->sql_freeresult($result);
448    }
449}