DbDumpTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Drupal\KernelTests\Core\Command;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Core\Command\DbDumpApplication;
  5. use Drupal\Core\Config\DatabaseStorage;
  6. use Drupal\Core\Database\Database;
  7. use Drupal\Core\DependencyInjection\ContainerBuilder;
  8. use Drupal\KernelTests\KernelTestBase;
  9. use Drupal\Tests\Traits\Core\PathAliasTestTrait;
  10. use Drupal\user\Entity\User;
  11. use Symfony\Component\Console\Tester\CommandTester;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. /**
  14. * Tests for the database dump commands.
  15. *
  16. * @group Update
  17. */
  18. class DbDumpTest extends KernelTestBase {
  19. use PathAliasTestTrait;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static $modules = [
  24. 'system',
  25. 'config',
  26. 'dblog',
  27. 'menu_link_content',
  28. 'link',
  29. 'block_content',
  30. 'file',
  31. 'path_alias',
  32. 'user',
  33. ];
  34. /**
  35. * Test data to write into config.
  36. *
  37. * @var array
  38. */
  39. protected $data;
  40. /**
  41. * An array of original table schemas.
  42. *
  43. * @var array
  44. */
  45. protected $originalTableSchemas = [];
  46. /**
  47. * An array of original table indexes (including primary and unique keys).
  48. *
  49. * @var array
  50. */
  51. protected $originalTableIndexes = [];
  52. /**
  53. * Tables that should be part of the exported script.
  54. *
  55. * @var array
  56. */
  57. protected $tables;
  58. /**
  59. * {@inheritdoc}
  60. *
  61. * Register a database cache backend rather than memory-based.
  62. */
  63. public function register(ContainerBuilder $container) {
  64. parent::register($container);
  65. $container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory')
  66. ->addArgument(new Reference('database'))
  67. ->addArgument(new Reference('cache_tags.invalidator.checksum'))
  68. ->addArgument(new Reference('settings'));
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function setUp() {
  74. parent::setUp();
  75. if (Database::getConnection()->databaseType() !== 'mysql') {
  76. $this->markTestSkipped("Skipping test since the DbDumpCommand is currently only compatible with MySql");
  77. }
  78. // Create some schemas so our export contains tables.
  79. $this->installSchema('system', [
  80. 'key_value_expire',
  81. 'sessions',
  82. ]);
  83. $this->installSchema('dblog', ['watchdog']);
  84. $this->installEntitySchema('block_content');
  85. $this->installEntitySchema('user');
  86. $this->installEntitySchema('file');
  87. $this->installEntitySchema('menu_link_content');
  88. $this->installEntitySchema('path_alias');
  89. $this->installSchema('system', 'sequences');
  90. // Place some sample config to test for in the export.
  91. $this->data = [
  92. 'foo' => $this->randomMachineName(),
  93. 'bar' => $this->randomMachineName(),
  94. ];
  95. $storage = new DatabaseStorage(Database::getConnection(), 'config');
  96. $storage->write('test_config', $this->data);
  97. // Create user account with some potential syntax issues.
  98. $account = User::create(['mail' => 'q\'uote$dollar@example.com', 'name' => '$dollar']);
  99. $account->save();
  100. // Create a path alias.
  101. $this->createPathAlias('/user/' . $account->id(), '/user/example');
  102. // Create a cache table (this will create 'cache_discovery').
  103. \Drupal::cache('discovery')->set('test', $this->data);
  104. // These are all the tables that should now be in place.
  105. $this->tables = [
  106. 'block_content',
  107. 'block_content_field_data',
  108. 'block_content_field_revision',
  109. 'block_content_revision',
  110. 'cachetags',
  111. 'config',
  112. 'cache_bootstrap',
  113. 'cache_config',
  114. 'cache_data',
  115. 'cache_discovery',
  116. 'cache_entity',
  117. 'file_managed',
  118. 'key_value_expire',
  119. 'menu_link_content',
  120. 'menu_link_content_data',
  121. 'menu_link_content_revision',
  122. 'menu_link_content_field_revision',
  123. 'sequences',
  124. 'sessions',
  125. 'path_alias',
  126. 'path_alias_revision',
  127. 'user__roles',
  128. 'users',
  129. 'users_field_data',
  130. 'watchdog',
  131. ];
  132. }
  133. /**
  134. * Test the command directly.
  135. */
  136. public function testDbDumpCommand() {
  137. $application = new DbDumpApplication();
  138. $command = $application->find('dump-database-d8-mysql');
  139. $command_tester = new CommandTester($command);
  140. $command_tester->execute([]);
  141. // Tables that are schema-only should not have data exported.
  142. $pattern = preg_quote("\$connection->insert('sessions')");
  143. $this->assertNotRegExp('/' . $pattern . '/', $command_tester->getDisplay(), 'Tables defined as schema-only do not have data exported to the script.');
  144. // Table data is exported.
  145. $pattern = preg_quote("\$connection->insert('config')");
  146. $this->assertRegExp('/' . $pattern . '/', $command_tester->getDisplay(), 'Table data is properly exported to the script.');
  147. // The test data are in the dump (serialized).
  148. $pattern = preg_quote(serialize($this->data));
  149. $this->assertRegExp('/' . $pattern . '/', $command_tester->getDisplay(), 'Generated data is found in the exported script.');
  150. // Check that the user account name and email address was properly escaped.
  151. $pattern = preg_quote('"q\'uote\$dollar@example.com"');
  152. $this->assertRegExp('/' . $pattern . '/', $command_tester->getDisplay(), 'The user account email address was properly escaped in the exported script.');
  153. $pattern = preg_quote('\'$dollar\'');
  154. $this->assertRegExp('/' . $pattern . '/', $command_tester->getDisplay(), 'The user account name was properly escaped in the exported script.');
  155. }
  156. /**
  157. * Test loading the script back into the database.
  158. */
  159. public function testScriptLoad() {
  160. // Generate the script.
  161. $application = new DbDumpApplication();
  162. $command = $application->find('dump-database-d8-mysql');
  163. $command_tester = new CommandTester($command);
  164. $command_tester->execute([]);
  165. $script = $command_tester->getDisplay();
  166. // Store original schemas and drop tables to avoid errors.
  167. $connection = Database::getConnection();
  168. $schema = $connection->schema();
  169. foreach ($this->tables as $table) {
  170. $this->originalTableSchemas[$table] = $this->getTableSchema($table);
  171. $this->originalTableIndexes[$table] = $this->getTableIndexes($table);
  172. $schema->dropTable($table);
  173. }
  174. // This will load the data.
  175. $file = sys_get_temp_dir() . '/' . $this->randomMachineName();
  176. file_put_contents($file, $script);
  177. require_once $file;
  178. // The tables should now exist and the schemas should match the originals.
  179. foreach ($this->tables as $table) {
  180. $this->assertTrue($schema
  181. ->tableExists($table), new FormattableMarkup('Table @table created by the database script.', ['@table' => $table]));
  182. $this->assertSame($this->originalTableSchemas[$table], $this->getTableSchema($table), new FormattableMarkup('The schema for @table was properly restored.', ['@table' => $table]));
  183. $this->assertSame($this->originalTableIndexes[$table], $this->getTableIndexes($table), new FormattableMarkup('The indexes for @table were properly restored.', ['@table' => $table]));
  184. }
  185. // Ensure the test config has been replaced.
  186. $config = unserialize($connection->query("SELECT data FROM {config} WHERE name = 'test_config'")->fetchField());
  187. $this->assertIdentical($config, $this->data, 'Script has properly restored the config table data.');
  188. // Ensure the cache data was not exported.
  189. $this->assertFalse(\Drupal::cache('discovery')
  190. ->get('test'), 'Cache data was not exported to the script.');
  191. }
  192. /**
  193. * Helper function to get a simplified schema for a given table.
  194. *
  195. * @param string $table
  196. *
  197. * @return array
  198. * Array keyed by field name, with the values being the field type.
  199. */
  200. protected function getTableSchema($table) {
  201. // Verify the field type on the data column in the cache table.
  202. // @todo this is MySQL specific.
  203. $query = Database::getConnection()->query("SHOW COLUMNS FROM {" . $table . "}");
  204. $definition = [];
  205. while ($row = $query->fetchAssoc()) {
  206. $definition[$row['Field']] = $row['Type'];
  207. }
  208. return $definition;
  209. }
  210. /**
  211. * Returns indexes for a given table.
  212. *
  213. * @param string $table
  214. * The table to find indexes for.
  215. *
  216. * @return array
  217. * The 'primary key', 'unique keys', and 'indexes' portion of the Drupal
  218. * table schema.
  219. */
  220. protected function getTableIndexes($table) {
  221. $query = Database::getConnection()->query("SHOW INDEX FROM {" . $table . "}");
  222. $definition = [];
  223. while ($row = $query->fetchAssoc()) {
  224. $index_name = $row['Key_name'];
  225. $column = $row['Column_name'];
  226. // Key the arrays by the index sequence for proper ordering (start at 0).
  227. $order = $row['Seq_in_index'] - 1;
  228. // If specified, add length to the index.
  229. if ($row['Sub_part']) {
  230. $column = [$column, $row['Sub_part']];
  231. }
  232. if ($index_name === 'PRIMARY') {
  233. $definition['primary key'][$order] = $column;
  234. }
  235. elseif ($row['Non_unique'] == 0) {
  236. $definition['unique keys'][$index_name][$order] = $column;
  237. }
  238. else {
  239. $definition['indexes'][$index_name][$order] = $column;
  240. }
  241. }
  242. return $definition;
  243. }
  244. }