You are here

public function MetatagAdminTest::testOverrides in Metatag 8

Tests entity and bundle overrides.

File

tests/src/Functional/MetatagAdminTest.php, line 254

Class

MetatagAdminTest
Tests the Metatag administration.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testOverrides() {

  // Initiate session with a user who can manage metatags.
  $permissions = [
    'administer site configuration',
    'administer meta tags',
    'access content',
    'create article content',
    'administer nodes',
    'create article content',
    'create page content',
  ];
  $account = $this
    ->drupalCreateUser($permissions);
  $this
    ->drupalLogin($account);

  // Update the Metatag Node defaults.
  $this
    ->drupalGet('admin/config/search/metatag/node');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $values = [
    'title' => 'Test title for a node.',
    'description' => 'Test description for a node.',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Content Metatag defaults.');

  // Create a test node.
  $node = $this
    ->drupalCreateNode([
    'title' => $this
      ->t('Hello, world!'),
    'type' => 'article',
  ]);

  // Check that the new values are found in the response.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $session
    ->statusCodeEquals(200);
  foreach ($values as $metatag => $value) {
    $session
      ->responseContains($value, $this
      ->t('Node metatag @tag overrides Global defaults.', [
      '@tag' => $metatag,
    ]));
  }

  // Check that when the node defaults don't define a metatag, the Global one
  // is used.
  // First unset node defaults.
  $this
    ->drupalGet('admin/config/search/metatag/node');
  $session
    ->statusCodeEquals(200);
  $values = [
    'title' => '',
    'description' => '',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Content Metatag defaults.');

  // Then, set global ones.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $values = [
    'title' => 'Global title',
    'description' => 'Global description',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');

  // Next, test that global defaults are rendered since node ones are empty.
  // We are creating a new node as doing a get on the previous one would
  // return cached results.
  // @todo BookTest.php resets the cache of a single node, which is way more
  // performant than creating a node for every set of assertions.
  // @see BookTest::testDelete()
  $node = $this
    ->drupalCreateNode([
    'title' => $this
      ->t('Hello, world!'),
    'type' => 'article',
  ]);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $session
    ->statusCodeEquals(200);
  foreach ($values as $metatag => $value) {
    $session
      ->responseContains($value, $this
      ->t('Found global @tag tag as Node does not set it.', [
      '@tag' => $metatag,
    ]));
  }

  // Now create article overrides and then test them.
  $this
    ->drupalGet('admin/config/search/metatag/add');
  $session
    ->statusCodeEquals(200);
  $values = [
    'id' => 'node__article',
    'title' => 'Article title override',
    'description' => 'Article description override',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains(strip_tags('Created the Content: Article Metatag defaults.'));

  // Confirm the fields load properly on the node/add/article page.
  $node = $this
    ->drupalCreateNode([
    'title' => $this
      ->t('Hello, world!'),
    'type' => 'article',
  ]);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $session
    ->statusCodeEquals(200);
  unset($values['id']);
  foreach ($values as $metatag => $value) {
    $session
      ->responseContains($value, $this
      ->t('Found bundle override for tag @tag.', [
      '@tag' => $metatag,
    ]));
  }

  // Test deleting the article defaults.
  $this
    ->drupalGet('admin/config/search/metatag/node__article/delete');
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [], 'Delete');
  $session
    ->pageTextContains('Deleted Content: Article defaults.');
}