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