diff --git a/modules/content_lock_timeout/content_lock_timeout.module b/modules/content_lock_timeout/content_lock_timeout.module index 90fea24..14e7d2d 100644 --- a/modules/content_lock_timeout/content_lock_timeout.module +++ b/modules/content_lock_timeout/content_lock_timeout.module @@ -5,6 +5,7 @@ * Allowed time-based automatic unlocking of nodes. */ +use Drupal\Core\Session\SessionManager; use Drupal\node\NodeInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\user\Entity\User; @@ -115,7 +116,7 @@ function content_lock_timeout_user_logout($account) { // Only do the database check if the original drupal session manager is used. // Otherwise its not sure if sessions table has correct data. As it would be // a common practice to extend the Class, instanceof is not used here! - if (get_class(Drupal::service('session_manager')) == \Drupal\Core\Session\SessionManager::class) { + if (get_class(Drupal::service('session_manager')) == SessionManager::class) { $query = \Drupal::database() ->select('sessions'); $query->condition('uid', $account->id()); diff --git a/modules/content_lock_timeout/src/Tests/ContentLockTimeoutTest.php b/modules/content_lock_timeout/src/Tests/ContentLockTimeoutTest.php index aed050b..89f15ca 100644 --- a/modules/content_lock_timeout/src/Tests/ContentLockTimeoutTest.php +++ b/modules/content_lock_timeout/src/Tests/ContentLockTimeoutTest.php @@ -14,11 +14,32 @@ use Drupal\Core\Serialization\Yaml; */ class ContentLockTimeoutTest extends ContentLockTestBase { + /** + * Modules to install. + * + * @var array + */ + public static $modules = [ + 'system', + 'language', + 'user', + 'node', + 'field', + 'field_ui', + 'taxonomy', + 'block', + 'block_content', + 'content_lock_timeout' + ]; + /** * @var \Drupal\content_lock\ContentLock\ContentLock */ protected $lockService; + /** + * {@inheritDoc} + */ public function setUp() { parent::setUp(); @@ -60,6 +81,9 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->rebuildContainer(); } + /** + * Test content lock timeout with nodes. + */ protected function testContentLockNode() { // We protect the bundle created. $this->drupalLogin($this->adminUser); @@ -71,6 +95,9 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->doTestForEntity($this->article1); } + /** + * Test content lock timeout with blocks. + */ protected function testContentLockBlock() { // We protect the bundle created. $this->drupalLogin($this->adminUser); @@ -82,6 +109,9 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->doTestForEntity($this->block1); } + /** + * Test content lock timeout with terms. + */ protected function testContentLockTerm() { // We protect the bundle created. $this->drupalLogin($this->adminUser); @@ -96,9 +126,8 @@ class ContentLockTimeoutTest extends ContentLockTestBase { /** * Run the same tests for node, block and term. * - * @param \Drupal\Core\Entity\Entity $entity - * - * @throws \Drupal\Core\Entity\EntityMalformedException + * @param Entity $entity + * The entity to tests. */ protected function doTestForEntity(Entity $entity) { // We lock article1. @@ -106,18 +135,18 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->lockContentByUser1($entity); - // Content should be locked + // Content should be locked. $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(), ])); - // Jump into future to release lock + // Jump into future to release lock. \Drupal::time()->setCurrentTime(time() + 60 * 60); $this->cronRun(); \Drupal::time()->resetCurrentTime(); - // Content should be unlocked by cron + // Content should be unlocked by cron. $this->assertNoLockOnContent($entity); $this->drupalGet($entity->toUrl('edit-form')->toString()); $this->assertText(t('This content is now locked against simultaneous editing.')); @@ -131,15 +160,15 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->drupalLogin($this->user2); - // Content should be locked + // Content should be locked. $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(), ])); - // Jump into the future + // Jump into the future. \Drupal::time()->setCurrentTime(time() + 60 * 60); - // lock should be release by form prepare + // lock should be release by form prepare. $this->drupalGet($entity->toUrl('edit-form')->toString()); $this->assertText(t('This content is now locked against simultaneous editing.')); } @@ -159,9 +188,15 @@ class ContentLockTimeoutTest extends ContentLockTestBase { $this->assertEqual($this->user1->label(), $lock->name, 'Lock present for correct user.'); } + /** + * Assert if no lock is present for content. + * + * @param \Drupal\Core\Entity\Entity $entity + * The entity which should not have a lock. + */ protected function assertNoLockOnContent(Entity $entity) { $lock = $this->lockService->fetchLock($entity->id(), $entity->language()->getId(), 'edit', $entity->getEntityTypeId()); - $this->assertFalse($lock, 'No lock present'); + $this->assertFalse($lock, 'No lock present.'); } } diff --git a/modules/content_lock_timeout/src/Tests/TimeChanger.php b/modules/content_lock_timeout/src/Tests/TimeChanger.php index 5970d4c..aaa0061 100644 --- a/modules/content_lock_timeout/src/Tests/TimeChanger.php +++ b/modules/content_lock_timeout/src/Tests/TimeChanger.php @@ -13,6 +13,9 @@ class TimeChanger extends Time { protected $overwrittenTime = NULL; + /** + * {@inheritDoc} + */ public function getCurrentTime() { $time = \Drupal::keyValue('time')->get('time', NULL); if (!empty($time)) { @@ -24,7 +27,8 @@ class TimeChanger extends Time { /** * Forward current time to the given timestamp. * - * @param $time + * @param int $time + * New time to set. */ public function setCurrentTime($time) { \Drupal::keyValue('time')->set('time', $time); @@ -37,6 +41,9 @@ class TimeChanger extends Time { \Drupal::keyValue('time')->delete('time'); } + /** + * {@inheritDoc} + */ public function getRequestTime() { $time = \Drupal::keyValue('time')->get('time', NULL); if (!empty($time)) { @@ -44,4 +51,5 @@ class TimeChanger extends Time { } return parent::getRequestTime(); } + } diff --git a/src/Tests/ContentLockTestBase.php b/src/Tests/ContentLockTestBase.php index 0ce1373..f827f2e 100644 --- a/src/Tests/ContentLockTestBase.php +++ b/src/Tests/ContentLockTestBase.php @@ -31,7 +31,6 @@ class ContentLockTestBase extends WebTestBase { 'block', 'block_content', 'content_lock', - 'content_lock_timeout', ]; /**