|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpMyAdmin\Tests\Controllers\Table\Structure; |
| 6 | + |
| 7 | +use PhpMyAdmin\Controllers\Table\Structure\SpatialController; |
| 8 | +use PhpMyAdmin\Controllers\Table\StructureController; |
| 9 | +use PhpMyAdmin\Http\ServerRequest; |
| 10 | +use PhpMyAdmin\Message; |
| 11 | +use PhpMyAdmin\Template; |
| 12 | +use PhpMyAdmin\Tests\AbstractTestCase; |
| 13 | +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; |
| 14 | + |
| 15 | +/** |
| 16 | + * @covers \PhpMyAdmin\Controllers\Table\Structure\SpatialController |
| 17 | + */ |
| 18 | +class SpatialControllerTest extends AbstractTestCase |
| 19 | +{ |
| 20 | + public function testAddSpatialKeyToSingleField(): void |
| 21 | + { |
| 22 | + $GLOBALS['db'] = 'test_db'; |
| 23 | + $GLOBALS['table'] = 'test_table'; |
| 24 | + $GLOBALS['message'] = null; |
| 25 | + $GLOBALS['sql_query'] = null; |
| 26 | + $_POST['selected_fld'] = ['test_field']; |
| 27 | + |
| 28 | + $dbiDummy = $this->createDbiDummy(); |
| 29 | + $dbiDummy->addSelectDb('test_db'); |
| 30 | + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', []); |
| 31 | + $dbi = $this->createDatabaseInterface($dbiDummy); |
| 32 | + $GLOBALS['dbi'] = $dbi; |
| 33 | + $request = $this->createStub(ServerRequest::class); |
| 34 | + $controllerStub = $this->createMock(StructureController::class); |
| 35 | + $controllerStub->expects($this->once())->method('__invoke')->with($request); |
| 36 | + |
| 37 | + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); |
| 38 | + $controller($request); |
| 39 | + |
| 40 | + $this->assertEquals(Message::success(), $GLOBALS['message']); |
| 41 | + /** @psalm-suppress TypeDoesNotContainType */ |
| 42 | + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', $GLOBALS['sql_query']); |
| 43 | + $dbiDummy->assertAllSelectsConsumed(); |
| 44 | + $dbiDummy->assertAllQueriesConsumed(); |
| 45 | + } |
| 46 | + |
| 47 | + public function testAddSpatialKeyToMultipleFields(): void |
| 48 | + { |
| 49 | + $GLOBALS['db'] = 'test_db'; |
| 50 | + $GLOBALS['table'] = 'test_table'; |
| 51 | + $GLOBALS['message'] = null; |
| 52 | + $GLOBALS['sql_query'] = null; |
| 53 | + $_POST['selected_fld'] = ['test_field1', 'test_field2']; |
| 54 | + |
| 55 | + $dbiDummy = $this->createDbiDummy(); |
| 56 | + $dbiDummy->addSelectDb('test_db'); |
| 57 | + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field1`, `test_field2`);', []); |
| 58 | + $dbi = $this->createDatabaseInterface($dbiDummy); |
| 59 | + $GLOBALS['dbi'] = $dbi; |
| 60 | + $request = $this->createStub(ServerRequest::class); |
| 61 | + $controllerStub = $this->createMock(StructureController::class); |
| 62 | + $controllerStub->expects($this->once())->method('__invoke')->with($request); |
| 63 | + |
| 64 | + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); |
| 65 | + $controller($request); |
| 66 | + |
| 67 | + $this->assertEquals(Message::success(), $GLOBALS['message']); |
| 68 | + /** @psalm-suppress TypeDoesNotContainType */ |
| 69 | + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field1`, `test_field2`);', $GLOBALS['sql_query']); |
| 70 | + $dbiDummy->assertAllSelectsConsumed(); |
| 71 | + $dbiDummy->assertAllQueriesConsumed(); |
| 72 | + } |
| 73 | + |
| 74 | + public function testNoColumnsSelected(): void |
| 75 | + { |
| 76 | + $GLOBALS['db'] = 'test_db'; |
| 77 | + $GLOBALS['table'] = 'test_table'; |
| 78 | + $GLOBALS['message'] = null; |
| 79 | + $GLOBALS['sql_query'] = null; |
| 80 | + $_POST['selected_fld'] = null; |
| 81 | + |
| 82 | + $dbi = $this->createDatabaseInterface(); |
| 83 | + $GLOBALS['dbi'] = $dbi; |
| 84 | + $request = $this->createStub(ServerRequest::class); |
| 85 | + $controllerStub = $this->createMock(StructureController::class); |
| 86 | + $controllerStub->expects($this->never())->method('__invoke'); |
| 87 | + $response = new ResponseRenderer(); |
| 88 | + |
| 89 | + $controller = new SpatialController($response, new Template(), $dbi, $controllerStub); |
| 90 | + $controller($request); |
| 91 | + |
| 92 | + $this->assertFalse($response->hasSuccessState()); |
| 93 | + $this->assertSame(['message' => 'No column selected.'], $response->getJSONResult()); |
| 94 | + /** @psalm-suppress RedundantCondition */ |
| 95 | + $this->assertNull($GLOBALS['message']); |
| 96 | + /** @psalm-suppress RedundantCondition */ |
| 97 | + $this->assertNull($GLOBALS['sql_query']); |
| 98 | + } |
| 99 | + |
| 100 | + public function testAddSpatialKeyWithError(): void |
| 101 | + { |
| 102 | + $GLOBALS['db'] = 'test_db'; |
| 103 | + $GLOBALS['table'] = 'test_table'; |
| 104 | + $GLOBALS['message'] = null; |
| 105 | + $GLOBALS['sql_query'] = null; |
| 106 | + $_POST['selected_fld'] = ['test_field']; |
| 107 | + |
| 108 | + $dbiDummy = $this->createDbiDummy(); |
| 109 | + $dbiDummy->addSelectDb('test_db'); |
| 110 | + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', false); |
| 111 | + $dbiDummy->addErrorCode('#1210 - Incorrect arguments to SPATIAL INDEX'); |
| 112 | + $dbi = $this->createDatabaseInterface($dbiDummy); |
| 113 | + $GLOBALS['dbi'] = $dbi; |
| 114 | + $request = $this->createStub(ServerRequest::class); |
| 115 | + $controllerStub = $this->createMock(StructureController::class); |
| 116 | + $controllerStub->expects($this->once())->method('__invoke')->with($request); |
| 117 | + |
| 118 | + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); |
| 119 | + $controller($request); |
| 120 | + |
| 121 | + $this->assertEquals( |
| 122 | + Message::error('#1210 - Incorrect arguments to SPATIAL INDEX'), |
| 123 | + $GLOBALS['message'] |
| 124 | + ); |
| 125 | + /** @psalm-suppress TypeDoesNotContainType */ |
| 126 | + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', $GLOBALS['sql_query']); |
| 127 | + $dbiDummy->assertAllSelectsConsumed(); |
| 128 | + $dbiDummy->assertAllQueriesConsumed(); |
| 129 | + $dbiDummy->assertAllErrorCodesConsumed(); |
| 130 | + } |
| 131 | +} |
0 commit comments