Newer
Older
* Contains \Drupal\views\Tests\Plugin\DisplayTest.
namespace Drupal\views\Tests\Plugin;
use Drupal\views_test_data\Plugin\views\display\DisplayTest as DisplayTestPlugin;
* Tests the basic display plugin.
class DisplayTest extends PluginTestBase {
catch
committed
/**
* Views used by this test.
*
* @var array
*/
Dries Buytaert
committed
public static $testViews = array('test_filter_groups', 'test_get_attach_displays', 'test_view', 'test_display_more', 'test_display_invalid');
catch
committed
/**
* Modules to enable.
*
* @var array
*/
Dries Buytaert
committed
public static $modules = array('views_ui', 'node', 'block');
public static function getInfo() {
return array(
'name' => 'Display',
'description' => 'Tests the basic display plugin.',
'group' => 'Views Plugins',
);
}
public function setUp() {
parent::setUp();
$this->enableViewsTestModule();
$this->adminUser = $this->drupalCreateUser(array('administer views'));
$this->drupalLogin($this->adminUser);
// Create 10 nodes.
Tim Plunkett
committed
for ($i = 0; $i <= 10; $i++) {
$this->drupalCreateNode(array('promote' => TRUE));
}
}
/**
* Tests the display test plugin.
*
* @see Drupal\views_test_data\Plugin\views\display\DisplayTest
*/
public function testDisplayPlugin() {
$view = views_get_view('test_view');
// Add a new 'display_test' display and test it's there.
Tim Plunkett
committed
$view->storage->addDisplay('display_test');
Angie Byron
committed
$displays = $view->storage->get('display');
Angie Byron
committed
$this->assertTrue(isset($displays['display_test_1']), 'Added display has been assigned to "display_test_1"');
Daniel Wehner
committed
// Check the the display options are like expected.
$options = array(
'display_options' => array(),
'display_plugin' => 'display_test',
'id' => 'display_test_1',
'display_title' => 'Display test',
Alex Pott
committed
'position' => 1,
);
Angie Byron
committed
$this->assertEqual($displays['display_test_1'], $options);
Alex Pott
committed
// Add another one to ensure that position is counted up.
$view->storage->addDisplay('display_test');
$displays = $view->storage->get('display');
$options = array(
'display_options' => array(),
'display_plugin' => 'display_test',
'id' => 'display_test_2',
'display_title' => 'Display test 2',
'position' => 2,
);
$this->assertEqual($displays['display_test_2'], $options);
// Move the second display before the first one in order to test custom
// sorting.
$displays['display_test_1']['position'] = 2;
$displays['display_test_2']['position'] = 1;
$view->storage->set('display', $displays);
$view->save();
$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();
Angie Byron
committed
$output = drupal_render($output);
$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();
Alex Pott
committed
$output = $view->preview();
Angie Byron
committed
$output = drupal_render($output);
// 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/test_view/edit/display_test_1');
$this->assertText('Display test settings');
Alex Pott
committed
// Ensure that the order is as expected.
$result = $this->xpath('//ul[@id="views-display-menu-tabs"]/li');
$this->assertEqual((string) $result[0]->a, 'Display test 2');
$this->assertEqual((string) $result[1]->a, 'Display test');
$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/test_view/edit/display_test_1');
Tim Plunkett
committed
$this->assertRaw($this->randomString);
Daniel Wehner
committed
// Test the enable/disable status of a display.
$view->display_handler->setOption('enabled', FALSE);
$this->assertFalse($view->display_handler->isEnabled(), 'Make sure that isEnabled returns FALSE on a disabled display.');
$view->display_handler->setOption('enabled', TRUE);
$this->assertTrue($view->display_handler->isEnabled(), 'Make sure that isEnabled returns TRUE on a disabled display.');
}
/**
* Tests the overriding of filter_groups.
*/
public function testFilterGroupsOverriding() {
catch
committed
$view = views_get_view('test_filter_groups');
Damian Lee
committed
$view->initDisplay();
// mark is as overridden, yes FALSE, means overridden.
$view->displayHandlers->get('page')->setOverride('filter_groups', FALSE);
$this->assertFalse($view->displayHandlers->get('page')->isDefaulted('filter_groups'), "Take sure that 'filter_groups' is marked as overridden.");
$this->assertFalse($view->displayHandlers->get('page')->isDefaulted('filters'), "Take sure that 'filters'' is marked as overridden.");
catch
committed
/**
* Tests the getAttachedDisplays method.
*/
public function testGetAttachedDisplays() {
$view = views_get_view('test_get_attach_displays');
// Both the feed_1 and the feed_2 display are attached to the page display.
$view->setDisplay('page_1');
$this->assertEqual($view->display_handler->getAttachedDisplays(), array('feed_1', 'feed_2'));
$view->setDisplay('feed_1');
$this->assertEqual($view->display_handler->getAttachedDisplays(), array());
}
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Tests the readmore functionality.
*/
public function testReadMore() {
$expected_more_text = 'custom more text';
$view = views_get_view('test_display_more');
$this->executeView($view);
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output);
$result = $this->xpath('//div[@class=:class]/a', array(':class' => 'more-link'));
$this->assertEqual($result[0]->attributes()->href, url('test_display_more'), 'The right more link is shown.');
$this->assertEqual(trim($result[0][0]), $expected_more_text, 'The right link text is shown.');
// Test the renderMoreLink method directly. This could be directly unit
// tested.
$more_link = $view->display_handler->renderMoreLink();
$this->drupalSetContent($more_link);
$result = $this->xpath('//div[@class=:class]/a', array(':class' => 'more-link'));
$this->assertEqual($result[0]->attributes()->href, url('test_display_more'), 'The right more link is shown.');
$this->assertEqual(trim($result[0][0]), $expected_more_text, 'The right link text is shown.');
// Test the useMoreText method directly. This could be directly unit
// tested.
$more_text = $view->display_handler->useMoreText();
$this->assertEqual($more_text, $expected_more_text, 'The right more text is chosen.');
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
$view = views_get_view('test_display_more');
$view->setDisplay();
$view->display_handler->setOption('use_more', 0);
$this->executeView($view);
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output);
$result = $this->xpath('//div[@class=:class]/a', array(':class' => 'more-link'));
$this->assertTrue(empty($result), 'The more link is not shown.');
$view = views_get_view('test_display_more');
$view->setDisplay();
$view->display_handler->setOption('use_more', 0);
$view->display_handler->setOption('use_more_always', 0);
$view->display_handler->setOption('pager', array(
'type' => 'some',
'options' => array(
'items_per_page' => 1,
'offset' => 0,
),
));
$this->executeView($view);
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output);
$result = $this->xpath('//div[@class=:class]/a', array(':class' => 'more-link'));
$this->assertTrue(empty($result), 'The more link is not shown when view has more records.');
}
Dries Buytaert
committed
/**
* Tests invalid display plugins.
*/
public function testInvalidDisplayPlugins() {
$this->drupalGet('test_display_invalid');
$this->assertResponse(200);
// Change the page plugin id to an invalid one. Bypass the entity system
// so no menu rebuild was executed (so the path is still available).
Angie Byron
committed
$config = \Drupal::config('views.view.test_display_invalid');
Dries Buytaert
committed
$config->set('display.page_1.display_plugin', 'invalid');
$config->save();
$this->drupalGet('test_display_invalid');
$this->assertResponse(200);
$this->assertText(t('The plugin (invalid) did not specify an instance class.'));
// Rebuild the menu, and ensure that the path is not accessible anymore.
menu_router_rebuild();
$this->drupalGet('test_display_invalid');
$this->assertResponse(404);
// Change the display plugin ID back to the correct ID.
Angie Byron
committed
$config = \Drupal::config('views.view.test_display_invalid');
Dries Buytaert
committed
$config->set('display.page_1.display_plugin', 'page');
$config->save();
// Place the block display.
$block = $this->drupalPlaceBlock('views_block:test_display_invalid-block_1', array(), array('title' => 'Invalid display'));
$this->drupalGet('<front>');
$this->assertResponse(200);
$this->assertBlockAppears($block);
// Change the block plugin ID to an invalid one.
Angie Byron
committed
$config = \Drupal::config('views.view.test_display_invalid');
Dries Buytaert
committed
$config->set('display.block_1.display_plugin', 'invalid');
$config->save();
// Test the page is still displayed, the block not present, and has the
// plugin warning message.
$this->drupalGet('<front>');
$this->assertResponse(200);
$this->assertText(t('The plugin (invalid) did not specify an instance class.'));
$this->assertNoBlockAppears($block);
}