View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerPermissionsTest extends SchedulerBrowserTestBase {
protected function setUp() : void {
parent::setUp();
$permissions = [
'create ' . $this->type . ' content',
'edit own ' . $this->type . ' content',
'administer nodes',
'create ' . $this->mediaTypeName . ' media',
'edit own ' . $this->mediaTypeName . ' media',
'view own unpublished media',
'create ' . $this->productTypeName . ' commerce_product',
'update own ' . $this->productTypeName . ' commerce_product',
'view own unpublished commerce_product',
'administer commerce_store',
];
$this->nodeUser = $this
->drupalCreateUser(array_merge($permissions, [
'schedule publishing of nodes',
]));
$this->nodeUser
->set('name', 'Noddy the Node Editor')
->save();
$this->mediaUser = $this
->drupalCreateUser(array_merge($permissions, [
'schedule publishing of media',
]));
$this->mediaUser
->set('name', 'Medina the Media Editor')
->save();
$this->commerce_productUser = $this
->drupalCreateUser(array_merge($permissions, [
'schedule publishing of commerce_product',
]));
$this->commerce_productUser
->set('name', 'Proctor the Product Editor')
->save();
}
public function testUserPermissionsAdd($entityTypeId, $bundle, $user) {
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$this
->drupalLogin($this->{$user});
$this
->entityTypeObject($entityTypeId)
->setThirdPartySetting('scheduler', 'publish_required', FALSE)
->setThirdPartySetting('scheduler', 'unpublish_required', FALSE)
->save();
$add_url = $this
->entityAddUrl($entityTypeId, $bundle);
$this
->drupalGet($add_url);
if (strpos($user, $entityTypeId) !== FALSE) {
$this
->assertSession()
->fieldExists('publish_on[0][value][date]');
$this
->assertSession()
->fieldExists('unpublish_on[0][value][date]');
}
else {
$this
->assertSession()
->fieldNotExists('publish_on[0][value][date]');
$this
->assertSession()
->fieldNotExists('unpublish_on[0][value][date]');
}
$title = 'Published - ' . $this
->randomString(15);
$edit = [
"{$titleField}[0][value]" => $title,
'status[value]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');
$this
->assertNotEmpty($entity = $this
->getEntityByTitle($entityTypeId, $title), sprintf('The new %s with title "%s" was created sucessfully.', $entityTypeId, $title));
$this
->assertTrue($entity
->isPublished(), 'The new entity is published');
$title = 'Unpublished - ' . $this
->randomString(15);
$edit = [
"{$titleField}[0][value]" => $title,
'status[value]' => FALSE,
];
$this
->drupalGet($add_url);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextMatches('/' . preg_quote($title, '/') . ' has been (created|successfully saved)/');
$this
->assertNotEmpty($entity = $this
->getEntityByTitle($entityTypeId, $title), sprintf('The new %s with title "%s" was created sucessfully.', $entityTypeId, $title));
$this
->assertFalse($entity
->isPublished(), 'The new entity is unpublished');
}
public function testUserPermissionsEdit($entityTypeId, $bundle, $user) {
$storage = $this
->entityStorageObject($entityTypeId);
$titleField = $entityTypeId == 'media' ? 'name' : 'title';
$this
->drupalLogin($this->{$user});
$publish_time = strtotime('+ 6 hours', $this->requestTime);
$unpublish_time = strtotime('+ 10 hours', $this->requestTime);
$unpublished_entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => FALSE,
'publish_on' => $publish_time,
]);
$this
->assertEquals($publish_time, $unpublished_entity->publish_on->value, 'The publish_on value is stored correctly before edit.');
$this
->drupalGet($unpublished_entity
->toUrl('edit-form'));
if (strpos($user, $entityTypeId) !== FALSE) {
$this
->assertSession()
->fieldExists('publish_on[0][value][date]');
$this
->assertSession()
->fieldExists('unpublish_on[0][value][date]');
}
else {
$this
->assertSession()
->fieldNotExists('publish_on[0][value][date]');
$this
->assertSession()
->fieldNotExists('unpublish_on[0][value][date]');
}
$title = 'For Publishing ' . $this
->randomString(10);
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$unpublished_entity = $storage
->load($unpublished_entity
->id());
$this
->assertEquals($title, $unpublished_entity
->label(), 'The unpublished entity title has been updated correctly after edit.');
$this
->assertEquals($publish_time, $unpublished_entity->publish_on->value, 'The publish_on value is still stored correctly after edit.');
$published_entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => TRUE,
'unpublish_on' => $unpublish_time,
]);
$this
->assertEquals($unpublish_time, $published_entity->unpublish_on->value, 'The unpublish_on value is stored correctly before edit.');
$title = 'For Unpublishing ' . $this
->randomString(10);
$this
->drupalGet($published_entity
->toUrl('edit-form'));
$this
->submitForm([
"{$titleField}[0][value]" => $title,
], 'Save');
$published_entity = $storage
->load($published_entity
->id());
$this
->assertEquals($title, $published_entity
->label(), 'The published entity title has been updated correctly after edit.');
$this
->assertEquals($unpublish_time, $published_entity->unpublish_on->value, 'The unpublish_on value is still stored correctly after edit.');
}
public function dataPermissionsTest() {
$data = [];
foreach ($this
->dataStandardEntityTypes() as $key => $values) {
$data["{$key}-1"] = array_merge($values, [
'nodeUser',
]);
$data["{$key}-2"] = array_merge($values, [
'mediaUser',
]);
$data["{$key}-3"] = array_merge($values, [
'commerce_productUser',
]);
}
return $data;
}
}