UpdatePathTestBaseTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\FunctionalTests\Update;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Utility\SafeMarkup;
  5. /**
  6. * Tests the update path base class.
  7. *
  8. * @group Update
  9. */
  10. class UpdatePathTestBaseTest extends UpdatePathTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected static $modules = ['update_test_schema'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function setDatabaseDumpFiles() {
  19. $this->databaseDumpFiles = [
  20. __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
  21. __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.update-test-schema-enabled.php',
  22. ];
  23. }
  24. /**
  25. * Tests that the database was properly loaded.
  26. */
  27. public function testDatabaseLoaded() {
  28. foreach (['user', 'node', 'system', 'update_test_schema'] as $module) {
  29. $this->assertEqual(drupal_get_installed_schema_version($module), 8000, SafeMarkup::format('Module @module schema is 8000', ['@module' => $module]));
  30. }
  31. // Ensure that all {router} entries can be unserialized. If they cannot be
  32. // unserialized a notice will be thrown by PHP.
  33. $result = \Drupal::database()->query("SELECT name, route from {router}")->fetchAllKeyed(0, 1);
  34. // For the purpose of fetching the notices and displaying more helpful error
  35. // messages, let's override the error handler temporarily.
  36. set_error_handler(function ($severity, $message, $filename, $lineno) {
  37. throw new \ErrorException($message, 0, $severity, $filename, $lineno);
  38. });
  39. foreach ($result as $route_name => $route) {
  40. try {
  41. unserialize($route);
  42. }
  43. catch (\Exception $e) {
  44. $this->fail(sprintf('Error "%s" while unserializing route %s', $e->getMessage(), Html::escape($route_name)));
  45. }
  46. }
  47. restore_error_handler();
  48. // Before accessing the site we need to run updates first or the site might
  49. // be broken.
  50. $this->runUpdates();
  51. $this->assertEqual(\Drupal::config('system.site')->get('name'), 'Site-Install');
  52. $this->drupalGet('<front>');
  53. $this->assertText('Site-Install');
  54. // Ensure that the database tasks have been run during set up. Neither MySQL
  55. // nor SQLite make changes that are testable.
  56. $database = $this->container->get('database');
  57. if ($database->driver() == 'pgsql') {
  58. $this->assertEqual('on', $database->query("SHOW standard_conforming_strings")->fetchField());
  59. $this->assertEqual('escape', $database->query("SHOW bytea_output")->fetchField());
  60. }
  61. }
  62. /**
  63. * Test that updates are properly run.
  64. */
  65. public function testUpdateHookN() {
  66. // Increment the schema version.
  67. \Drupal::state()->set('update_test_schema_version', 8001);
  68. $this->runUpdates();
  69. $select = \Drupal::database()->select('watchdog');
  70. $select->orderBy('wid', 'DESC');
  71. $select->range(0, 5);
  72. $select->fields('watchdog', ['message']);
  73. $container_cannot_be_saved_messages = array_filter(iterator_to_array($select->execute()), function ($row) {
  74. return strpos($row->message, 'Container cannot be saved to cache.') !== FALSE;
  75. });
  76. $this->assertEqual([], $container_cannot_be_saved_messages);
  77. // Ensure schema has changed.
  78. $this->assertEqual(drupal_get_installed_schema_version('update_test_schema', TRUE), 8001);
  79. // Ensure the index was added for column a.
  80. $this->assertTrue(db_index_exists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.');
  81. }
  82. }