function PendingRevisionTest::testTaxonomyIndexWithPendingRevision

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Kernel/PendingRevisionTest.php \Drupal\Tests\taxonomy\Kernel\PendingRevisionTest::testTaxonomyIndexWithPendingRevision()
  2. 8.9.x core/modules/taxonomy/tests/src/Kernel/PendingRevisionTest.php \Drupal\Tests\taxonomy\Kernel\PendingRevisionTest::testTaxonomyIndexWithPendingRevision()
  3. 10 core/modules/taxonomy/tests/src/Kernel/PendingRevisionTest.php \Drupal\Tests\taxonomy\Kernel\PendingRevisionTest::testTaxonomyIndexWithPendingRevision()

Tests that the taxonomy index work correctly with pending revisions.

File

core/modules/taxonomy/tests/src/Kernel/PendingRevisionTest.php, line 50

Class

PendingRevisionTest
Kernel tests for taxonomy pending revisions.

Namespace

Drupal\Tests\taxonomy\Kernel

Code

public function testTaxonomyIndexWithPendingRevision() : void {
    \Drupal::configFactory()->getEditable('taxonomy.settings')
        ->set('maintain_index_table', TRUE)
        ->save();
    Vocabulary::create([
        'name' => 'test',
        'vid' => 'test',
    ])->save();
    $term = Term::create([
        'name' => 'term1',
        'vid' => 'test',
    ]);
    $term->save();
    $term2 = Term::create([
        'name' => 'term2',
        'vid' => 'test',
    ]);
    $term2->save();
    NodeType::create([
        'type' => 'page',
        'name' => 'Page',
    ])->save();
    FieldStorageConfig::create([
        'entity_type' => 'node',
        'field_name' => 'field_tags',
        'type' => 'entity_reference',
        'settings' => [
            'target_type' => 'taxonomy_term',
        ],
    ])->save();
    FieldConfig::create([
        'field_name' => 'field_tags',
        'entity_type' => 'node',
        'bundle' => 'page',
    ])->save();
    $node = Node::create([
        'type' => 'page',
        'title' => 'test_title',
        'field_tags' => [
            $term->id(),
        ],
    ]);
    $node->save();
    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
    // Normal new revision.
    $node->setNewRevision(TRUE);
    $node->isDefaultRevision(TRUE);
    $node->field_tags->target_id = $term2->id();
    $node->save();
    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
    // Check that saving a pending revision does not affect the taxonomy index.
    $node->setNewRevision(TRUE);
    $node->isDefaultRevision(FALSE);
    $node->field_tags->target_id = $term->id();
    $node->save();
    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
    // Check that making the previously created pending revision the default
    // revision updates the taxonomy index correctly.
    $node->isDefaultRevision(TRUE);
    $node->save();
    $taxonomy_index = $this->getTaxonomyIndex();
    $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
}

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