DbDumpTest.php 9.1 KB

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