View source
<?php
namespace Drupal\KernelTests\Core\Command;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Command\DbDumpApplication;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
use Drupal\user\Entity\User;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Reference;
class DbDumpTest extends KernelTestBase {
use PathAliasTestTrait;
protected static $modules = [
'system',
'config',
'dblog',
'menu_link_content',
'link',
'block_content',
'file',
'path_alias',
'user',
];
protected $data;
protected $originalTableSchemas = [];
protected $originalTableIndexes = [];
protected $tables;
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('cache_factory', 'Drupal\\Core\\Cache\\DatabaseBackendFactory')
->addArgument(new Reference('database'))
->addArgument(new Reference('cache_tags.invalidator.checksum'))
->addArgument(new Reference('settings'));
}
protected function setUp() : void {
parent::setUp();
if (Database::getConnection()
->databaseType() !== 'mysql') {
$this
->markTestSkipped("Skipping test since the DbDumpCommand is currently only compatible with MySql");
}
$this
->installSchema('system', [
'sessions',
]);
$this
->installSchema('dblog', [
'watchdog',
]);
$this
->installEntitySchema('block_content');
$this
->installEntitySchema('user');
$this
->installEntitySchema('file');
$this
->installEntitySchema('menu_link_content');
$this
->installEntitySchema('path_alias');
$this
->installSchema('system', 'sequences');
$this->data = [
'foo' => $this
->randomMachineName(),
'bar' => $this
->randomMachineName(),
];
$storage = new DatabaseStorage(Database::getConnection(), 'config');
$storage
->write('test_config', $this->data);
$account = User::create([
'mail' => 'q\'uote$dollar@example.com',
'name' => '$dollar',
]);
$account
->save();
$this
->createPathAlias('/user/' . $account
->id(), '/user/example');
\Drupal::cache('discovery')
->set('test', $this->data);
$this->tables = [
'block_content',
'block_content_field_data',
'block_content_field_revision',
'block_content_revision',
'cachetags',
'config',
'cache_bootstrap',
'cache_config',
'cache_discovery',
'cache_entity',
'file_managed',
'menu_link_content',
'menu_link_content_data',
'menu_link_content_revision',
'menu_link_content_field_revision',
'sequences',
'sessions',
'path_alias',
'path_alias_revision',
'user__roles',
'users',
'users_field_data',
'watchdog',
];
}
public function testDbDumpCommand() {
$application = new DbDumpApplication();
$command = $application
->find('dump-database-d8-mysql');
$command_tester = new CommandTester($command);
$command_tester
->execute([]);
$pattern = preg_quote("\$connection->insert('sessions')");
$this
->assertDoesNotMatchRegularExpression('/' . $pattern . '/', $command_tester
->getDisplay(), 'Tables defined as schema-only do not have data exported to the script.');
$pattern = preg_quote("\$connection->insert('config')");
$this
->assertMatchesRegularExpression('/' . $pattern . '/', $command_tester
->getDisplay(), 'Table data is properly exported to the script.');
$pattern = preg_quote(serialize($this->data));
$this
->assertMatchesRegularExpression('/' . $pattern . '/', $command_tester
->getDisplay(), 'Generated data is found in the exported script.');
$pattern = preg_quote('"q\'uote\\$dollar@example.com"');
$this
->assertMatchesRegularExpression('/' . $pattern . '/', $command_tester
->getDisplay(), 'The user account email address was properly escaped in the exported script.');
$pattern = preg_quote('\'$dollar\'');
$this
->assertMatchesRegularExpression('/' . $pattern . '/', $command_tester
->getDisplay(), 'The user account name was properly escaped in the exported script.');
}
public function testScriptLoad() {
$application = new DbDumpApplication();
$command = $application
->find('dump-database-d8-mysql');
$command_tester = new CommandTester($command);
$command_tester
->execute([]);
$script = $command_tester
->getDisplay();
$connection = Database::getConnection();
$schema = $connection
->schema();
foreach ($this->tables as $table) {
$this->originalTableSchemas[$table] = $this
->getTableSchema($table);
$this->originalTableIndexes[$table] = $this
->getTableIndexes($table);
$schema
->dropTable($table);
}
$file = sys_get_temp_dir() . '/' . $this
->randomMachineName();
file_put_contents($file, $script);
require_once $file;
foreach ($this->tables as $table) {
$this
->assertTrue($schema
->tableExists($table), new FormattableMarkup('Table @table created by the database script.', [
'@table' => $table,
]));
$this
->assertSame($this->originalTableSchemas[$table], $this
->getTableSchema($table), new FormattableMarkup('The schema for @table was properly restored.', [
'@table' => $table,
]));
$this
->assertSame($this->originalTableIndexes[$table], $this
->getTableIndexes($table), new FormattableMarkup('The indexes for @table were properly restored.', [
'@table' => $table,
]));
}
$config = unserialize($connection
->select('config', 'c')
->fields('c', [
'data',
])
->condition('name', 'test_config')
->execute()
->fetchField());
$this
->assertSame($this->data, $config, 'Script has properly restored the config table data.');
$this
->assertFalse(\Drupal::cache('discovery')
->get('test'), 'Cache data was not exported to the script.');
}
protected function getTableSchema($table) {
$query = Database::getConnection()
->query("SHOW COLUMNS FROM {" . $table . "}");
$definition = [];
while ($row = $query
->fetchAssoc()) {
$definition[$row['Field']] = $row['Type'];
}
return $definition;
}
protected function getTableIndexes($table) {
$query = Database::getConnection()
->query("SHOW INDEX FROM {" . $table . "}");
$definition = [];
while ($row = $query
->fetchAssoc()) {
$index_name = $row['Key_name'];
$column = $row['Column_name'];
$order = $row['Seq_in_index'] - 1;
if ($row['Sub_part']) {
$column = [
$column,
$row['Sub_part'],
];
}
if ($index_name === 'PRIMARY') {
$definition['primary key'][$order] = $column;
}
elseif ($row['Non_unique'] == 0) {
$definition['unique keys'][$index_name][$order] = $column;
}
else {
$definition['indexes'][$index_name][$order] = $column;
}
}
return $definition;
}
}