Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.66% covered (danger)
20.66%
25 / 121
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_content_visibility_get_visibility_sql_test
20.66% covered (danger)
20.66%
25 / 121
66.67% covered (warning)
66.67%
2 / 3
7.49
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
 get_visibility_sql_data
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_visibility_sql
100.00% covered (success)
100.00%
24 / 24
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
14class phpbb_content_visibility_get_visibility_sql_test extends phpbb_database_test_case
15{
16    public function getDataSet()
17    {
18        return $this->createXMLDataSet(__DIR__ . '/fixtures/get_visibility_sql.xml');
19    }
20
21    public static function get_visibility_sql_data()
22    {
23        return array(
24            // data set 0: display_unapproved_posts=false, moderator, can see all posts
25            array(
26                'phpbb_posts',
27                0,
28                false,
29                'post', 1, '',
30                array(
31                    array('m_approve', 1, true),
32                ),
33                array(
34                    array('post_id' => 1),
35                    array('post_id' => 2),
36                    array('post_id' => 3),
37                    array('post_id' => 4),
38                ),
39            ),
40            // data set 1: display_unapproved_posts=false, normal user, cannot see any unapproved posts
41            array(
42                'phpbb_posts',
43                0,
44                false,
45                'post', 1, '',
46                array(
47                ),
48                array(
49                    array('post_id' => 2),
50                ),
51            ),
52            // data set 2: display_unapproved_posts=false, moderator, can see all topics
53            array(
54                'phpbb_topics',
55                0,
56                false,
57                'topic', 1, '',
58                array(
59                    array('m_approve', 1, true),
60                ),
61                array(
62                    array('topic_id' => 1),
63                    array('topic_id' => 2),
64                    array('topic_id' => 3),
65                    array('topic_id' => 4),
66                ),
67            ),
68            // data set 3: display_unapproved_posts=false, normal user, cannot see unapproved posts topic
69            array(
70                'phpbb_topics',
71                0,
72                false,
73                'topic', 1, '',
74                array(),
75                array(
76                    array('topic_id' => 2),
77                ),
78            ),
79            // data set 4: display_unapproved_posts=true, guest user, cannot see unapproved posts
80            array(
81                'phpbb_posts',
82                1,
83                true,
84                'post', 1, '',
85                array(
86                ),
87                array(
88                    array('post_id' => 2),
89                ),
90            ),
91            // data set 5: display_unapproved_posts=true, guest user, cannot see unapproved posts topic
92            array(
93                'phpbb_topics',
94                1,
95                true,
96                'topic', 1, '',
97                array(),
98                array(
99                    array('topic_id' => 2),
100                ),
101            ),
102            // data set 6: display_unapproved_posts=true, normal user, can see own unapproved posts
103            array(
104                'phpbb_posts',
105                0,
106                true,
107                'post', 1, '',
108                array(),
109                array(
110                    array('post_id' => 1),
111                    array('post_id' => 2),
112                ),
113            ),
114            // data set 7: display_unapproved_posts=true, normal user, can see own unapproved posts topic
115            array(
116                'phpbb_topics',
117                0,
118                true,
119                'topic', 1, '',
120                array(),
121                array(
122                    array('topic_id' => 1),
123                    array('topic_id' => 2),
124                ),
125            ),
126        );
127    }
128
129    /**
130    * @dataProvider get_visibility_sql_data
131    */
132    public function test_get_visibility_sql($table, $user_id, $display_unapproved, $mode, $forum_id, $table_alias, $permissions, $expected)
133    {
134        global $cache, $db, $auth, $phpbb_root_path, $phpEx;
135
136        $cache = new phpbb_mock_cache;
137        $db = $this->new_dbal();
138
139        // Create auth mock
140        $auth = $this->createMock('\phpbb\auth\auth');
141        $auth->expects($this->any())
142            ->method('acl_get')
143            ->with($this->stringContains('_'), $this->anything())
144            ->will($this->returnValueMap($permissions));
145        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
146        $lang = new \phpbb\language\language($lang_loader);
147        $user = new \phpbb\user($lang, '\phpbb\datetime');
148        $user->data['user_id'] = $user_id;
149        $config = new \phpbb\config\config(array(
150            'display_unapproved_posts'            => $display_unapproved,
151        ));
152        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
153        $content_visibility = new \phpbb\content_visibility($auth, $config, $phpbb_dispatcher, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE);
154
155        $sql = 'SELECT ' . $mode . '_id
156            FROM ' . $table . '
157            WHERE ' . $content_visibility->get_visibility_sql($mode, $forum_id, $table_alias) . '
158            ORDER BY ' . $mode . '_id ASC';
159        $result = $db->sql_query($sql);
160
161        $this->assertEquals($expected, $db->sql_fetchrowset($result));
162        $db->sql_freeresult($result);
163    }
164}