AccessUnpublishedTest.php in Access unpublished 8
File
tests/src/FunctionalJavascript/AccessUnpublishedTest.php
View source
<?php
namespace Drupal\Tests\access_unpublished\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\user\RoleInterface;
class AccessUnpublishedTest extends WebDriverTestBase {
use NodeCreationTrait;
protected $defaultTheme = 'classy';
protected static $modules = [
'access_unpublished',
'node',
];
protected static $userPermissions = [
'create page content',
'edit any page content',
'access content',
'access_unpublished node page',
'delete token',
'renew token',
'administer nodes',
];
protected $entity;
protected function setUp() {
parent::setUp();
NodeType::create([
'type' => 'page',
'name' => 'page',
])
->save();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access content',
'access_unpublished node page',
]);
$this->entity = $this
->createNode([
'status' => FALSE,
]);
$assert_session = $this
->assertSession();
$this
->drupalGet($this->entity
->toUrl());
$assert_session
->pageTextContains('Access denied');
$account = $this
->drupalCreateUser(static::$userPermissions);
$this
->drupalLogin($account);
}
public function testAccessUnpublished() {
$this
->drupalGet($this->entity
->toUrl('edit-form'));
$page = $this
->getSession()
->getPage();
$page
->clickLink('Temporary unpublished access');
$page
->find('xpath', '//*[@data-drupal-selector="edit-generate-token"]')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$copyToClipboard = $page
->find('xpath', '//*[@data-drupal-selector="access-token-list"]//a[contains(@class, "clipboard-button")]');
$tokenUrl = $copyToClipboard
->getAttribute('data-unpublished-access-url');
$loggedInUser = $this->loggedInUser;
$this
->drupalLogout();
$this
->drupalGet($tokenUrl);
$this
->assertSession()
->pageTextContains($this->entity
->label());
$this
->drupalGet($this->entity
->toUrl());
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalLogin($loggedInUser);
$this
->drupalGet($this->entity
->toUrl('edit-form'));
$page
->clickLink('Temporary unpublished access');
$page
->find('css', '[data-drupal-selector="access-token-list"] li.dropbutton-toggle > button')
->click();
$page
->find('css', '[data-drupal-selector="access-token-list"] li.delete > a')
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalLogout();
$this
->drupalGet($tokenUrl);
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalLogin($loggedInUser);
$this
->drupalGet($this->entity
->toUrl('edit-form'));
$page
->checkField('Published');
$page
->pressButton('Save');
$this
->drupalLogout();
$this
->drupalGet($this->entity
->toUrl());
$this
->assertSession()
->pageTextContains($this->entity
->label());
}
}