Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
29.08% covered (danger)
29.08%
41 / 141
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_content_visibility_set_post_visibility_test
29.08% covered (danger)
29.08%
41 / 141
60.00% covered (warning)
60.00%
3 / 5
24.48
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
 set_post_visibility_data
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 1
2
 test_set_post_visibility
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
3
 set_post_soft_deleted_data
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 test_set_post_soft_deleted
100.00% covered (success)
100.00%
15 / 15
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_set_post_visibility_test extends phpbb_database_test_case
18{
19    public function getDataSet()
20    {
21        return $this->createXMLDataSet(__DIR__ . '/fixtures/set_post_visibility.xml');
22    }
23
24    public static function set_post_visibility_data()
25    {
26        return array(
27            array(
28                ITEM_APPROVED,
29                1, 1, 1,
30                2, time(), 'approve',
31                true, false,
32                array(
33                    array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => 'approve'),
34                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
35                    array('post_id' => 3, 'post_visibility' => 2, 'post_delete_reason' => ''),
36                ),
37                array(
38                    array('topic_visibility' => 1, 'topic_first_post_id' => 1, 'topic_last_post_id' => 2),
39                ),
40            ),
41            array(
42                ITEM_APPROVED,
43                3, 1, 1,
44                2, time(), 'approve',
45                false, true,
46                array(
47                    array('post_id' => 1, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
48                    array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
49                    array('post_id' => 3, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => 'approve'),
50                ),
51                array(
52                    array('topic_visibility' => 1, 'topic_first_post_id' => 2, 'topic_last_post_id' => 3),
53                ),
54            ),
55            array(
56                ITEM_DELETED,
57                2, 1, 1,
58                2, time(), 'deleted',
59                true, true,
60                array(
61                    array('post_id' => 1, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
62                    array('post_id' => 2, 'post_visibility' => 2, 'post_delete_reason' => 'deleted'),
63                    array('post_id' => 3, 'post_visibility' => 2, 'post_delete_reason' => ''),
64                ),
65                array(
66                    array('topic_visibility' => 2, 'topic_first_post_id' => 1, 'topic_last_post_id' => 3),
67                ),
68            ),
69            array(
70                ITEM_DELETED,
71                5, 2, 1,
72                2, time(), 'deleted',
73                true, false,
74                array(
75                    array('post_id' => 4, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
76                    array('post_id' => 5, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'deleted'),
77                    array('post_id' => 6, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
78                    array('post_id' => 7, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => ''),
79                ),
80                array(
81                    array('topic_visibility' => 1, 'topic_first_post_id' => 6, 'topic_last_post_id' => 6),
82                ),
83            ),
84            array(
85                ITEM_DELETED,
86                6, 2, 1,
87                2, time(), 'deleted',
88                false, true,
89                array(
90                    array('post_id' => 4, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
91                    array('post_id' => 5, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
92                    array('post_id' => 6, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'deleted'),
93                    array('post_id' => 7, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => ''),
94                ),
95                array(
96                    array('topic_visibility' => 1, 'topic_first_post_id' => 5, 'topic_last_post_id' => 5),
97                ),
98            ),
99            array(
100                ITEM_DELETED,
101                8, 3, 1,
102                2, time(), 'deleted',
103                true, true,
104                array(
105                    array('post_id' => 8, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'deleted'),
106                ),
107                array(
108                    array('topic_visibility' => 2, 'topic_first_post_id' => 8, 'topic_last_post_id' => 8),
109                ),
110            ),
111        );
112    }
113
114    /**
115    * @dataProvider set_post_visibility_data
116    */
117    public function test_set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $expected, $expected_topic)
118    {
119        global $cache, $db, $auth, $phpbb_dispatcher, $phpbb_root_path, $phpEx;
120
121        $cache = new phpbb_mock_cache;
122        $db = $this->new_dbal();
123        $auth = $this->createMock('\phpbb\auth\auth');
124        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
125        $lang = new \phpbb\language\language($lang_loader);
126        $user = new \phpbb\user($lang, '\phpbb\datetime');
127        $before_posts = 5;
128        $config = new phpbb\config\config(['num_posts' => $before_posts]);
129        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
130        $content_visibility = new \phpbb\content_visibility($auth, $config, $phpbb_dispatcher, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE);
131
132        $content_visibility->set_post_visibility($visibility, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest);
133
134        $result = $db->sql_query('SELECT post_id, post_visibility, post_delete_reason
135            FROM phpbb_posts
136            WHERE topic_id = ' . $topic_id . '
137            ORDER BY post_id ASC');
138
139        $this->assertEquals($expected, $db->sql_fetchrowset($result));
140        $db->sql_freeresult($result);
141
142        $result = $db->sql_query('SELECT topic_visibility, topic_first_post_id, topic_last_post_id
143            FROM phpbb_topics
144            WHERE topic_id = ' . $topic_id);
145
146        $this->assertEquals($expected_topic, $db->sql_fetchrowset($result));
147        $db->sql_freeresult($result);
148        if ($visibility == ITEM_APPROVED)
149        {
150            $this->assertEquals($before_posts + 1, $config['num_posts'], 'Number of posts did not increment as expected');
151        }
152        else if ($visibility == ITEM_DELETED)
153        {
154            $this->assertEquals($before_posts - 1, $config['num_posts'], 'Number of posts did not increment as expected');
155        }
156    }
157
158    public static function set_post_soft_deleted_data()
159    {
160        return array(
161            array(
162                10, 10, 10,
163                1, time(), 'soft-deleted',
164                true, false,
165                array(array('topic_attachment' => 1)),
166            ),
167            array(
168                13, 11, 10,
169                1, time(), 'soft-deleted',
170                true, false,
171                array(array('topic_attachment' => 0)),
172            ),
173        );
174    }
175
176    /**
177    * @dataProvider set_post_soft_deleted_data
178    */
179    public function test_set_post_soft_deleted($post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $expected)
180    {
181        global $cache, $db, $auth, $phpbb_dispatcher, $phpbb_root_path, $phpEx;
182
183        $cache = new phpbb_mock_cache;
184        $db = $this->new_dbal();
185        $auth = $this->createMock('\phpbb\auth\auth');
186        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
187        $lang = new \phpbb\language\language($lang_loader);
188        $user = new \phpbb\user($lang, '\phpbb\datetime');
189        $config = new phpbb\config\config(array());
190        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
191        $content_visibility = new \phpbb\content_visibility($auth, $config, $phpbb_dispatcher, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE);
192
193        $content_visibility->set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest);
194
195        $result = $db->sql_query('SELECT topic_attachment
196            FROM phpbb_topics
197            WHERE topic_id = ' . $topic_id);
198
199        $this->assertEquals($expected, $db->sql_fetchrowset($result));
200        $db->sql_freeresult($result);
201    }
202}