diff --git a/test/classes/UtilTest.php b/test/classes/UtilTest.php index 0e7ecbe59a..1ee439ce24 100644 --- a/test/classes/UtilTest.php +++ b/test/classes/UtilTest.php @@ -11,6 +11,7 @@ namespace PhpMyAdmin\Tests; use PhpMyAdmin\Config; use PhpMyAdmin\Core; +use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\MoTranslator\Loader; use PhpMyAdmin\SqlParser\Context; use PhpMyAdmin\SqlParser\Token; @@ -2900,4 +2901,109 @@ class UtilTest extends PmaTestCase ]*/ ]; } + + /** + * Some data to test Util::asWKT for testAsWKT + * + * @return array + */ + public function dataProviderAsWKT(): array + { + return [ + [ + 'SELECT ASTEXT(x\'000000000101000000000000000000f03f000000000000f03f\')', + [ + 'POINT(1 1)' + ], + 'POINT(1 1)', + false, + 50300, + ], + [ + 'SELECT ASTEXT(x\'000000000101000000000000000000f03f000000000000f03f\'), SRID(x\'000000000101000000000000000000f03f000000000000f03f\')', + [ + 'POINT(1 1)', + '0', + ], + '\'POINT(1 1)\',0', + true, + 50300, + ], + [ + 'SELECT ST_ASTEXT(x\'000000000101000000000000000000f03f000000000000f03f\')', + [ + 'POINT(1 1)' + ], + 'POINT(1 1)', + false, + 50700, + ], + [ + 'SELECT ST_ASTEXT(x\'000000000101000000000000000000f03f000000000000f03f\'), ST_SRID(x\'000000000101000000000000000000f03f000000000000f03f\')', + [ + 'POINT(1 1)', + '0', + ], + '\'POINT(1 1)\',0', + true, + 50700, + ], + ]; + } + + /** + * Test to get data asWKT + * + * @dataProvider dataProviderAsWKT + * + * @param string $expectedQuery The query to expect + * @param array $returnData The data to return for fetchRow + * @param string $functionResult Result of the Util::asWKT invocation + * @param string $SRIDOption Use the SRID option or not + * @param int $mysqlVersion The mysql version to return for getVersion + * + * @return void + */ + public function testAsWKT( + string $expectedQuery, + array $returnData, + string $functionResult, + bool $SRIDOption, + int $mysqlVersion + ): void { + $oldDbi = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder(DatabaseInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $dbi->expects($SRIDOption ? $this->once() : $this->exactly(2)) + ->method('getVersion') + ->will($this->returnValue($mysqlVersion)); + + $dbi->expects($SRIDOption ? $this->once() : $this->exactly(2)) + ->method('tryQuery') + ->with($expectedQuery) + ->will($this->returnValue([ + // Omit the real object + ])); + + $dbi->expects($SRIDOption ? $this->once() : $this->exactly(2)) + ->method('fetchRow') + ->will($this->returnValue($returnData)); + + $GLOBALS['dbi'] = $dbi; + + if (! $SRIDOption) { + // Also test default signature + $this->assertSame($functionResult, Util::asWKT( + hex2bin('000000000101000000000000000000F03F000000000000F03F') + )); + } + $this->assertSame($functionResult, Util::asWKT( + hex2bin('000000000101000000000000000000F03F000000000000F03F'), + $SRIDOption + )); + + $GLOBALS['dbi'] = $oldDbi; + } }