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