aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functions/user_delete_test.php
blob: f4ea5696b95914a8e397e1cf412ab46016de0eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php';

class phpbb_functions_user_delete_test extends phpbb_database_test_case
{
	/** @var \phpbb\db\driver\driver_interface */
	protected $db;

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/user_delete.xml');
	}

	protected function setUp(): void
	{
		parent::setUp();

		global $cache, $config, $db, $phpbb_container, $phpbb_dispatcher, $user, $phpbb_root_path, $phpEx;

		$this->db = $db = $this->new_dbal();
		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
		$lang = new \phpbb\language\language($lang_loader);
		$user = new \phpbb\user($lang, '\phpbb\datetime');
		$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
		$phpbb_container = new phpbb_mock_container_builder();
		$config = new \phpbb\config\config(array(
			'auth_method' => 'oauth',
			'auth_oauth_google_key'	=> 'foo',
			'auth_oauth_google_secret'	=> 'bar',
		));
		$cache = new \phpbb\cache\driver\dummy();
		$request = new phpbb_mock_request();
		$notification_manager = new phpbb_mock_notification_manager();
		$provider_collection =  new \phpbb\auth\provider_collection($phpbb_container, $config);
		$oauth_provider_google = new \phpbb\auth\provider\oauth\service\google($config, $request);
		$oauth_provider_collection = new \phpbb\di\service_collection($phpbb_container);
		$oauth_provider_collection->offsetSet('auth.provider.oauth.service.google', $oauth_provider_google);

		$driver_helper = new \phpbb\passwords\driver\helper($config);
		$passwords_drivers = array(
			'passwords.driver.bcrypt_2y'	=> new \phpbb\passwords\driver\bcrypt_2y($config, $driver_helper),
			'passwords.driver.bcrypt'		=> new \phpbb\passwords\driver\bcrypt($config, $driver_helper),
			'passwords.driver.salted_md5'	=> new \phpbb\passwords\driver\salted_md5($config, $driver_helper),
			'passwords.driver.phpass'		=> new \phpbb\passwords\driver\phpass($config, $driver_helper),
		);

		$passwords_helper = new \phpbb\passwords\helper;
		// Set up passwords manager
		$passwords_manager = new \phpbb\passwords\manager($config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers));

		$plugins = new \phpbb\di\service_collection($phpbb_container);
		$plugins->add('core.captcha.plugins.nogd');
		$phpbb_container->set(
			'captcha.factory',
			new \phpbb\captcha\factory($phpbb_container, $plugins)
		);
		$phpbb_container->set(
			'core.captcha.plugins.nogd',
			new \phpbb\captcha\plugins\nogd()
		);
		// Set up passwords manager
		$db_auth_provider = new \phpbb\auth\provider\db(
			new \phpbb\captcha\factory($phpbb_container, $plugins),
			$config,
			$db,
			$passwords_manager,
			$request,
			$user,
			$phpbb_root_path,
			$phpEx
		);

		$oauth_provider = new \phpbb\auth\provider\oauth\oauth(
			$config,
			$db,
			$db_auth_provider,
			$phpbb_dispatcher,
			$lang,
			$request,
			$oauth_provider_collection,
			$user,
			'phpbb_oauth_tokens',
			'phpbb_oauth_states',
			'phpbb_oauth_accounts',
			'phpbb_users',
			$phpbb_root_path,
			$phpEx
		);
		$provider_collection->offsetSet('auth.provider.oauth', $oauth_provider);

		$phpbb_container->set('auth.provider.oauth', $oauth_provider);
		$phpbb_container->set('auth.provider.oauth.service.google', $oauth_provider_google);
		$phpbb_container->set('auth.provider_collection', $provider_collection);
		$phpbb_container->set('notification_manager', $notification_manager);

		$phpbb_container->setParameter('tables.auth_provider_oauth_token_storage', 'phpbb_oauth_tokens');
		$phpbb_container->setParameter('tables.auth_provider_oauth_states', 'phpbb_oauth_states');
		$phpbb_container->setParameter('tables.auth_provider_oauth_account_assoc', 'phpbb_oauth_accounts');

		$phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications');
	}

	public function test_user_delete()
	{
		// Check that user is linked
		$sql = 'SELECT ot.user_id AS user_id
			FROM phpbb_oauth_accounts oa, phpbb_oauth_tokens ot
			WHERE oa.user_id = 2
				AND ot.user_id = oa.user_id';
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		$this->assertEquals(array('user_id' => '2'), $row);

		// user_delete() should return false
		$this->assertFalse(user_delete('remove', array(2)));

		// Make sure user link was removed
		$sql = 'SELECT ot.user_id AS user_id
			FROM phpbb_oauth_accounts oa, phpbb_oauth_tokens ot
			WHERE oa.user_id = 2
				AND ot.user_id = oa.user_id';
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		$this->assertEmpty($row);
	}
}