From d14351874c4296663483417e90ac917507290234 Mon Sep 17 00:00:00 2001
From: Daniel Wehner <daniel@tag1consulting.com>
Date: Fri, 27 Nov 2015 17:22:27 +0100
Subject: [PATCH] rename methods, added test

---
 src/Revision/EnhancedEntityRevisionTrait.php  |  86 ------------
 ...ace.php => EntityRevisionLogInterface.php} |  28 ++--
 src/Revision/EntityRevisionLogTrait.php       | 122 ++++++++++++++++++
 tests/Kernel/EntityRevisionLogTraitTest.php   |  63 +++++++++
 .../modules/entity_test/entity_test.info.yml  |   3 +
 .../src/Entity/EntityWithRevisionLog.php      |  72 +++++++++++
 6 files changed, 273 insertions(+), 101 deletions(-)
 delete mode 100644 src/Revision/EnhancedEntityRevisionTrait.php
 rename src/Revision/{EnhancedEntityRevisionInterface.php => EntityRevisionLogInterface.php} (62%)
 create mode 100644 src/Revision/EntityRevisionLogTrait.php
 create mode 100644 tests/Kernel/EntityRevisionLogTraitTest.php
 create mode 100644 tests/modules/entity_test/entity_test.info.yml
 create mode 100644 tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php

