comparison core/modules/node/tests/src/Functional/NodeAdminTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\user\RoleInterface;
6
7 /**
8 * Tests node administration page functionality.
9 *
10 * @group node
11 */
12 class NodeAdminTest extends NodeTestBase {
13 /**
14 * A user with permission to bypass access content.
15 *
16 * @var \Drupal\user\UserInterface
17 */
18 protected $adminUser;
19
20 /**
21 * A user with the 'access content overview' permission.
22 *
23 * @var \Drupal\user\UserInterface
24 */
25 protected $baseUser1;
26
27 /**
28 * A normal user with permission to view own unpublished content.
29 *
30 * @var \Drupal\user\UserInterface
31 */
32 protected $baseUser2;
33
34 /**
35 * A normal user with permission to bypass node access content.
36 *
37 * @var \Drupal\user\UserInterface
38 */
39 protected $baseUser3;
40
41 /**
42 * Modules to enable.
43 *
44 * @var array
45 */
46 public static $modules = ['views'];
47
48 protected function setUp() {
49 parent::setUp();
50
51 // Remove the "view own unpublished content" permission which is set
52 // by default for authenticated users so we can test this permission
53 // correctly.
54 user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['view own unpublished content']);
55
56 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'access content overview', 'administer nodes', 'bypass node access']);
57 $this->baseUser1 = $this->drupalCreateUser(['access content overview']);
58 $this->baseUser2 = $this->drupalCreateUser(['access content overview', 'view own unpublished content']);
59 $this->baseUser3 = $this->drupalCreateUser(['access content overview', 'bypass node access']);
60 }
61
62 /**
63 * Tests that the table sorting works on the content admin pages.
64 */
65 public function testContentAdminSort() {
66 $this->drupalLogin($this->adminUser);
67
68 $changed = REQUEST_TIME;
69 foreach (['dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB'] as $prefix) {
70 $changed += 1000;
71 $node = $this->drupalCreateNode(['title' => $prefix . $this->randomMachineName(6)]);
72 db_update('node_field_data')
73 ->fields(['changed' => $changed])
74 ->condition('nid', $node->id())
75 ->execute();
76 }
77
78 // Test that the default sort by node.changed DESC actually fires properly.
79 $nodes_query = db_select('node_field_data', 'n')
80 ->fields('n', ['title'])
81 ->orderBy('changed', 'DESC')
82 ->execute()
83 ->fetchCol();
84
85 $this->drupalGet('admin/content');
86 foreach ($nodes_query as $delta => $string) {
87 $elements = $this->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [':class' => 'views-table', ':label' => $string]);
88 $this->assertTrue(!empty($elements), 'The node was found in the correct order.');
89 }
90
91 // Compare the rendered HTML node list to a query for the nodes ordered by
92 // title to account for possible database-dependent sort order.
93 $nodes_query = db_select('node_field_data', 'n')
94 ->fields('n', ['title'])
95 ->orderBy('title')
96 ->execute()
97 ->fetchCol();
98
99 $this->drupalGet('admin/content', ['query' => ['sort' => 'asc', 'order' => 'title']]);
100 foreach ($nodes_query as $delta => $string) {
101 $elements = $this->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [':class' => 'views-table', ':label' => $string]);
102 $this->assertTrue(!empty($elements), 'The node was found in the correct order.');
103 }
104 }
105
106 /**
107 * Tests content overview with different user permissions.
108 *
109 * Taxonomy filters are tested separately.
110 *
111 * @see TaxonomyNodeFilterTestCase
112 */
113 public function testContentAdminPages() {
114 $this->drupalLogin($this->adminUser);
115
116 // Use an explicit changed time to ensure the expected order in the content
117 // admin listing. We want these to appear in the table in the same order as
118 // they appear in the following code, and the 'content' View has a table
119 // style configuration with a default sort on the 'changed' field DESC.
120 $time = time();
121 $nodes['published_page'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--]);
122 $nodes['published_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--]);
123 $nodes['unpublished_page_1'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'uid' => $this->baseUser1->id(), 'status' => 0]);
124 $nodes['unpublished_page_2'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time, 'uid' => $this->baseUser2->id(), 'status' => 0]);
125
126 // Verify view, edit, and delete links for any content.
127 $this->drupalGet('admin/content');
128 $this->assertResponse(200);
129
130 $node_type_labels = $this->xpath('//td[contains(@class, "views-field-type")]');
131 $delta = 0;
132 foreach ($nodes as $node) {
133 $this->assertLinkByHref('node/' . $node->id());
134 $this->assertLinkByHref('node/' . $node->id() . '/edit');
135 $this->assertLinkByHref('node/' . $node->id() . '/delete');
136 // Verify that we can see the content type label.
137 $this->assertEqual(trim($node_type_labels[$delta]->getText()), $node->type->entity->label());
138 $delta++;
139 }
140
141 // Verify filtering by publishing status.
142 $this->drupalGet('admin/content', ['query' => ['status' => TRUE]]);
143
144 $this->assertLinkByHref('node/' . $nodes['published_page']->id() . '/edit');
145 $this->assertLinkByHref('node/' . $nodes['published_article']->id() . '/edit');
146 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id() . '/edit');
147
148 // Verify filtering by status and content type.
149 $this->drupalGet('admin/content', ['query' => ['status' => TRUE, 'type' => 'page']]);
150
151 $this->assertLinkByHref('node/' . $nodes['published_page']->id() . '/edit');
152 $this->assertNoLinkByHref('node/' . $nodes['published_article']->id() . '/edit');
153
154 // Verify no operation links are displayed for regular users.
155 $this->drupalLogout();
156 $this->drupalLogin($this->baseUser1);
157 $this->drupalGet('admin/content');
158 $this->assertResponse(200);
159 $this->assertLinkByHref('node/' . $nodes['published_page']->id());
160 $this->assertLinkByHref('node/' . $nodes['published_article']->id());
161 $this->assertNoLinkByHref('node/' . $nodes['published_page']->id() . '/edit');
162 $this->assertNoLinkByHref('node/' . $nodes['published_page']->id() . '/delete');
163 $this->assertNoLinkByHref('node/' . $nodes['published_article']->id() . '/edit');
164 $this->assertNoLinkByHref('node/' . $nodes['published_article']->id() . '/delete');
165
166 // Verify no unpublished content is displayed without permission.
167 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id());
168 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id() . '/edit');
169 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id() . '/delete');
170
171 // Verify no tableselect.
172 $this->assertNoFieldByName('nodes[' . $nodes['published_page']->id() . ']', '', 'No tableselect found.');
173
174 // Verify unpublished content is displayed with permission.
175 $this->drupalLogout();
176 $this->drupalLogin($this->baseUser2);
177 $this->drupalGet('admin/content');
178 $this->assertResponse(200);
179 $this->assertLinkByHref('node/' . $nodes['unpublished_page_2']->id());
180 // Verify no operation links are displayed.
181 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_2']->id() . '/edit');
182 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_2']->id() . '/delete');
183
184 // Verify user cannot see unpublished content of other users.
185 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id());
186 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id() . '/edit');
187 $this->assertNoLinkByHref('node/' . $nodes['unpublished_page_1']->id() . '/delete');
188
189 // Verify no tableselect.
190 $this->assertNoFieldByName('nodes[' . $nodes['unpublished_page_2']->id() . ']', '', 'No tableselect found.');
191
192 // Verify node access can be bypassed.
193 $this->drupalLogout();
194 $this->drupalLogin($this->baseUser3);
195 $this->drupalGet('admin/content');
196 $this->assertResponse(200);
197 foreach ($nodes as $node) {
198 $this->assertLinkByHref('node/' . $node->id());
199 $this->assertLinkByHref('node/' . $node->id() . '/edit');
200 $this->assertLinkByHref('node/' . $node->id() . '/delete');
201 }
202 }
203
204 }