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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Import;
use PhpMyAdmin\Controllers\Import\SimulateDmlController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Import\SimulateDml;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\DeleteStatement;
use PhpMyAdmin\SqlParser\Statements\UpdateStatement;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PhpMyAdmin\Url;
use function count;
/**
* @covers \PhpMyAdmin\Controllers\Import\SimulateDmlController
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Controllers\Import\SimulateDmlController::class)]
class SimulateDmlControllerTest extends AbstractTestCase
{
/**
* @param array<array<mixed>> $expectedPerQuery
* @psalm-param list<
* array{
* simulated: string,
* columns: list<string>,
* result: list<list<string|int|null>>,
* }
* > $expectedPerQuery
*
* @dataProvider providerForTestGetMatchedRows
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerForTestGetMatchedRows')]
public function testGetMatchedRows(string $sqlQuery, array $expectedPerQuery): void
{
$GLOBALS['db'] = 'PMA';
foreach ($expectedPerQuery as $expected) {
$this->dummyDbi->addSelectDb('PMA');
$this->dummyDbi->addResult($expected['simulated'], $expected['result'], $expected['columns']);
}
$controller = new SimulateDmlController(
new ResponseRenderer(),
new Template(),
new SimulateDml($this->dbi)
);
/** @var Parser $parser */
$parser = $this->callFunction($controller, SimulateDmlController::class, 'createParser', [$sqlQuery, ';']);
self::assertCount(count($expectedPerQuery), $parser->statements);
$this->callFunction($controller, SimulateDmlController::class, 'process', [$parser]);
$this->assertAllSelectsConsumed();
$this->assertAllQueriesConsumed();
/** @var string $error */
$error = $this->getProperty($controller, SimulateDmlController::class, 'error');
self::assertSame('', $error);
/** @var list<array<mixed>> $result */
$result = $this->getProperty($controller, SimulateDmlController::class, 'data');
foreach ($expectedPerQuery as $idx => $expectedData) {
/** @var DeleteStatement|UpdateStatement $statement */
$statement = $parser->statements[$idx];
$expected = [
'sql_query' => Generator::formatSql($statement->build()),
'matched_rows' => count($expectedData['result']),
'matched_rows_url' => Url::getFromRoute('/sql', [
'db' => 'PMA',
'sql_query' => $expectedData['simulated'],
'sql_signature' => Core::signSqlQuery($expectedData['simulated']),
]),
];
self::assertSame($expected, $result[$idx]);
}
}
/**
* @return array<string, array<mixed>>
* @psalm-return array<
* array{
* string,
* list<array{
* simulated: string,
* columns: list<string>,
* result: list<list<string|int|null>>,
* }>
* }
* >
*/
public static function providerForTestGetMatchedRows(): array
{
// Data from table:
// CREATE TABLE `t` AS
// SELECT 1 AS `id`, 2 AS `a`, 'test' AS `b` UNION ALL
// SELECT 2 AS `id`, 1 AS `a`, NULL AS `b` UNION ALL
// SELECT 3 AS `id`, 1 AS `a`, NULL AS `b` UNION ALL
// SELECT 4 AS `id`, 1 AS `a`, NULL AS `b` UNION ALL
// SELECT 5 AS `id`, 2 AS `a`, 'test' AS `b` UNION ALL
// SELECT 6 AS `id`, 2 AS `a`, NULL AS `b`
return [
'update statement set null' => [
'UPDATE t SET `b` = NULL, a = a ORDER BY id DESC LIMIT 3',
[
[
'simulated' =>
'SELECT * FROM (' .
'SELECT *, a AS `a ``new```, NULL AS `b ``new``` FROM `t` ORDER BY id DESC LIMIT 3' .
') AS `pma_tmp`' .
' WHERE NOT (`a`, `b`) <=> (`a ``new```, `b ``new```)',
'columns' => ['id', 'a', 'b', 'a `new`', 'b `new`'],
'result' => [[5, 2, 'test', 2, null]],
],
],
],
'update statement' => [
'UPDATE `t` SET `a` = 20 WHERE `id` > 4',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 20 AS `a ``new``` FROM `t` WHERE `id` > 4) AS `pma_tmp`' .
' WHERE NOT (`a`) <=> (`a ``new```)',
'columns' => ['id', 'a', 'b', 'a `new`'],
'result' => [
[5, 2, 'test', 20],
[6, 2, null, 20],
],
],
],
],
'update statement false condition' => [
'UPDATE `t` SET `a` = 20 WHERE 0',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 20 AS `a ``new``` FROM `t` WHERE 0) AS `pma_tmp`' .
' WHERE NOT (`a`) <=> (`a ``new```)',
'columns' => ['id', 'a', 'b', 'a `new`'],
'result' => [],
],
],
],
'update statement no condition' => [
'UPDATE `t` SET `a` = 2',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 2 AS `a ``new``` FROM `t`) AS `pma_tmp`' .
' WHERE NOT (`a`) <=> (`a ``new```)',
'columns' => ['id', 'a', 'b', 'a `new`'],
'result' => [
[2, 1, null, 2],
[3, 1, null, 2],
[4, 1, null, 2],
],
],
],
],
'update order by limit' => [
'UPDATE `t` SET `id` = 20 ORDER BY `id` ASC LIMIT 3',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 20 AS `id ``new``` FROM `t` ORDER BY `id` ASC LIMIT 3) AS `pma_tmp`' .
' WHERE NOT (`id`) <=> (`id ``new```)',
'columns' => ['id', 'a', 'b', 'id `new`'],
'result' => [
[1, 2, 'test', 20],
[2, 1, null, 20],
[3, 1, null, 20],
],
],
],
],
'update duplicate set' => [
'UPDATE `t` SET `id` = 2, `id` = 1 WHERE `id` = 1',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 1 AS `id ``new``` FROM `t` WHERE `id` = 1) AS `pma_tmp`' .
' WHERE NOT (`id`) <=> (`id ``new```)',
'columns' => ['id', 'a', 'b', 'id `new`'],
'result' => [],
],
],
],
'delete statement' => [
'DELETE FROM `t` WHERE `id` > 4',
[
[
'simulated' => 'SELECT * FROM `t` WHERE `id` > 4',
'columns' => ['id', 'a', 'b'],
'result' => [
[5, 2, 'test'],
[6, 2, null],
],
],
],
],
'delete statement false condition' => [
'DELETE FROM `t` WHERE 0',
[
[
'simulated' => 'SELECT * FROM `t` WHERE 0',
'columns' => ['id', 'a', 'b'],
'result' => [],
],
],
],
'delete statement order by limit' => [
'DELETE FROM `t` ORDER BY `id` ASC LIMIT 3',
[
[
'simulated' => 'SELECT * FROM `t` ORDER BY `id` ASC LIMIT 3',
'columns' => ['id', 'a', 'b'],
'result' => [
[1, 2, 'test'],
[2, 1, null],
[3, 1, null],
],
],
],
],
'multiple statments' => [
'UPDATE `t` SET `b` = `a`; DELETE FROM `t` WHERE 1',
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, `a` AS `b ``new``` FROM `t`) AS `pma_tmp`' .
' WHERE NOT (`b`) <=> (`b ``new```)',
'columns' => ['id', 'a', 'b', 'b `new`'],
'result' => [
[1, 2, 2, 'test'],
[2, 1, 1, null],
[3, 1, 1, null],
[4, 1, 1, null],
[5, 2, 2, 'test'],
[6, 2, 2, null],
],
],
[
'simulated' => 'SELECT * FROM `t` WHERE 1',
'columns' => ['id', 'a', 'b'],
'result' => [
[1, 2, 'test'],
[2, 1, null],
[3, 1, null],
[4, 1, null],
[5, 2, 'test'],
[6, 2, null],
],
],
],
],
'statement with comment' => [
"UPDATE `t` SET `a` = 20 -- oops\nWHERE 0",
[
[
'simulated' =>
'SELECT *' .
' FROM (SELECT *, 20 AS `a ``new``` FROM `t` WHERE 0) AS `pma_tmp`' .
' WHERE NOT (`a`) <=> (`a ``new```)',
'columns' => ['id', 'a', 'b', 'a `new`'],
'result' => [],
],
],
],
];
}
}
|