You are here

public function NodeAdminTest::testContentAdminPages in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminPages()

Tests content overview with different user permissions.

Taxonomy filters are tested separately.

See also

TaxonomyNodeFilterTestCase

File

core/modules/node/tests/src/Functional/NodeAdminTest.php, line 138

Class

NodeAdminTest
Tests node administration page functionality.

Namespace

Drupal\Tests\node\Functional

Code

public function testContentAdminPages() {
  $this
    ->drupalLogin($this->adminUser);

  // Use an explicit changed time to ensure the expected order in the content
  // admin listing. We want these to appear in the table in the same order as
  // they appear in the following code, and the 'content' View has a table
  // style configuration with a default sort on the 'changed' field DESC.
  $time = time();
  $nodes['published_page'] = $this
    ->drupalCreateNode([
    'type' => 'page',
    'changed' => $time--,
  ]);
  $nodes['published_article'] = $this
    ->drupalCreateNode([
    'type' => 'article',
    'changed' => $time--,
  ]);
  $nodes['unpublished_page_1'] = $this
    ->drupalCreateNode([
    'type' => 'page',
    'changed' => $time--,
    'uid' => $this->baseUser1
      ->id(),
    'status' => 0,
  ]);
  $nodes['unpublished_page_2'] = $this
    ->drupalCreateNode([
    'type' => 'page',
    'changed' => $time,
    'uid' => $this->baseUser2
      ->id(),
    'status' => 0,
  ]);

  // Verify view, edit, and delete links for any content.
  $this
    ->drupalGet('admin/content');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $node_type_labels = $this
    ->xpath('//td[contains(@class, "views-field-type")]');
  $delta = 0;
  foreach ($nodes as $node) {
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id());
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id() . '/delete');

    // Verify that we can see the content type label.
    $this
      ->assertEquals(trim($node_type_labels[$delta]
      ->getText()), $node->type->entity
      ->label());
    $delta++;
  }

  // Verify filtering by publishing status.
  $this
    ->drupalGet('admin/content', [
    'query' => [
      'status' => TRUE,
    ],
  ]);
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['published_page']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['published_article']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id() . '/edit');

  // Verify filtering by status and content type.
  $this
    ->drupalGet('admin/content', [
    'query' => [
      'status' => TRUE,
      'type' => 'page',
    ],
  ]);
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['published_page']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['published_article']
    ->id() . '/edit');

  // Verify no operation links are displayed for regular users.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->baseUser1);
  $this
    ->drupalGet('admin/content');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['published_page']
    ->id());
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['published_article']
    ->id());
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['published_page']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['published_page']
    ->id() . '/delete');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['published_article']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['published_article']
    ->id() . '/delete');

  // Verify no unpublished content is displayed without permission.
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id());
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id() . '/delete');

  // Verify no tableselect.
  $this
    ->assertSession()
    ->fieldNotExists('nodes[' . $nodes['published_page']
    ->id() . ']');

  // Verify unpublished content is displayed with permission.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->baseUser2);
  $this
    ->drupalGet('admin/content');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkByHrefExists('node/' . $nodes['unpublished_page_2']
    ->id());

  // Verify no operation links are displayed.
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_2']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_2']
    ->id() . '/delete');

  // Verify user cannot see unpublished content of other users.
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id());
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkByHrefNotExists('node/' . $nodes['unpublished_page_1']
    ->id() . '/delete');

  // Verify no tableselect.
  $this
    ->assertSession()
    ->fieldNotExists('nodes[' . $nodes['unpublished_page_2']
    ->id() . ']');

  // Verify node access can be bypassed.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->baseUser3);
  $this
    ->drupalGet('admin/content');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  foreach ($nodes as $node) {
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id());
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->linkByHrefExists('node/' . $node
      ->id() . '/delete');
  }
}