commit 6039b1b6e1b27927eb86d2548acc77f8b263cf00
Author: Bart Feenstra <bart@mynameisbart.com>
Date:   Thu Nov 14 15:30:47 2013 +0100

    Strength through unit testing.

diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
index 5447e90..181ca82 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
@@ -139,7 +139,7 @@
   public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
     $this->entityType = $entity_type;
     $this->bundle = $bundle ? $bundle : $this->entityType;
-    $this->languages = language_list(Language::STATE_ALL);
+    $this->languages = $this->languageList(Language::STATE_ALL);
 
     foreach ($values as $key => $value) {
       // If the key matches an existing property set the value to the property
@@ -167,6 +167,36 @@ public function __construct(array $values, $entity_type, $bundle = FALSE, $trans
   }
 
   /**
+   * Wraps language_list().
+   */
+  protected function languageList($flags = Language::STATE_CONFIGURABLE) {
+    return language_list($flags);
+  }
+
+  /**
+   * Wraps check_plain().
+   */
+  protected function checkPlain($text) {
+    return check_plain($text);
+  }
+
+  /**
+   * Wraps format_string().
+   */
+  protected function formatString($string, array $args = array()) {
+    return format_string($string, $args);
+  }
+
+  /**
+   * Returns the typed data manager.
+   *
+   * @return \Drupal\Core\TypedData\TypedDataManager
+   */
+  protected function getTypedDataManager() {
+    return \Drupal::typedData();
+  }
+
+  /**
    * {@inheritdoc}
    */
   public function setNewRevision($value = TRUE) {
@@ -203,8 +233,7 @@ public function getRevisionId() {
    * {@inheritdoc}
    */
   public function isTranslatable() {
-    // @todo Inject the entity manager and retrieve bundle info from it.
-    $bundles = entity_get_bundles($this->entityType);
+    $bundles = $this->getEntityManager()->getBundleInfo($this->entityType);
     return !empty($bundles[$this->bundle()]['translatable']);
   }
 
@@ -258,8 +287,7 @@ public function getString() {
    * {@inheritdoc}
    */
   public function validate() {
-    // @todo: Add the typed data manager as proper dependency.
-    return \Drupal::typedData()->getValidator()->validate($this);
+    return $this->getTypedDataManager()->getValidator()->validate($this);
   }
 
   /**
@@ -395,7 +423,7 @@ protected function getTranslatedField($property_name, $langcode) {
     if (!isset($this->fields[$property_name][$langcode])) {
       $definition = $this->getPropertyDefinition($property_name);
       if (!$definition) {
-        throw new \InvalidArgumentException('Field ' . check_plain($property_name) . ' is unknown.');
+        throw new \InvalidArgumentException('Field ' . $this->checkPlain($property_name) . ' is unknown.');
       }
       // Non-translatable fields are always stored with
       // Language::LANGCODE_DEFAULT as key.
@@ -410,7 +438,7 @@ protected function getTranslatedField($property_name, $langcode) {
         if (isset($this->values[$property_name][$langcode])) {
           $value = $this->values[$property_name][$langcode];
         }
-        $field = \Drupal::typedData()->getPropertyInstance($this, $property_name, $value);
+        $field = $this->getTypedDataManager()->getPropertyInstance($this, $property_name, $value);
         $field->setLangcode($langcode);
         $this->fields[$property_name][$langcode] = $field;
       }
@@ -472,7 +500,7 @@ public function getPropertyDefinition($name) {
   public function getPropertyDefinitions() {
     if (!isset($this->fieldDefinitions)) {
       $bundle = $this->bundle != $this->entityType ? $this->bundle : NULL;
-      $this->fieldDefinitions = \Drupal::entityManager()->getFieldDefinitions($this->entityType, $bundle);
+      $this->fieldDefinitions = $this->getEntityManager()->getFieldDefinitions($this->entityType, $bundle);
     }
     return $this->fieldDefinitions;
   }
@@ -515,13 +543,13 @@ public function isEmpty() {
   /**
    * {@inheritdoc}
    */
-  public function access($operation = 'view', AccountInterface $account = NULL) {
+  public function access($operation, AccountInterface $account = NULL) {
     if ($operation == 'create') {
-      return \Drupal::entityManager()
+      return $this->getEntityManager()
         ->getAccessController($this->entityType)
         ->createAccess($this->bundle(), $account);
     }
-    return \Drupal::entityManager()
+    return $this->getEntityManager()
       ->getAccessController($this->entityType)
       ->access($this, $operation, $this->activeLangcode, $account);
   }
@@ -532,7 +560,7 @@ public function access($operation = 'view', AccountInterface $account = NULL) {
   public function language() {
     if ($this->activeLangcode != Language::LANGCODE_DEFAULT) {
       if (!isset($this->languages[$this->activeLangcode])) {
-        $this->languages += language_list(Language::STATE_ALL);
+        $this->languages += $this->languageList(Language::STATE_ALL);
       }
       return $this->languages[$this->activeLangcode];
     }
@@ -608,7 +636,7 @@ public function getTranslation($langcode) {
       else {
         // If we were given a valid language and there is no translation for it,
         // we return a new one.
-        $languages = language_list(Language::STATE_ALL);
+        $languages = $this->languageList(Language::STATE_ALL);
         if (isset($languages[$langcode])) {
           // If the entity or the requested language  is not a configured
           // language, we fall back to the entity itself, since in this case it
@@ -685,17 +713,17 @@ public function hasTranslation($langcode) {
    * {@inheritdoc}
    */
   public function addTranslation($langcode, array $values = array()) {
-    $languages = language_list(Language::STATE_ALL);
+    $languages = $this->languageList(Language::STATE_ALL);
     if (!isset($languages[$langcode]) || $this->hasTranslation($langcode)) {
       $message = 'Invalid translation language (@langcode) specified.';
-      throw new \InvalidArgumentException(format_string($message, array('@langcode' => $langcode)));
+      throw new \InvalidArgumentException($this->formatString($message, array('@langcode' => $langcode)));
     }
 
     // Instantiate a new empty entity so default values will be populated in the
     // specified language.
     $info = $this->entityInfo();
     $default_values = array($info['entity_keys']['bundle'] => $this->bundle, 'langcode' => $langcode);
-    $entity = \Drupal::entityManager()
+    $entity = $this->getEntityManager()
       ->getStorageController($this->entityType())
       ->create($default_values);
 
@@ -733,7 +761,7 @@ public function removeTranslation($langcode) {
     }
     else {
       $message = 'The specified translation (@langcode) cannot be removed.';
-      throw new \InvalidArgumentException(format_string($message, array('@langcode' => $langcode)));
+      throw new \InvalidArgumentException($this->formatString($message, array('@langcode' => $langcode)));
     }
   }
 
@@ -759,7 +787,7 @@ public function getTranslationLanguages($include_default = TRUE) {
     }
 
     // Now load language objects based upon translation langcodes.
-    return array_intersect_key(language_list(Language::STATE_ALL), $translations);
+    return array_intersect_key($this->languageList(Language::STATE_ALL), $translations);
   }
 
   /**
@@ -876,7 +904,7 @@ public function __unset($name) {
   public function createDuplicate() {
     if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
       $message = 'The entity object refers to a removed translation (@langcode) and cannot be manipulated.';
-      throw new \InvalidArgumentException(format_string($message, array('@langcode' => $this->activeLangcode)));
+      throw new \InvalidArgumentException($this->formatString($message, array('@langcode' => $this->activeLangcode)));
     }
 
     $duplicate = clone $this;
@@ -885,8 +913,7 @@ public function createDuplicate() {
 
     // Check if the entity type supports UUIDs and generate a new one if so.
     if (!empty($entity_info['entity_keys']['uuid'])) {
-      // @todo Inject the UUID service into the Entity class once possible.
-      $duplicate->{$entity_info['entity_keys']['uuid']}->value = \Drupal::service('uuid')->generate();
+      $duplicate->{$entity_info['entity_keys']['uuid']}->value = $this->getUuid()->generate();
     }
 
     // Check whether the entity type supports revisions and initialize it if so.
diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php
index 9d20fb9..dc359fa 100644
--- a/core/lib/Drupal/Core/Entity/Entity.php
+++ b/core/lib/Drupal/Core/Entity/Entity.php
@@ -54,6 +54,31 @@ public function __construct(array $values, $entity_type) {
   }
 
   /**
+   * Wraps language_load().
+   */
+  protected function languageLoad($langcode) {
+    return language_load($langcode);
+  }
+
+  /**
+   * Returns the entity manager.
+   *
+   * @return \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected function getEntityManager() {
+    return \Drupal::entityManager();
+  }
+
+  /**
+   * Returns the UUID generator.
+   *
+   * @return \Drupal\Component\Uuid\UuidInterface
+   */
+  protected function getUuid() {
+    return \Drupal::service('uuid');
+  }
+
+  /**
    * {@inheritdoc}
    */
   public function id() {
@@ -79,6 +104,8 @@ public function isNew() {
    */
   public function enforceIsNew($value = TRUE) {
     $this->enforceIsNew = $value;
+
+    return $this;
   }
 
   /**
@@ -101,8 +128,8 @@ public function bundle() {
   public function label($langcode = NULL) {
     $label = NULL;
     $entity_info = $this->entityInfo();
-    if (isset($entity_info['label_callback']) && function_exists($entity_info['label_callback'])) {
-      $label = $entity_info['label_callback']($this->entityType, $this, $langcode);
+    if (isset($entity_info['label_callback']) && is_callable($entity_info['label_callback'])) {
+      $label = call_user_func($entity_info['label_callback'], $this->entityType, $this, $langcode);
     }
     elseif (!empty($entity_info['entity_keys']['label']) && isset($this->{$entity_info['entity_keys']['label']})) {
       $label = $this->{$entity_info['entity_keys']['label']};
@@ -165,7 +192,7 @@ public function uri($rel = 'canonical') {
     $bundle = $this->bundle();
     // A bundle-specific callback takes precedence over the generic one for
     // the entity type.
-    $bundles = entity_get_bundles($this->entityType);
+    $bundles = $this->getEntityManager()->getBundleInfo($this->entityType);
     if (isset($bundles[$bundle]['uri_callback'])) {
       $uri_callback = $bundles[$bundle]['uri_callback'];
     }
@@ -175,8 +202,8 @@ public function uri($rel = 'canonical') {
 
     // Invoke the callback to get the URI. If there is no callback, use the
     // default URI format.
-    if (isset($uri_callback) && function_exists($uri_callback)) {
-      $uri = $uri_callback($this);
+    if (isset($uri_callback) && is_callable($uri_callback)) {
+      $uri = call_user_func($uri_callback, $this);
     }
     // Only use these defaults for a canonical link (that is, a link to self).
     // Other relationship types are not supported by this logic.
@@ -235,13 +262,13 @@ public function uriRelationships() {
   /**
    * {@inheritdoc}
    */
-  public function access($operation = 'view', AccountInterface $account = NULL) {
+  public function access($operation, AccountInterface $account = NULL) {
     if ($operation == 'create') {
-      return \Drupal::entityManager()
+      return $this->getEntityManager()
         ->getAccessController($this->entityType)
         ->createAccess($this->bundle(), $account);
     }
-    return \Drupal::entityManager()
+    return $this->getEntityManager()
       ->getAccessController($this->entityType)
       ->access($this, $operation, Language::LANGCODE_DEFAULT, $account);
   }
@@ -250,7 +277,7 @@ public function access($operation = 'view', AccountInterface $account = NULL) {
    * {@inheritdoc}
    */
   public function language() {
-    $language = language_load($this->langcode);
+    $language = $this->languageLoad($this->langcode);
     if (!$language) {
       // Make sure we return a proper language object.
       $language = new Language(array('id' => Language::LANGCODE_NOT_SPECIFIED));
@@ -262,7 +289,7 @@ public function language() {
    * {@inheritdoc}
    */
   public function save() {
-    return \Drupal::entityManager()->getStorageController($this->entityType)->save($this);
+    return $this->getEntityManager()->getStorageController($this->entityType)->save($this);
   }
 
   /**
@@ -270,7 +297,7 @@ public function save() {
    */
   public function delete() {
     if (!$this->isNew()) {
-      \Drupal::entityManager()->getStorageController($this->entityType)->delete(array($this->id() => $this));
+      $this->getEntityManager()->getStorageController($this->entityType)->delete(array($this->id() => $this));
     }
   }
 
@@ -284,8 +311,7 @@ public function createDuplicate() {
 
     // Check if the entity type supports UUIDs and generate a new one if so.
     if (!empty($entity_info['entity_keys']['uuid'])) {
-      // @todo Inject the UUID service into the Entity class once possible.
-      $duplicate->{$entity_info['entity_keys']['uuid']} = \Drupal::service('uuid')->generate();
+      $duplicate->{$entity_info['entity_keys']['uuid']} = $this->getUuid()->generate();
     }
     return $duplicate;
   }
@@ -294,7 +320,7 @@ public function createDuplicate() {
    * {@inheritdoc}
    */
   public function entityInfo() {
-    return \Drupal::entityManager()->getDefinition($this->entityType());
+    return $this->getEntityManager()->getDefinition($this->entityType());
   }
 
   /**
@@ -363,8 +389,8 @@ public function changed() {
     }
 
     foreach ($referenced_entity_ids as $entity_type => $entity_ids) {
-      if (\Drupal::entityManager()->hasController($entity_type, 'view_builder')) {
-        \Drupal::entityManager()->getViewBuilder($entity_type)->resetCache(array_keys($entity_ids));
+      if ($this->getEntityManager()->hasController($entity_type, 'view_builder')) {
+        $this->getEntityManager()->getViewBuilder($entity_type)->resetCache(array_keys($entity_ids));
       }
     }
   }
diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php
index f02bb1e..b2bb238 100644
--- a/core/lib/Drupal/Core/Entity/EntityInterface.php
+++ b/core/lib/Drupal/Core/Entity/EntityInterface.php
@@ -65,6 +65,8 @@ public function isNew();
    *   (optional) Whether the entity should be forced to be new. Defaults to
    *   TRUE.
    *
+   * @return self
+   *
    * @see \Drupal\Core\Entity\EntityInterface::isNew()
    */
   public function enforceIsNew($value = TRUE);
diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php
new file mode 100644
index 0000000..6701538
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php
@@ -0,0 +1,595 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest.
+ */
+
+namespace Drupal\Tests\Core\Entity;
+
+use Drupal\Tests\UnitTestCase;
+use MyProject\Proxies\__CG__\OtherProject\Proxies\__CG__\stdClass;
+
+/**
+ * Tests \Drupal\Core\Entity\ContentEntityBase;
+ */
+class ContentEntityBaseUnitTest extends UnitTestCase {
+
+  /**
+   * The bundle of the entity under test.
+   *
+   * @var string
+   */
+  protected $bundle;
+
+  /**
+   * The entity under test.
+   *
+   * @var \Drupal\Core\Entity\ContentEntityBase
+   */
+  protected $entity;
+
+  /**
+   * The entity manager used for testing.
+   *
+   * @var \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected $entityManager;
+
+  /**
+   * The type of the entity under test.
+   *
+   * @var string
+   */
+  protected $entityType;
+
+  /**
+   * The typed data manager used for testing.
+   *
+   * @var \Drupal\Core\TypedData\TypedDataManager
+   */
+  protected $typedDataManager;
+
+  /**
+   * The UUID generator used for testing.
+   *
+   * @var \Drupal\Component\Uuid\UuidInterface
+   */
+  protected $uuid;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'description' => '',
+      'name' => '\Drupal\Core\Entity\ContentEntityBase unit test',
+      'group' => 'Entity',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    $values = array();
+    $this->entityType = $this->randomName();
+    $this->bundle = $this->randomName();
+
+    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
+
+    $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
+
+    $this->typedDataManager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManager')
+      ->disableOriginalConstructor()
+      ->getMock();
+
+    $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityBase')
+      ->setConstructorArgs(array($values, $this->entityType, $this->bundle))
+      ->setMethods(array('baseFieldDefinitions', 'checkPlain', 'formatString', 'getEntityManager', 'getTypedDataManager', 'getUuid', 'languageList', 'languageLoad'))
+      ->getMock();
+    $this->entity->expects($this->any())
+      ->method('baseFieldDefinitions')
+      ->will($this->returnValue(array()));
+    $this->entity->expects($this->any())
+      ->method('getEntityManager')
+      ->will($this->returnValue($this->entityManager));
+    $this->entity->expects($this->any())
+      ->method('getUuid')
+      ->will($this->returnValue($this->uuid));
+    $this->entity->expects($this->any())
+      ->method('getTypedDataManager')
+      ->will($this->returnValue($this->typedDataManager));
+    $this->entity->expects($this->any())
+      ->method('languageList')
+      ->will($this->returnValue(array()));
+    $this->entity->expects($this->any())
+      ->method('languageLoad')
+      ->will($this->returnValue(NULL));
+    $this->entity->expects($this->any())
+      ->method('checkPlain')
+      ->will($this->returnArgument(0));
+    $this->entity->expects($this->any())
+      ->method('formatString')
+      ->will($this->returnArgument(0));
+  }
+
+  /**
+   * Tests setNewRevision().
+   */
+  public function testSetNewRevision() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests isNewRevision().
+   */
+  public function testIsNewRevision() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests isDefaultRevision().
+   */
+  public function testIsDefaultRevision() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getRevisionId().
+   */
+  public function testGetRevisionId() {
+    $this->assertNull($this->entity->getRevisionId());
+  }
+
+  /**
+   * Tests isTranslatable().
+   */
+  public function testIsTranslatable() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests preSaveRevision().
+   */
+  public function testPreSaveRevision() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $record = new \stdClass();
+    $this->entity->preSaveRevision($storage, $record);
+  }
+
+  /**
+   * Tests getDefinition().
+   */
+  public function testGetDefinition() {
+    $definition = $this->entity->getDefinition();
+    $this->assertInternalType('array', $definition);
+    $this->arrayHasKey('type', $definition);
+    $this->assertInternalType('string', $definition['type']);
+  }
+
+  /**
+   * Tests getValue().
+   */
+  public function testGetValue() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests setValue().
+   */
+  public function testSetValue() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getString().
+   */
+  public function testGetString() {
+    $this->assertInternalType('string', $this->entity->getString());
+  }
+
+  /**
+   * Tests validate().
+   */
+  public function testValidate() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests applyDefaultValue().
+   */
+  public function testApplyDefaultValue() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getConstraints().
+   */
+  public function testGetConstraints() {
+    $this->assertInternalType('array', $this->entity->getConstraints());
+  }
+
+  /**
+   * Tests getName().
+   */
+  public function testGetName() {
+    $this->assertNull($this->entity->getConstraints());
+  }
+
+  /**
+   * Tests getRoot().
+   */
+  public function testGetRoot() {
+    $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->getRoot()));
+  }
+
+  /**
+   * Tests getPropertyPath().
+   */
+  public function testGetPropertyPath() {
+    $this->assertSame('', $this->entity->getPropertyPath());
+  }
+
+  /**
+   * Tests getParent().
+   */
+  public function testGetParent() {
+    $this->assertNull($this->entity->getParent());
+  }
+
+  /**
+   * Tests setContext().
+   */
+  public function testSetContext() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests clearTranslationCache().
+   */
+  public function testClearTranslationCache() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests id().
+   */
+  public function testId() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests bundle().
+   */
+  public function testBundle() {
+    $this->assertSame($this->bundle, $this->entity->bundle());
+  }
+
+  /**
+   * Tests uuid().
+   */
+  public function testUuid() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests hasField().
+   */
+  public function testHasField() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests get().
+   */
+  public function testGet() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getTranslatedField().
+   */
+  public function testGetTranslatedField() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests set().
+   */
+  public function testSet() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getProperties().
+   */
+  public function testGetProperties() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getIterator().
+   */
+  public function testGetIterator() {
+    $this->assertInstanceOf('\Traversable', $this->entity->getIterator());
+  }
+
+  /**
+   * Tests getPropertyDefinition().
+   */
+  public function testGetPropertyDefinition() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getPropertyDefinitions().
+   */
+  public function testGetPropertyDefinitions() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getPropertyValues().
+   */
+  public function testGetPropertyValues() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests setPropertyValues().
+   */
+  public function testSetPropertyValues() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests isEmpty().
+   */
+  public function testIsEmpty() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests access().
+   */
+  public function testAccess() {
+    $access = $this->getMock('\Drupal\Core\Entity\EntityAccessControllerInterface');
+    $operation = $this->randomName();
+    $access->expects($this->at(0))
+      ->method('access')
+      ->with($this->entity, $operation)
+      ->will($this->returnValue(TRUE));
+    $access->expects($this->at(1))
+      ->method('createAccess')
+      ->will($this->returnValue(TRUE));
+    $this->entityManager->expects($this->exactly(2))
+      ->method('getAccessController')
+      ->will($this->returnValue($access));
+    $this->assertTrue($this->entity->access($operation));
+    $this->assertTrue($this->entity->access('create'));
+  }
+
+  /**
+   * Tests language().
+   */
+  public function testLanguage() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getDefaultLanguage().
+   */
+  public function testGetDefaultLanguage() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests onChange().
+   */
+  public function testOnChange() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getTranslation().
+   */
+  public function testGetTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getUntranslated().
+   */
+  public function testGetUntranslated() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests initializeTranslation().
+   */
+  public function testInitializeTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests hasTranslation().
+   */
+  public function testHasTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests addTranslation().
+   */
+  public function testAddTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests removeTranslation().
+   */
+  public function testRemoveTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests initTranslation().
+   */
+  public function testInitTranslation() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests getTranslationLanguages().
+   */
+  public function testGetTranslationLanguages() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests translations().
+   */
+  public function testTranslations() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests updateOriginalValues().
+   */
+  public function testUpdateOriginalValues() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests __get().
+   */
+  public function test__get() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests __set().
+   */
+  public function test__set() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests __isset().
+   */
+  public function test__isset() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests __unset().
+   */
+  public function test__unset() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests createDuplicate().
+   */
+  public function testCreateDuplicate() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests __clone().
+   */
+  public function test__clone() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests label().
+   */
+  public function testLabel() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests referencedEntities().
+   */
+  public function testReferencedEntities() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests enforceIsNew().
+   */
+  public function testEnforceIsNew() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests entityType().
+   */
+  public function testEntityType() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests entityInfo().
+   */
+  public function testEntityInfo() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+
+  /**
+   * Tests preSave().
+   */
+  public function testPreSave() {
+    // @todo Finish this test method.
+    $this->fail();
+  }
+}
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php
new file mode 100644
index 0000000..812d891
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php
@@ -0,0 +1,374 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Entity\EntityUnitTest.
+ */
+
+namespace Drupal\Tests\Core\Entity;
+
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests \Drupal\Core\Entity\Entity;
+ */
+class EntityUnitTest extends UnitTestCase {
+
+  /**
+   * The entity under test.
+   *
+   * @var \Drupal\Core\Entity\Entity
+   */
+  protected $entity;
+
+  /**
+   * The entity manager used for testing.
+   *
+   * @var \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected $entityManager;
+
+  /**
+   * The type of the entity under test.
+   *
+   * @var string
+   */
+  protected $entityType;
+
+  /**
+   * The UUID generator used for testing.
+   *
+   * @var \Drupal\Component\Uuid\UuidInterface
+   */
+  protected $uuid;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'description' => '',
+      'name' => '\Drupal\Core\Entity\Entity unit test',
+      'group' => 'Entity',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    $values = array();
+    $this->entityType = $this->randomName();
+
+    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
+
+    $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
+
+    $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\Entity')
+      ->setConstructorArgs(array($values, $this->entityType))
+      ->setMethods(array('getEntityManager', 'getUuid', 'languageLoad'))
+      ->getMock();
+    $this->entity->expects($this->any())
+      ->method('getEntityManager')
+      ->will($this->returnValue($this->entityManager));
+    $this->entity->expects($this->any())
+      ->method('getUuid')
+      ->will($this->returnValue($this->uuid));
+    $this->entity->expects($this->any())
+      ->method('languageLoad')
+      ->will($this->returnValue(NULL));
+  }
+
+  /**
+   * Tests id().
+   */
+  public function testId() {
+    // @todo How to test this?
+    $this->assertNull($this->entity->id());
+  }
+
+  /**
+   * Tests uuid().
+   */
+  public function testUuid() {
+    // @todo How to test this?
+    $this->assertNull($this->entity->uuid());
+  }
+
+  /**
+   * Tests isNew().
+   */
+  public function testIsNew() {
+    // @todo How to test this?
+    $this->assertInternalType('bool', $this->entity->isNew());
+  }
+
+  /**
+   * Tests enforceIsNew().
+   */
+  public function testEnforceIsNew() {
+    $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->enforceIsNew()));
+  }
+
+  /**
+   * Tests entityType().
+   */
+  public function testEntityType() {
+    $this->assertSame($this->entityType, $this->entity->entityType());
+  }
+
+  /**
+   * Tests bundle().
+   */
+  public function testBundle() {
+    $this->assertSame($this->entityType, $this->entity->bundle());
+  }
+
+  /**
+   * Tests label().
+   */
+  public function testLabel() {
+    // Make a mock with one method that we use as the entity's uri_callback. We
+    // check that it is called, and that the entity's label is the callback's
+    // return value.
+    $callback_label = $this->randomName();
+    $property_label = $this->randomName();
+    $callback_container = $this->getMock(get_class());
+    $callback_container->expects($this->once())
+      ->method(__FUNCTION__)
+      ->will($this->returnValue($callback_label));
+    $this->entityManager->expects($this->at(0))
+      ->method('getDefinition')
+      ->with($this->entityType)
+      ->will($this->returnValue(array(
+        'label_callback' => array($callback_container, __FUNCTION__),
+      )));
+
+    // Set a dummy property on the entity under test to test that the label can
+    // be returned form a property if there is no callback.
+    $this->entityManager->expects($this->at(1))
+      ->method('getDefinition')
+      ->with($this->entityType)
+      ->will($this->returnValue(array(
+        'entity_keys' => array(
+          'label' => 'label',
+        ),
+      )));
+    $this->entity->label = $property_label;
+
+    $this->assertSame($callback_label, $this->entity->label());
+    $this->assertSame($property_label, $this->entity->label());
+  }
+
+  /**
+   * Tests uri().
+   */
+  public function testUri() {
+    // Test link templates.
+    $this->entity->id = $this->randomName();
+    $expected_template_path = 'foo/' . $this->entity->id();
+    $this->entityManager->expects($this->at(0))
+      ->method('getDefinition')
+      ->with($this->entityType)
+      ->will($this->returnValue(array(
+        'links' => array(
+          'canonical' => 'foo/{id}',
+        ),
+      )));
+
+    // Make a mock with one method that we use as the entity's uri_callback. We
+    // check that it is called, and that the entity's uri is the callback's
+    // return value.
+    $expected_callback_path = $this->randomName();
+    $uri = array(
+      'path' => $expected_callback_path,
+    );
+    $callback_container = $this->getMock(get_class());
+    $callback_container->expects($this->once())
+      ->method(__FUNCTION__)
+      ->will($this->returnValue($uri));
+    $this->entityManager->expects($this->at(2))
+      ->method('getBundleInfo')
+      ->with($this->entityType)
+      ->will($this->returnValue(array(
+        $this->entityType => array(
+          'uri_callback' => array($callback_container, __FUNCTION__)
+        ),
+      )));
+
+    $actual_template_uri = $this->entity->uri();
+    $this->assertSame($expected_template_path, $actual_template_uri['path']);
+
+    $actual_callback_uri = $this->entity->uri();
+    $this->assertSame($expected_callback_path, $actual_callback_uri['path']);
+
+    $expected_fallback_path = 'entity/' . $this->entityType . '/' . $this->entity->id();
+    $actual_fallback_uri = $this->entity->uri();
+    $this->assertSame($expected_fallback_path, $actual_fallback_uri['path']);
+  }
+
+  /**
+   * Tests access().
+   */
+  public function testAccess() {
+    $access = $this->getMock('\Drupal\Core\Entity\EntityAccessControllerInterface');
+    $operation = $this->randomName();
+    $access->expects($this->at(0))
+      ->method('access')
+      ->with($this->entity, $operation)
+      ->will($this->returnValue(TRUE));
+    $access->expects($this->at(1))
+      ->method('createAccess')
+      ->will($this->returnValue(TRUE));
+    $this->entityManager->expects($this->exactly(2))
+      ->method('getAccessController')
+      ->will($this->returnValue($access));
+    $this->assertTrue($this->entity->access($operation));
+    $this->assertTrue($this->entity->access('create'));
+  }
+
+  /**
+   * Tests language().
+   */
+  public function testLanguage() {
+    $this->assertInstanceOf('\Drupal\Core\Language\Language', $this->entity->language());
+  }
+
+  /**
+   * Tests save().
+   */
+  public function testSave() {
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $storage->expects($this->once())
+      ->method('save')
+      ->with($this->entity);
+    $this->entityManager->expects($this->once())
+      ->method('getStorageController')
+      ->with($this->entityType)
+      ->will($this->returnValue($storage));
+    $this->entity->save();
+  }
+
+  /**
+   * Tests delete().
+   */
+  public function testDelete() {
+    $this->entity->id = $this->randomName();
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    // Testing the argument of the delete() method consumes too much memory.
+    $storage->expects($this->once())
+      ->method('delete');
+    $this->entityManager->expects($this->once())
+      ->method('getStorageController')
+      ->with($this->entityType)
+      ->will($this->returnValue($storage));
+    $this->entity->delete();
+  }
+
+  /**
+   * Tests createDuplicate().
+   */
+  public function testCreateDuplicate() {
+    $this->entityManager->expects($this->at(0))
+      ->method('getDefinition')
+      ->with($this->entityType)
+      ->will($this->returnValue(array(
+        'entity_keys' => array(
+          'id' => 'id',
+          'uuid' => 'uuid',
+        ),
+      )));
+    $duplicate = $this->entity->createDuplicate();
+    $this->assertInstanceOf('\Drupal\Core\Entity\Entity', $duplicate);
+    $this->assertNotSame(spl_object_hash($this->entity), spl_object_hash($duplicate));
+  }
+
+  /**
+   * Tests entityInfo().
+   */
+  public function testEntityInfo() {
+    $this->entityManager->expects($this->once())
+      ->method('getDefinition')
+      ->with($this->entityType);
+    $this->entity->entityInfo();
+  }
+
+  /**
+   * Tests preSave().
+   */
+  public function testPreSave() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->preSave($storage);
+  }
+
+  /**
+   * Tests postSave().
+   */
+  public function testPostSave() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->postSave($storage);
+  }
+
+  /**
+   * Tests preCreate().
+   */
+  public function testPreCreate() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $values = array();
+    $this->entity->preCreate($storage, $values);
+  }
+
+  /**
+   * Tests postCreate().
+   */
+  public function testPostCreate() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->postCreate($storage);
+  }
+
+  /**
+   * Tests preDelete().
+   */
+  public function testPreDelete() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->preDelete($storage, array($this->entity));
+  }
+
+  /**
+   * Tests postDelete().
+   */
+  public function testPostDelete() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->postDelete($storage, array($this->entity));
+  }
+
+  /**
+   * Tests postLoad().
+   */
+  public function testPostLoad() {
+    // This method is internal, so check for errors on calling it only.
+    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
+    $this->entity->postLoad($storage, array($this->entity));
+  }
+
+  /**
+   * Tests referencedEntities().
+   */
+  public function testReferencedEntities() {
+    $this->assertSame(array(), $this->entity->referencedEntities());
+  }
+
+  /**
+   * Tests changed().
+   */
+  public function testChanged() {
+    // This method is internal, so check for errors on calling it only.
+    $this->entity->changed();
+  }
+}
