Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#51] Add permission to administer Redirect settings #85

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions redirect.permissions.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
administer redirects:
title: 'Administer URL redirections'
change redirect settings:
title: 'Change redirect settings'
4 changes: 2 additions & 2 deletions redirect.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ redirect.settings:
_form: '\Drupal\redirect\Form\RedirectSettingsForm'
_title: 'Settings'
requirements:
_permission: 'administer redirects'
_permission: 'change redirect settings'

redirect.fix_404:
path: '/admin/config/search/redirect/404'
defaults:
_title: 'Fix 404 pages'
_form: '\Drupal\redirect\Form\RedirectFix404Form'
requirements:
_permission: 'administer redirects'
_permission: 'change redirect settings'

#redirect.devel_generate:
# requirements:
Expand Down
44 changes: 41 additions & 3 deletions src/Tests/RedirectUITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
class RedirectUITest extends WebTestBase {

/**
* @var array
*/
protected $userPermissions;

/**
* @var \Drupal\Core\Session\AccountInterface
*/
Expand Down Expand Up @@ -48,15 +53,16 @@ protected function setUp() {
parent::setUp();

$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
$this->adminUser = $this->drupalCreateUser(array(
$this->userPermissions = [
'administer redirects',
'access site reports',
'access content',
'bypass node access',
'create url aliases',
'administer taxonomy',
'administer url aliases',
));
];
$this->adminUser = $this->drupalCreateUser($this->userPermissions);

$this->repository = \Drupal::service('redirect.repository');

Expand Down Expand Up @@ -228,6 +234,18 @@ public function testRedirectUI() {
public function testFix404Pages() {
$this->drupalLogin($this->adminUser);

// Test the permission "Change Redirect settings".
$this->drupalGet('admin/config/search/redirect/404');
$this->assertResponse(403);

// Now create and log in a user with the proper permissions.
$adminUser = $this->drupalCreateUser(array_merge($this->userPermissions, ['change redirect settings']));
$this->drupalLogin($adminUser);

// Test access again.
$this->drupalGet('admin/config/search/redirect/404');
$this->assertResponse(200);

// Visit a non existing page to have the 404 watchdog entry.
$this->drupalGet('non-existing');

Expand All @@ -251,6 +269,25 @@ public function testFix404Pages() {
$this->assertUrl('node');
}

/**
* Tests the Settings page.
*/
public function testSettingsPage() {
$this->drupalLogin($this->adminUser);

// Test the permission "Change Redirect settings".
$this->drupalGet('admin/config/search/redirect/settings');
$this->assertResponse(403);

// Now create and log in a user with the proper permissions.
$adminUser = $this->drupalCreateUser(array_merge($this->userPermissions, ['change redirect settings']));
$this->drupalLogin($adminUser);

// Test access again.
$this->drupalGet('admin/config/search/redirect/settings');
$this->assertResponse(200);
}

/**
* Tests redirects being automatically created upon path alias change.
*/
Expand Down Expand Up @@ -474,7 +511,8 @@ public function testExternal() {
$redirect->setStatusCode(301);
$redirect->save();
$this->assertRedirect('a-path', 'https://www.example.org');
$this->drupalLogin($this->adminUser);
$adminUser = $this->drupalCreateUser(array_merge($this->userPermissions, ['change redirect settings']));
$this->drupalLogin($adminUser);
$this->drupalPostForm('admin/config/search/redirect/settings', ['redirect_deslash' => 1], t('Save configuration'));
$this->drupalGet('/2015/10/10/');
$this->assertResponse(404);
Expand Down