View source
<?php
namespace Drupal\Tests\content_lock_timeout\Functional;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Tests\Traits\Core\CronRunTrait;
class ContentLockTimeoutTest extends BrowserTestBase {
use TaxonomyTestTrait;
use CronRunTrait;
public static $modules = [
'system',
'language',
'user',
'node',
'field',
'field_ui',
'taxonomy',
'block',
'block_content',
'content_lock',
'content_lock_timeout',
'content_lock_timeout_test',
];
protected $defaultTheme = 'stark';
protected $permissions1;
protected $permissions2;
protected $adminUser;
protected $user1;
protected $user2;
protected $article1;
protected $vocabulary;
protected $term1;
protected $block1;
protected $lockService;
public function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this->adminUser = $this
->drupalCreateUser([
'edit any article content',
'delete any article content',
'administer nodes',
'administer content types',
'administer users',
'administer blocks',
'administer taxonomy',
'administer content lock',
]);
$this->permissions1 = [
'create article content',
'edit any article content',
'delete any article content',
'access content',
'administer blocks',
'administer taxonomy',
];
$this->permissions2 = [
'create article content',
'edit any article content',
'delete any article content',
'access content',
'administer blocks',
'administer taxonomy',
'break content lock',
];
$this->article1 = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 1',
]);
$this->vocabulary = $this
->createVocabulary();
$this->term1 = $this
->createTerm($this->vocabulary);
$this->user1 = $this
->drupalCreateUser($this->permissions1);
$this->user2 = $this
->drupalCreateUser($this->permissions2);
node_access_rebuild();
$this
->cronRun();
$this
->setNewDatetimeTimeService();
$this
->drupalLogin($this->adminUser);
$edit = [
'content_lock_timeout_minutes' => 10,
'content_lock_timeout_on_edit' => 1,
];
$this
->drupalPostForm('/admin/config/content/content_lock/timeout', $edit, t('Save configuration'));
$this->lockService = \Drupal::service('content_lock');
}
protected function setNewDatetimeTimeService() {
$filename = $this->siteDirectory . '/services.yml';
chmod($filename, 0666);
$content = file_get_contents($filename);
$content = preg_replace('/:$\\n^\\s+{\\s*}$/m', ': {}', $content);
$services = Yaml::decode($content);
$services['services']['datetime.time'] = [
'class' => 'Drupal\\content_lock_timeout_test\\TimeChanger',
'arguments' => [
'@request_stack',
],
];
file_put_contents($filename, Yaml::encode($services));
$file_cache = FileCacheFactory::get('container_yaml_loader');
$file_cache
->delete($filename);
$this
->rebuildContainer();
}
public function testContentLockNode() {
$this
->drupalLogin($this->adminUser);
$edit = [
'node[bundles][article]' => 1,
];
$this
->drupalPostForm('admin/config/content/content_lock', $edit, t('Save configuration'));
$this
->doTestForEntity($this->article1);
}
public function testContentLockTerm() {
$this
->drupalLogin($this->adminUser);
$edit = [
'taxonomy_term[bundles][' . $this->term1
->bundle() . ']' => 1,
];
$this
->drupalPostForm('admin/config/content/content_lock', $edit, t('Save configuration'));
$this
->doTestForEntity($this->term1);
}
protected function doTestForEntity(EntityInterface $entity) {
$this
->drupalLogin($this->user2);
$this
->lockContentByUser1($entity);
$this
->drupalGet($entity
->toUrl('edit-form')
->toString());
$this
->assertText(t('This content is being edited by the user @name and is therefore locked to prevent other users changes.', [
'@name' => $this->user1
->getDisplayName(),
]));
\Drupal::time()
->setCurrentTime(time() + 60 * 60);
$this
->cronRun();
\Drupal::time()
->resetCurrentTime();
$this
->assertNoLockOnContent($entity);
$this
->drupalGet($entity
->toUrl('edit-form')
->toString());
$this
->assertText(t('This content is now locked against simultaneous editing.'));
$this
->drupalLogout();
$this
->assertNoLockOnContent($entity);
$this
->lockContentByUser1($entity);
$this
->drupalLogin($this->user2);
$this
->drupalGet($entity
->toUrl('edit-form')
->toString());
$this
->assertText(t('This content is being edited by the user @name and is therefore locked to prevent other users changes.', [
'@name' => $this->user1
->getDisplayName(),
]));
\Drupal::time()
->setCurrentTime(time() + 60 * 60);
$this
->drupalGet($entity
->toUrl('edit-form')
->toString());
$this
->assertText(t('This content is now locked against simultaneous editing.'));
}
protected function lockContentByUser1(EntityInterface $entity) {
$this->lockService
->releaseAllUserLocks($this->user2
->id());
$this->lockService
->locking($entity
->id(), $entity
->language()
->getId(), 'edit', $this->user1
->id(), $entity
->getEntityTypeId());
$lock = $this->lockService
->fetchLock($entity
->id(), $entity
->language()
->getId(), 'edit', $entity
->getEntityTypeId());
$this
->assertNotNull($lock, 'Lock present');
$this
->assertEqual($this->user1
->label(), $lock->name, 'Lock present for correct user.');
}
protected function assertNoLockOnContent(EntityInterface $entity) {
$lock = $this->lockService
->fetchLock($entity
->id(), $entity
->language()
->getId(), 'edit', $entity
->getEntityTypeId());
$this
->assertFalse($lock, 'No lock present.');
}
}