320 lines
9.0 KiB
PHP
320 lines
9.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpMyAdmin\Tests\Plugins\Export;
|
|
|
|
use PhpMyAdmin\Config;
|
|
use PhpMyAdmin\Config\Settings\Export;
|
|
use PhpMyAdmin\ConfigStorage\Relation;
|
|
use PhpMyAdmin\Current;
|
|
use PhpMyAdmin\Export\OutputHandler;
|
|
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
|
|
use PhpMyAdmin\Plugins\Export\ExportCsv;
|
|
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
|
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
|
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
|
|
use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
|
|
use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
|
|
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
|
|
use PhpMyAdmin\Tests\AbstractTestCase;
|
|
use PhpMyAdmin\Transformations;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Medium;
|
|
use ReflectionMethod;
|
|
use ReflectionProperty;
|
|
|
|
use function ob_get_clean;
|
|
use function ob_start;
|
|
|
|
#[CoversClass(ExportCsv::class)]
|
|
#[Medium]
|
|
final class ExportCsvTest extends AbstractTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Current::$database = '';
|
|
Current::$table = '';
|
|
Current::$lang = '';
|
|
}
|
|
|
|
public function testSetProperties(): void
|
|
{
|
|
$exportCsv = $this->getExportCsv();
|
|
|
|
$method = new ReflectionMethod(ExportCsv::class, 'setProperties');
|
|
$method->invoke($exportCsv, null);
|
|
|
|
$attrProperties = new ReflectionProperty(ExportCsv::class, 'properties');
|
|
$properties = $attrProperties->getValue($exportCsv);
|
|
|
|
self::assertInstanceOf(ExportPluginProperties::class, $properties);
|
|
|
|
self::assertSame(
|
|
'CSV',
|
|
$properties->getText(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'csv',
|
|
$properties->getExtension(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'text/comma-separated-values',
|
|
$properties->getMimeType(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Options',
|
|
$properties->getOptionsText(),
|
|
);
|
|
|
|
$options = $properties->getOptions();
|
|
|
|
self::assertInstanceOf(OptionsPropertyRootGroup::class, $options);
|
|
|
|
self::assertSame(
|
|
'Format Specific Options',
|
|
$options->getName(),
|
|
);
|
|
|
|
$generalOptionsArray = $options->getProperties();
|
|
$generalOptions = $generalOptionsArray->current();
|
|
|
|
self::assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
|
|
|
|
self::assertSame(
|
|
'csv_general_opts',
|
|
$generalOptions->getName(),
|
|
);
|
|
|
|
$generalProperties = $generalOptions->getProperties();
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(TextPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_separator',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Columns separated with:',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(TextPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_enclosed',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Columns enclosed with:',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(TextPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_escaped',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Columns escaped with:',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(TextPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_terminated',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Lines terminated with:',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(TextPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_null',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Replace NULL with:',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_removeCRLF',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Remove carriage return/line feed characters within columns',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
$generalProperties->next();
|
|
|
|
self::assertInstanceOf(BoolPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_columns',
|
|
$property->getName(),
|
|
);
|
|
|
|
self::assertSame(
|
|
'Put columns names in the first row',
|
|
$property->getText(),
|
|
);
|
|
|
|
$property = $generalProperties->current();
|
|
|
|
self::assertInstanceOf(HiddenPropertyItem::class, $property);
|
|
|
|
self::assertSame(
|
|
'csv_structure_or_data',
|
|
$property->getName(),
|
|
);
|
|
}
|
|
|
|
public function testExportHeader(): void
|
|
{
|
|
// case 1
|
|
$this->expectNotToPerformAssertions();
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->exportHeader();
|
|
}
|
|
|
|
public function testExportFooter(): void
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->exportFooter();
|
|
}
|
|
|
|
public function testExportDBHeader(): void
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->exportDBHeader('testDB');
|
|
}
|
|
|
|
public function testExportDBFooter(): void
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->exportDBFooter('testDB');
|
|
}
|
|
|
|
public function testExportDBCreate(): void
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->exportDBCreate('testDB');
|
|
}
|
|
|
|
public function testExportData(): void
|
|
{
|
|
// case 1
|
|
OutputHandler::$asFile = true;
|
|
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
|
|
->withParsedBody(['csv_terminated' => ';', 'csv_columns' => 'On']);
|
|
|
|
$exportCsv = $this->getExportCsv();
|
|
$exportCsv->setExportOptions($request, new Export());
|
|
$exportCsv->exportHeader();
|
|
|
|
ob_start();
|
|
$exportCsv->exportData('test_db', 'test_table', 'SELECT * FROM `test_db`.`test_table_csv_export`;');
|
|
$result = ob_get_clean();
|
|
|
|
self::assertSame(
|
|
'id,name,datetimefield;1,"ab""cd",2011-01-20 02:00:02;2,"foo;",2010-01-20 02:00:02;3,'
|
|
. "\n" . 'Abcd,2012-01-20 02:00:02;',
|
|
$result,
|
|
);
|
|
|
|
// case 3
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
|
|
->withParsedBody(['csv_enclosed' => '"', 'csv_terminated' => ';', 'csv_columns' => 'On']);
|
|
|
|
$exportCsv->setExportOptions($request, new Export());
|
|
$exportCsv->exportHeader();
|
|
|
|
ob_start();
|
|
$exportCsv->exportData('test_db', 'test_table_csv_export', 'SELECT * FROM `test_db`.`test_table_csv_export`;');
|
|
$result = ob_get_clean();
|
|
|
|
self::assertSame(
|
|
'id,name,datetimefield;1,"ab""cd",2011-01-20 02:00:02;2,"foo;",2010-01-20 02:00:02;3,'
|
|
. "\n" . 'Abcd,2012-01-20 02:00:02;',
|
|
$result,
|
|
);
|
|
|
|
// case 4
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
|
|
->withParsedBody([
|
|
'csv_enclosed' => '|',
|
|
'csv_terminated' => "\n",
|
|
'csv_escaped' => '\\',
|
|
'csv_columns' => 'On',
|
|
]);
|
|
|
|
$exportCsv->setExportOptions($request, new Export());
|
|
$exportCsv->exportHeader();
|
|
|
|
ob_start();
|
|
$exportCsv->exportData('test_db', 'test_table_csv_export', 'SELECT * FROM `test_db`.`test_table_csv_export`;');
|
|
$result = ob_get_clean();
|
|
|
|
self::assertSame(
|
|
'id,name,datetimefield' . "\n"
|
|
. '1,ab"cd,2011-01-20 02:00:02' . "\n"
|
|
. '2,foo;,2010-01-20 02:00:02' . "\n"
|
|
. '3,|' . "\n" . 'Abcd|,2012-01-20 02:00:02' . "\n",
|
|
$result,
|
|
);
|
|
}
|
|
|
|
private function getExportCsv(): ExportCsv
|
|
{
|
|
$dbi = $this->createDatabaseInterface();
|
|
$config = new Config();
|
|
$relation = new Relation($dbi, $config);
|
|
|
|
return new ExportCsv($relation, new OutputHandler(), new Transformations($dbi, $relation), $dbi, $config);
|
|
}
|
|
}
|