View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Views;
class DisplayAttachmentTest extends ViewTestBase {
public static $testViews = [
'test_display_attachment',
'test_attached_disabled',
];
public static $modules = [
'node',
'views',
];
protected $defaultTheme = 'classy';
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
}
public function testAttachment() {
$this
->drupalGet('test-display-attachment');
$result = $this
->xpath('//div[contains(@class, "view-content")]');
$this
->assertCount(2, $result, 'Both actual view and the attachment is rendered.');
$result = $this
->xpath('//div[contains(@class, "attachment-after")]');
$this
->assertCount(0, $result, 'The attachment is not rendered after the actual view.');
$result = $this
->xpath('//div[contains(@class, "attachment-before")]');
$this
->assertCount(1, $result, 'The attachment is rendered before the actual view.');
}
public function testDisabledAttachments() {
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->drupalCreateNode();
$view = Views::getView('test_attached_disabled');
$view
->setDisplay('page_1');
$attached_displays = $view->display_handler
->getAttachedDisplays();
$this
->assertContains('attachment_1', $attached_displays, 'The attachment_1 display is attached to the page display.');
$this
->assertContains('attachment_2', $attached_displays, 'The attachment_2 display is attached to the page display.');
$this
->drupalGet('test-attached-disabled');
$result = $this
->xpath('//div[contains(@class, "view-content")]');
$this
->assertCount(3, $result, 'The page view and the attachments are rendered.');
$result = $this
->xpath('//div[contains(@class, "attachment-before")]');
$this
->assertCount(1, $result, 'The attachment is rendered before the page view.');
$result = $this
->xpath('//div[contains(@class, "attachment-after")]');
$this
->assertCount(1, $result, 'The attachment is rendered after the page view.');
$view->displayHandlers
->get('attachment_1')
->setOption('enabled', FALSE);
$view
->save();
$this
->drupalGet('/test-attached-disabled');
$result = $this
->xpath('//div[contains(@class, "view-content")]');
$this
->assertCount(2, $result, 'The page view and only one attachment are rendered.');
$result = $this
->xpath('//div[contains(@class, "attachment-before")]');
$this
->assertCount(0, $result, 'The attachment_1 is not rendered.');
$view->displayHandlers
->get('attachment_2')
->setOption('enabled', FALSE);
$view
->save();
$this
->drupalGet('/test-attached-disabled');
$result = $this
->xpath('//div[contains(@class, "view-content")]');
$this
->assertCount(1, $result, 'The page view is rendered without attachments.');
$result = $this
->xpath('//div[contains(@class, "attachment-after")]');
$this
->assertCount(0, $result, 'The attachment_2 is not rendered.');
}
}