View source
<?php
namespace Drupal\Tests\url_redirect\Functional;
use Drupal\Tests\BrowserTestBase;
class UrlRedirectNegateTest extends BrowserTestBase {
protected static $modules = [
'node',
'url_redirect',
'user',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $userOne;
protected $userTwo;
protected $ridOne;
protected $ridTwo;
protected $sourcePath;
protected $destPath;
public function setUp() {
parent::setUp();
$this->ridOne = $this
->drupalCreateRole(array(), 'custom_role_1', 'custom_role_1');
$this->ridTwo = $this
->drupalCreateRole(array(), 'custom_role_2', 'custom_role_2');
$this->userOne = $this
->drupalCreateUser();
$this->userOne
->addRole($this->ridOne);
$this->userOne
->save();
$this->userTwo = $this
->drupalCreateUser();
$this->userTwo
->addRole($this->ridTwo);
$this->userTwo
->save();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$permissions = array(
'create page content',
'access content',
'access url redirect settings page',
'access url redirect edit page',
);
$this->adminUser = $this
->drupalCreateUser($permissions);
$sourceNode = $this
->drupalCreateNode(array(
'type' => 'page',
'uid' => $this->adminUser
->id(),
));
$destNode = $this
->drupalCreateNode(array(
'type' => 'page',
'uid' => $this->adminUser
->id(),
));
$this->sourcePath = "/node/" . $sourceNode
->id();
$this->destPath = "/node/" . $destNode
->id();
}
public function testRoleNegation() {
$this
->drupalLogin($this->adminUser);
$this
->addUrlRedirect($this->sourcePath, $this->destPath, 'Role', [
$this->ridOne,
], '1', 'No', '1');
$this
->drupalLogin($this->userOne);
$this
->drupalGet($this->sourcePath);
$this
->assertSession()
->addressNotEquals($this->destPath);
$this
->drupalLogin($this->userTwo);
$this
->drupalGet($this->sourcePath);
$this
->assertSession()
->addressNotEquals($this->destPath);
}
public function testUserNegation() {
$this
->drupalLogin($this->adminUser);
$this
->addUrlRedirect($this->sourcePath, $this->destPath, 'User', [
$this->userOne
->getAccountName(),
], '1', 'No', '1');
$this
->drupalLogin($this->userOne);
$this
->drupalGet($this->sourcePath);
$this
->assertSession()
->addressNotEquals($this->destPath);
$this
->drupalLogin($this->userTwo);
$this
->drupalGet($this->sourcePath);
$this
->assertSession()
->addressNotEquals($this->destPath);
}
private function addUrlRedirect($path, $redirect_path, $check_for, $check_value, $negate, $message, $status) {
$formValues = [];
$formValues['path'] = $path;
$formValues['redirect_path'] = $redirect_path;
$formValues['checked_for'] = $check_for;
$formValues['negate'] = $negate;
$formValues['message'] = $message;
$formValues['status'] = $status;
if ($check_for === 'User') {
$formValues['user'] = implode(',', $check_value);
}
else {
$formValues['roles[]'] = $check_value;
}
$this
->drupalPostForm('admin/config/system/url_redirect/add', $formValues, t('Save'));
}
}