Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / DisplayAttachmentTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the attachment display plugin.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\display\Attachment
13  */
14 class DisplayAttachmentTest extends ViewTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_display_attachment', 'test_attached_disabled'];
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['node', 'views'];
29
30   protected function setUp($import_test_views = TRUE) {
31     parent::setUp($import_test_views);
32
33     $this->enableViewsTestModule();
34
35     $admin_user = $this->drupalCreateUser(['administer site configuration']);
36     $this->drupalLogin($admin_user);
37   }
38
39   /**
40    * Tests the attachment plugin.
41    */
42   public function testAttachment() {
43     $this->drupalGet('test-display-attachment');
44
45     $result = $this->xpath('//div[contains(@class, "view-content")]');
46     $this->assertEqual(count($result), 2, 'Both actual view and the attachment is rendered.');
47
48     $result = $this->xpath('//div[contains(@class, "attachment-after")]');
49     $this->assertEqual(count($result), 0, 'The attachment is not rendered after the actual view.');
50
51     $result = $this->xpath('//div[contains(@class, "attachment-before")]');
52     $this->assertEqual(count($result), 1, 'The attachment is rendered before the actual view.');
53   }
54
55   /**
56    * Tests that nothing is output when the attachment displays are disabled.
57    */
58   public function testDisabledAttachments() {
59     $this->drupalCreateContentType(['type' => 'page']);
60     $this->drupalCreateNode();
61
62     // Ensure that the feed_1 display is attached to the page_1 display.
63     $view = Views::getView('test_attached_disabled');
64     $view->setDisplay('page_1');
65     $attached_displays = $view->display_handler->getAttachedDisplays();
66     $this->assertTrue(in_array('attachment_1', $attached_displays), 'The attachment_1 display is attached to the page display.');
67     $this->assertTrue(in_array('attachment_2', $attached_displays), 'The attachment_2 display is attached to the page display.');
68
69     // Check that the attachments are output on the page display.
70     $this->drupalGet('test-attached-disabled');
71
72     $result = $this->xpath('//div[contains(@class, "view-content")]');
73     $this->assertEqual(count($result), 3, 'The page view and the attachments are rendered.');
74
75     $result = $this->xpath('//div[contains(@class, "attachment-before")]');
76     $this->assertEqual(count($result), 1, 'The attachment is rendered before the page view.');
77
78     $result = $this->xpath('//div[contains(@class, "attachment-after")]');
79     $this->assertEqual(count($result), 1, 'The attachment is rendered after the page view.');
80
81     // Disable the attachment_1 display.
82     $view->displayHandlers->get('attachment_1')->setOption('enabled', FALSE);
83     $view->save();
84
85     // Test that the before attachment is not displayed.
86     $this->drupalGet('/test-attached-disabled');
87     $result = $this->xpath('//div[contains(@class, "view-content")]');
88     $this->assertEqual(count($result), 2, 'The page view and only one attachment are rendered.');
89
90     $result = $this->xpath('//div[contains(@class, "attachment-before")]');
91     $this->assertEqual(count($result), 0, 'The attachment_1 is not rendered.');
92
93     // Disable the attachment_2 display.
94     $view->displayHandlers->get('attachment_2')->setOption('enabled', FALSE);
95     $view->save();
96
97     // Test that the after attachment is not displayed.
98     $this->drupalGet('/test-attached-disabled');
99     $result = $this->xpath('//div[contains(@class, "view-content")]');
100     $this->assertEqual(count($result), 1, 'The page view is rendered without attachments.');
101
102     $result = $this->xpath('//div[contains(@class, "attachment-after")]');
103     $this->assertEqual(count($result), 0, 'The attachment_2 is not rendered.');
104   }
105
106 }