Skip to content

Commit

Permalink
[md-systems#51] Add permission to administer Redirect settings
Browse files Browse the repository at this point in the history
  • Loading branch information
juampynr committed Feb 6, 2016
1 parent f04c284 commit 5ea1b83
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
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

0 comments on commit 5ea1b83

Please sign in to comment.