ad690ad77718b39a1a8b978d5cc8fb53f205028f
[yaffs-website] / web / core / modules / views / tests / src / Functional / Wizard / BasicTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Wizard;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Url;
8 use Drupal\views\Views;
9
10 /**
11  * Tests creating views with the wizard and viewing them on the listing page.
12  *
13  * @group views
14  */
15 class BasicTest extends WizardTestBase {
16
17   protected function setUp($import_test_views = TRUE) {
18     parent::setUp($import_test_views);
19
20     $this->drupalPlaceBlock('page_title_block');
21   }
22
23   public function testViewsWizardAndListing() {
24     $this->drupalCreateContentType(['type' => 'article']);
25     $this->drupalCreateContentType(['type' => 'page']);
26
27     // Check if we can access the main views admin page.
28     $this->drupalGet('admin/structure/views');
29     $this->assertText(t('Add view'));
30
31     // Create a simple and not at all useful view.
32     $view1 = [];
33     $view1['label'] = $this->randomMachineName(16);
34     $view1['id'] = strtolower($this->randomMachineName(16));
35     $view1['description'] = $this->randomMachineName(16);
36     $view1['page[create]'] = FALSE;
37     $this->drupalPostForm('admin/structure/views/add', $view1, t('Save and edit'));
38     $this->assertResponse(200);
39     $this->drupalGet('admin/structure/views');
40     $this->assertText($view1['label']);
41     $this->assertText($view1['description']);
42     $this->assertLinkByHref(\Drupal::url('entity.view.edit_form', ['view' => $view1['id']]));
43     $this->assertLinkByHref(\Drupal::url('entity.view.delete_form', ['view' => $view1['id']]));
44     $this->assertLinkByHref(\Drupal::url('entity.view.duplicate_form', ['view' => $view1['id']]));
45
46     // The view should not have a REST export display.
47     $this->assertNoText('REST export', 'When no options are enabled in the wizard, the resulting view does not have a REST export display.');
48
49     // This view should not have a block.
50     $this->drupalGet('admin/structure/block');
51     $this->assertNoText($view1['label']);
52
53     // Create two nodes.
54     $node1 = $this->drupalCreateNode(['type' => 'page']);
55     $node2 = $this->drupalCreateNode(['type' => 'article']);
56
57     // Now create a page with simple node listing and an attached feed.
58     $view2 = [];
59     $view2['label'] = $this->randomMachineName(16);
60     $view2['id'] = strtolower($this->randomMachineName(16));
61     $view2['description'] = $this->randomMachineName(16);
62     $view2['page[create]'] = 1;
63     $view2['page[title]'] = $this->randomMachineName(16);
64     $view2['page[path]'] = $this->randomMachineName(16);
65     $view2['page[feed]'] = 1;
66     $view2['page[feed_properties][path]'] = $this->randomMachineName(16);
67     $this->drupalPostForm('admin/structure/views/add', $view2, t('Save and edit'));
68     $this->drupalGet($view2['page[path]']);
69     $this->assertResponse(200);
70
71     // Since the view has a page, we expect to be automatically redirected to
72     // it.
73     $this->assertUrl($view2['page[path]']);
74     $this->assertText($view2['page[title]']);
75     $this->assertText($node1->label());
76     $this->assertText($node2->label());
77
78     // Check if we have the feed.
79     $this->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.feed_1')->toString());
80     $elements = $this->cssSelect('link[href="' . Url::fromRoute('view.' . $view2['id'] . '.feed_1', [], ['absolute' => TRUE])->toString() . '"]');
81     $this->assertEqual(count($elements), 1, 'Feed found.');
82     $this->drupalGet($view2['page[feed_properties][path]']);
83     // Because the response is XML we can't use the page which depends on an
84     // HTML tag being present.
85     $this->assertEquals('2.0', $this->getSession()->getDriver()->getAttribute('//rss', 'version'));
86     // The feed should have the same title and nodes as the page.
87     $this->assertText($view2['page[title]']);
88     $this->assertRaw($node1->url('canonical', ['absolute' => TRUE]));
89     $this->assertText($node1->label());
90     $this->assertRaw($node2->url('canonical', ['absolute' => TRUE]));
91     $this->assertText($node2->label());
92
93     // Go back to the views page and check if this view is there.
94     $this->drupalGet('admin/structure/views');
95     $this->assertText($view2['label']);
96     $this->assertText($view2['description']);
97     $this->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.page_1')->toString());
98
99     // The view should not have a REST export display.
100     $this->assertNoText('REST export', 'If only the page option was enabled in the wizard, the resulting view does not have a REST export display.');
101
102     // This view should not have a block.
103     $this->drupalGet('admin/structure/block');
104     $this->assertNoText('View: ' . $view2['label']);
105
106     // Create a view with a page and a block, and filter the listing.
107     $view3 = [];
108     $view3['label'] = $this->randomMachineName(16);
109     $view3['id'] = strtolower($this->randomMachineName(16));
110     $view3['description'] = $this->randomMachineName(16);
111     $view3['show[wizard_key]'] = 'node';
112     $view3['show[type]'] = 'page';
113     $view3['page[create]'] = 1;
114     $view3['page[title]'] = $this->randomMachineName(16);
115     $view3['page[path]'] = $this->randomMachineName(16);
116     $view3['block[create]'] = 1;
117     $view3['block[title]'] = $this->randomMachineName(16);
118     $this->drupalPostForm('admin/structure/views/add', $view3, t('Save and edit'));
119     $this->drupalGet($view3['page[path]']);
120     $this->assertResponse(200);
121
122     // Make sure the view only displays the node we expect.
123     $this->assertUrl($view3['page[path]']);
124     $this->assertText($view3['page[title]']);
125     $this->assertText($node1->label());
126     $this->assertNoText($node2->label());
127
128     // Go back to the views page and check if this view is there.
129     $this->drupalGet('admin/structure/views');
130     $this->assertText($view3['label']);
131     $this->assertText($view3['description']);
132     $this->assertLinkByHref(Url::fromRoute('view.' . $view3['id'] . '.page_1')->toString());
133
134     // The view should not have a REST export display.
135     $this->assertNoText('REST export', 'If only the page and block options were enabled in the wizard, the resulting view does not have a REST export display.');
136
137     // Confirm that the block is available in the block administration UI.
138     $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
139     $this->clickLink('Place block');
140     $this->assertText($view3['label']);
141
142     // Place the block.
143     $this->drupalPlaceBlock("views_block:{$view3['id']}-block_1");
144
145     // Visit a random page (not the one that displays the view itself) and look
146     // for the expected node title in the block.
147     $this->drupalGet('user');
148     $this->assertText($node1->label());
149     $this->assertNoText($node2->label());
150
151     // Make sure the listing page doesn't show disabled default views.
152     $this->assertNoText('tracker', 'Default tracker view does not show on the listing page.');
153
154     // Create a view with only a REST export.
155     $view4 = [];
156     $view4['label'] = $this->randomMachineName(16);
157     $view4['id'] = strtolower($this->randomMachineName(16));
158     $view4['description'] = $this->randomMachineName(16);
159     $view4['show[wizard_key]'] = 'node';
160     $view4['show[type]'] = 'page';
161     $view4['rest_export[create]'] = 1;
162     $view4['rest_export[path]'] = $this->randomMachineName(16);
163     $this->drupalPostForm('admin/structure/views/add', $view4, t('Save and edit'));
164     $this->assertRaw(t('The view %view has been saved.', ['%view' => $view4['label']]));
165
166     // Check that the REST export path works. JSON will work, as all core
167     // formats will be allowed. JSON and XML by default.
168     $this->drupalGet($view4['rest_export[path]'], ['query' => ['_format' => 'json']]);
169     $this->assertResponse(200);
170     $data = Json::decode($this->getSession()->getPage()->getContent());
171     $this->assertEqual(count($data), 1, 'Only the node of type page is exported.');
172     $node = reset($data);
173     $this->assertEqual($node['nid'][0]['value'], $node1->id(), 'The node of type page is exported.');
174   }
175
176   /**
177    * Tests default plugin values are populated from the wizard form.
178    *
179    * @see \Drupal\views\Plugin\views\display\DisplayPluginBase::mergeDefaults()
180    */
181   public function testWizardDefaultValues() {
182     $random_id = strtolower($this->randomMachineName(16));
183     // Create a basic view.
184     $view = [];
185     $view['label'] = $this->randomMachineName(16);
186     $view['id'] = $random_id;
187     $view['description'] = $this->randomMachineName(16);
188     $view['page[create]'] = FALSE;
189     $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
190
191     // Make sure the plugin types that should not have empty options don't have.
192     // Test against all values is unit tested.
193     // @see \Drupal\Tests\views\Kernel\Plugin\DisplayKernelTest
194     $view = Views::getView($random_id);
195     $displays = $view->storage->get('display');
196
197     foreach ($displays as $display) {
198       foreach (['query', 'exposed_form', 'pager', 'style', 'row'] as $type) {
199         $this->assertFalse(empty($display['display_options'][$type]['options']), new FormattableMarkup('Default options found for @plugin.', ['@plugin' => $type]));
200       }
201     }
202   }
203
204 }