diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-09-21 06:06:14 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-09-21 06:06:14 +0300 |
commit | a56b425a2a5c3cc16d170d3c3f503bd899949326 (patch) | |
tree | 7f36a5e66c51d9b9e38ed49914c8cb68d5ea6e2c /test/classes/Controllers | |
parent | c2018c4c2195fc26f188e15ae56a0a30e385174f (diff) |
Extract /console/bookmark/add action from ImportController
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'test/classes/Controllers')
-rw-r--r-- | test/classes/Controllers/Console/Bookmark/AddControllerTest.php | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/classes/Controllers/Console/Bookmark/AddControllerTest.php b/test/classes/Controllers/Console/Bookmark/AddControllerTest.php new file mode 100644 index 0000000000..29b2cf29de --- /dev/null +++ b/test/classes/Controllers/Console/Bookmark/AddControllerTest.php @@ -0,0 +1,97 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Console\Bookmark; + +use PhpMyAdmin\ConfigStorage\RelationParameters; +use PhpMyAdmin\Controllers\Console\Bookmark\AddController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Template; +use PhpMyAdmin\Tests\AbstractTestCase; +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; + +/** + * @covers \PhpMyAdmin\Controllers\Console\Bookmark\AddController + */ +class AddControllerTest extends AbstractTestCase +{ + public function testWithInvalidParams(): void + { + $dbi = $this->createDatabaseInterface(); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['db', null, null], + ['label', null, null], + ['bookmark_query', null, null], + ['shared', null, null], + ]); + $controller = new AddController($response, new Template(), $dbi); + $controller($request); + $this->assertSame(['message' => 'Incomplete params'], $response->getJSONResult()); + } + + public function testWithoutRelationParameters(): void + { + $GLOBALS['cfg']['Server']['user'] = 'user'; + $_SESSION['relation'] = []; + $dbi = $this->createDatabaseInterface(); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['db', null, 'test'], + ['label', null, 'test'], + ['bookmark_query', null, 'test'], + ['shared', null, 'test'], + ]); + $controller = new AddController($response, new Template(), $dbi); + $controller($request); + $this->assertSame(['message' => 'Failed'], $response->getJSONResult()); + } + + public function testWithValidParameters(): void + { + $GLOBALS['cfg']['Server']['user'] = 'test_user'; + $GLOBALS['server'] = 1; + $_SESSION['relation'] = []; + $_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([ + 'user' => 'test_user', + 'db' => 'pmadb', + 'bookmarkwork' => true, + 'bookmark' => 'bookmark', + ])->toArray(); + + $dbiDummy = $this->createDbiDummy(); + // phpcs:ignore Generic.Files.LineLength.TooLong + $dbiDummy->addResult('INSERT INTO `pmadb`.`bookmark` (id, dbase, user, query, label) VALUES (NULL, \'test_db\', \'\', \'test_query\', \'test_label\')', []); + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $response = new ResponseRenderer(); + $request = $this->createStub(ServerRequest::class); + $request->method('getParsedBodyParam')->willReturnMap([ + ['db', null, 'test_db'], + ['label', null, 'test_label'], + ['bookmark_query', null, 'test_query'], + ['shared', null, 'true'], + ]); + $controller = new AddController($response, new Template(), $dbi); + $controller($request); + $this->assertSame( + [ + 'message' => 'Succeeded', + 'data' => [ + 'bkm_database' => 'test_db', + 'bkm_user' => 'test_user', + 'bkm_sql_query' => 'test_query', + 'bkm_label' => 'test_label', + ], + 'isShared' => true, + ], + $response->getJSONResult() + ); + $dbiDummy->assertAllQueriesConsumed(); + } +} |