You are here

public function TermTest::testTermInterface in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermInterface()

Save, edit and delete a term using the user interface.

File

core/modules/taxonomy/tests/src/Functional/TermTest.php, line 334

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermInterface() {
  \Drupal::service('module_installer')
    ->install([
    'views',
  ]);
  $edit = [
    'name[0][value]' => $this
      ->randomMachineName(12),
    'description[0][value]' => $this
      ->randomMachineName(100),
  ];

  // Explicitly set the parents field to 'root', to ensure that
  // TermForm::save() handles the invalid term ID correctly.
  $edit['parent[]'] = [
    0,
  ];

  // Create the term to edit.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add');
  $this
    ->submitForm($edit, 'Save');
  $terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties([
    'name' => $edit['name[0][value]'],
  ]);
  $term = reset($terms);
  $this
    ->assertNotNull($term, 'Term found in database.');

  // Submitting a term takes us to the add page; we need the List page.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $this
    ->clickLink('Edit');

  // Verify that the randomly generated term is present.
  $this
    ->assertSession()
    ->pageTextContains($edit['name[0][value]']);
  $this
    ->assertSession()
    ->pageTextContains($edit['description[0][value]']);
  $edit = [
    'name[0][value]' => $this
      ->randomMachineName(14),
    'description[0][value]' => $this
      ->randomMachineName(102),
  ];

  // Edit the term.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Check that the term is still present at admin UI after edit.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $this
    ->assertSession()
    ->pageTextContains($edit['name[0][value]']);
  $this
    ->assertSession()
    ->linkExists('Edit');

  // Check the term link can be clicked through to the term page.
  $this
    ->clickLink($edit['name[0][value]']);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // View the term and check that it is correct.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id());
  $this
    ->assertSession()
    ->pageTextContains($edit['name[0][value]']);
  $this
    ->assertSession()
    ->pageTextContains($edit['description[0][value]']);

  // Did this page request display a 'term-listing-heading'?
  $this
    ->assertSession()
    ->elementExists('xpath', '//div[contains(@class, "field--name-description")]');

  // Check that it does NOT show a description when description is blank.
  $term
    ->setDescription(NULL);
  $term
    ->save();
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id());
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[contains(@class, "field--entity-taxonomy-term--description")]');

  // Check that the description value is processed.
  $value = $this
    ->randomMachineName();
  $term
    ->setDescription($value);
  $term
    ->save();
  $this
    ->assertEquals("<p>{$value}</p>\n", $term->description->processed);

  // Check that the term feed page is working.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/feed');

  // Delete the term.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/edit');
  $this
    ->clickLink('Delete');
  $this
    ->submitForm([], 'Delete');

  // Assert that the term no longer exists.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Test "save and go to list" action while creating term.
  // Create the term to edit.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add');
  $edit = [
    'name[0][value]' => $this
      ->randomMachineName(12),
    'description[0][value]' => $this
      ->randomMachineName(100),
  ];

  // Create the term to edit.
  $this
    ->submitForm($edit, 'Save and go to list');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $this
    ->assertSession()
    ->pageTextContains($edit['name[0][value]']);

  // Validate that "Save and go to list" doesn't exist when destination
  // parameter is present.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add', [
    'query' => [
      'destination' => 'node/add',
    ],
  ]);
  $this
    ->assertSession()
    ->pageTextNotContains('Save and go to list');
}