public function DbDumpTest::testScriptLoad in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Update/DbDumpTest.php \Drupal\system\Tests\Update\DbDumpTest::testScriptLoad()
Test loading the script back into the database.
File
- core/
modules/ system/ src/ Tests/ Update/ DbDumpTest.php, line 181 - Contains \Drupal\system\Tests\Update\DbDumpTest.
Class
- DbDumpTest
- Tests for the database dump commands.
Namespace
Drupal\system\Tests\UpdateCode
public function testScriptLoad() {
if ($this->skipTests) {
$this
->pass("Skipping test since the DbDumpCommand is currently only compatible with MySql");
return;
}
// Generate the script.
$application = new DbDumpApplication();
$command = $application
->find('dump-database-d8-mysql');
$command_tester = new CommandTester($command);
$command_tester
->execute([]);
$script = $command_tester
->getDisplay();
// Store original schemas and drop tables to avoid errors.
foreach ($this->tables as $table) {
$this->originalTableSchemas[$table] = $this
->getTableSchema($table);
$this->originalTableIndexes[$table] = $this
->getTableIndexes($table);
Database::getConnection()
->schema()
->dropTable($table);
}
// This will load the data.
$file = sys_get_temp_dir() . '/' . $this
->randomMachineName();
file_put_contents($file, $script);
require_once $file;
// The tables should now exist and the schemas should match the originals.
foreach ($this->tables as $table) {
$this
->assertTrue(Database::getConnection()
->schema()
->tableExists($table), SafeMarkup::format('Table @table created by the database script.', [
'@table' => $table,
]));
$this
->assertIdentical($this->originalTableSchemas[$table], $this
->getTableSchema($table), SafeMarkup::format('The schema for @table was properly restored.', [
'@table' => $table,
]));
$this
->assertIdentical($this->originalTableIndexes[$table], $this
->getTableIndexes($table), SafeMarkup::format('The indexes for @table were properly restored.', [
'@table' => $table,
]));
}
// Ensure the test config has been replaced.
$config = unserialize(db_query("SELECT data FROM {config} WHERE name = 'test_config'")
->fetchField());
$this
->assertIdentical($config, $this->data, 'Script has properly restored the config table data.');
// Ensure the cache data was not exported.
$this
->assertFalse(\Drupal::cache('discovery')
->get('test'), 'Cache data was not exported to the script.');
}