function DbDumpTest::testScriptLoad

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\DbDumpTest::testScriptLoad()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php \Drupal\KernelTests\Core\Command\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/modules/mysql/tests/src/Kernel/mysql/DbDumpTest.php, line 178

Class

DbDumpTest
Tests for the database dump commands.

Namespace

Drupal\Tests\mysql\Kernel\mysql

Code

public function testScriptLoad() : void {
    // 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), "Table {$table} created by the database script.");
        $this->assertSame($this->originalTableSchemas[$table], $this->getTableSchema($table), "The schema for {$table} was properly restored.");
        $this->assertSame($this->originalTableIndexes[$table], $this->getTableIndexes($table), "The indexes for {$table} were properly restored.");
    }
    // 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.