phpmyadmin/tests/classes/Plugins/Export/ExportPdfTest.php
Maurício Meneghini Fauth 1d1c55e250
Move test directory to tests
- Related to #18512

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2023-11-30 10:47:42 -03:00

260 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Plugins\Export;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Export\Export;
use PhpMyAdmin\Plugins\Export\ExportPdf;
use PhpMyAdmin\Plugins\Export\Helpers\Pdf;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
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\Group;
use ReflectionMethod;
use ReflectionProperty;
use function __;
#[CoversClass(ExportPdf::class)]
#[Group('medium')]
class ExportPdfTest extends AbstractTestCase
{
protected ExportPdf $object;
/**
* Configures global environment.
*/
protected function setUp(): void
{
parent::setUp();
$dbi = $this->createDatabaseInterface();
DatabaseInterface::$instance = $dbi;
$GLOBALS['server'] = 0;
$GLOBALS['output_kanji_conversion'] = false;
$GLOBALS['output_charset_conversion'] = false;
$GLOBALS['buffer_needed'] = false;
$GLOBALS['asfile'] = true;
$GLOBALS['save_on_server'] = false;
$this->object = new ExportPdf(
new Relation($dbi),
new Export($dbi),
new Transformations(),
);
}
/**
* tearDown for test cases
*/
protected function tearDown(): void
{
parent::tearDown();
unset($this->object);
}
public function testSetProperties(): void
{
$method = new ReflectionMethod(ExportPdf::class, 'setProperties');
$method->invoke($this->object, null);
$attrProperties = new ReflectionProperty(ExportPdf::class, 'properties');
$properties = $attrProperties->getValue($this->object);
$this->assertInstanceOf(ExportPluginProperties::class, $properties);
$this->assertEquals(
'PDF',
$properties->getText(),
);
$this->assertEquals(
'pdf',
$properties->getExtension(),
);
$this->assertEquals(
'application/pdf',
$properties->getMimeType(),
);
$this->assertEquals(
'Options',
$properties->getOptionsText(),
);
$this->assertTrue(
$properties->getForceFile(),
);
$options = $properties->getOptions();
$this->assertInstanceOf(OptionsPropertyRootGroup::class, $options);
$this->assertEquals(
'Format Specific Options',
$options->getName(),
);
$generalOptionsArray = $options->getProperties();
$generalOptions = $generalOptionsArray->current();
$generalOptionsArray->next();
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
$this->assertEquals(
'general_opts',
$generalOptions->getName(),
);
$generalProperties = $generalOptions->getProperties();
$property = $generalProperties->current();
$this->assertInstanceOf(TextPropertyItem::class, $property);
$this->assertEquals(
'report_title',
$property->getName(),
);
$generalOptions = $generalOptionsArray->current();
$this->assertInstanceOf(OptionsPropertyMainGroup::class, $generalOptions);
$this->assertEquals(
'dump_what',
$generalOptions->getName(),
);
$this->assertEquals(
'Dump table',
$generalOptions->getText(),
);
$generalProperties = $generalOptions->getProperties();
$property = $generalProperties->current();
$this->assertInstanceOf(RadioPropertyItem::class, $property);
$this->assertEquals(
'structure_or_data',
$property->getName(),
);
$this->assertEquals(
['structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')],
$property->getValues(),
);
}
public function testExportHeader(): void
{
$pdf = $this->getMockBuilder(Pdf::class)
->disableOriginalConstructor()
->getMock();
$pdf->expects($this->once())
->method('Open');
$pdf->expects($this->once())
->method('setTopMargin');
$attrPdf = new ReflectionProperty(ExportPdf::class, 'pdf');
$attrPdf->setValue($this->object, $pdf);
$this->assertTrue(
$this->object->exportHeader(),
);
}
public function testExportFooter(): void
{
$pdf = $this->getMockBuilder(Pdf::class)
->disableOriginalConstructor()
->getMock();
$pdf->expects($this->once())
->method('getPDFData')
->willReturn('');
$attrPdf = new ReflectionProperty(ExportPdf::class, 'pdf');
$attrPdf->setValue($this->object, $pdf);
$this->assertTrue(
$this->object->exportFooter(),
);
}
public function testExportDBHeader(): void
{
$this->assertTrue(
$this->object->exportDBHeader('testDB'),
);
}
public function testExportDBFooter(): void
{
$this->assertTrue(
$this->object->exportDBFooter('testDB'),
);
}
public function testExportDBCreate(): void
{
$this->assertTrue(
$this->object->exportDBCreate('testDB', 'database'),
);
}
public function testExportData(): void
{
$pdf = $this->getMockBuilder(Pdf::class)
->disableOriginalConstructor()
->getMock();
$pdf->expects($this->once())
->method('mysqlReport')
->with('SELECT');
$attrPdf = new ReflectionProperty(ExportPdf::class, 'pdf');
$attrPdf->setValue($this->object, $pdf);
$this->assertTrue(
$this->object->exportData(
'db',
'table',
'phpmyadmin.net/err',
'SELECT',
),
);
}
/**
* Test for
* - PhpMyAdmin\Plugins\Export\ExportPdf::setPdf
* - PhpMyAdmin\Plugins\Export\ExportPdf::getPdf
*/
public function testSetGetPdf(): void
{
$setter = new ReflectionMethod(ExportPdf::class, 'setPdf');
$setter->invoke($this->object, new Pdf());
$getter = new ReflectionMethod(ExportPdf::class, 'getPdf');
$this->assertInstanceOf(
Pdf::class,
$getter->invoke($this->object),
);
}
}