You are here

public function MetatagFrontpageTest::testFrontPageMetatagDisabledConfig in Metatag 8

Test front page meta tags when front page config is disabled.

File

tests/src/Functional/MetatagFrontpageTest.php, line 137

Class

MetatagFrontpageTest
Ensures that meta tags are rendering correctly on home page.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testFrontPageMetatagDisabledConfig() {

  // Disable front page metatag, enable node metatag & check.
  $this
    ->drupalGet('admin/config/search/metatag/front/delete');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [], $this
    ->t('Delete'));
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextContains('Deleted Front page defaults.');

  // Update the Metatag Node defaults.
  $this
    ->drupalGet('admin/config/search/metatag/node');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => 'Test title for a node.',
    'description' => 'Test description for a node.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $session
    ->pageTextContains('Saved the Content Metatag defaults.');
  $this
    ->drupalGet('<front>');
  foreach ($edit as $metatag => $metatag_value) {
    $xpath = $this
      ->xpath("//meta[@name='" . $metatag . "']");
    if ($metatag == 'title') {
      $this
        ->assertCount(0, $xpath, 'Title meta tag not found.');
      $xpath = $this
        ->xpath("//title");
      $this
        ->assertCount(1, $xpath, 'Head title tag found.');
      $value = $xpath[0]
        ->getText();
    }
    else {
      $this
        ->assertCount(1, $xpath, 'Exactly one ' . $metatag . ' meta tag found.');
      $value = $xpath[0]
        ->getAttribute('content');
    }
    $this
      ->assertEquals($value, $metatag_value);
  }

  // Change the front page to a valid path.
  $this
    ->drupalGet('admin/config/system/site-information');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'site_frontpage' => '/test-page',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save configuration'));
  $session
    ->pageTextContains('The configuration options have been saved.');

  // Front page is custom route.
  // Update the Metatag Node global.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => 'Test title.',
    'description' => 'Test description.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');

  // Test Metatags.
  $this
    ->drupalGet('test-page');
  $session
    ->statusCodeEquals(200);
  foreach ($edit as $metatag => $metatag_value) {
    $xpath = $this
      ->xpath("//meta[@name='" . $metatag . "']");
    if ($metatag == 'title') {
      $this
        ->assertCount(0, $xpath, 'Title meta tag not found.');
      $xpath = $this
        ->xpath("//title");
      $this
        ->assertCount(1, $xpath, 'Head title tag found.');
      $value = $xpath[0]
        ->getText();
    }
    else {
      $this
        ->assertCount(1, $xpath, 'Exactly one ' . $metatag . ' meta tag found.');
      $value = $xpath[0]
        ->getAttribute('content');
    }
    $this
      ->assertEquals($value, $metatag_value);
  }
}