View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerViewsAccessTest extends SchedulerBrowserTestBase {
protected static $modules = [
'views',
];
protected function createScheduledItems($entityTypeId, $bundle) {
if ($entityTypeId == 'node') {
$edit_key = 'nodes';
$view_key = 'content';
}
else {
$edit_key = $view_key = $entityTypeId;
}
$base_permissions = [
"view own unpublished {$view_key}",
];
$this->webUser = $this
->drupalCreateUser(array_merge($base_permissions, [
"access {$view_key} overview",
]));
$this->webUser
->set('name', 'Webisa the Web User')
->save();
$this->schedulerEditor = $this
->drupalCreateUser(array_merge($base_permissions, [
"schedule publishing of {$edit_key}",
]));
$this->schedulerEditor
->set('name', 'Eddie the Scheduler Editor')
->save();
$this->schedulerViewer = $this
->drupalCreateUser(array_merge($base_permissions, [
"view scheduled {$view_key}",
]));
$this->schedulerViewer
->set('name', 'Vicenza the Scheduler Viewer')
->save();
$this
->addPermissionsToUser($this->adminUser, [
'access user profiles',
]);
$this
->createEntity($entityTypeId, $bundle, [
'title' => "{$entityTypeId} created by Scheduler Editor for publishing",
'uid' => $this->schedulerEditor
->id(),
'status' => FALSE,
'publish_on' => strtotime('+1 week'),
]);
$this
->createEntity($entityTypeId, $bundle, [
'title' => "{$entityTypeId} created by Scheduler Editor for unpublishing",
'uid' => $this->schedulerEditor
->id(),
'status' => TRUE,
'unpublish_on' => strtotime('+1 week'),
]);
$this
->createEntity($entityTypeId, $bundle, [
'title' => "{$entityTypeId} created by Scheduler Viewer for publishing",
'uid' => $this->schedulerViewer
->id(),
'status' => FALSE,
'publish_on' => strtotime('+1 week'),
]);
$this
->createEntity($entityTypeId, $bundle, [
'title' => "{$entityTypeId} created by Scheduler Viewer for unpublishing",
'uid' => $this->schedulerViewer
->id(),
'status' => TRUE,
'unpublish_on' => strtotime('+1 week'),
]);
}
public function testViewScheduledContentUser($entityTypeId, $bundle) {
$this
->createScheduledItems($entityTypeId, $bundle);
$url_end = $entityTypeId == 'node' ? 'scheduled' : "scheduled_{$entityTypeId}";
$assert = $this
->assertSession();
$this
->drupalGet("user/{$this->schedulerEditor->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->webUser);
$this
->drupalGet("user/{$this->webUser->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->schedulerEditor);
$this
->drupalGet("user/{$this->schedulerEditor->id()}/{$url_end}");
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Editor for publishing");
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Editor for unpublishing");
$assert
->pageTextNotContains("{$entityTypeId} created by Scheduler Viewer for publishing");
$assert
->pageTextNotContains("{$entityTypeId} created by Scheduler Viewer for unpublishing");
$this
->drupalGet("user/{$this->schedulerViewer->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->schedulerViewer);
$this
->drupalGet("user/{$this->schedulerViewer->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
$this
->drupalGet("user/{$this->schedulerEditor->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("user/{$this->schedulerEditor->id()}/{$url_end}");
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Editor for publishing");
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Editor for unpublishing");
$assert
->pageTextNotContains("{$entityTypeId} created by Scheduler Viewer for publishing");
$assert
->pageTextNotContains("{$entityTypeId} created by Scheduler Viewer for unpublishing");
$this
->drupalGet("user/{$this->schedulerViewer->id()}/{$url_end}");
$assert
->statusCodeEquals(403);
}
public function dataViewScheduledContentUser() {
$data = $this
->dataStandardEntityTypes();
unset($data['#commerce_product']);
return $data;
}
public function testViewScheduledContentOverview($entityTypeId, $bundle) {
$this
->createScheduledItems($entityTypeId, $bundle);
$scheduled_urls = [
'node' => 'admin/content/scheduled',
'media' => 'admin/content/media/scheduled',
'commerce_product' => 'admin/commerce/products/scheduled',
];
$scheduled_url = $scheduled_urls[$entityTypeId];
$assert = $this
->assertSession();
$this
->drupalGet($scheduled_url);
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->webUser);
$this
->drupalGet($scheduled_url);
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->schedulerEditor);
$this
->drupalGet($scheduled_url);
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->schedulerViewer);
$this
->drupalGet($scheduled_url);
$assert
->statusCodeEquals(200);
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Editor for unpublishing");
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Viewer for publishing");
$assert
->pageTextContains("{$entityTypeId} created by Scheduler Viewer for unpublishing");
}
}