Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.35% covered (warning)
57.35%
78 / 136
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_dbal_write_test
57.35% covered (warning)
57.35%
78 / 136
62.50% covered (warning)
62.50%
5 / 8
20.39
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
 build_array_insert_data
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 test_build_array_insert
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 test_delete
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 test_delete_rollback
82.35% covered (warning)
82.35%
28 / 34
0.00% covered (danger)
0.00%
0 / 1
4.09
 test_multiple_insert
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 update_data
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
2
 test_update
100.00% covered (success)
100.00%
9 / 9
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_dbal_write_test extends phpbb_database_test_case
15{
16    public function getDataSet()
17    {
18        return $this->createXMLDataSet(__DIR__.'/fixtures/config.xml');
19    }
20
21    public static function build_array_insert_data()
22    {
23        return array(
24            array(array(
25                'config_name'    => 'test_version',
26                'config_value'    => '0.0.0',
27                'is_dynamic'    => 1,
28            )),
29            array(array(
30                'config_name'    => 'second config',
31                'config_value'    => '10',
32                'is_dynamic'    => 0,
33            )),
34        );
35    }
36
37    /**
38    * @dataProvider build_array_insert_data
39    */
40    public function test_build_array_insert($sql_ary)
41    {
42        $db = $this->new_dbal();
43
44        $sql = 'INSERT INTO phpbb_config ' . $db->sql_build_array('INSERT', $sql_ary);
45        $result = $db->sql_query($sql);
46
47        $sql = "SELECT *
48            FROM phpbb_config
49            WHERE config_name = '" . $sql_ary['config_name'] . "'";
50        $result = $db->sql_query_limit($sql, 1);
51
52        $this->assertEquals($sql_ary, $db->sql_fetchrow($result));
53
54        $db->sql_freeresult($result);
55    }
56
57    public function test_delete()
58    {
59        $db = $this->new_dbal();
60
61        $sql = "DELETE FROM phpbb_config
62            WHERE config_name = 'config1'";
63        $result = $db->sql_query($sql);
64
65        $sql = 'SELECT *
66            FROM phpbb_config';
67        $result = $db->sql_query($sql);
68        $rows = $db->sql_fetchrowset($result);
69
70        $this->assertEquals(1, count($rows));
71        $this->assertEquals('config2', $rows[0]['config_name']);
72
73        $db->sql_freeresult($result);
74    }
75
76    public function test_delete_rollback()
77    {
78        $db = $this->new_dbal();
79
80        $is_myisam = false;
81        if ($db->get_sql_layer() === 'mysqli')
82        {
83            $table_status = $db->get_table_status('phpbb_config');
84            $is_myisam = isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM';
85        }
86
87        $db->sql_transaction('begin');
88
89        $sql = "DELETE FROM phpbb_config
90            WHERE config_name = 'config1'";
91        $db->sql_query($sql);
92
93        // Rollback and check that nothing was changed
94        $db->sql_transaction('rollback');
95
96        $sql = 'SELECT *
97            FROM phpbb_config';
98        $result = $db->sql_query($sql);
99        $rows = $db->sql_fetchrowset($result);
100        $db->sql_freeresult($result);
101
102        if (!$is_myisam)
103        {
104            $this->assertEquals(2, count($rows));
105            $this->assertEquals('config1', $rows[0]['config_name']);
106        }
107        else
108        {
109            // Rollback does not work on MyISAM
110            $this->assertEquals(1, count($rows));
111            $this->assertEquals('config2', $rows[0]['config_name']);
112
113            // Restore deleted config value on MyISAM
114            $sql = "INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('config1', 'foo', 0)";
115            $db->sql_query($sql);
116        }
117
118        $db->sql_transaction('begin');
119
120        $sql = "DELETE FROM phpbb_config
121            WHERE config_name = 'config1'";
122        $db->sql_query($sql);
123
124        // Commit and check that data was actually changed
125        $db->sql_transaction('commit');
126
127        $sql = 'SELECT *
128            FROM phpbb_config';
129        $result = $db->sql_query($sql);
130        $rows = $db->sql_fetchrowset($result);
131        $db->sql_freeresult($result);
132
133        $this->assertEquals(1, count($rows));
134        $this->assertEquals('config2', $rows[0]['config_name']);
135    }
136
137    public function test_multiple_insert()
138    {
139        $db = $this->new_dbal();
140
141        // empty the table
142        $sql = 'DELETE FROM phpbb_config';
143        $db->sql_query($sql);
144
145        $batch_ary = array(
146            array(
147                'config_name'    => 'batch one',
148                'config_value'    => 'b1',
149                'is_dynamic'    => 0,
150            ),
151            array(
152                'config_name'    => 'batch two',
153                'config_value'    => 'b2',
154                'is_dynamic'    => 1,
155            ),
156        );
157
158        $result = $db->sql_multi_insert('phpbb_config', $batch_ary);
159
160        $sql = 'SELECT *
161            FROM phpbb_config
162            ORDER BY config_name ASC';
163        $result = $db->sql_query($sql);
164
165        $this->assertEquals($batch_ary, $db->sql_fetchrowset($result));
166
167        $db->sql_freeresult($result);
168    }
169
170    public static function update_data()
171    {
172        return array(
173            array(
174                array(
175                    'config_value'    => '23',
176                    'is_dynamic'    => 0,
177                ),
178                " WHERE config_name = 'config1'",
179                array(
180                    array(
181                        'config_name'    => 'config1',
182                        'config_value'    => '23',
183                        'is_dynamic'    => 0,
184                    ),
185                    array(
186                        'config_name'    => 'config2',
187                        'config_value'    => 'bar',
188                        'is_dynamic'    => 1,
189                    ),
190                ),
191            ),
192            array(
193                array(
194                    'config_value'    => '0',
195                    'is_dynamic'    => 1,
196                ),
197                '',
198                array(
199                    array(
200                        'config_name'    => 'config1',
201                        'config_value'    => '0',
202                        'is_dynamic'    => 1,
203                    ),
204                    array(
205                        'config_name'    => 'config2',
206                        'config_value'    => '0',
207                        'is_dynamic'    => 1,
208                    ),
209                ),
210            ),
211        );
212    }
213
214    /**
215    * @dataProvider update_data
216    */
217    public function test_update($sql_ary, $where, $expected)
218    {
219        $db = $this->new_dbal();
220
221        $sql = 'UPDATE phpbb_config
222            SET ' . $db->sql_build_array('UPDATE', $sql_ary) . $where;
223        $result = $db->sql_query($sql);
224
225        $sql = 'SELECT *
226            FROM phpbb_config
227            ORDER BY config_name ASC';
228        $result = $db->sql_query($sql);
229
230        $this->assertEquals($expected, $db->sql_fetchrowset($result));
231
232        $db->sql_freeresult($result);
233    }
234}