You are here

public function DisabledDefaultTags::testEntityTypeDefaults in Metatag 8

Test that a disabled Node metatag default doesn't load.

Throws

\Behat\Mink\Exception\ExpectationException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityMalformedException

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/DisabledDefaultTags.php, line 168

Class

DisabledDefaultTags
Verify that disabled metatag defaults do not load.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testEntityTypeDefaults() {
  $node = $this
    ->createContentTypeNode();
  $this_page_url = $node
    ->toUrl('canonical', [
    'absolute' => TRUE,
  ])
    ->toString();

  // Change the node type default's canonical to a hardcoded test string.
  // Will be inherited by node:page, as normally neither has canonical filled
  // in and inherit it anyway from Global.

  /** @var \Drupal\Core\Entity\EntityStorageInterface $global_metatag_manager */
  $global_metatag_manager = \Drupal::entityTypeManager()
    ->getStorage('metatag_defaults');

  /** @var \Drupal\metatag\Entity\MetatagDefaults $entity_metatags */
  $entity_metatags = $global_metatag_manager
    ->load('node');
  $entity_metatags
    ->overwriteTags([
    'canonical_url' => 'https://test.canonical',
  ]);
  $entity_metatags
    ->save();

  // Load the node's entity page.
  $this
    ->drupalGet($this_page_url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Check the meta tags.
  $xpath = $this
    ->xpath("//link[@rel='canonical']");
  $this
    ->assertEquals((string) $xpath[0]
    ->getAttribute('href'), 'https://test.canonical');

  // Now disable this metatag.
  $entity_metatags
    ->set('status', 0);
  $entity_metatags
    ->save();

  // Clear caches.
  drupal_flush_all_caches();

  // Load the node's entity page.
  $this
    ->drupalGet($this_page_url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Check the meta tags.
  $xpath = $this
    ->xpath("//link[@rel='canonical']");

  // Should now match global or content one, which is node URL.
  $this
    ->assertEquals((string) $xpath[0]
    ->getAttribute('href'), $this_page_url);
}