View source
<?php
namespace Drupal\admin_menu\Tests;
class AdminMenuPermissionsTest extends AdminMenuTestBase {
public static $modules = [
'node',
];
public static function getInfo() {
return [
'name' => 'Menu link access permissions',
'description' => 'Tests appearance of menu links depending on user permissions.',
'group' => 'Administration menu',
];
}
function testPermissions() {
module_enable([
'contact',
]);
$this
->resetAll();
$this
->drupalGet('');
$this
->assertNoElementByXPath('//div[@id="admin-menu"]', [], t('Administration menu not found.'));
$permissions = $this->basePermissions + [
'access content overview',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$this
->assertElementByXPath('//div[@id="admin-menu"]', [], 'Administration menu found.');
$this
->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', [
':path' => 'admin/content',
], 'Content link found.');
$this
->assertNoElementByXPath('//div[@id="admin-menu"]//a[@href=:path]', [
':path' => 'http://drupal.org',
], 'Icon » Drupal.org link not found.');
$this
->assertNoElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', [
':path' => 'admin/structure/contact',
], 'Structure » Contact link not found.');
$permissions = $this->basePermissions + [
'display drupal links',
'administer contact forms',
];
$admin_user2 = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user2);
$this
->assertElementByXPath('//div[@id="admin-menu"]', [], 'Administration menu found.');
$this
->assertNoElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', [
':path' => 'admin/content',
], 'Content link not found.');
$this
->assertElementByXPath('//div[@id="admin-menu"]//a[@href=:path]', [
':path' => 'http://drupal.org',
], 'Icon » Drupal.org link found.');
$this
->assertElementByXPath('//div[@id="admin-menu"]//a[contains(@href, :path)]', [
':path' => 'admin/structure/contact',
], 'Structure » Contact link found.');
}
function testCategories() {
$admin_user = $this
->drupalCreateUser($this->basePermissions);
$this
->drupalLogin($admin_user);
$this
->assertNoLinkTrailByTitle([
t('Structure'),
]);
$this
->assertNoLinkTrailByTitle([
t('Configuration'),
]);
$permissions = $this->basePermissions + [
'administer users',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$this
->assertNoLinkTrailByTitle([
t('Structure'),
]);
$this
->assertLinkTrailByTitle([
t('People'),
]);
$this
->assertLinkTrailByTitle([
t('Configuration'),
]);
$this
->assertLinkTrailByTitle([
t('Configuration'),
t('People'),
]);
$this
->assertNoLinkTrailByTitle([
t('Configuration'),
t('Media'),
]);
$this
->assertNoLinkTrailByTitle([
t('Configuration'),
t('System'),
]);
}
function testPermissionChanges() {
$permissions = $this->basePermissions + [
'administer permissions',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$rid = key(array_diff_key($admin_user->roles, [
DRUPAL_AUTHENTICATED_RID => 0,
]));
$this
->assertNoLinkTrailByTitle([
t('Configuration'),
]);
$edit = [
$rid . '[administer site configuration]' => TRUE,
];
$this
->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
$this
->assertLinkTrailByTitle([
t('Configuration'),
]);
$this
->assertNoLinkTrailByTitle([
t('Structure'),
t('Content types'),
]);
$test_rid = drupal_strtolower($this
->randomName(8));
$edit = [
'role[label]' => 'test',
'role[id]' => $test_rid,
];
$this
->drupalPost('admin/people/roles', $edit, t('Add role'));
$edit = [
$test_rid . '[administer content types]' => TRUE,
];
$this
->drupalPost('admin/people/permissions/' . $test_rid, $edit, t('Save permissions'));
$this
->assertNoLinkTrailByTitle([
t('Structure'),
t('Content types'),
]);
$edit = [
'roles[' . $test_rid . ']' => TRUE,
];
$this
->drupalPost('user/' . $admin_user->uid . '/edit', $edit, t('Save'));
$this
->assertLinkTrailByTitle([
t('Structure'),
t('Content types'),
]);
$this
->drupalPost('admin/people/roles/edit/' . $test_rid, [], t('Delete role'));
$this
->drupalPost(NULL, [], t('Delete'));
$this
->assertNoLinkTrailByTitle([
t('Structure'),
t('Content types'),
]);
}
}