You are here

public function MetatagFrontpageTest::testFrontPageMetatagsEnabledConfig in Metatag 8

The front page config is enabled, its meta tags should be used.

File

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

Class

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

Namespace

Drupal\Tests\metatag\Functional

Code

public function testFrontPageMetatagsEnabledConfig() {

  // Add something to the front page config.
  $this
    ->drupalGet('admin/config/search/metatag/front');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => 'Test title',
    'description' => 'Test description',
    'keywords' => 'testing,keywords',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextContains('Saved the Front page Metatag defaults.');

  // Testing front page metatags.
  $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);
  }
  $node_path = '/node/' . $this->nodeId;

  // Testing front page metatags.
  $this
    ->drupalGet($node_path);
  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 custom route.
  $site_edit = [
    'site_frontpage' => '/test-page',
  ];
  $this
    ->drupalGet('admin/config/system/site-information');
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, $site_edit, $this
    ->t('Save configuration'));
  $session
    ->pageTextContains('The configuration options have been saved.');
  return;

  // @todo Finish this?
  $this
    ->drupalGet('test-page');
  $session
    ->statusCodeEquals(200);
  foreach ($edit as $metatag => $metatag_value) {
    $xpath = $this
      ->xpath("//meta[@name='" . $metatag . "']");
    $this
      ->assertCount(1, $xpath, 'Exactly one ' . $metatag . ' meta tag found.');
    $value = $xpath[0]
      ->getAttribute('content');
    $this
      ->assertEquals($value, $metatag_value);
  }
}