Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
36.11% covered (danger)
36.11%
52 / 144
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_log_function_add_log_test
36.11% covered (danger)
36.11%
52 / 144
33.33% covered (danger)
33.33%
1 / 3
49.55
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
 add_log_function_data
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 1
2
 test_add_log_function
92.73% covered (success)
92.73%
51 / 55
0.00% covered (danger)
0.00%
0 / 1
10.04
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_log_function_add_log_test extends phpbb_database_test_case
15{
16    public function getDataSet()
17    {
18        return $this->createXMLDataSet(__DIR__ . '/fixtures/empty_log.xml');
19    }
20
21    public static function add_log_function_data()
22    {
23        return array(
24            /**
25            * Case documentation
26            array(
27                // Row that is in the database afterwards
28                array(
29                    'user_id'        => ANONYMOUS,
30                    'log_type'        => LOG_MOD,
31                    'log_operation'    => 'LOG_MOD_ADDITIONAL',
32                    // log_data will be serialized
33                    'log_data'        => array(
34                        'argument3',
35                    ),
36                    'reportee_id'    => 0,
37                    'forum_id'        => 56,
38                    'topic_id'        => 78,
39                ),
40                // user_id        Can also be false, then ANONYMOUS is used
41                false,
42                // log_mode        Used to determine the log_type
43                'mod',
44                // Followed by some additional arguments
45                // forum_id, topic_id and reportee_id are specified before log_operation
46                // The rest is specified afterwards.
47                56,
48                78,
49                'LOG_MOD_ADDITIONAL', // log_operation
50                'argument3',
51            ),
52            */
53            array(
54                array(
55                    'user_id'        => 2,
56                    'log_type'        => LOG_CRITICAL,
57                    'log_operation'    => 'LOG_NO_ADDITIONAL',
58                    'log_data'        => '',
59                    'reportee_id'    => 0,
60                    'forum_id'        => 0,
61                    'topic_id'        => 0,
62                ),
63                2, 'critical', 'LOG_NO_ADDITIONAL',
64            ),
65            array(
66                array(
67                    'user_id'        => 2,
68                    'log_type'        => LOG_CRITICAL,
69                    'log_operation'    => 'LOG_ONE_ADDITIONAL',
70                    'log_data'        => array(
71                        'argument1',
72                    ),
73                    'reportee_id'    => 0,
74                    'forum_id'        => 0,
75                    'topic_id'        => 0,
76                ),
77                2, 'critical', 'LOG_ONE_ADDITIONAL', 'argument1',
78            ),
79            array(
80                array(
81                    'user_id'        => ANONYMOUS,
82                    'log_type'        => LOG_ADMIN,
83                    'log_operation'    => 'LOG_TWO_ADDITIONAL',
84                    'log_data'        => array(
85                        'argument1',
86                        'argument2',
87                    ),
88                    'reportee_id'    => 0,
89                    'forum_id'        => 0,
90                    'topic_id'        => 0,
91                ),
92                false, 'admin', 'LOG_TWO_ADDITIONAL', 'argument1', 'argument2',
93            ),
94            array(
95                array(
96                    'user_id'        => ANONYMOUS,
97                    'log_type'        => LOG_USERS,
98                    'log_operation'    => 'LOG_USERS_ADDITIONAL',
99                    'log_data'        => array(
100                        'argument2',
101                    ),
102                    'reportee_id'    => 2,
103                    'forum_id'        => 0,
104                    'topic_id'        => 0,
105                ),
106                false, 'user', 2, 'LOG_USERS_ADDITIONAL', 'argument2',
107            ),
108            array(
109                array(
110                    'user_id'        => ANONYMOUS,
111                    'log_type'        => LOG_MOD,
112                    'log_operation'    => 'LOG_MOD_TOPIC_AND_FORUM',
113                    'log_data'        => '',
114                    'reportee_id'    => 0,
115                    'forum_id'        => 12,
116                    'topic_id'        => 34,
117                ),
118                false, 'mod', 12, 34, 'LOG_MOD_TOPIC_AND_FORUM',
119            ),
120            array(
121                array(
122                    'user_id'        => ANONYMOUS,
123                    'log_type'        => LOG_MOD,
124                    'log_operation'    => 'LOG_MOD_ADDITIONAL',
125                    'log_data'        => array(
126                        'argument3',
127                    ),
128                    'reportee_id'    => 0,
129                    'forum_id'        => 56,
130                    'topic_id'        => 78,
131                ),
132                false, 'mod', 56, 78, 'LOG_MOD_ADDITIONAL', 'argument3',
133            ),
134            array(
135                array(
136                ),
137                false, 'mode_does_not_exist', 'LOG_MOD_ADDITIONAL', 'argument1',
138            ),
139        );
140    }
141
142    /**
143    * @dataProvider add_log_function_data
144    */
145    public function test_add_log_function($expected, $user_id, $mode, $required1, $additional1 = null, $additional2 = null, $additional3 = null)
146    {
147        global $db, $cache, $user, $phpbb_log, $phpbb_dispatcher, $phpbb_root_path, $phpEx;
148
149        if ($expected)
150        {
151            // Serialize the log data if we have some
152            if (is_array($expected['log_data']))
153            {
154                $expected['log_data'] = serialize($expected['log_data']);
155            }
156            $expected = array($expected);
157        }
158
159        $db = $this->new_dbal();
160        $cache = new phpbb_mock_cache;
161        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
162        $user = $this->createMock('\phpbb\user');
163        $auth = $this->createMock('\phpbb\auth\auth');
164
165        $phpbb_log = new \phpbb\log\log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE);
166
167        $user->ip = 'user_ip';
168        if ($user_id)
169        {
170            $user->data['user_id'] = $user_id;
171        }
172
173        if ($additional3 != null)
174        {
175            $additional_data = [
176                'forum_id'    => $required1,
177                'topic_id'    => $additional1,
178                $additional3,
179            ];
180            $phpbb_log->add($mode, $user_id, '', $additional2, false, $additional_data);
181        }
182        else if ($additional2 != null)
183        {
184            if ($mode == 'user')
185            {
186                $additional_data = [
187                    'reportee_id'    => $required1,
188                    $additional2,
189                ];
190                $log_operation = $additional1;
191            }
192            else if ($mode == 'mod')
193            {
194                $additional_data = [
195                    'forum_id'    => $required1,
196                    'topic_id'    => $additional1,
197                ];
198                $log_operation = $additional2;
199            }
200            else
201            {
202                $log_operation = $required1;
203                $additional_data = [
204                    $additional1,
205                    $additional2,
206                ];
207            }
208            $phpbb_log->add($mode, $user_id, '', $log_operation, false, $additional_data);
209        }
210        else if ($additional1 != null)
211        {
212            if ($mode == 'user')
213            {
214                $additional_data = [
215                    'reportee_id'    => $required1,
216                ];
217                $log_operation = $additional1;
218            }
219            else
220            {
221                $log_operation = $required1;
222                $additional_data = [
223                    $additional1,
224                ];
225            }
226
227            $phpbb_log->add($mode, $user_id, '', $log_operation, false, $additional_data);
228        }
229        else
230        {
231            $phpbb_log->add($mode, $user_id, '', $required1);
232        }
233
234        $result = $db->sql_query('SELECT user_id, log_type, log_operation, log_data, reportee_id, forum_id, topic_id
235            FROM ' . LOG_TABLE);
236
237        $this->assertEquals($expected, $db->sql_fetchrowset($result));
238    }
239}