function ForumIndexUpdateTest::testUpdatePath

Same name in other branches
  1. 10 core/modules/forum/tests/src/Functional/ForumIndexUpdateTest.php \Drupal\Tests\forum\Functional\ForumIndexUpdateTest::testUpdatePath()

Tests the update path to add the new primary key.

File

core/modules/forum/tests/src/Functional/ForumIndexUpdateTest.php, line 29

Class

ForumIndexUpdateTest
Tests addition of the forum_index primary key.

Namespace

Drupal\Tests\forum\Functional

Code

public function testUpdatePath() : void {
    // Set the batch size to 1 to validate the sandbox logic in the update hook.
    $settings = Settings::getInstance() ? Settings::getAll() : [];
    $settings['entity_update_batch_size'] = 1;
    new Settings($settings);
    $schema = \Drupal::database()->schema();
    // We can't reliably call ::indexExists for each database driver as sqlite
    // doesn't have named indexes for primary keys like mysql (PRIMARY) and
    // pgsql (pkey).
    $find_primary_key_columns = new \ReflectionMethod(get_class($schema), 'findPrimaryKeyColumns');
    $columns = $find_primary_key_columns->invoke($schema, 'forum_index');
    $this->assertEmpty($columns);
    $count = \Drupal::database()->select('forum_index')
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(9, $count);
    $duplicates = \Drupal::database()->select('forum_index')
        ->condition('nid', 1)
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(2, $duplicates);
    $duplicates = \Drupal::database()->select('forum_index')
        ->condition('nid', 2)
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(3, $duplicates);
    $this->runUpdates();
    $this->assertEquals([
        'nid',
        'tid',
    ], $find_primary_key_columns->invoke($schema, 'forum_index'));
    $count = \Drupal::database()->select('forum_index')
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(6, $count);
    $duplicates = \Drupal::database()->select('forum_index')
        ->condition('nid', 1)
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(1, $duplicates);
    $duplicates = \Drupal::database()->select('forum_index')
        ->condition('nid', 2)
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(1, $duplicates);
    // This entry is associated with two terms so two records should remain.
    $duplicates = \Drupal::database()->select('forum_index')
        ->condition('nid', 4)
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEquals(2, $duplicates);
    $entry = \Drupal::database()->select('forum_index', 'f')
        ->fields('f')
        ->condition('nid', 5)
        ->execute()
        ->fetchAssoc();
    $this->assertEquals([
        'nid' => 5,
        'title' => 'AFL',
        'tid' => 5,
        'sticky' => 0,
        'created' => 1695264369,
        'last_comment_timestamp' => 1695264403,
        'comment_count' => 1,
    ], $entry);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.