From ef1f2eace6ef1cbde20dd5117c63dddae325aa5e Mon Sep 17 00:00:00 2001 From: dereine <dereine@99340.no-reply.drupal.org> Date: Wed, 19 Sep 2012 14:53:07 +0200 Subject: [PATCH] Issue #1754264 by dawehner: Add a generic testcase for the style base. --- lib/Drupal/views/Tests/Plugin/StyleTest.php | 37 ++++++++ .../Plugin/views/row/RowTest.php | 62 +++++++++++++ .../Plugin/views/style/StyleTest.php | 87 +++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/row/RowTest.php create mode 100644 tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/style/StyleTest.php diff --git a/lib/Drupal/views/Tests/Plugin/StyleTest.php b/lib/Drupal/views/Tests/Plugin/StyleTest.php index efada1d093a4..ed0efd8c789b 100644 --- a/lib/Drupal/views/Tests/Plugin/StyleTest.php +++ b/lib/Drupal/views/Tests/Plugin/StyleTest.php @@ -11,6 +11,8 @@ /** * Tests some general style plugin related functionality. + * + * @see Drupal\views_test_data\Plugin\views\style\StyleTest. */ class StyleTest extends StyleTestBase { @@ -22,6 +24,41 @@ public static function getInfo() { ); } + /** + * Tests the general renderering of styles. + */ + public function testStyle() { + $view = $this->getView(); + $view->display_handler->setOption('style_plugin', 'test_style'); + $view->initDisplay(); + $view->initStyle(); + $this->assertTrue($view->style_plugin instanceof \Drupal\views_test_data\Plugin\views\style\StyleTest, 'Make sure the right style plugin class is loaded.'); + + $random_text = $this->randomName(); + // Set some custom text to the output and make sure that this value is + // rendered. + $view->style_plugin->setOutput($random_text); + $output = $view->preview(); + $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the style plugin appears in the output of the view.'); + + // This run use the test row plugin and render with it. + $view = $this->getView(); + $view->display_handler->setOption('style_plugin', 'test_style'); + $view->display_handler->setOption('row_plugin', 'test_row'); + $view->initDisplay(); + $view->initStyle(); + $view->style_plugin->setUsesRowPlugin(TRUE); + // Reinitialize the style as it supports row plugins now. + $view->style_plugin->init($view, $view->display[$view->current_display], array()); + $this->assertTrue($view->style_plugin->row_plugin instanceof \Drupal\views_test_data\Plugin\views\row\RowTest, 'Make sure the right row plugin class is loaded.'); + + $random_text = $this->randomName(); + $view->style_plugin->row_plugin->setOutput($random_text); + + $output = $view->preview(); + $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Take sure that the rendering of the row plugin appears in the output of the view.'); + } + /** * Tests the grouping legacy features of styles. */ diff --git a/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/row/RowTest.php b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/row/RowTest.php new file mode 100644 index 000000000000..3176d6be84a8 --- /dev/null +++ b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/row/RowTest.php @@ -0,0 +1,62 @@ +<?php + +/** + * @file + * Definition of Drupal\views_test_data\Plugin\views\row\RowTest. + */ + +namespace Drupal\views_test_data\Plugin\views\row; + +use Drupal\Core\Annotation\Plugin; +use Drupal\Core\Annotation\Translation; +use Drupal\views\Plugin\views\row\RowPluginBase; + +/** + * Provides a general test row plugin. + * + * @ingroup views_row_plugins + * + * @Plugin( + * id = "test_row", + * title = @Translation("Test row plugin"), + * help = @Translation("Provides a generic row test plugin."), + * theme = "views_view_row_test", + * type = "normal" + * ) + */ +class RowTest extends RowPluginBase { + + /** + * A string which will be output when the view is rendered. + * + * @var string + */ + public $output; + + /** + * Sets the output property. + * + * @param string $output + * The string to output by this plugin. + */ + public function setOutput($output) { + $this->output = $output; + } + + /** + * Returns the output property. + * + * @return string + */ + public function getOutput() { + return $this->output; + } + + /** + * Overrides Drupal\views\Plugin\views\row\RowPluginBase::render() + */ + public function render($row) { + return $this->getOutput(); + } + +} diff --git a/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/style/StyleTest.php b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/style/StyleTest.php new file mode 100644 index 000000000000..27b21933fae3 --- /dev/null +++ b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/style/StyleTest.php @@ -0,0 +1,87 @@ +<?php + +/** + * @file + * Definition of Drupal\views_test_data\Plugin\views\style\StyleTest. + */ + +namespace Drupal\views_test_data\Plugin\views\style; + +use Drupal\Core\Annotation\Plugin; +use Drupal\Core\Annotation\Translation; +use Drupal\views\Plugin\views\style\StylePluginBase; + +/** + * Provides a general test style plugin. + * + * @ingroup views_style_plugins + * + * @Plugin( + * id = "test_style", + * title = @Translation("Test style plugin"), + * help = @Translation("Provides a generic style test plugin."), + * theme = "views_view_style_test", + * type = "normal" + * ) + */ +class StyleTest extends StylePluginBase { + + /** + * A string which will be output when the view is rendered. + * + * @var string + */ + public $output; + + function usesRowPlugin() { + return parent::usesRowPlugin(); + } + + /** + * Sets the usesRowPlugin property. + * + * @param bool $status + * TRUE if this style plugin should use rows. + */ + public function setUsesRowPlugin($status) { + $this->usesRowPlugin = $status; + } + + /** + * Sets the output property. + * + * @param string $output + * The string to output by this plugin. + */ + public function setOutput($output) { + $this->output = $output; + } + + /** + * Returns the output property. + * + * @return string + */ + public function getOutput() { + return $this->output; + } + + /** + * Overrides Drupal\views\Plugin\views\style\StylePluginBase::render() + */ + public function render() { + $output = ''; + if (!$this->usesRowPlugin()) { + $output = $this->getOutput(); + } + else { + foreach ($this->view->result as $index => $row) { + $this->view->row_index = $index; + $output .= $this->row_plugin->render($row) . "\n"; + } + } + + return $output; + } + +} -- GitLab