function TermTest::testRedirects
Tests destination after saving terms.
File
- 
              core/
modules/ taxonomy/ tests/ src/ Functional/ TermTest.php, line 573  
Class
- TermTest
 - Tests load, save and delete for taxonomy terms.
 
Namespace
Drupal\Tests\taxonomy\FunctionalCode
public function testRedirects() : void {
  // Save a new term.
  $addUrl = Url::fromRoute('entity.taxonomy_term.add_form', [
    'taxonomy_vocabulary' => $this->vocabulary
      ->id(),
  ]);
  $this->drupalGet($addUrl);
  $this->submitForm([
    'name[0][value]' => $this->randomMachineName(),
  ], 'Save');
  // Adding a term reloads the form.
  $this->assertSession()
    ->addressEquals($addUrl->toString());
  $this->assertSession()
    ->pageTextContains('Created new term');
  // Update a term.
  $term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => $this->randomMachineName(),
  ]);
  $term->save();
  $this->drupalGet($term->toUrl('edit-form'));
  $this->submitForm(edit: [], submit: 'Save');
  // Updating a term sends user to view the term.
  $this->assertSession()
    ->addressEquals($term->toUrl()
    ->setAbsolute());
  $this->assertSession()
    ->pageTextContains('Updated term');
  // Unless the term is not accessible to the user.
  // Label triggers forbidden in taxonomy_test_entity_access().
  $term = Term::create([
    'vid' => $this->vocabulary
      ->id(),
    'name' => 'Inaccessible view',
  ]);
  $term->save();
  $this->drupalGet($term->toUrl('edit-form'));
  $this->submitForm(edit: [], submit: 'Save');
  // In which case, the edit form is reloaded.
  $this->assertSession()
    ->addressEquals($term->toUrl('edit-form')
    ->setAbsolute());
  $this->assertSession()
    ->pageTextContains('Updated term');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.