function TaxonomyTermContentModerationTest::testTaxonomyTermParents

Same name in other branches
  1. 11.x core/modules/taxonomy/tests/src/Functional/TaxonomyTermContentModerationTest.php \Drupal\Tests\taxonomy\Functional\TaxonomyTermContentModerationTest::testTaxonomyTermParents()

Tests taxonomy term parents on a moderated vocabulary.

File

core/modules/taxonomy/tests/src/Functional/TaxonomyTermContentModerationTest.php, line 65

Class

TaxonomyTermContentModerationTest
Tests taxonomy terms with Content Moderation.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyTermParents() : void {
    $assert_session = $this->assertSession();
    // Create a simple hierarchy in the vocabulary, a root term and three parent
    // terms.
    $root = $this->createTerm($this->vocabulary, [
        'langcode' => 'en',
        'moderation_state' => 'published',
    ]);
    $parent_1 = $this->createTerm($this->vocabulary, [
        'langcode' => 'en',
        'moderation_state' => 'published',
        'parent' => $root->id(),
    ]);
    $parent_2 = $this->createTerm($this->vocabulary, [
        'langcode' => 'en',
        'moderation_state' => 'published',
        'parent' => $root->id(),
    ]);
    $parent_3 = $this->createTerm($this->vocabulary, [
        'langcode' => 'en',
        'moderation_state' => 'published',
        'parent' => $root->id(),
    ]);
    // Create a child term and assign one of the parents above.
    $child = $this->createTerm($this->vocabulary, [
        'langcode' => 'en',
        'moderation_state' => 'published',
        'parent' => $parent_1->id(),
    ]);
    
    /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
    $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $validation_message = 'You can only change the hierarchy for the published version of this term.';
    // Add a pending revision without changing the term parent.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    $assert_session->pageTextNotContains($validation_message);
    // Add a pending revision and change the parent.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'parent[]' => [
            $parent_2->id(),
        ],
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    // Check that parents were not changed.
    $assert_session->pageTextContains($validation_message);
    $taxonomy_storage->resetCache();
    $this->assertEquals([
        $parent_1->id(),
    ], array_keys($taxonomy_storage->loadParents($child->id())));
    // Add a pending revision and add a new parent.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'parent[]' => [
            $parent_1->id(),
            $parent_3->id(),
        ],
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    // Check that parents were not changed.
    $assert_session->pageTextContains($validation_message);
    $taxonomy_storage->resetCache();
    $this->assertEquals([
        $parent_1->id(),
    ], array_keys($taxonomy_storage->loadParents($child->id())));
    // Add a pending revision and use the root term as a parent.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'parent[]' => [
            $root->id(),
        ],
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    // Check that parents were not changed.
    $assert_session->pageTextContains($validation_message);
    $taxonomy_storage->resetCache();
    $this->assertEquals([
        $parent_1->id(),
    ], array_keys($taxonomy_storage->loadParents($child->id())));
    // Add a pending revision and remove the parent.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'parent[]' => [],
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    // Check that parents were not changed.
    $assert_session->pageTextContains($validation_message);
    $taxonomy_storage->resetCache();
    $this->assertEquals([
        $parent_1->id(),
    ], array_keys($taxonomy_storage->loadParents($child->id())));
    // Add a published revision.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'moderation_state[0][state]' => 'published',
    ], 'Save');
    // Change the parents.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'parent[]' => [
            $parent_2->id(),
        ],
    ], 'Save');
    // Check that parents were changed.
    $assert_session->pageTextNotContains($validation_message);
    $taxonomy_storage->resetCache();
    $this->assertNotEquals([
        $parent_1->id(),
    ], array_keys($taxonomy_storage->loadParents($child->id())));
    // Add a pending revision and change the weight.
    $this->drupalGet($child->toUrl('edit-form'));
    $this->submitForm([
        'weight' => 10,
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    // Check that weight was not changed.
    $assert_session->pageTextContains($validation_message);
    // Add a new term without any parent and publish it.
    $edit = [
        'name[0][value]' => $this->randomMachineName(),
        'moderation_state[0][state]' => 'published',
    ];
    $this->drupalGet(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $this->vocabulary
            ->id(),
    ]));
    $this->submitForm($edit, 'Save');
    // Add a pending revision without any changes.
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
        ->loadByProperties([
        'name' => $edit['name[0][value]'],
    ]);
    $term = reset($terms);
    $this->drupalGet($term->toUrl('edit-form'));
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    $assert_session->pageTextNotContains($validation_message);
}

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