View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Database\Database;
use Drupal\user\RoleInterface;
class NodeAdminTest extends NodeTestBase {
protected $defaultTheme = 'classy';
protected $adminUser;
protected $baseUser1;
protected $baseUser2;
protected $baseUser3;
protected static $modules = [
'views',
];
protected function setUp() : void {
parent::setUp();
user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, [
'view own unpublished content',
]);
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'access content overview',
'administer nodes',
'bypass node access',
]);
$this->baseUser1 = $this
->drupalCreateUser([
'access content overview',
]);
$this->baseUser2 = $this
->drupalCreateUser([
'access content overview',
'view own unpublished content',
]);
$this->baseUser3 = $this
->drupalCreateUser([
'access content overview',
'bypass node access',
]);
}
public function testContentAdminSort() {
$this
->drupalLogin($this->adminUser);
$changed = REQUEST_TIME;
$connection = Database::getConnection();
foreach ([
'dd',
'aa',
'DD',
'bb',
'cc',
'CC',
'AA',
'BB',
] as $prefix) {
$changed += 1000;
$node = $this
->drupalCreateNode([
'title' => $prefix . $this
->randomMachineName(6),
]);
$connection
->update('node_field_data')
->fields([
'changed' => $changed,
])
->condition('nid', $node
->id())
->execute();
}
$nodes_query = $connection
->select('node_field_data', 'n')
->fields('n', [
'title',
])
->orderBy('changed', 'DESC')
->execute()
->fetchCol();
$this
->drupalGet('admin/content');
foreach ($nodes_query as $delta => $string) {
$this
->assertSession()
->elementExists('xpath', $this
->assertSession()
->buildXPathQuery('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
':class' => 'views-table',
':label' => $string,
]));
}
$nodes_query = $connection
->select('node_field_data', 'n')
->fields('n', [
'title',
])
->orderBy('title')
->execute()
->fetchCol();
$this
->drupalGet('admin/content', [
'query' => [
'sort' => 'asc',
'order' => 'title',
],
]);
foreach ($nodes_query as $delta => $string) {
$this
->assertSession()
->elementExists('xpath', $this
->assertSession()
->buildXPathQuery('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
':class' => 'views-table',
':label' => $string,
]));
}
}
public function testContentAdminPages() {
$this
->drupalLogin($this->adminUser);
$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,
]);
$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');
$this
->assertEquals(trim($node_type_labels[$delta]
->getText()), $node->type->entity
->label());
$delta++;
}
$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');
$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');
$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');
$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');
$this
->assertSession()
->fieldNotExists('nodes[' . $nodes['published_page']
->id() . ']');
$this
->drupalLogout();
$this
->drupalLogin($this->baseUser2);
$this
->drupalGet('admin/content');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkByHrefExists('node/' . $nodes['unpublished_page_2']
->id());
$this
->assertSession()
->linkByHrefNotExists('node/' . $nodes['unpublished_page_2']
->id() . '/edit');
$this
->assertSession()
->linkByHrefNotExists('node/' . $nodes['unpublished_page_2']
->id() . '/delete');
$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');
$this
->assertSession()
->fieldNotExists('nodes[' . $nodes['unpublished_page_2']
->id() . ']');
$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');
}
}
}