View source
<?php
namespace Drupal\Tests\moderation_dashboard\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ModerationDashboardPermissionTest extends BrowserTestBase {
use ContentModerationTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'moderation_dashboard',
];
public function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'page');
$workflow
->save();
}
protected $canViewOwnDashboardCases = [
[
'permissions' => [
'use moderation dashboard',
],
],
[
'permissions' => [
'view any moderation dashboard',
'use moderation dashboard',
],
],
];
public function testCanViewOwnDashboard() {
foreach ($this->canViewOwnDashboardCases as $i => $testCase) {
$user = $this
->createUser($testCase['permissions']);
$this
->drupalLogin($user);
$this
->assertSession()
->addressEquals("/user/{$user->id()}/moderation-dashboard");
$status_code = $this
->getSession()
->getStatusCode();
$message = "#{$i}: expected 200, got {$status_code}.";
$this
->assertEquals(200, $status_code, $message);
}
}
protected $canNotViewOwnDashboardCases = [
[
'permissions' => [],
],
[
'permissions' => [
'view any moderation dashboard',
],
],
];
public function testCanNotViewOwnDashboard() {
foreach ($this->canNotViewOwnDashboardCases as $i => $testCase) {
$user = $this
->createUser($testCase['permissions']);
$this
->drupalLogin($user);
$this
->drupalGet("/user/{$user->id()}/moderation-dashboard");
$status_code = $this
->getSession()
->getStatusCode();
if ($user
->hasPermission('view any moderation dashboard')) {
$message = "#{$i}: expected 200, got {$status_code}.";
$this
->assertEquals(200, $status_code, $message);
}
else {
$message = "#{$i}: expected 403, got {$status_code}.";
$this
->assertEquals(403, $status_code, $message);
}
}
}
protected $canViewOtherDashboardCases = [
[
'permissions_a' => [
'view any moderation dashboard',
],
'permissions_b' => [
'use moderation dashboard',
],
],
[
'permissions_a' => [
'view any moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
'use moderation dashboard',
],
],
[
'permissions_a' => [
'view any moderation dashboard',
'use moderation dashboard',
],
'permissions_b' => [
'use moderation dashboard',
],
],
[
'permissions_a' => [
'view any moderation dashboard',
'use moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
'use moderation dashboard',
],
],
];
public function testCanViewOtherDashboard() {
foreach ($this->canViewOtherDashboardCases as $i => $testCase) {
$user_a = $this
->createUser($testCase['permissions_a']);
$user_b = $this
->createUser($testCase['permissions_b']);
$this
->drupalLogin($user_a);
$this
->drupalGet("/user/{$user_b->id()}/moderation-dashboard");
$status_code = $this
->getSession()
->getStatusCode();
$message = "#{$i}: expected 200, got {$status_code}.";
$this
->assertEquals(200, $status_code, $message);
}
}
protected $canNotViewOtherDashboardCases = [
[
'permissions_a' => [],
'permissions_b' => [],
],
[
'permissions_a' => [],
'permissions_b' => [
'view any moderation dashboard',
],
],
[
'permissions_a' => [
'view any moderation dashboard',
],
'permissions_b' => [],
],
[
'permissions_a' => [
'view any moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
],
],
[
'permissions_a' => [
'view any moderation dashboard',
'use moderation dashboard',
],
'permissions_b' => [],
],
[
'permissions_a' => [
'view any moderation dashboard',
'use moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
],
],
[
'permissions_a' => [
'use moderation dashboard',
],
'permissions_b' => [],
],
[
'permissions_a' => [
'use moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
],
],
[
'permissions_a' => [],
'permissions_b' => [
'use moderation dashboard',
],
],
[
'permissions_a' => [],
'permissions_b' => [
'view any moderation dashboard',
'use moderation dashboard',
],
],
[
'permissions_a' => [
'use moderation dashboard',
],
'permissions_b' => [
'use moderation dashboard',
],
],
[
'permissions_a' => [
'use moderation dashboard',
],
'permissions_b' => [
'view any moderation dashboard',
'use moderation dashboard',
],
],
];
public function testCanNotViewOtherDashboard() {
foreach ($this->canNotViewOtherDashboardCases as $i => $testCase) {
$user_a = $this
->createUser($testCase['permissions_a']);
$user_b = $this
->createUser($testCase['permissions_b']);
$this
->drupalLogin($user_a);
$this
->drupalGet("/user/{$user_b->id()}/moderation-dashboard");
$status_code = $this
->getSession()
->getStatusCode();
if ($user_a
->hasPermission('view any moderation dashboard')) {
$message = "#{$i}: expected 200, got {$status_code}.";
$this
->assertEquals(200, $status_code, $message);
}
else {
$message = "#{$i}: expected 403, got {$status_code}.";
$this
->assertEquals(403, $status_code, $message);
}
}
}
}