You are here

public function MetatagFieldTestBase::testEntityFieldValuesNewEntity in Metatag 8

Confirm that the default values load correctly.

For an entity created after the custom field is added.

File

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

Class

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

Namespace

Drupal\Tests\metatag\Functional

Code

public function testEntityFieldValuesNewEntity() {

  // Set a global default.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $global_values = [
    'metatag_test_tag' => 'Global description',
  ];
  $this
    ->drupalPostForm(NULL, $global_values, 'Save');
  $session
    ->pageTextContains(strip_tags('Saved the Global Metatag defaults.'));

  // Set an entity default if it's supported by the entity type.
  if ($this->entitySupportsDefaults) {
    $this
      ->drupalGet('admin/config/search/metatag/' . $this->entityType);
    $session
      ->statusCodeEquals(200);
    $entity_values = [
      'metatag_test_tag' => 'Entity description',
    ];
    $this
      ->drupalPostForm(NULL, $entity_values, 'Save');
    $session
      ->pageTextContains(strip_tags("Saved the {$this->entityLabel} Metatag defaults."));
  }

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

  // Load the entity form for this entity type.
  $title = 'Barfoo';
  $this
    ->drupalGet($this->entityAddPath);
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextNotContains('Fatal error');

  // If this entity type supports defaults then verify the global default is
  // not present but that the entity default *is* present.
  if ($this->entitySupportsDefaults) {
    $session
      ->fieldValueNotEquals('field_metatag[0][basic][metatag_test_tag]', $global_values['metatag_test_tag']);
    $session
      ->fieldValueEquals('field_metatag[0][basic][metatag_test_tag]', $entity_values['metatag_test_tag']);
  }
  else {
    $session
      ->fieldValueEquals('field_metatag[0][basic][metatag_test_tag]', $global_values['metatag_test_tag']);
  }

  // Allow the fields to be customized if needed.
  $edit = $this
    ->entityDefaultValues();
  if (empty($edit)) {
    $edit = [
      $this->entityTitleField . '[0][value]' => $title,
    ];
  }

  // Create a new entity object.
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t($this->entitySaveButtonLabel));
  $entities = \Drupal::entityTypeManager()
    ->getStorage($this->entityType)
    ->loadByProperties([
    $this->entityTitleField => $title,
  ]);
  $this
    ->assertCount(1, $entities, 'Entity was saved');
  $entity = reset($entities);

  // @todo Confirm the values output correctly.
  // Open the 'edit' form for the entity.
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $session
    ->statusCodeEquals(200);

  // If this entity type supports defaults then verify the global default is
  // not present but that the entity default *is* present.
  if ($this->entitySupportsDefaults) {
    $session
      ->fieldValueNotEquals('field_metatag[0][basic][metatag_test_tag]', $global_values['metatag_test_tag']);
    $session
      ->fieldValueEquals('field_metatag[0][basic][metatag_test_tag]', $entity_values['metatag_test_tag']);
  }
  else {
    $session
      ->fieldValueEquals('field_metatag[0][basic][metatag_test_tag]', $global_values['metatag_test_tag']);
  }

  // @todo Confirm the values output correctly.
}