Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19111 from MauricioFauth/recent-tables-controller…
…-removal Extract recent table handling from Header into a middleware
- Loading branch information
Showing
13 changed files
with
112 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Http\Middleware; | ||
|
||
use PhpMyAdmin\Config; | ||
use PhpMyAdmin\Favorites\RecentFavoriteTable; | ||
use PhpMyAdmin\Favorites\RecentFavoriteTables; | ||
use PhpMyAdmin\Favorites\TableType; | ||
use PhpMyAdmin\Http\ServerRequest; | ||
use PhpMyAdmin\Identifiers\DatabaseName; | ||
use PhpMyAdmin\Identifiers\TableName; | ||
use PhpMyAdmin\Message; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function assert; | ||
|
||
final class RecentTableHandling implements MiddlewareInterface | ||
{ | ||
public function __construct(private readonly Config $config) | ||
{ | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
assert($request instanceof ServerRequest); | ||
if ($this->config->settings['NumRecentTables'] === 0) { | ||
Check warning on line 31 in src/Http/Middleware/RecentTableHandling.php GitHub Actions / Infection (8.2, ubuntu-latest)
|
||
return $handler->handle($request); | ||
} | ||
|
||
$response = $handler->handle($request); | ||
|
||
$db = DatabaseName::tryFrom($request->getParam('db')); | ||
$table = TableName::tryFrom($request->getParam('table')); | ||
if ($db !== null && $table !== null) { | ||
Check warning on line 39 in src/Http/Middleware/RecentTableHandling.php GitHub Actions / Infection (8.2, ubuntu-latest)
|
||
$recentTable = new RecentFavoriteTable($db, $table); | ||
$isAddedOrError = RecentFavoriteTables::getInstance(TableType::Recent)->add($recentTable); | ||
if ($isAddedOrError instanceof Message) { | ||
Check warning on line 42 in src/Http/Middleware/RecentTableHandling.php GitHub Actions / Infection (8.2, ubuntu-latest)
|
||
$response->getBody()->write($isAddedOrError->getMessage()); | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Tests\Http\Middleware; | ||
|
||
use PhpMyAdmin\Config; | ||
use PhpMyAdmin\DatabaseInterface; | ||
use PhpMyAdmin\Favorites\RecentFavoriteTable; | ||
use PhpMyAdmin\Favorites\RecentFavoriteTables; | ||
use PhpMyAdmin\Favorites\TableType; | ||
use PhpMyAdmin\Http\Factory\ResponseFactory; | ||
use PhpMyAdmin\Http\Factory\ServerRequestFactory; | ||
use PhpMyAdmin\Http\Middleware\RecentTableHandling; | ||
use PhpMyAdmin\Identifiers\DatabaseName; | ||
use PhpMyAdmin\Identifiers\TableName; | ||
use PhpMyAdmin\Tests\AbstractTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use ReflectionProperty; | ||
|
||
#[CoversClass(RecentTableHandling::class)] | ||
final class RecentTableHandlingTest extends AbstractTestCase | ||
{ | ||
public function testProcess(): void | ||
{ | ||
$dbiDummy = $this->createDbiDummy(); | ||
DatabaseInterface::$instance = $this->createDatabaseInterface($dbiDummy); | ||
|
||
$dbiDummy->addResult('SELECT 1 FROM `test_db`.`test_table` LIMIT 1;', [['1']]); | ||
|
||
$config = new Config(); | ||
$config->settings['NumRecentTables'] = 10; | ||
|
||
$request = ServerRequestFactory::create()->createServerRequest('GET', 'https://example.com/') | ||
->withQueryParams(['db' => 'test_db', 'table' => 'test_table']); | ||
|
||
$response = ResponseFactory::create()->createResponse(); | ||
$handler = self::createMock(RequestHandlerInterface::class); | ||
$handler->method('handle')->with($request)->willReturn($response); | ||
|
||
$reflectionProperty = new ReflectionProperty(RecentFavoriteTables::class, 'instances'); | ||
$reflectionProperty->setValue(null, []); | ||
$recentTables = RecentFavoriteTables::getInstance(TableType::Recent); | ||
self::assertSame([], $recentTables->getTables()); | ||
|
||
$actualResponse = (new RecentTableHandling($config))->process($request, $handler); | ||
|
||
self::assertSame($response, $actualResponse); | ||
self::assertSame('', (string) $actualResponse->getBody()); | ||
self::assertEquals( | ||
[new RecentFavoriteTable(DatabaseName::from('test_db'), TableName::from('test_table'))], | ||
$recentTables->getTables(), | ||
); | ||
|
||
$dbiDummy->assertAllQueriesConsumed(); | ||
$reflectionProperty->setValue(null, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters