View source
<?php
namespace Drupal\Tests\content_lock\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Tests\BrowserTestBase;
class ContentLockBlockTest extends BrowserTestBase {
public static $modules = [
'block',
'block_content',
'content_lock',
];
protected $defaultTheme = 'stark';
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = $title ?: $this
->randomMachineName();
$block_content = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
]);
if ($block_content && $save === TRUE) {
$block_content
->save();
}
return $block_content;
}
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE,
]);
$bundle
->save();
if ($create_body) {
block_content_add_body_field($bundle
->id());
}
return $bundle;
}
public function testContentLockBlock() {
$this
->createBlockContentType('basic', TRUE);
$block1 = $this
->createBlockContent('Block 1');
$admin = $this
->drupalCreateUser([
'administer blocks',
'administer content lock',
]);
$user1 = $this
->drupalCreateUser([
'administer blocks',
'access content',
]);
$user2 = $this
->drupalCreateUser([
'administer blocks',
'break content lock',
]);
$this
->drupalLogin($admin);
$edit = [
'block_content[bundles][basic]' => 1,
];
$this
->drupalPostForm('admin/config/content/content_lock', $edit, t('Save configuration'));
$this
->drupalLogin($user1);
$this
->drupalGet("block/{$block1->id()}");
$assert_session = $this
->assertSession();
$assert_session
->pageTextContains(t('This content is now locked against simultaneous editing.'));
$this
->drupalLogin($user2);
$this
->drupalGet("block/{$block1->id()}");
$assert_session
->pageTextContains(t('This content is being edited by the user @name and is therefore locked to prevent other users changes.', [
'@name' => $user1
->getDisplayName(),
]));
$assert_session
->linkExists(t('Break lock'));
$submit = $assert_session
->buttonExists('edit-submit');
$this
->assertTrue($submit
->hasAttribute('disabled'));
$this
->drupalLogin($user1);
$this
->drupalGet("block/{$block1->id()}");
$assert_session
->pageTextContains(t('This content is now locked by you against simultaneous editing.'));
$this
->drupalPostForm('/block/' . $block1
->id(), [], t('Save'));
$this
->drupalLogin($user2);
$this
->drupalGet("block/{$block1->id()}");
$assert_session
->pageTextContains(t('This content is now locked against simultaneous editing.'));
$this
->drupalLogin($user1);
$this
->drupalGet("block/{$block1->id()}");
$assert_session
->pageTextContains(t('This content is being edited by the user @name and is therefore locked to prevent other users changes.', [
'@name' => $user2
->getDisplayName(),
]));
$assert_session
->linkNotExists(t('Break lock'));
$submit = $assert_session
->buttonExists('edit-submit');
$this
->assertTrue($submit
->hasAttribute('disabled'));
$this
->drupalLogin($user2);
$this
->drupalGet("block/{$block1->id()}");
$assert_session
->pageTextContains(t('This content is now locked by you against simultaneous editing.'));
$this
->drupalPostForm('/block/' . $block1
->id(), [], t('Save'));
$assert_session
->pageTextContains(t('has been updated.'));
}
}