From 4bfc1aff83c5986cdcc62c2fee1978c023a2373d Mon Sep 17 00:00:00 2001 From: Dimitris Tsamitros Date: Thu, 4 Mar 2021 13:19:50 +0200 Subject: [PATCH] Quote strings even when they are numeric on Yaml exports Signed-off-by: Dimitris Tsamitros --- .../classes/Plugins/Export/ExportYaml.php | 5 +- .../classes/Plugins/Export/ExportYamlTest.php | 74 ++++++++++--------- 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/libraries/classes/Plugins/Export/ExportYaml.php b/libraries/classes/Plugins/Export/ExportYaml.php index 960f7582ba..b220509325 100644 --- a/libraries/classes/Plugins/Export/ExportYaml.php +++ b/libraries/classes/Plugins/Export/ExportYaml.php @@ -16,6 +16,7 @@ use PhpMyAdmin\Properties\Plugins\ExportPluginProperties; use function is_numeric; use function str_replace; use function stripslashes; +use function strpos; /** * Handles the export for the YAML format @@ -159,6 +160,8 @@ class ExportYaml extends ExportPlugin ); $columns_cnt = $dbi->numFields($result); + $fieldsMeta = $dbi->getFieldsMeta($result); + $columns = []; for ($i = 0; $i < $columns_cnt; $i++) { $col_as = $dbi->fieldName($result, $i); @@ -191,7 +194,7 @@ class ExportYaml extends ExportPlugin continue; } - if (is_numeric($record[$i])) { + if (is_numeric($record[$i]) && strpos($fieldsMeta[$i]->type, 'string') === false) { $buffer .= ' ' . $columns[$i] . ': ' . $record[$i] . $crlf; continue; } diff --git a/test/classes/Plugins/Export/ExportYamlTest.php b/test/classes/Plugins/Export/ExportYamlTest.php index b767aa4210..6db4e34054 100644 --- a/test/classes/Plugins/Export/ExportYamlTest.php +++ b/test/classes/Plugins/Export/ExportYamlTest.php @@ -13,6 +13,8 @@ use PhpMyAdmin\Properties\Plugins\ExportPluginProperties; use PhpMyAdmin\Tests\AbstractTestCase; use ReflectionMethod; use ReflectionProperty; +use stdClass; + use function array_shift; use function ob_get_clean; use function ob_start; @@ -169,6 +171,28 @@ class ExportYamlTest extends AbstractTestCase ->disableOriginalConstructor() ->getMock(); + $flags = []; + $a = new stdClass(); + $a->type = ''; + $flags[] = $a; + $b = new stdClass(); + $b->type = ''; + $flags[] = $b; + $c = new stdClass(); + $c->type = ''; + $flags[] = $c; + $d = new stdClass(); + $d->type = 'string'; + $flags[] = $d; + $e = new stdClass(); + $e->type = 'string'; + $flags[] = $e; + + $dbi->expects($this->once()) + ->method('getFieldsMeta') + ->with(true) + ->will($this->returnValue($flags)); + $dbi->expects($this->once()) ->method('query') ->with('SELECT', DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_UNBUFFERED) @@ -177,44 +201,24 @@ class ExportYamlTest extends AbstractTestCase $dbi->expects($this->once()) ->method('numFields') ->with(true) - ->will($this->returnValue(4)); + ->will($this->returnValue(5)); - $dbi->expects($this->at(2)) + $dbi->expects($this->exactly(5)) ->method('fieldName') - ->will($this->returnValue('fName1')); + ->willReturn('fName1', 'fNa"me2', 'fNa\\me3', 'fName4', 'fName5'); - $dbi->expects($this->at(3)) - ->method('fieldName') - ->will($this->returnValue('fNa"me2')); - - $dbi->expects($this->at(4)) - ->method('fieldName') - ->will($this->returnValue('fNa\\me3')); - - $dbi->expects($this->at(5)) - ->method('fieldName') - ->will($this->returnValue('fName4')); - - $dbi->expects($this->at(6)) + $dbi->expects($this->exactly(3)) ->method('fetchRow') - ->with(true) - ->will( - $this->returnValue( - [ - null, - '123', - "\"c\\a\nb\r", - ] - ) - ); - - $dbi->expects($this->at(7)) - ->method('fetchRow') - ->with(true) - ->will( - $this->returnValue( - [null] - ) + ->willReturn( + [ + null, + '123', + "\"c\\a\nb\r", + '123', + '+30.2103210000', + ], + [null], + null ); $GLOBALS['dbi'] = $dbi; @@ -236,6 +240,8 @@ class ExportYamlTest extends AbstractTestCase '-' . "\n" . ' fNa"me2: 123' . "\n" . ' fName3: "\"c\\\\a\nb\r"' . "\n" . + ' fName4: "123"' . "\n" . + ' fName5: "+30.2103210000"' . "\n" . '-' . "\n", $result );