You are here

public function MetatagAdminTest::testDefaults in Metatag 8

Tests the interface to manage metatag defaults.

File

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

Class

MetatagAdminTest
Tests the Metatag administration.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testDefaults() {

  // Save the default title to test the Revert operation at the end.
  $metatag_defaults = \Drupal::config('metatag.metatag_defaults.global');
  $default_title = $metatag_defaults
    ->get('tags')['title'];

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

  // Check that the user can see the list of metatag defaults.
  $this
    ->drupalGet('admin/config/search/metatag');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);

  // Check that the Global defaults were created.
  $session
    ->linkByHrefExists('admin/config/search/metatag/global');

  // Check that Global and entity defaults can't be deleted.
  $session
    ->linkByHrefNotExists('admin/config/search/metatag/global/delete');
  $session
    ->linkByHrefNotExists('admin/config/search/metatag/node/delete');

  // Check that the module defaults were injected into the Global config
  // entity.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $this
    ->assertFieldById('edit-title', $metatag_defaults
    ->get('title'), $this
    ->t('Metatag defaults were injected into the Global configuration entity.'));

  // Update the Global defaults and test them.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $values = [
    'title' => 'Test title',
    'description' => 'Test description',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');
  $this
    ->drupalGet('hit-a-404');
  $session
    ->statusCodeEquals(404);
  foreach ($values as $metatag => $value) {
    $session
      ->responseContains($value, $this
      ->t('Updated metatag @tag was found in the HEAD section of the page.', [
      '@tag' => $metatag,
    ]));
  }

  // Check that tokens are processed.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $values = [
    'title' => '[site:name] | Test title',
    'description' => '[site:name] | Test description',
  ];
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');
  drupal_flush_all_caches();
  $this
    ->drupalGet('hit-a-404');
  $session
    ->statusCodeEquals(404);
  foreach ($values as $metatag => $value) {
    $processed_value = \Drupal::token()
      ->replace($value);
    $session
      ->responseContains($processed_value, $this
      ->t('Processed token for metatag @tag was found in the HEAD section of the page.', [
      '@tag' => $metatag,
    ]));
  }

  // Test the Robots plugin.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session
    ->statusCodeEquals(200);
  $robots_values = [
    'index',
    'follow',
    'noydir',
  ];
  $values = [];
  foreach ($robots_values as $value) {
    $values['robots[' . $value . ']'] = TRUE;
  }
  $this
    ->drupalPostForm(NULL, $values, 'Save');
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');
  drupal_flush_all_caches();

  // Trigger a 404 request.
  $this
    ->drupalGet('hit-a-404');
  $session
    ->statusCodeEquals(404);
  $robots_value = implode(', ', $robots_values);
  $session
    ->responseContains($robots_value, $this
    ->t('Robots metatag has the expected values.'));

  // Test reverting global configuration to its defaults.
  $this
    ->drupalGet('admin/config/search/metatag/global/revert');
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [], 'Revert');
  $session
    ->pageTextContains('Reverted Global defaults.');
  $session
    ->pageTextContains($default_title);
  $this
    ->drupalLogout();
}