NodeAdminTest.php 8.2 KB

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