3 namespace Drupal\Tests\node\Functional;
5 use Drupal\user\RoleInterface;
8 * Tests node administration page functionality.
12 class NodeAdminTest extends NodeTestBase {
14 * A user with permission to bypass access content.
16 * @var \Drupal\user\UserInterface
21 * A user with the 'access content overview' permission.
23 * @var \Drupal\user\UserInterface
28 * A normal user with permission to view own unpublished content.
30 * @var \Drupal\user\UserInterface
35 * A normal user with permission to bypass node access content.
37 * @var \Drupal\user\UserInterface
46 public static $modules = ['views'];
48 protected function setUp() {
51 // Remove the "view own unpublished content" permission which is set
52 // by default for authenticated users so we can test this permission
54 user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['view own unpublished content']);
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']);
63 * Tests that the table sorting works on the content admin pages.
65 public function testContentAdminSort() {
66 $this->drupalLogin($this->adminUser);
68 $changed = REQUEST_TIME;
69 foreach (['dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB'] as $prefix) {
71 $node = $this->drupalCreateNode(['title' => $prefix . $this->randomMachineName(6)]);
72 db_update('node_field_data')
73 ->fields(['changed' => $changed])
74 ->condition('nid', $node->id())
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')
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.');
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'])
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.');
107 * Tests content overview with different user permissions.
109 * Taxonomy filters are tested separately.
111 * @see TaxonomyNodeFilterTestCase
113 public function testContentAdminPages() {
114 $this->drupalLogin($this->adminUser);
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.
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]);
126 // Verify view, edit, and delete links for any content.
127 $this->drupalGet('admin/content');
128 $this->assertResponse(200);
130 $node_type_labels = $this->xpath('//td[contains(@class, "views-field-type")]');
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());
141 // Verify filtering by publishing status.
142 $this->drupalGet('admin/content', ['query' => ['status' => TRUE]]);
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');
148 // Verify filtering by status and content type.
149 $this->drupalGet('admin/content', ['query' => ['status' => TRUE, 'type' => 'page']]);
151 $this->assertLinkByHref('node/' . $nodes['published_page']->id() . '/edit');
152 $this->assertNoLinkByHref('node/' . $nodes['published_article']->id() . '/edit');
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');
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');
171 // Verify no tableselect.
172 $this->assertNoFieldByName('nodes[' . $nodes['published_page']->id() . ']', '', 'No tableselect found.');
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');
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');
189 // Verify no tableselect.
190 $this->assertNoFieldByName('nodes[' . $nodes['unpublished_page_2']->id() . ']', '', 'No tableselect found.');
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');