function DbDumpTest::testScriptLoad

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::testScriptLoad()
  2. 10 core/modules/mysql/tests/src/Kernel/mysql/DbDumpTest.php \Drupal\Tests\mysql\Kernel\mysql\DbDumpTest::testScriptLoad()
  3. 11.x core/modules/mysql/tests/src/Kernel/mysql/DbDumpTest.php \Drupal\Tests\mysql\Kernel\mysql\DbDumpTest::testScriptLoad()

Tests loading the script back into the database.

File

core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php, line 180

Class

DbDumpTest
Tests for the database dump commands.

Namespace

Drupal\KernelTests\Core\Command

Code

public function testScriptLoad() {
    // 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.
    $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);
    }
    // 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($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,
        ]));
    }
    // Ensure the test config has been replaced.
    $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.');
    // Ensure the cache data was not exported.
    $this->assertFalse(\Drupal::cache('discovery')->get('test'), 'Cache data was not exported to the script.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.