1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Tests; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\Config; |
8
|
|
|
use PhpMyAdmin\Core; |
9
|
|
|
use PhpMyAdmin\DatabaseInterface; |
10
|
|
|
use PhpMyAdmin\Language; |
11
|
|
|
use PhpMyAdmin\LanguageManager; |
12
|
|
|
use PhpMyAdmin\SqlParser\Translator; |
13
|
|
|
use PhpMyAdmin\Tests\Stubs\DbiDummy; |
14
|
|
|
use PhpMyAdmin\Tests\Stubs\Response; |
15
|
|
|
use PhpMyAdmin\Theme; |
16
|
|
|
use PhpMyAdmin\Utils\HttpRequest; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
|
20
|
|
|
use function in_array; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Abstract class to hold some usefull methods used in tests |
24
|
|
|
* And make tests clean |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractTestCase extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The variables to keep between tests |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $globalsAllowList = [ |
34
|
|
|
'__composer_autoload_files', |
35
|
|
|
'GLOBALS', |
36
|
|
|
'_SERVER', |
37
|
|
|
'__composer_autoload_files', |
38
|
|
|
'__PHPUNIT_CONFIGURATION_FILE', |
39
|
|
|
'__PHPUNIT_BOOTSTRAP', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The DatabaseInterface loaded by setGlobalDbi |
44
|
|
|
* |
45
|
|
|
* @var DatabaseInterface |
46
|
|
|
*/ |
47
|
|
|
protected $dbi; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The DbiDummy loaded by setGlobalDbi |
51
|
|
|
* |
52
|
|
|
* @var DbiDummy |
53
|
|
|
*/ |
54
|
|
|
protected $dummyDbi; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prepares environment for the test. |
58
|
|
|
* Clean all variables |
59
|
|
|
*/ |
60
|
|
|
protected function setUp(): void |
61
|
|
|
{ |
62
|
|
|
foreach ($GLOBALS as $key => $val) { |
63
|
|
|
if (in_array($key, $this->globalsAllowList)) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
unset($GLOBALS[$key]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$_GET = []; |
71
|
|
|
$_POST = []; |
72
|
|
|
$_SERVER = [ |
73
|
|
|
// https://github.com/sebastianbergmann/phpunit/issues/4033 |
74
|
|
|
'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
75
|
|
|
'REQUEST_TIME' => $_SERVER['REQUEST_TIME'], |
76
|
|
|
'REQUEST_TIME_FLOAT' => $_SERVER['REQUEST_TIME_FLOAT'], |
77
|
|
|
'PHP_SELF' => $_SERVER['PHP_SELF'], |
78
|
|
|
'argv' => $_SERVER['argv'], |
79
|
|
|
]; |
80
|
|
|
$_SESSION = [' PMA_token ' => 'token']; |
81
|
|
|
$_COOKIE = []; |
82
|
|
|
$_FILES = []; |
83
|
|
|
$_REQUEST = []; |
84
|
|
|
// Config before DBI |
85
|
|
|
$this->setGlobalConfig(); |
86
|
|
|
$this->setGlobalDbi(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function loadDefaultConfig(): void |
90
|
|
|
{ |
91
|
|
|
global $cfg; |
92
|
|
|
|
93
|
|
|
require ROOT_PATH . 'libraries/config.default.php'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function assertAllQueriesConsumed(): void |
97
|
|
|
{ |
98
|
|
|
if ($this->dummyDbi->hasUnUsedQueries() === false) { |
99
|
|
|
$this->assertTrue(true);// increment the assertion count |
100
|
|
|
|
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->fail('Some queries where no used !'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function loadContainerBuilder(): void |
108
|
|
|
{ |
109
|
|
|
global $containerBuilder; |
110
|
|
|
|
111
|
|
|
$containerBuilder = Core::getContainerBuilder(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function loadDbiIntoContainerBuilder(): void |
115
|
|
|
{ |
116
|
|
|
global $containerBuilder, $dbi; |
117
|
|
|
|
118
|
|
|
$containerBuilder->set(DatabaseInterface::class, $dbi); |
119
|
|
|
$containerBuilder->setAlias('dbi', DatabaseInterface::class); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function loadResponseIntoContainerBuilder(): void |
123
|
|
|
{ |
124
|
|
|
global $containerBuilder; |
125
|
|
|
|
126
|
|
|
$response = new Response(); |
127
|
|
|
$containerBuilder->set(Response::class, $response); |
128
|
|
|
$containerBuilder->setAlias('response', Response::class); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getResponseHtmlResult(): string |
132
|
|
|
{ |
133
|
|
|
global $containerBuilder; |
134
|
|
|
|
135
|
|
|
/** @var Response $response */ |
136
|
|
|
$response = $containerBuilder->get(Response::class); |
137
|
|
|
|
138
|
|
|
return $response->getHTMLResult(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getResponseJsonResult(): array |
142
|
|
|
{ |
143
|
|
|
global $containerBuilder; |
144
|
|
|
|
145
|
|
|
/** @var Response $response */ |
146
|
|
|
$response = $containerBuilder->get(Response::class); |
147
|
|
|
|
148
|
|
|
return $response->getJSONResult(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function setGlobalDbi(): void |
152
|
|
|
{ |
153
|
|
|
global $dbi; |
154
|
|
|
$this->dummyDbi = new DbiDummy(); |
155
|
|
|
$this->dbi = DatabaseInterface::load($this->dummyDbi); |
156
|
|
|
$dbi = $this->dbi; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function setGlobalConfig(): void |
160
|
|
|
{ |
161
|
|
|
global $config; |
162
|
|
|
$config = new Config(); |
163
|
|
|
$config->set('environment', 'development'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function setTheme(): void |
167
|
|
|
{ |
168
|
|
|
global $theme; |
169
|
|
|
$theme = Theme::load('pmahomme'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function setLanguage(string $code = 'en'): void |
173
|
|
|
{ |
174
|
|
|
global $lang; |
175
|
|
|
|
176
|
|
|
$lang = $code; |
177
|
|
|
/* Ensure default language is active */ |
178
|
|
|
/** @var Language $languageEn */ |
179
|
|
|
$languageEn = LanguageManager::getInstance()->getLanguage($code); |
180
|
|
|
$languageEn->activate(); |
181
|
|
|
Translator::load(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function setProxySettings(): void |
185
|
|
|
{ |
186
|
|
|
HttpRequest::setProxySettingsFromEnv(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Desctroys the environment built for the test. |
191
|
|
|
* Clean all variables |
192
|
|
|
*/ |
193
|
|
|
protected function tearDown(): void |
194
|
|
|
{ |
195
|
|
|
foreach ($GLOBALS as $key => $val) { |
196
|
|
|
if (in_array($key, $this->globalsAllowList)) { |
197
|
|
|
continue; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
unset($GLOBALS[$key]); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Call protected functions by setting visibility to public. |
206
|
|
|
* |
207
|
|
|
* @param object|null $object The object to inspect, pass null for static objects() |
208
|
|
|
* @param string $className The class name |
209
|
|
|
* @param string $methodName The method name |
210
|
|
|
* @param array $params The parameters for the invocation |
211
|
|
|
* @phpstan-param class-string $className |
212
|
|
|
* |
213
|
|
|
* @return mixed the output from the protected method. |
214
|
|
|
*/ |
215
|
|
|
protected function callFunction($object, string $className, string $methodName, array $params) |
216
|
|
|
{ |
217
|
|
|
$class = new ReflectionClass($className); |
218
|
|
|
$method = $class->getMethod($methodName); |
219
|
|
|
$method->setAccessible(true); |
220
|
|
|
|
221
|
|
|
return $method->invokeArgs($object, $params); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|