annotate core/modules/node/tests/src/Functional/NodeTitleTest.php @ 6:875880e46745

Styling
author Chris Cannam
date Fri, 08 Dec 2017 13:21:27 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\node\Functional;
Chris@0 4
Chris@0 5 use Drupal\comment\Tests\CommentTestTrait;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests node title.
Chris@0 10 *
Chris@0 11 * @group node
Chris@0 12 */
Chris@0 13 class NodeTitleTest extends NodeTestBase {
Chris@0 14
Chris@0 15 use CommentTestTrait;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to enable.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['comment', 'views', 'block'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * A user with permission to bypass access content.
Chris@0 26 *
Chris@0 27 * @var \Drupal\user\UserInterface
Chris@0 28 */
Chris@0 29 protected $adminUser;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * {@inheritdoc}
Chris@0 33 */
Chris@0 34 protected function setUp() {
Chris@0 35 parent::setUp();
Chris@0 36 $this->drupalPlaceBlock('system_breadcrumb_block');
Chris@0 37
Chris@0 38 $this->adminUser = $this->drupalCreateUser(['administer nodes', 'create article content', 'create page content', 'post comments']);
Chris@0 39 $this->drupalLogin($this->adminUser);
Chris@0 40 $this->addDefaultCommentField('node', 'page');
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Creates one node and tests if the node title has the correct value.
Chris@0 45 */
Chris@0 46 public function testNodeTitle() {
Chris@0 47 // Create "Basic page" content with title.
Chris@0 48 // Add the node to the frontpage so we can test if teaser links are
Chris@0 49 // clickable.
Chris@0 50 $settings = [
Chris@0 51 'title' => $this->randomMachineName(8),
Chris@0 52 'promote' => 1,
Chris@0 53 ];
Chris@0 54 $node = $this->drupalCreateNode($settings);
Chris@0 55
Chris@0 56 // Test <title> tag.
Chris@0 57 $this->drupalGet('node/' . $node->id());
Chris@0 58 $xpath = '//title';
Chris@0 59 $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label() . ' | Drupal', 'Page title is equal to node title.', 'Node');
Chris@0 60
Chris@0 61 // Test breadcrumb in comment preview.
Chris@0 62 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
Chris@0 63 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
Chris@0 64 $this->assertEqual($this->xpath($xpath)[0]->getText(), $node->label(), 'Node breadcrumb is equal to node title.', 'Node');
Chris@0 65
Chris@0 66 // Test node title in comment preview.
Chris@0 67 $this->assertEqual($this->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]/h2/a/span', [':node-class' => ' node--type-' . $node->bundle() . ' '])[0]->getText(), $node->label(), 'Node preview title is equal to node title.', 'Node');
Chris@0 68
Chris@0 69 // Test node title is clickable on teaser list (/node).
Chris@0 70 $this->drupalGet('node');
Chris@0 71 $this->clickLink($node->label());
Chris@0 72
Chris@0 73 // Test edge case where node title is set to 0.
Chris@0 74 $settings = [
Chris@0 75 'title' => 0,
Chris@0 76 ];
Chris@0 77 $node = $this->drupalCreateNode($settings);
Chris@0 78 // Test that 0 appears as <title>.
Chris@0 79 $this->drupalGet('node/' . $node->id());
Chris@0 80 $this->assertTitle(0 . ' | Drupal', 'Page title is equal to 0.', 'Node');
Chris@0 81 // Test that 0 appears in the template <h1>.
Chris@0 82 $xpath = '//h1';
Chris@0 83 $this->assertEqual(current($this->xpath($xpath)), 0, 'Node title is displayed as 0.', 'Node');
Chris@0 84
Chris@0 85 // Test edge case where node title contains special characters.
Chris@0 86 $edge_case_title = 'article\'s "title".';
Chris@0 87 $settings = [
Chris@0 88 'title' => $edge_case_title,
Chris@0 89 ];
Chris@0 90 $node = $this->drupalCreateNode($settings);
Chris@0 91 // Test that the title appears as <title>. The title will be escaped on the
Chris@0 92 // the page.
Chris@0 93 $edge_case_title_escaped = Html::escape($edge_case_title);
Chris@0 94 $this->drupalGet('node/' . $node->id());
Chris@0 95 $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
Chris@0 96
Chris@0 97 // Test that the title appears as <title> when reloading the node page.
Chris@0 98 $this->drupalGet('node/' . $node->id());
Chris@0 99 $this->assertRaw('<title>' . $edge_case_title_escaped . ' | Drupal</title>', 'Page title is equal to article\'s "title".', 'Node');
Chris@0 100
Chris@0 101 }
Chris@0 102
Chris@0 103 }