diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index b81e1eb..f63b1a0 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -549,7 +549,7 @@ public function language() { $language = NULL; if ($this->activeLangcode != Language::LANGCODE_DEFAULT) { if (!isset($this->languages[$this->activeLangcode])) { - $this->languages += $this->languageList(Language::STATE_ALL); + $this->languages += $this->languageManager()->getLanguages(Language::STATE_ALL); } $language = $this->languages[$this->activeLangcode]; } diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index d395399..2bddb37 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -24,13 +24,6 @@ public $langcode = Language::LANGCODE_NOT_SPECIFIED; /** - * The UUID generator. - * - * @var \Drupal\Component\Uuid\UuidInterface - */ - protected $uuidGenerator; - - /** * The entity type. * * @var string @@ -52,13 +45,6 @@ protected $urlGenerator; /** - * The language manager. - * - * @var \Drupal\Core\Language\LanguageManagerInterface - */ - protected $languageManager; - - /** * Constructs an Entity object. * * @param array $values @@ -99,11 +85,7 @@ protected function languageManager() { * @return \Drupal\Component\Uuid\UuidInterface */ protected function uuidGenerator() { - if (!$this->uuidGenerator) { - $this->uuidGenerator = \Drupal::service('uuid'); - } - - return $this->uuidGenerator; + return \Drupal::service('uuid'); } /** @@ -392,7 +374,6 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr * {@inheritdoc} */ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) { - /** @var self[] $entities */ foreach ($entities as $entity) { $entity->onSaveOrDelete(); } diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index abecab4..9deeaa6 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; /** @@ -62,7 +63,6 @@ class ConfigEntityBaseUnitTest extends UnitTestCase { */ protected $provider; - /** * The language manager. * @@ -71,6 +71,13 @@ class ConfigEntityBaseUnitTest extends UnitTestCase { protected $languageManager; /** + * The entity ID. + * + * @var string + */ + protected $id; + + /** * {@inheritdoc} */ public static function getInfo() { @@ -85,7 +92,12 @@ public static function getInfo() { * {@inheritdoc} */ public function setUp() { - $values = array(); + $this->id = $this->randomName(); + $values = array( + 'id' => $this->id, + 'langcode' => 'en', + 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', + ); $this->entityTypeId = $this->randomName(); $this->provider = $this->randomName(); $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); @@ -104,7 +116,8 @@ public function setUp() { $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager->expects($this->any()) ->method('getLanguage') - ->will($this->returnValue(NULL)); + ->with('en') + ->will($this->returnValue(new Language(array('id' => 'en')))); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); @@ -255,9 +268,11 @@ public function testCalculateDependenciesWithPluginBagSameProviderAsEntityType() * @covers ::getOriginalId */ public function testGetOriginalId() { - $id = $this->randomName(); - $this->assertSame($this->entity, $this->entity->setOriginalId($id)); - $this->assertSame($id, $this->entity->getOriginalId()); + $new_id = $this->randomName(); + $this->entity->set('id', $new_id); + $this->assertSame($this->id, $this->entity->getOriginalId()); + $this->assertSame($this->entity, $this->entity->setOriginalId($new_id)); + $this->assertSame($new_id, $this->entity->getOriginalId()); } /** @@ -276,9 +291,9 @@ public function testIsNew() { * @covers ::get */ public function testGet() { - $name = $this->randomName(); + $name = 'id'; $value = $this->randomName(); - $this->assertNull($this->entity->get($name)); + $this->assertSame($this->id, $this->entity->get($name)); $this->assertSame($this->entity, $this->entity->set($name, $value)); $this->assertSame($value, $this->entity->get($name)); } @@ -331,19 +346,33 @@ public function testIsSyncing() { * @covers ::createDuplicate */ public function testCreateDuplicate() { - $this->entityType->expects($this->once()) + $this->entityType->expects($this->at(0)) ->method('getKey') ->with('id') ->will($this->returnValue('id')); - $this->entity->setOriginalId('foo'); - /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $duplicate */ + $this->entityType->expects($this->at(1)) + ->method('hasKey') + ->with('uuid') + ->will($this->returnValue(TRUE)); + + $this->entityType->expects($this->at(2)) + ->method('getKey') + ->with('uuid') + ->will($this->returnValue('uuid')); + + $new_uuid = '8607ef21-42bc-4913-978f-8c06207b0395'; + $this->uuid->expects($this->once()) + ->method('generate') + ->will($this->returnValue($new_uuid)); + $duplicate = $this->entity->createDuplicate(); $this->assertInstanceOf('\Drupal\Core\Entity\Entity', $duplicate); $this->assertNotSame($this->entity, $duplicate); $this->assertNull($duplicate->id()); $this->assertNull($duplicate->getOriginalId()); - $this->assertNull($duplicate->uuid()); + $this->assertNotEquals($this->entity->uuid(), $duplicate->uuid()); + $this->assertSame($new_uuid, $duplicate->uuid()); } /** @@ -374,7 +403,7 @@ public function testSort() { } /** - * @covers ::getExportProperties + * @covers ::toArray */ public function testToArray() { $properties = $this->entity->toArray(); diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php index 2aab55a..406651b 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; +use Drupal\Core\Language\Language; /** * @coversDefaultClass \Drupal\Core\Entity\ContentEntityBase @@ -74,6 +75,13 @@ class ContentEntityBaseUnitTest extends UnitTestCase { protected $uuid; /** + * The entity ID. + * + * @var int + */ + protected $id; + + /** * {@inheritdoc} */ public static function getInfo() { @@ -88,7 +96,12 @@ public static function getInfo() { * {@inheritdoc} */ public function setUp() { - $values = array(); + $this->id = 1; + $values = array( + 'id' => $this->id, + 'langcode' => 'en', + 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', + ); $this->entityTypeId = $this->randomName(); $this->bundle = $this->randomName(); @@ -114,13 +127,15 @@ public function setUp() { ->disableOriginalConstructor() ->getMock(); + $language = new Language(array('id' => 'en')); $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager->expects($this->any()) ->method('getLanguages') - ->will($this->returnValue(array())); + ->will($this->returnValue(array('en' => $language))); $this->languageManager->expects($this->any()) ->method('getLanguage') - ->will($this->returnValue(NULL)); + ->with('en') + ->will($this->returnValue($language)); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); @@ -137,6 +152,8 @@ public function setUp() { * @covers ::setNewRevision */ public function testIsNewRevision() { + // Set up the entity type so that on the first call there is no revision key + // and on the second call there is one. $this->entityType->expects($this->at(0)) ->method('hasKey') ->with('revision') @@ -158,7 +175,7 @@ public function testIsNewRevision() { public function testIsDefaultRevision() { // The default value is TRUE. $this->assertTrue($this->entity->isDefaultRevision()); - // We override the value, but it does not affect this call. + // Change the default revision, verify that the old value is returned. $this->assertTrue($this->entity->isDefaultRevision(FALSE)); // The last call changed the return value for this call. $this->assertFalse($this->entity->isDefaultRevision()); @@ -168,6 +185,7 @@ public function testIsDefaultRevision() { * @covers ::getRevisionId */ public function testGetRevisionId() { + // The default getRevisionId() implementation returns NULL. $this->assertNull($this->entity->getRevisionId()); } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index 2918af7..be09ad8 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; /** @@ -67,6 +68,13 @@ class EntityUnitTest extends UnitTestCase { protected $languageManager; /** + * The entity values. + * + * @var array + */ + protected $values; + + /** * {@inheritdoc} */ public static function getInfo() { @@ -81,7 +89,11 @@ public static function getInfo() { * {@inheritdoc} */ public function setUp() { - $values = array(); + $this->values = array( + 'id' => 1, + 'langcode' => 'en', + 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', + ); $this->entityTypeId = $this->randomName(); $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); @@ -97,7 +109,8 @@ public function setUp() { $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager->expects($this->any()) ->method('getLanguage') - ->will($this->returnValue(NULL)); + ->with('en') + ->will($this->returnValue(new Language(array('id' => 'en')))); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); @@ -106,45 +119,40 @@ public function setUp() { \Drupal::setContainer($container); - $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\Entity', array($values, $this->entityTypeId)); + $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\Entity', array($this->values, $this->entityTypeId)); } /** * @covers ::id */ public function testId() { - // @todo How to test this? - $this->assertNull($this->entity->id()); + $this->assertSame($this->values['id'], $this->entity->id()); } /** * @covers ::uuid */ public function testUuid() { - // @todo How to test this? - $this->assertNull($this->entity->uuid()); + $this->assertSame($this->values['uuid'], $this->entity->uuid()); } /** * @covers ::isNew - */ - public function testIsNew() { - // @todo How to test this? - $this->assertInternalType('bool', $this->entity->isNew()); - } - - /** * @covers ::enforceIsNew */ - public function testEnforceIsNew() { + public function testIsNew() { + // We provided an ID, so the entity is not new. + $this->assertFalse($this->entity->isNew()); + // Force it to be new. $this->assertSame($this->entity, $this->entity->enforceIsNew()); + $this->assertTrue($this->entity->isNew()); } /** * @covers ::getEntityType */ public function testGetEntityType() { - $this->assertInstanceOf('\Drupal\Core\Entity\EntityTypeInterface', $this->entity->getEntityType()); + $this->assertSame($this->entityType, $this->entity->getEntityType()); } /** @@ -218,7 +226,7 @@ public function testAccess() { * @covers ::language */ public function testLanguage() { - $this->assertInstanceOf('\Drupal\Core\Language\Language', $this->entity->language()); + $this->assertSame('en', $this->entity->language()->id); } /**