public function RowPluginTest::testRowPlugin

Tests the node row plugin.

File

drupal/core/modules/node/lib/Drupal/node/Tests/Views/RowPluginTest.php, line 108
Contains \Drupal\node\Tests\Views\RowPluginTest.

Class

RowPluginTest
Tests the node row plugin.

Namespace

Drupal\node\Tests\Views

Code

public function testRowPlugin() {
  $view = views_get_view('test_node_row_plugin');
  $view
    ->initDisplay();
  $view
    ->setDisplay('page_1');
  $view
    ->initStyle();
  $view->rowPlugin->options['view_mode'] = 'full';

  // Test with view_mode full.
  $output = $view
    ->preview();
  $output = drupal_render($output);
  foreach ($this->nodes as $node) {
    $body = $node->body;
    $teaser = $body[Language::LANGCODE_NOT_SPECIFIED][0]['summary'];
    $full = $body[Language::LANGCODE_NOT_SPECIFIED][0]['value'];
    $this
      ->assertFalse(strpos($output, $teaser) !== FALSE, 'Make sure the teaser appears in the output of the view.');
    $this
      ->assertTrue(strpos($output, $full) !== FALSE, 'Make sure the full text appears in the output of the view.');
  }

  // Test with teasers.
  $view->rowPlugin->options['view_mode'] = 'teaser';
  $output = $view
    ->preview();
  $output = drupal_render($output);
  foreach ($this->nodes as $node) {
    $body = $node->body;
    $teaser = $body[Language::LANGCODE_NOT_SPECIFIED][0]['summary'];
    $full = $body[Language::LANGCODE_NOT_SPECIFIED][0]['value'];
    $this
      ->assertTrue(strpos($output, $teaser) !== FALSE, 'Make sure the teaser appears in the output of the view.');
    $this
      ->assertFalse(strpos($output, $full) !== FALSE, 'Make sure the full text does not appears in the output of the view if teaser is set as viewmode.');
  }

  // Test with links disabled.
  $view->rowPlugin->options['links'] = FALSE;
  $output = $view
    ->preview();
  $output = drupal_render($output);
  $this
    ->drupalSetContent($output);
  foreach ($this->nodes as $node) {
    $this
      ->assertFalse($this
      ->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(
      ':class' => 'node-readmore',
      ':href' => "node/{$node->id()}",
    )), 'Make sure no readmore link appears.');
  }

  // Test with links enabled.
  $view->rowPlugin->options['links'] = TRUE;
  $output = $view
    ->preview();
  $output = drupal_render($output);
  $this
    ->drupalSetContent($output);
  foreach ($this->nodes as $node) {
    $this
      ->assertTrue($this
      ->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(
      ':class' => 'node-readmore',
      ':href' => "node/{$node->id()}",
    )), 'Make sure no readmore link appears.');
  }

  // Test with comments enabled.
  $view->rowPlugin->options['comments'] = TRUE;
  $output = $view
    ->preview();
  $output = drupal_render($output);
  foreach ($this->nodes as $node) {
    foreach ($this->comments[$node
      ->id()] as $comment) {
      $this
        ->assertTrue(strpos($output, $comment->comment_body->value) !== FALSE, 'Make sure the comment appears in the output.');
    }
  }

  // Test with comments disabled.
  $view->rowPlugin->options['comments'] = FALSE;
  $output = $view
    ->preview();
  $output = drupal_render($output);
  foreach ($this->nodes as $node) {
    foreach ($this->comments[$node
      ->id()] as $comment) {
      $this
        ->assertFalse(strpos($output, $comment->comment_body->value) !== FALSE, 'Make sure the comment does not appears in the output when the comments option disabled.');
    }
  }
}