UpdatePathTestBaseTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Drupal\FunctionalTests\Update;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Render\FormattableMarkup;
  5. use Drupal\Core\Database\Database;
  6. /**
  7. * Tests the update path base class.
  8. *
  9. * @group Update
  10. * @group legacy
  11. */
  12. class UpdatePathTestBaseTest extends UpdatePathTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected static $modules = ['update_test_schema'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setDatabaseDumpFiles() {
  21. $this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.8.0.bare.standard.php.gz';
  22. $this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.update-test-schema-enabled.php';
  23. $this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.update-test-semver-update-n-enabled.php';
  24. }
  25. /**
  26. * Tests that the database was properly loaded.
  27. */
  28. public function testDatabaseLoaded() {
  29. // Set a value in the cache to prove caches are cleared.
  30. \Drupal::service('cache.default')->set(__CLASS__, 'Test');
  31. foreach (['user' => 8100, 'node' => 8700, 'system' => 8805, 'update_test_schema' => 8000] as $module => $schema) {
  32. $this->assertEqual(drupal_get_installed_schema_version($module), $schema, new FormattableMarkup('Module @module schema is @schema', ['@module' => $module, '@schema' => $schema]));
  33. }
  34. // Ensure that all {router} entries can be unserialized. If they cannot be
  35. // unserialized a notice will be thrown by PHP.
  36. $result = \Drupal::database()->query("SELECT name, route from {router}")->fetchAllKeyed(0, 1);
  37. // For the purpose of fetching the notices and displaying more helpful error
  38. // messages, let's override the error handler temporarily.
  39. set_error_handler(function ($severity, $message, $filename, $lineno) {
  40. throw new \ErrorException($message, 0, $severity, $filename, $lineno);
  41. });
  42. foreach ($result as $route_name => $route) {
  43. try {
  44. unserialize($route);
  45. }
  46. catch (\Exception $e) {
  47. $this->fail(sprintf('Error "%s" while unserializing route %s', $e->getMessage(), Html::escape($route_name)));
  48. }
  49. }
  50. restore_error_handler();
  51. // Before accessing the site we need to run updates first or the site might
  52. // be broken.
  53. $this->runUpdates();
  54. $this->assertEquals('standard', \Drupal::config('core.extension')->get('profile'));
  55. $this->assertEqual(\Drupal::config('system.site')->get('name'), 'Site-Install');
  56. $this->drupalGet('<front>');
  57. $this->assertText('Site-Install');
  58. // Ensure that the database tasks have been run during set up. Neither MySQL
  59. // nor SQLite make changes that are testable.
  60. $database = $this->container->get('database');
  61. if ($database->driver() == 'pgsql') {
  62. $this->assertEqual('on', $database->query("SHOW standard_conforming_strings")->fetchField());
  63. $this->assertEqual('escape', $database->query("SHOW bytea_output")->fetchField());
  64. }
  65. // Ensure the test runners cache has been cleared.
  66. $this->assertFalse(\Drupal::service('cache.default')->get(__CLASS__));
  67. }
  68. /**
  69. * Test that updates are properly run.
  70. */
  71. public function testUpdateHookN() {
  72. $connection = Database::getConnection();
  73. // Increment the schema version.
  74. \Drupal::state()->set('update_test_schema_version', 8001);
  75. $this->runUpdates();
  76. $select = $connection->select('watchdog');
  77. $select->orderBy('wid', 'DESC');
  78. $select->range(0, 5);
  79. $select->fields('watchdog', ['message']);
  80. $container_cannot_be_saved_messages = array_filter(iterator_to_array($select->execute()), function ($row) {
  81. return strpos($row->message, 'Container cannot be saved to cache.') !== FALSE;
  82. });
  83. $this->assertEqual([], $container_cannot_be_saved_messages);
  84. // Ensure schema has changed.
  85. $this->assertEqual(drupal_get_installed_schema_version('update_test_schema', TRUE), 8001);
  86. $this->assertEqual(drupal_get_installed_schema_version('update_test_semver_update_n', TRUE), 8001);
  87. // Ensure the index was added for column a.
  88. $this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.');
  89. // Ensure update_test_semver_update_n_update_8001 was run.
  90. $this->assertEquals(\Drupal::state()->get('update_test_semver_update_n_update_8001'), 'Yes, I was run. Thanks for testing!');
  91. }
  92. /**
  93. * Tests that path aliases are not processed during database updates.
  94. */
  95. public function testPathAliasProcessing() {
  96. // Add a path alias for the '/admin' system path.
  97. $values = [
  98. 'path' => '/admin/structure',
  99. 'alias' => '/admin-structure-alias',
  100. 'langcode' => 'und',
  101. 'status' => 1,
  102. ];
  103. $database = \Drupal::database();
  104. $id = $database->insert('path_alias')
  105. ->fields($values + ['uuid' => \Drupal::service('uuid')->generate()])
  106. ->execute();
  107. $revision_id = $database->insert('path_alias_revision')
  108. ->fields($values + ['id' => $id, 'revision_default' => 1])
  109. ->execute();
  110. $database->update('path_alias')
  111. ->fields(['revision_id' => $revision_id])
  112. ->condition('id', $id)
  113. ->execute();
  114. // Increment the schema version.
  115. \Drupal::state()->set('update_test_schema_version', 8002);
  116. $this->runUpdates();
  117. // Check that the alias defined earlier is not used during the update
  118. // process.
  119. $this->assertSession()->linkByHrefExists('/admin/structure');
  120. $this->assertSession()->linkByHrefNotExists('/admin-structure-alias');
  121. $account = $this->createUser(['administer site configuration', 'access administration pages', 'access site reports']);
  122. $this->drupalLogin($account);
  123. // Go to the status report page and check that the alias is used.
  124. $this->drupalGet('admin/reports/status');
  125. $this->assertSession()->linkByHrefNotExists('/admin/structure');
  126. $this->assertSession()->linkByHrefExists('/admin-structure-alias');
  127. }
  128. /**
  129. * Tests that test running environment is updated when module list changes.
  130. *
  131. * @see update_test_schema_update_8003()
  132. */
  133. public function testModuleListChange() {
  134. // Set a value in the cache to prove caches are cleared.
  135. \Drupal::service('cache.default')->set(__CLASS__, 'Test');
  136. // Ensure that modules are installed and uninstalled as expected prior to
  137. // running updates.
  138. $extension_config = $this->config('core.extension')->get();
  139. $this->assertArrayHasKey('page_cache', $extension_config['module']);
  140. $this->assertArrayNotHasKey('module_test', $extension_config['module']);
  141. $module_list = \Drupal::moduleHandler()->getModuleList();
  142. $this->assertArrayHasKey('page_cache', $module_list);
  143. $this->assertArrayNotHasKey('module_test', $module_list);
  144. $namespaces = \Drupal::getContainer()->getParameter('container.namespaces');
  145. $this->assertArrayHasKey('Drupal\page_cache', $namespaces);
  146. $this->assertArrayNotHasKey('Drupal\module_test', $namespaces);
  147. // Increment the schema version so that update_test_schema_update_8003()
  148. // runs.
  149. \Drupal::state()->set('update_test_schema_version', 8003);
  150. $this->runUpdates();
  151. // Ensure that test running environment has been updated with the changes to
  152. // the module list.
  153. $extension_config = $this->config('core.extension')->get();
  154. $this->assertArrayNotHasKey('page_cache', $extension_config['module']);
  155. $this->assertArrayHasKey('module_test', $extension_config['module']);
  156. $module_list = \Drupal::moduleHandler()->getModuleList();
  157. $this->assertArrayNotHasKey('page_cache', $module_list);
  158. $this->assertArrayHasKey('module_test', $module_list);
  159. $namespaces = \Drupal::getContainer()->getParameter('container.namespaces');
  160. $this->assertArrayNotHasKey('Drupal\page_cache', $namespaces);
  161. $this->assertArrayHasKey('Drupal\module_test', $namespaces);
  162. // Ensure the test runners cache has been cleared.
  163. $this->assertFalse(\Drupal::service('cache.default')->get(__CLASS__));
  164. }
  165. /**
  166. * Tests that schema can be excluded from testing.
  167. *
  168. * @see \Drupal\FunctionalTests\Update\UpdatePathTestBase::runUpdates()
  169. * @see \Drupal\Core\Test\TestSetupTrait::$configSchemaCheckerExclusions
  170. */
  171. public function testSchemaChecking() {
  172. // Create some configuration that should be skipped.
  173. $this->config('config_schema_test.noschema')->set('foo', 'bar')->save();
  174. $this->runUpdates();
  175. $this->assertSame('bar', $this->config('config_schema_test.noschema')->get('foo'));
  176. }
  177. /**
  178. * Test the database fixtures are setup correctly.
  179. */
  180. public function testFixturesSetup() {
  181. $this->assertCount(3, $this->databaseDumpFiles);
  182. }
  183. }