UpdatePathTestBaseTest.php 3.5 KB

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