From 587ceeb417d62d6ade319da2c0da32e01c1afd22 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 8 Apr 2013 10:40:50 +0100 Subject: [PATCH] Issue #1793696 by dawehner, tim.plunkett, damiankloip: Fixed views_preprocess_node() check for the wrong row_plugin(). --- .../Drupal/node/Tests/Views/RowPluginTest.php | 176 ++++++++++++++++++ .../views.view.test_node_row_plugin.yml | 56 ++++++ core/modules/views/views.module | 3 +- 3 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 core/modules/node/lib/Drupal/node/Tests/Views/RowPluginTest.php create mode 100644 core/modules/node/tests/modules/node_test_views/test_views/views.view.test_node_row_plugin.yml diff --git a/core/modules/node/lib/Drupal/node/Tests/Views/RowPluginTest.php b/core/modules/node/lib/Drupal/node/Tests/Views/RowPluginTest.php new file mode 100644 index 00000000000..6a28f7f3334 --- /dev/null +++ b/core/modules/node/lib/Drupal/node/Tests/Views/RowPluginTest.php @@ -0,0 +1,176 @@ + 'Node: Row plugin', + 'description' => 'Tests the node row plugin.', + 'group' => 'Views module integration', + ); + } + + protected function setUp() { + parent::setUp(); + + $this->drupalCreateContentType(array('type' => 'article')); + + // Create two nodes, with 5 comments on all of them. + for ($i = 0; $i < 2; $i++) { + $this->nodes[] = $this->drupalCreateNode( + array( + 'type' => 'article', + 'body' => array( + array( + 'value' => $this->randomName(42), + 'format' => filter_default_format(), + 'summary' => $this->randomName(), + ), + ), + ) + ); + } + + foreach ($this->nodes as $node) { + for ($i = 0; $i < 5; $i++) { + $this->comments[$node->id()][] = $this->drupalCreateComment(array('nid' => $node->id())); + } + } + } + + /** + * Helper function to create a random comment. + * + * @param array $settings + * (optional) An associative array of settings for the comment, as used in + * entity_create(). + * + * @return \Drupal\comment\Plugin\Core\Entity\Comment + * Returns the created and saved comment. + */ + public function drupalCreateComment(array $settings = array()) { + $node = node_load($settings['nid']); + $settings += array( + 'subject' => $this->randomName(), + 'node_type' => "comment_node_{$node->bundle()}", + 'comment_body' => $this->randomName(40), + ); + + $comment = entity_create('comment', $settings); + $comment->save(); + return $comment; + } + + /** + * Tests the node row plugin. + */ + 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_NOT_SPECIFIED][0]['summary']; + $full = $body[LANGUAGE_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_NOT_SPECIFIED][0]['summary']; + $full = $body[LANGUAGE_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.'); + } + } + } + +} diff --git a/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_node_row_plugin.yml b/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_node_row_plugin.yml new file mode 100644 index 00000000000..91ee12d7c76 --- /dev/null +++ b/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_node_row_plugin.yml @@ -0,0 +1,56 @@ +base_field: nid +base_table: node +core: 8 +description: '' +status: '1' +display: + default: + display_options: + access: + type: perm + cache: + type: none + exposed_form: + type: basic + filters: + status: + expose: + operator: '0' + field: status + group: '1' + id: status + table: node + value: '1' + plugin_id: boolean + pager: + options: + items_per_page: '10' + type: full + query: + type: views_query + row: + options: + build_mode: teaser + comments: '0' + links: '1' + type: node + sorts: { } + style: + type: default + title: test_node_row_plugin + display_plugin: default + display_title: Master + id: default + position: { } + page_1: + display_options: + path: test-node-row-plugin + display_plugin: page + display_title: Page + id: page_1 + position: { } +label: test_node_row_plugin +langcode: en +module: views +id: test_node_row_plugin +tag: default diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 58a7f358a2a..3bc0bd11df9 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -236,6 +236,7 @@ function views_plugin_list() { * node portion of the theme registry. */ function views_preprocess_node(&$vars) { + Drupal::moduleHandler()->loadInclude('node', 'views.inc'); // The 'view' attribute of the node is added in views_preprocess_node() if (!empty($vars['node']->view) && $vars['node']->view->storage->id()) { $vars['view'] = $vars['node']->view; @@ -252,7 +253,7 @@ function views_preprocess_node(&$vars) { } // Allow to alter comments and links based on the settings in the row plugin. - if (!empty($vars['view']->style_plugin->row_plugin) && get_class($vars['view']->style_plugin->row_plugin) == 'views_plugin_row_node_view') { + if (!empty($vars['view']->rowPlugin) && $vars['view']->rowPlugin->getPluginId() == 'node') { node_row_node_view_preprocess_node($vars); } }