You are here

public function MetatagFieldTestBase::tofixTestEntityField in Metatag 8

Tests adding and editing values on a given entity type.

@todo Finish this.

File

tests/src/Functional/MetatagFieldTestBase.php, line 469

Class

MetatagFieldTestBase
Base class for ensuring that the Metatag field works correctly.

Namespace

Drupal\Tests\metatag\Functional

Code

public function tofixTestEntityField() {

  // Add a field to the entity type.
  $this
    ->addField();

  // Create a test entity.
  $this
    ->drupalGet($this->entityAddPath);
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextNotContains('Fatal error');
  $edit = $this
    ->entityDefaultValues($title) + [
    'field_metatag[0][basic][metatag_test_tag]' => 'Kilimanjaro',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $entities = \Drupal::entityTypeManager()
    ->getStorage('entity_test')
    ->loadByProperties([
    $this->entityTitleField => 'Barfoo',
  ]);
  $this
    ->assertCount(1, $entities, 'Entity was saved');
  $entity = reset($entities);

  // Make sure tags that have a field value but no default value still show
  // up.
  $this
    ->drupalGet($entity
    ->toUrl());
  $session
    ->statusCodeEquals(200);
  $elements = $this
    ->cssSelect('meta[name=metatag_test_tag]');
  $this
    ->assertCount(1, $elements, 'Found keywords metatag_test_tag from defaults');
  $this
    ->assertEquals((string) $elements[0]['content'], 'Kilimanjaro', 'Field value for metatag_test_tag found when no default set.');

  // @todo This should not be required, but meta tags does not invalidate
  // cache upon setting globals.
  Cache::invalidateTags([
    'entity_test:' . $entity
      ->id(),
  ]);

  // Update the Global defaults and test them.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $values = [
    'metatag_test_tag' => 'Purple monkey dishwasher',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');
  $this
    ->drupalGet($entity
    ->toUrl());
  $session
    ->statusCodeEquals(200);
  $elements = $this
    ->cssSelect('meta[name=metatag_test_tag]');
  $this
    ->assertCount(1, $elements, 'Found test metatag from defaults');
  $this
    ->verbose('<pre>' . print_r($elements, TRUE) . '</pre>');
  $this
    ->assertEquals((string) $elements[0]['content'], $values['metatag_test_tag']);
}