Tests update of timestamp fields to bigint.

@group system

Hierarchy

Expanded class hierarchy of Y2038TimestampUpdateTest

File

core/modules/system/tests/src/Functional/Update/Y2038TimestampUpdateTest.php, line 15

Namespace

Drupal\Tests\system\Functional\Update
View source
class Y2038TimestampUpdateTest extends UpdatePathTestBase {
  use UpdatePathTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'forum',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * The tables and column name of the time fields.
   *
   * The key is the table name and the values are the names of the time fields.
   *
   * @var string[][]
   */
  protected $tables = [
    'comment_entity_statistics' => [
      'last_comment_timestamp',
    ],
    'forum_index' => [
      'created',
      'last_comment_timestamp',
    ],
    'history' => [
      'timestamp',
    ],
    'locale_file' => [
      'timestamp',
      'last_checked',
    ],
    'node_counter' => [
      'timestamp',
    ],
    'sessions' => [
      'timestamp',
    ],
    'tracker_node' => [
      'changed',
    ],
    'tracker_user' => [
      'changed',
    ],
    'watchdog' => [
      'timestamp',
    ],
    'batch' => [
      'timestamp',
    ],
    'queue' => [
      'created',
      'expire',
    ],
    'flood' => [
      'expiration',
      'timestamp',
    ],
  ];

  /**
   * The database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The name of the test database.
   *
   * @var string
   */
  protected $databaseName;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    /** @var \Drupal\Core\Database\Connection $connection */
    $this->connection = \Drupal::service('database');
    if ($this->connection
      ->databaseType() == 'pgsql') {
      $this->databaseName = 'public';
    }
    else {
      $this->databaseName = $this->connection
        ->getConnectionOptions()['database'];
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function setDatabaseDumpFiles() {
    $this->databaseDumpFiles = [
      // Start with a standard install of Drupal 9.3.0 with the following
      // enabled modules: forum, language, locale, statistics and tracker.
      DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
      DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/Y2038-timestamp.php',
    ];
  }

  /**
   * Tests update of time fields.
   */
  public function testUpdate() {
    if (\Drupal::service('database')
      ->databaseType() == 'sqlite') {
      $this
        ->markTestSkipped("This test does not support the SQLite database driver.");
    }

    // Create a table starting with cache that is not a cache bin.
    \Drupal::service('database')
      ->schema()
      ->createTable('cache_bogus', [
      'fields' => [
        'id' => [
          'type' => 'int',
          'not null' => TRUE,
        ],
      ],
      'primary key' => [
        'id',
      ],
    ]);
    $this
      ->collectTimestampFieldsFromDatabase();

    // PostgreSQL returns the value 'integer' instead of 'int' when queried
    // about the column type. Some PostgreSQL tables are already of the type
    // 'bigint'.
    $this
      ->assertTimestampFields([
      'int',
      'integer',
      'bigint',
    ]);
    $this
      ->runUpdates();
    $this
      ->assertTimestampFields([
      'bigint',
    ]);
  }

  /**
   * Collect the timestamp fields from the database and update table list.
   */
  public function collectTimestampFieldsFromDatabase() {

    /** @var \Drupal\Core\Database\Connection $connection */
    $connection = \Drupal::service('database');

    // Build list of all tables and fields to check.
    $tables = $connection
      ->schema()
      ->findTables('migrate_map_%');
    foreach ($tables as $table) {
      $this->tables[$table] = [
        'last_imported',
      ];
    }
    $tables = $connection
      ->schema()
      ->findTables('cache_%');
    $tables = array_filter($tables, function ($table) {
      return str_starts_with($table, 'cache_') && $table !== 'cache_bogus';
    });
    $this
      ->assertNotEmpty($tables);
    foreach ($tables as $table) {
      $this->tables[$table] = [
        'expire',
      ];
    }
  }

  /**
   * Asserts the size of the timestamp fields.
   */
  public function assertTimestampFields($expected_values) {

    // Check the size of all the fields.
    foreach ($this->tables as $table => $column_names) {
      $table_name = $this->connection
        ->getPrefix() . $table;
      foreach ($column_names as $column_name) {
        $result = $this->connection
          ->query("SELECT data_type FROM information_schema.columns WHERE table_schema = '{$this->databaseName}' and table_name = '{$table_name}' and column_name = '{$column_name}';")
          ->fetchField();
        $this
          ->assertContains($result, $expected_values, "Failed for '{$table_name}' column '{$column_name}'");
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequirementsPageTrait::assertErrorSummaries protected function Asserts the given error summaries are present on the page.
RequirementsPageTrait::assertRequirementSummaries protected function Asserts the given requirements section summaries are present on the page.
RequirementsPageTrait::assertWarningSummaries protected function Asserts the given warning summaries are present on the page.
RequirementsPageTrait::continueOnExpectedWarnings protected function Continues installation when the expected warnings are found.
RequirementsPageTrait::updateRequirementsProblem protected function Handles the update requirements page.
SchemaCheckTestTrait::assertConfigSchema public function Asserts the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTestTrait::assertConfigSchemaByName public function Asserts configuration, specified by name, has a valid schema.
UpdatePathTestBase::$databaseDumpFiles protected property The file path(s) to the dumped database(s) to load into the child site.
UpdatePathTestBase::$strictConfigSchema protected property Disable strict config schema checking.
UpdatePathTestBase::$updateUrl protected property The update URL.
UpdatePathTestBase::doInstall protected function
UpdatePathTestBase::initFrontPage protected function
UpdatePathTestBase::installDrupal public function Overrides BrowserTestBase::installDrupal() for update testing.
UpdatePathTestBase::prepareSettings protected function Add settings that are missed since the installer isn't run.
UpdatePathTestBase::replaceUser1 protected function Replace User 1 with the user created here. 1
UpdatePathTestBase::runDbTasks protected function Runs the install database tasks for the driver used by the test runner.
UpdatePathTestBase::testDatabaseLoaded protected function Tests that the database was properly loaded.
UpdatePathTestTrait::$checkFailedUpdates protected property Fail the test if there are failed updates.
UpdatePathTestTrait::doSelectionTest protected function Tests the selection page. 5
UpdatePathTestTrait::ensureUpdatesToRun protected function Installs the update_script_test module and makes an update available.
UpdatePathTestTrait::runUpdates protected function Helper function to run pending database updates.
UpdatePathTestTrait::runUpdates protected function Helper function to run pending database updates. Aliased as: doRunUpdates
Y2038TimestampUpdateTest::$connection protected property The database.
Y2038TimestampUpdateTest::$databaseName protected property The name of the test database.
Y2038TimestampUpdateTest::$defaultTheme protected property
Y2038TimestampUpdateTest::$modules protected static property Modules to enable after the database is loaded. Overrides UpdatePathTestBase::$modules
Y2038TimestampUpdateTest::$tables protected property The tables and column name of the time fields.
Y2038TimestampUpdateTest::assertTimestampFields public function Asserts the size of the timestamp fields.
Y2038TimestampUpdateTest::collectTimestampFieldsFromDatabase public function Collect the timestamp fields from the database and update table list.
Y2038TimestampUpdateTest::setDatabaseDumpFiles protected function Set database dump files to be used. Overrides UpdatePathTestBase::setDatabaseDumpFiles
Y2038TimestampUpdateTest::setUp protected function Overrides UpdatePathTestBase::setUp
Y2038TimestampUpdateTest::testUpdate public function Tests update of time fields.