diff --git a/lib/Drupal/views/Tests/Plugin/DisplayTest.php b/lib/Drupal/views/Tests/Plugin/DisplayTest.php index 90d6432709485619efa527cb0df0c333e00e54fd..813e65bb8df64410fff7d14882132669a762365f 100644 --- a/lib/Drupal/views/Tests/Plugin/DisplayTest.php +++ b/lib/Drupal/views/Tests/Plugin/DisplayTest.php @@ -7,11 +7,21 @@ namespace Drupal\views\Tests\Plugin; +use Drupal\views\ViewDisplay; +use Drupal\views_test_data\Plugin\views\display\DisplayTest as DisplayTestPlugin; + /** * Tests the basic display plugin. */ class DisplayTest extends PluginTestBase { + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('views_ui'); + public static function getInfo() { return array( 'name' => 'Display', @@ -20,10 +30,83 @@ public static function getInfo() { ); } + public function setUp() { + parent::setUp(); + + $this->enableViewsTestModule(); + + $this->adminUser = $this->drupalCreateUser(array('administer views')); + $this->drupalLogin($this->adminUser); + + // Create 10 nodes. + $this->nodes = array(); + for ($i = 0; $i < 11; $i++) { + $this->drupalCreateNode(array('promote' => TRUE)); + } + } + + /** + * Tests the display test plugin. + * + * @see Drupal\views_test_data\Plugin\views\display\DisplayTest + */ + function testDisplayPlugin() { + $view = views_get_view('frontpage'); + + // Add a new 'display_test' display and test it's there. + $view->addDisplay('display_test'); + + $this->assertTrue(isset($view->display['display_test_1']), 'Added display has been assigned to "display_test_1"'); + + // Create an expected ViewDisplay and check that it's equal. + $options = array( + 'display_options' => array(), + 'display_plugin' => 'display_test', + 'id' => 'display_test_1', + 'display_title' => 'Display test', + 'position' => NULL, + ); + $expected_display = new ViewDisplay($options); + $this->assertEqual($view->display['display_test_1'], $expected_display); + + $view->setDisplay('display_test_1'); + + $this->assertTrue($view->display_handler instanceof DisplayTestPlugin, 'The correct display handler instance is on the view object.'); + + // Check the test option. + $this->assertIdentical($view->display_handler->getOption('test_option'), ''); + + $output = $view->preview(); + + $this->assertTrue(strpos($output, '<h1></h1>') !== FALSE, 'An empty value for test_option found in output.'); + + // Change this option and check the title of out output. + $view->display_handler->overrideOption('test_option', 'Test option title'); + + $view->save(); + $output = $view->preview(); + + // Test we have our custom <h1> tag in the output of the view. + $this->assertTrue(strpos($output, '<h1>Test option title</h1>') !== FALSE, 'The test_option value found in display output title.'); + + // Test that the display category/summary is in the UI. + $this->drupalGet('admin/structure/views/view/frontpage/edit/display_test_1'); + $this->assertText('Display test settings'); + + $this->clickLink('Test option title'); + + $this->randomString = $this->randomString(); + $this->drupalPost(NULL, array('test_option' => $this->randomString), t('Apply')); + + // Check the new value has been saved by checking the UI summary text. + $this->drupalGet('admin/structure/views/view/frontpage/edit/display_test_1'); + $this->assertText($this->randomString); + } + /** * Tests the overriding of filter_groups. */ - function testFilterGroupsOverriding() { + public function testFilterGroupsOverriding() { $view = $this->createViewFromConfig('test_filter_groups'); $view->initDisplay(); diff --git a/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1cc7ef5c7a4f2a58829fb3d62858420a1ef06c6c --- /dev/null +++ b/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/display/DisplayTest.php @@ -0,0 +1,134 @@ +<?php + +/** + * @file + * Definition of Drupal\views_test_data\Plugin\views\display\DisplayTest. + */ + +namespace Drupal\views_test_data\Plugin\views\display; + +use Drupal\views\Plugin\views\display\DisplayPluginBase; +use Drupal\Core\Annotation\Plugin; +use Drupal\Core\Annotation\Translation; + +/** + * Defines a Display test plugin. + * + * @Plugin( + * id = "display_test", + * title = @Translation("Display test"), + * theme = "views_view", + * contextual_links_locations = {"view"} + * ) + */ +class DisplayTest extends DisplayPluginBase { + + /** + * Whether the display allows attachments. + * + * @var bool + */ + protected $usesAttachments = TRUE; + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions(). + */ + protected function defineOptions() { + $options = parent::defineOptions(); + $options['test_option'] = array('default' => ''); + + return $options; + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummaryv(). + */ + public function optionsSummary(&$categories, &$options) { + parent::optionsSummary($categories, $options); + + $categories['display_test'] = array( + 'title' => t('Display test settings'), + 'column' => 'second', + 'build' => array( + '#weight' => -100, + ), + ); + + $test_option = $this->getOption('test_option') ?: t('Empty'); + + $options['test_option'] = array( + 'category' => 'display_test', + 'title' => t('Test option'), + 'value' => views_ui_truncate($test_option, 24), + ); + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::buildOptionsForm(). + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + + switch ($form_state['section']) { + case 'test_option': + $form['#title'] .= t('Test option'); + $form['test_option'] = array( + '#type' => 'textfield', + '#description' => t('This is a textfield for test_option.'), + '#default_value' => $this->getOption('test_option'), + ); + break; + } + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::validateOptionsForm(). + */ + public function validateOptionsForm(&$form, &$form_state) { + parent::validateOptionsForm($form, $form_state); + watchdog('views', $form_state['values']['test_option']); + switch ($form_state['section']) { + case 'test_option': + if (!trim($form_state['values']['test_option'])) { + form_error($form['test_option'], t('You cannot have an empty option.')); + } + break; + } + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::submitOptionsForm(). + */ + public function submitOptionsForm(&$form, &$form_state) { + parent::submitOptionsForm($form, $form_state); + switch ($form_state['section']) { + case 'test_option': + $this->setOption('test_option', $form_state['values']['test_option']); + break; + } + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::execute(). + */ + public function execute() { + $this->view->build(); + + // Render the test option as the title before the view output. + $render = '<h1>' . filter_xss_admin($this->options['test_option']) . '</h1>'; + // And now render the view. + $render .= $this->view->render(); + + return $render; + } + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::preview(). + * + * Override so preview and execute are the same output. + */ + public function preview() { + return $this->execute(); + } + +}