Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.70% covered (success)
90.70%
39 / 43
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_tests_tree_nestedset_forum_base
90.70% covered (success)
90.70%
39 / 43
75.00% covered (warning)
75.00%
3 / 4
7.04
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
 setUp
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 set_up_forums
87.88% covered (warning)
87.88%
29 / 33
0.00% covered (danger)
0.00%
0 / 1
4.03
 create_forum
100.00% covered (success)
100.00%
2 / 2
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_tests_tree_nestedset_forum_base extends phpbb_database_test_case
15{
16    public function getDataSet()
17    {
18        return $this->createXMLDataSet(__DIR__ . '/fixtures/phpbb_forums.xml');
19    }
20
21    protected $forum_data = array(
22        // \__/
23        1    => array('forum_id' => 1, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'),
24        2    => array('forum_id' => 2, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'),
25        3    => array('forum_id' => 3, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'),
26
27        // \  /
28        //  \/
29        4    => array('forum_id' => 4, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'),
30        5    => array('forum_id' => 5, 'parent_id' => 4, 'user_id' => 0, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'),
31        6    => array('forum_id' => 6, 'parent_id' => 5, 'user_id' => 0, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'),
32
33        // \_  _/
34        //   \/
35        7    => array('forum_id' => 7, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'),
36        8    => array('forum_id' => 8, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'),
37        9    => array('forum_id' => 9, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'),
38        10    => array('forum_id' => 10, 'parent_id' => 9, 'user_id' => 0, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'),
39        11    => array('forum_id' => 11, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'),
40
41        // Non-existent forums
42        0    => array('forum_id' => 0, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'),
43        200    => array('forum_id' => 200, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'),
44    );
45
46    protected $set,
47        $config,
48        $lock,
49        $db;
50
51    protected function setUp(): void
52    {
53        parent::setUp();
54
55        $this->db = $this->new_dbal();
56
57        global $config;
58
59        $config = $this->config = new \phpbb\config\config(array('nestedset_forum_lock' => 0));
60
61        $this->lock = new \phpbb\lock\db('nestedset_forum_lock', $this->config, $this->db);
62        $this->set = new \phpbb\tree\nestedset_forum($this->db, $this->lock, 'phpbb_forums');
63
64        $this->set_up_forums();
65    }
66
67    protected function set_up_forums()
68    {
69        static $forums;
70
71        if (empty($forums))
72        {
73            $this->create_forum('Parent with two flat children');
74            $this->create_forum('Flat child #1', 1);
75            $this->create_forum('Flat child #2', 1);
76
77            $this->create_forum('Parent with two nested children');
78            $this->create_forum('Nested child #1', 4);
79            $this->create_forum('Nested child #2', 5);
80
81            $this->create_forum('Parent with flat and nested children');
82            $this->create_forum('Mixed child #1', 7);
83            $this->create_forum('Mixed child #2', 7);
84            $this->create_forum('Nested child #1 of Mixed child #2', 9);
85            $this->create_forum('Mixed child #3', 7);
86
87            // Updating forum_parents column here so it's not empty
88            // This is required, so we can see whether the methods
89            // correctly clear the values.
90            $sql = "UPDATE phpbb_forums
91                SET forum_parents = 'a:0:{}'";
92            $this->db->sql_query($sql);
93
94            // Copy the forums into a static array, so we can reuse the list later
95            $sql = 'SELECT *
96                FROM phpbb_forums';
97            $result = $this->db->sql_query($sql);
98            $forums = $this->db->sql_fetchrowset($result);
99            $this->db->sql_freeresult($result);
100        }
101        else
102        {
103            // Turn on identity insert on mssql to be able to insert into
104            // identity columns (e.g. forum_id)
105            if (strpos($this->db->sql_layer, 'mssql') !== false)
106            {
107                $sql = 'SET IDENTITY_INSERT phpbb_forums ON';
108                $this->db->sql_query($sql);
109            }
110            $buffer = new \phpbb\db\sql_insert_buffer($this->db, 'phpbb_forums');
111            $buffer->insert_all($forums);
112            $buffer->flush();
113
114            $this->database_synchronisation(array(
115                'phpbb_forums'    => array('forum_id'),
116            ));
117
118            // Disable identity insert on mssql again
119            if (strpos($this->db->sql_layer, 'mssql') !== false)
120            {
121                $sql = 'SET IDENTITY_INSERT phpbb_forums OFF';
122                $this->db->sql_query($sql);
123            }
124        }
125    }
126
127    protected function create_forum($name, $parent_id = 0)
128    {
129        $forum = $this->set->insert(array('forum_name' => $name, 'forum_desc' => '', 'forum_rules' => ''));
130        $this->set->change_parent($forum['forum_id'], $parent_id);
131    }
132}