Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
86 / 86
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_functions_user_delete_test
100.00% covered (success)
100.00%
86 / 86
100.00% covered (success)
100.00%
3 / 3
3
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%
72 / 72
100.00% covered (success)
100.00%
1 / 1
1
 test_user_delete
100.00% covered (success)
100.00%
13 / 13
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
14require_once __DIR__ . '/../../phpBB/includes/functions_user.php';
15
16class phpbb_functions_user_delete_test extends phpbb_database_test_case
17{
18    /** @var \phpbb\db\driver\driver_interface */
19    protected $db;
20
21    public function getDataSet()
22    {
23        return $this->createXMLDataSet(__DIR__ . '/fixtures/user_delete.xml');
24    }
25
26    protected function setUp(): void
27    {
28        parent::setUp();
29
30        global $cache, $config, $db, $phpbb_container, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx;
31
32        $this->db = $db = $this->new_dbal();
33        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
34        $lang = new \phpbb\language\language($lang_loader);
35        $user = new \phpbb\user($lang, '\phpbb\datetime');
36        $user->data['user_id'] = 2;
37        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
38        $phpbb_container = new phpbb_mock_container_builder();
39        $config = new \phpbb\config\config(array(
40            'auth_method' => 'oauth',
41            'auth_oauth_google_key'    => 'foo',
42            'auth_oauth_google_secret'    => 'bar',
43        ));
44        $cache = new \phpbb\cache\driver\dummy();
45        $request = new phpbb_mock_request();
46        $notification_manager = new phpbb_mock_notification_manager();
47        $provider_collection =  new \phpbb\auth\provider_collection($phpbb_container, $config);
48        $oauth_provider_google = new \phpbb\auth\provider\oauth\service\google($config, $request);
49        $oauth_provider_collection = new \phpbb\di\service_collection($phpbb_container);
50        $oauth_provider_collection->offsetSet('auth.provider.oauth.service.google', $oauth_provider_google);
51
52        $driver_helper = new \phpbb\passwords\driver\helper($config);
53        $passwords_drivers = array(
54            'passwords.driver.bcrypt_2y'    => new \phpbb\passwords\driver\bcrypt_2y($config, $driver_helper),
55            'passwords.driver.bcrypt'        => new \phpbb\passwords\driver\bcrypt($config, $driver_helper),
56            'passwords.driver.salted_md5'    => new \phpbb\passwords\driver\salted_md5($config, $driver_helper),
57            'passwords.driver.phpass'        => new \phpbb\passwords\driver\phpass($config, $driver_helper),
58        );
59
60        $passwords_helper = new \phpbb\passwords\helper;
61        // Set up passwords manager
62        $passwords_manager = new \phpbb\passwords\manager($config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers));
63
64        $plugins = new \phpbb\di\service_collection($phpbb_container);
65        $plugins->add('core.captcha.plugins.qa');
66        $phpbb_container->set(
67            'captcha.factory',
68            new \phpbb\captcha\factory($phpbb_container, $plugins)
69        );
70        $phpbb_container->set(
71            'core.captcha.plugins.qa',
72            new \phpbb\captcha\plugins\qa('', '', '')
73        );
74        // Set up passwords manager
75        $db_auth_provider = new \phpbb\auth\provider\db(
76            new \phpbb\captcha\factory($phpbb_container, $plugins),
77            $config,
78            $db,
79            $passwords_manager,
80            $user
81        );
82
83        $oauth_provider = new \phpbb\auth\provider\oauth\oauth(
84            $config,
85            $db,
86            $db_auth_provider,
87            $phpbb_dispatcher,
88            $lang,
89            $request,
90            $oauth_provider_collection,
91            $user,
92            'phpbb_oauth_tokens',
93            'phpbb_oauth_states',
94            'phpbb_oauth_accounts',
95            'phpbb_users',
96            $phpbb_root_path,
97            $phpEx
98        );
99        $provider_collection->offsetSet('auth.provider.oauth', $oauth_provider);
100
101        $phpbb_container->set('auth.provider.oauth', $oauth_provider);
102        $phpbb_container->set('auth.provider.oauth.service.google', $oauth_provider_google);
103        $phpbb_container->set('auth.provider_collection', $provider_collection);
104        $phpbb_container->set('notification_manager', $notification_manager);
105
106        $phpbb_container->setParameter('tables.auth_provider_oauth_token_storage', 'phpbb_oauth_tokens');
107        $phpbb_container->setParameter('tables.auth_provider_oauth_states', 'phpbb_oauth_states');
108        $phpbb_container->setParameter('tables.auth_provider_oauth_account_assoc', 'phpbb_oauth_accounts');
109
110        $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications');
111    }
112
113    public function test_user_delete()
114    {
115        // Check that user is linked
116        $sql = 'SELECT ot.user_id AS user_id
117            FROM phpbb_oauth_accounts oa, phpbb_oauth_tokens ot
118            WHERE oa.user_id = 2
119                AND ot.user_id = oa.user_id';
120        $result = $this->db->sql_query($sql);
121        $row = $this->db->sql_fetchrow($result);
122        $this->db->sql_freeresult($result);
123
124        $this->assertEquals(array('user_id' => '2'), $row);
125
126        // user_delete() should return false
127        $this->assertFalse(user_delete('remove', array(2)));
128
129        // Make sure user link was removed
130        $sql = 'SELECT ot.user_id AS user_id
131            FROM phpbb_oauth_accounts oa, phpbb_oauth_tokens ot
132            WHERE oa.user_id = 2
133                AND ot.user_id = oa.user_id';
134        $result = $this->db->sql_query($sql);
135        $row = $this->db->sql_fetchrow($result);
136        $this->db->sql_freeresult($result);
137
138        $this->assertEmpty($row);
139    }
140}