View source
<?php
namespace Drupal\Tests\metatag_extended_perms\Functional;
use Drupal\Tests\BrowserTestBase;
class PermissionsTest extends BrowserTestBase {
use \Drupal\Tests\metatag\Functional\MetatagHelperTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'field_ui',
'metatag_extended_perms',
];
protected $permissions = [
'basic' => [
'abstract' => 'Abstract',
'description' => 'Description',
'keywords' => 'Keywords',
'title' => 'Page title',
],
'advanced' => [
'cache_control' => 'Cache control',
'canonical_url' => 'Canonical URL',
'content_language' => 'Content Language',
'expires' => 'Expires',
'generator' => 'Generator',
'geo_placename' => 'Geographical place name',
'geo_position' => 'Geographical position',
'geo_region' => 'Geographical region',
'google' => 'Google',
'icbm' => 'ICBM',
'image_src' => 'Image',
'news_keywords' => 'News Keywords',
'next' => 'Next page URL',
'original_source' => 'Original source',
'pragma' => 'Pragma',
'prev' => 'Previous page URL',
'rating' => 'Rating',
'referrer' => 'Referrer policy',
'refresh' => 'Refresh',
'revisit_after' => 'Revisit After',
'rights' => 'Rights',
'set_cookie' => 'Set cookie',
'shortlink' => 'Shortlink URL',
'standout' => 'Standout',
],
];
protected function setUp() {
parent::setUp();
$this
->loginUser1();
$this
->createContentType();
$this
->drupalGet('admin/people/permissions');
}
public function testPermissionsExist() {
$this
->drupalGet('admin/people/permissions');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$session
->pageTextContains('Administer modules');
$session
->pageTextContains('Administer site configuration');
$session
->pageTextContains('Administer themes');
$session
->pageTextContains('Administer software updates');
foreach ($this->permissions as $group => $perms) {
foreach ($perms as $tag_name => $tag_label) {
$session
->fieldExists('anonymous[access metatag ' . $group . '__' . $tag_name . ']');
}
}
}
public function testUser1() {
$this
->drupalGet('node/add/page');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
foreach ($this->permissions as $group => $perms) {
foreach ($perms as $tag_name => $tag_label) {
$session
->fieldExists("field_metatag[0][{$group}][{$tag_name}]");
}
}
}
public function testUserPerms() {
$group_yes = 'basic';
$group_no = 'advanced';
$perms_yes = [
'access administration pages',
'access content',
'administer meta tags',
'administer nodes',
'create page content',
];
foreach ($this->permissions[$group_yes] as $tag_name => $tag_label) {
$perms_yes[] = "access metatag {$group_yes}__{$tag_name}";
}
$this
->verbose($perms_yes);
$user = $this
->createUser($perms_yes);
$this
->drupalLogin($user);
$this
->drupalGet('node/add/page');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
foreach ($this->permissions[$group_yes] as $tag_name => $tag_label) {
$session
->fieldExists("field_metatag[0][{$group_yes}][{$tag_name}]");
}
foreach ($this->permissions[$group_no] as $tag_name => $tag_label) {
$session
->fieldNotExists("field_metatag[0][{$group_no}][{$tag_name}]");
}
}
protected function createContentType(array $values = []) {
parent::createContentType([
'type' => 'page',
]);
$this
->drupalGet('node/add/page');
$this
->drupalGet('admin/structure/types/manage/page/fields/add-field');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'label' => 'Metatag',
'field_name' => 'metatag',
'new_storage_type' => 'metatag',
];
$this
->drupalPostForm(NULL, $edit, 'Save and continue');
$this
->drupalPostForm(NULL, [], 'Save field settings');
$this->container
->get('entity_field.manager')
->clearCachedFieldDefinitions();
}
}