function NodeViewTest::testHtmlHeadLinks

Tests the html head links.

File

core/modules/node/tests/src/Functional/NodeViewTest.php, line 23

Class

NodeViewTest
Tests the node/{node} page.

Namespace

Drupal\Tests\node\Functional

Code

public function testHtmlHeadLinks() {
  $node = $this->drupalCreateNode();
  $this->drupalGet($node->toUrl());
  $result = $this->xpath('//link[@rel = "canonical"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl()
    ->setAbsolute()
    ->toString());
  // Link relations are checked for access for anonymous users.
  $result = $this->xpath('//link[@rel = "version-history"]');
  $this->assertEmpty($result, 'Version history not present for anonymous users without access.');
  $result = $this->xpath('//link[@rel = "edit-form"]');
  $this->assertEmpty($result, 'Edit form not present for anonymous users without access.');
  $this->drupalLogin($this->createUser([
    'access content',
  ]));
  $this->drupalGet($node->toUrl());
  $result = $this->xpath('//link[@rel = "canonical"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl()
    ->setAbsolute()
    ->toString());
  // Link relations are present regardless of access for authenticated users.
  $result = $this->xpath('//link[@rel = "version-history"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl('version-history')
    ->setAbsolute()
    ->toString());
  $result = $this->xpath('//link[@rel = "edit-form"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl('edit-form')
    ->setAbsolute()
    ->toString());
  // Give anonymous users access to edit the node. Do this through the UI to
  // ensure caches are handled properly.
  $this->drupalLogin($this->rootUser);
  $edit = [
    'anonymous[edit own ' . $node->bundle() . ' content]' => TRUE,
  ];
  $this->drupalPostForm('admin/people/permissions', $edit, 'Save permissions');
  $this->drupalLogout();
  // Anonymous user's should now see the edit-form link but not the
  // version-history link.
  $this->drupalGet($node->toUrl());
  $result = $this->xpath('//link[@rel = "canonical"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl()
    ->setAbsolute()
    ->toString());
  $result = $this->xpath('//link[@rel = "version-history"]');
  $this->assertEmpty($result, 'Version history not present for anonymous users without access.');
  $result = $this->xpath('//link[@rel = "edit-form"]');
  $this->assertEqual($result[0]->getAttribute('href'), $node->toUrl('edit-form')
    ->setAbsolute()
    ->toString());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.