Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
manager_test
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 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%
37 / 37
100.00% covered (success)
100.00%
1 / 1
1
 test_disable_profilefields
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 test_enable_profilefields
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 test_purge_profilefields
100.00% covered (success)
100.00%
38 / 38
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 manager_test extends phpbb_database_test_case
15{
16    /** @var \phpbb\config\db_text */
17    protected $config_text;
18
19    /** @var \phpbb\db\driver\driver_interface */
20    protected $db;
21
22    /** @var \Doctrine\DBAL\Connection */
23    protected $db_doctrine;
24
25    /** @var \phpbb\db\tools\doctrine */
26    protected $db_tools;
27
28    /** @var \phpbb\log\log_interface */
29    protected $log;
30
31    /** @var \phpbb\profilefields\manager */
32    protected $manager;
33
34    /** @var string Table prefix */
35    protected $table_prefix;
36
37    /**
38     * {@inheritdoc}
39     */
40    public function getDataSet()
41    {
42        return $this->createXMLDataSet(__DIR__ . '/fixtures/manager.xml');
43    }
44
45    protected function setUp(): void
46    {
47        parent::setUp();
48
49        global $phpbb_root_path, $phpEx, $table_prefix;
50
51        $this->db            = $this->new_dbal();
52        $this->db_doctrine    = $this->new_doctrine_dbal();
53        $this->db_tools        = $this->getMockBuilder('\phpbb\db\tools\doctrine')
54            ->setConstructorArgs([$this->db_doctrine])
55            ->getMock();
56        $this->config_text    = new \phpbb\config\db_text($this->db, $table_prefix . 'config_text');
57        $this->table_prefix    = $table_prefix;
58
59        $container    = new phpbb_mock_container_builder();
60        $dispatcher    = new phpbb_mock_event_dispatcher();
61
62        $template    = $this->getMockBuilder('\phpbb\template\template')
63            ->disableOriginalConstructor()
64            ->getMock();
65
66        $auth        = new \phpbb\auth\auth();
67        $language    = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
68        $collection = new \phpbb\di\service_collection($container);
69        $user        = new \phpbb\user($language, '\phpbb\datetime');
70        $user->data['user_id'] = 2;
71        $user->ip = '';
72
73        $this->log    = new \phpbb\log\log($this->db, $user, $auth, $dispatcher, $phpbb_root_path, 'adm/', $phpEx, $table_prefix . 'log');
74
75        $this->manager = new \phpbb\profilefields\manager(
76            $auth,
77            $this->config_text,
78            $this->db,
79            $this->db_tools,
80            $dispatcher,
81            $language,
82            $this->log,
83            $template,
84            $collection,
85            $user,
86            $table_prefix . 'profile_fields',
87            $table_prefix . 'profile_fields_data',
88            $table_prefix . 'profile_fields_lang',
89            $table_prefix . 'profile_lang'
90        );
91    }
92
93    public function test_disable_profilefields()
94    {
95        // Disable the profile field type
96        $this->manager->disable_profilefields('foo_bar_type');
97
98        $sql = 'SELECT field_id, field_ident
99            FROM ' . $this->table_prefix . "profile_fields
100            WHERE field_active = 1
101                AND field_type = 'foo_bar_type'";
102        $this->assertSqlResultEquals([], $sql, 'All profile fields should be disabled');
103
104        // Test that the config entry exists
105        $saved = $this->config_text->get('foo_bar_type.saved');
106        $saved = (array) json_decode($saved, true);
107        $this->assertEquals([
108            1    => 'pf_1',
109            2    => 'pf_2',
110        ], $saved, 'All disable profile fields should be saved');
111    }
112
113    public function test_enable_profilefields()
114    {
115        // Enable the profile field type
116        $this->manager->enable_profilefields('foo_bar_type');
117
118        $sql = 'SELECT field_id
119            FROM ' . $this->table_prefix . "profile_fields
120            WHERE field_active = 1
121                AND field_type = 'foo_bar_type'
122            ORDER BY field_id ASC";
123        $this->assertSqlResultEquals([
124            ['field_id' => '1'],
125            ['field_id' => '2'],
126        ], $sql, 'All profile fields should be enabled');
127
128        // Test that the config entry was removed
129        $saved = $this->config_text->get('foo_bar_type.saved');
130        $this->assertEquals($saved, null, 'All disable profile fields should be removed');
131    }
132
133    public function test_purge_profilefields()
134    {
135        $this->db_tools
136            ->expects($this->exactly(2))
137            ->method('sql_column_remove')
138            ->with(
139                $this->table_prefix . 'profile_fields_data',
140                $this->stringStartsWith('pf_')
141            );
142
143        // Get the field identifiers
144        $sql = 'SELECT field_id
145            FROM ' . $this->table_prefix . "profile_fields
146            WHERE field_type = 'foo_bar_type'";
147        $result = $this->db->sql_query($sql);
148        $rowset = $this->db->sql_fetchrowset($result);
149        $this->db->sql_freeresult($result);
150
151        $field_ids = array_map('intval', array_column($rowset, 'field_id'));
152
153        // Purge the profile field type
154        $this->manager->purge_profilefields('foo_bar_type');
155
156        // Test all the profile field tables
157        $sql = 'SELECT field_id
158            FROM ' . $this->table_prefix . "profile_fields
159            WHERE field_type = 'foo_bar_type'";
160        $this->assertSqlResultEquals([], $sql, 'All profile fields should be removed');
161
162        $sql = 'SELECT field_id
163            FROM ' . $this->table_prefix . "profile_fields_lang
164            WHERE field_type = 'foo_bar_type'";
165        $this->assertSqlResultEquals([], $sql, 'All profile fields lang should be removed');
166
167        $sql = 'SELECT lang_name
168            FROM ' . $this->table_prefix . 'profile_lang
169            WHERE ' . $this->db->sql_in_set('field_id', $field_ids);
170        $this->assertSqlResultEquals([], $sql, 'All profile fields lang should be removed');
171
172        $sql = 'SELECT field_id, field_order
173            FROM ' . $this->table_prefix . 'profile_fields
174            ORDER BY field_id ASC';
175        $this->assertSqlResultEquals([
176            [
177                'field_id'        => '3',
178                 'field_order'    => '1'
179            ]
180        ], $sql, 'Profile fields order should be recalculated, starting by 1');
181
182        // Test that the config entry was removed
183        $saved = $this->config_text->get('foo_bar_type.saved');
184        $this->assertEquals($saved, null, 'All disable profile fields should be removed');
185    }
186}