diff --git a/src/Revision/EnhancedEntityRevisionTrait.php b/src/Revision/EnhancedEntityRevisionTrait.php
deleted file mode 100644
index c26338c..0000000
--- a/src/Revision/EnhancedEntityRevisionTrait.php
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\entity\Revision\EnhancedEntityRevisionTrait.
- */
-
-namespace Drupal\entity\Revision;
-use Drupal\user\Entity\User;
-
-/**
- * Provides a trait implementing \Drupal\entity\EnhanceredEntityRevisionInterface.
- */
-trait EnhancedEntityRevisionTrait {
-
-  /**
-   * Returns whether the entity type has a specific field.
-   *
-   * @param string $field_name
-   *   The field name.
-   *
-   * @return bool
-   */
-  abstract function hasField($field_name);
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getRevisionCreationTime() {
-    if ($this->hasField('revision_create')) {
-      return $this->revision_create->value;
-    }
-    return 0;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function setRevisionCreationTime($timestamp) {
-    if ($this->hasField('revision_create')) {
-      $this->revision_create->value = $timestamp;
-    }
-    return $this;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getRevisionAuthor() {
-    if ($this->hasField('revision_author')) {
-      return $this->revision_author->entity;
-    }
-    return User::getAnonymousUser();
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function setRevisionAuthorId($uid) {
-    if ($this->hasField('revision_author')) {
-      $this->revision_author->target_id = $uid;
-    }
-    return $this;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getRevisionLog() {
-    if ($this->hasField('revision_log')) {
-      return $this->revision_log->value;
-    }
-    return '';
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function setRevisionLog($revision_log) {
-    if ($this->hasField('revision_log')) {
-      $this->revision_log->value = $revision_log;
-    }
-    return $this;
-  }
-
-}
diff --git a/src/Revision/EnhancedEntityRevisionInterface.php b/src/Revision/EntityRevisionLogInterface.php
similarity index 62%
rename from src/Revision/EnhancedEntityRevisionInterface.php
rename to src/Revision/EntityRevisionLogInterface.php
index 72fbf30..3a15203 100644
--- a/src/Revision/EnhancedEntityRevisionInterface.php
+++ b/src/Revision/EntityRevisionLogInterface.php
@@ -2,18 +2,18 @@
 
 /**
  * @file
- * Contains \Drupal\entity\Revision\EnhancedEntityRevisionInterface.
+ * Contains \Drupal\entity\Revision\EntityRevisionLogInterface.
  */
 
 namespace Drupal\entity\Revision;
 
 /**
- * Defines an entity type with create/author/log information for revisions.
+ * Defines an entity type with create time/author/log information for revisions.
  */
-interface EnhancedEntityRevisionInterface {
+interface EntityRevisionLogInterface {
 
   /**
-   * Gets the node revision creation timestamp.
+   * Gets the entity revision creation timestamp.
    *
    * @return int|NULL
    *   The UNIX timestamp of when this revision was created. Return NULL if the
@@ -22,7 +22,7 @@ interface EnhancedEntityRevisionInterface {
   public function getRevisionCreationTime();
 
   /**
-   * Sets the node revision creation timestamp.
+   * Sets the entity revision creation timestamp.
    *
    * @param int $timestamp
    *   The UNIX timestamp of when this revision was created.
@@ -32,43 +32,41 @@ interface EnhancedEntityRevisionInterface {
   public function setRevisionCreationTime($timestamp);
 
   /**
-   * Gets the node revision author.
+   * Gets the entity revision author.
    *
    * @return \Drupal\user\UserInterface|NULL
    *   The user entity for the revision author. Return NULL if the entity type
    *   doesn't support revision authors.
    */
-  public function getRevisionAuthor();
+  public function getRevisionUser();
 
   /**
-   * Sets the node revision author.
+   * Sets the entity revision author.
    *
-   * @param int $uid
+   * @param int $user_id
    *   The user ID of the revision author.
    *
    * @return $this
    */
-  public function setRevisionAuthorId($uid);
+  public function setRevisionUser($user_id);
 
   /**
-   * @todo Ideally this would be its own interface?
-   *
    * Returns the entity revision log message.
    *
    * @return string|NULL
    *   The revision log message. Return NULL if the entity type doesn't support
    *   revision logs.
    */
-  public function getRevisionLog();
+  public function getRevisionLogMessage();
 
   /**
    * Sets the entity revision log message.
    *
-   * @param string $revision_log
+   * @param string $revision_log_message
    *   The revision log message.
    *
    * @return $this
    */
-  public function setRevisionLog($revision_log);
+  public function setRevisionLogMessage($revision_log_message);
 
 }
diff --git a/src/Revision/EntityRevisionLogTrait.php b/src/Revision/EntityRevisionLogTrait.php
new file mode 100644
index 0000000..a175931
--- /dev/null
+++ b/src/Revision/EntityRevisionLogTrait.php
@@ -0,0 +1,122 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\entity\Revision\EntityRevisionLogTrait.
+ */
+
+namespace Drupal\entity\Revision;
+use Drupal\Core\Field\BaseFieldDefinition;
+use Drupal\user\Entity\User;
+
+/**
+ * Provides a trait implementing \Drupal\entity\Revision\EntityRevisionLogInterface.
+ */
+trait EntityRevisionLogTrait {
+
+  /**
+   * Returns whether the entity type has a specific field.
+   *
+   * @param string $field_name
+   *   The field name.
+   *
+   * @return bool
+   */
+  abstract function hasField($field_name);
+
+  /**
+   * Provides the base fields for the entity revision log trait.
+   *
+   * @return \Drupal\Core\Field\BaseFieldDefinition[]
+   */
+  protected static function enhancedEntityRevisionDefaultBaseFields() {
+    $fields = [];
+
+    $fields['revision_create'] = BaseFieldDefinition::create('created')
+      ->setLabel(t('Revision create time'))
+      ->setDescription(t('The time that the current revision was created.'))
+      ->setRevisionable(TRUE);
+
+    $fields['revision_user'] = BaseFieldDefinition::create('entity_reference')
+      ->setLabel(t('Revision user'))
+      ->setDescription(t('The user ID of the author of the current revision.'))
+      ->setSetting('target_type', 'user')
+      ->setRevisionable(TRUE);
+
+    $fields['revision_log_message'] = BaseFieldDefinition::create('string_long')
+      ->setLabel(t('Revision log message'))
+      ->setDescription(t('Briefly describe the changes you have made.'))
+      ->setRevisionable(TRUE)
+      ->setDefaultValue('')
+      ->setDisplayOptions('form', [
+        'type' => 'string_textarea',
+        'weight' => 25,
+        'settings' => [
+          'rows' => 4,
+        ],
+      ]);
+
+    return $fields;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRevisionCreationTime() {
+    if ($this->hasField('revision_create')) {
+      return $this->revision_create->value;
+    }
+    return 0;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setRevisionCreationTime($timestamp) {
+    if ($this->hasField('revision_create')) {
+      $this->revision_create->value = $timestamp;
+    }
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRevisionUser() {
+    if ($this->hasField('revision_user')) {
+      return $this->revision_user->entity;
+    }
+    return User::getAnonymousUser();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setRevisionUser($user_id) {
+    if ($this->hasField('revision_user')) {
+      $this->revision_user->target_id = $user_id;
+    }
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRevisionLogMessage() {
+    if ($this->hasField('revision_log_message')) {
+      return $this->revision_log_message->value;
+    }
+    return '';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setRevisionLogMessage($revision_log_message) {
+    if ($this->hasField('revision_log_message')) {
+      $this->revision_log_message->value = $revision_log_message;
+    }
+    return $this;
+  }
+
+}
diff --git a/tests/Kernel/EntityRevisionLogTraitTest.php b/tests/Kernel/EntityRevisionLogTraitTest.php
new file mode 100644
index 0000000..ad9c943
--- /dev/null
+++ b/tests/Kernel/EntityRevisionLogTraitTest.php
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\entity\Kernel\EntityRevisionLogTraitTest.
+ */
+
+namespace Drupal\Tests\entity\Kernel;
+
+use Drupal\entity_test\Entity\EntityWithRevisionLog;
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\user\Entity\User;
+
+/**
+ * @coversDefaultClass \Drupal\entity\Revision\EntityRevisionLogTrait
+ */
+class EntityRevisionLogTraitTest extends KernelTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['entity', 'entity_test', 'user', 'system'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->installEntitySchema('user');
+    $this->installSchema('system', 'sequences');
+  }
+
+  public function testEntityRevisionLog() {
+    $user = User::create([
+      'name' => 'user',
+    ]);
+    $user->save();
+    $user2 = User::create([
+      'name' => 'user2',
+    ]);
+    $user2->save();
+
+    /** @var \Drupal\entity\Revision\EntityRevisionLogInterface $entity */
+    $entity = EntityWithRevisionLog::create([
+      'revision_user' => $user->id(),
+      'revision_create' => 1447941735,
+      'revision_log_message' => 'Test message',
+    ]);
+
+    $this->assertEquals(1447941735, $entity->getRevisionCreationTime());
+    $this->assertEquals($user->id(), $entity->getRevisionUser()->id());
+    $this->assertEquals('Test message', $entity->getRevisionLogMessage());
+
+    $entity->setRevisionCreationTime(1234567890);
+    $this->assertEquals(1234567890, $entity->getRevisionCreationTime());
+    $entity->setRevisionUser($user2->id());
+    $this->assertEquals($user2->id(), $entity->getRevisionUser()->id());
+    $entity->setRevisionLogMessage('Giraffe!');
+    $this->assertEquals('Giraffe!', $entity->getRevisionLogMessage());
+  }
+
+}
diff --git a/tests/modules/entity_test/entity_test.info.yml b/tests/modules/entity_test/entity_test.info.yml
new file mode 100644
index 0000000..83afba2
--- /dev/null
+++ b/tests/modules/entity_test/entity_test.info.yml
@@ -0,0 +1,3 @@
+name: Entity test
+type: module
+core: 8.x
diff --git a/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php b/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php
new file mode 100644
index 0000000..b290826
--- /dev/null
+++ b/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php
@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\entity_test\Entity\EntityWithRevisionLog.
+ */
+
+namespace Drupal\entity_test\Entity;
+
+use Drupal\Core\Entity\ContentEntityBase;
+use Drupal\Core\Entity\EntityTypeInterface;
+use Drupal\Core\Field\BaseFieldDefinition;
+use Drupal\entity\Revision\EntityRevisionLogTrait;
+
+/**
+ * @ContentEntityType(
+ *   id = "entity_test__revision_log",
+ *   label = @Translation("Entity test with revision log"),
+ *   handlers = {
+ *     "storage" = "\Drupal\Core\Entity\Sql\SqlContentEntityStorage",
+ *   },
+ *   translatable = TRUE,
+ *   revisionable = TRUE,
+ *   entity_keys = {
+ *     "id" = "id",
+ *     "revision" = "vid",
+ *     "langcode" = "langcode",
+ *   }
+ * )
+ */
+class EntityWithRevisionLog extends ContentEntityBase {
+
+  use EntityRevisionLogTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
+    $fields = [];
+
+    $fields['id'] = BaseFieldDefinition::create('integer')
+      ->setLabel(t(''))
+      ->setReadOnly(TRUE)
+      ->setSetting('unsigned', TRUE);
+
+    $fields['uuid'] = BaseFieldDefinition::create('uuid')
+      ->setLabel(t('UUID'))
+      ->setReadOnly(TRUE);
+
+    $fields['vid'] = BaseFieldDefinition::create('integer')
+      ->setLabel(t('Revision ID'))
+      ->setReadOnly(TRUE)
+      ->setSetting('unsigned', TRUE);
+
+    $fields['langcode'] = BaseFieldDefinition::create('language')
+      ->setLabel(t('Language'))
+      ->setTranslatable(TRUE)
+      ->setRevisionable(TRUE)
+      ->setDisplayOptions('view', [
+        'type' => 'hidden',
+      ])
+      ->setDisplayOptions('form', [
+        'type' => 'language_select',
+        'weight' => 2,
+      ]);
+
+    $fields += static::enhancedEntityRevisionDefaultBaseFields();
+
+    return $fields;
+  }
+
+}
-- 
GitLab