diff -u b/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php --- b/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -192,7 +192,7 @@ * * @return \Drupal\Core\TypedData\TypedDataManager */ - protected function getTypedDataManager() { + protected function typedDataManager() { return \Drupal::typedData(); } @@ -233,7 +233,7 @@ * {@inheritdoc} */ public function isTranslatable() { - $bundles = $this->getEntityManager()->getBundleInfo($this->entityType); + $bundles = $this->entityManager()->getBundleInfo($this->entityType); return !empty($bundles[$this->bundle()]['translatable']); } @@ -287,7 +287,7 @@ * {@inheritdoc} */ public function validate() { - return $this->getTypedDataManager()->getValidator()->validate($this); + return $this->typedDataManager()->getValidator()->validate($this); } /** @@ -438,7 +438,7 @@ if (isset($this->values[$property_name][$langcode])) { $value = $this->values[$property_name][$langcode]; } - $field = $this->getTypedDataManager()->getPropertyInstance($this, $property_name, $value); + $field = $this->typedDataManager()->getPropertyInstance($this, $property_name, $value); $field->setLangcode($langcode); $this->fields[$property_name][$langcode] = $field; } @@ -500,7 +500,7 @@ public function getPropertyDefinitions() { if (!isset($this->fieldDefinitions)) { $bundle = $this->bundle != $this->entityType ? $this->bundle : NULL; - $this->fieldDefinitions = $this->getEntityManager()->getFieldDefinitions($this->entityType, $bundle); + $this->fieldDefinitions = $this->entityManager()->getFieldDefinitions($this->entityType, $bundle); } return $this->fieldDefinitions; } @@ -545,11 +545,11 @@ */ public function access($operation, AccountInterface $account = NULL) { if ($operation == 'create') { - return $this->getEntityManager() + return $this->entityManager() ->getAccessController($this->entityType) ->createAccess($this->bundle(), $account); } - return $this->getEntityManager() + return $this->entityManager() ->getAccessController($this->entityType) ->access($this, $operation, $this->activeLangcode, $account); } @@ -723,7 +723,7 @@ // specified language. $info = $this->entityInfo(); $default_values = array($info['entity_keys']['bundle'] => $this->bundle, 'langcode' => $langcode); - $entity = $this->getEntityManager() + $entity = $this->entityManager() ->getStorageController($this->entityType()) ->create($default_values); @@ -913,7 +913,7 @@ // Check if the entity type supports UUIDs and generate a new one if so. if (!empty($entity_info['entity_keys']['uuid'])) { - $duplicate->{$entity_info['entity_keys']['uuid']}->value = $this->getUuid()->generate(); + $duplicate->{$entity_info['entity_keys']['uuid']}->value = $this->uuidGenerator()->generate(); } // Check whether the entity type supports revisions and initialize it if so. interdiff impossible; taking evasive action reverted: --- b/core/lib/Drupal/Core/Entity/Entity.php +++ a/core/lib/Drupal/Core/Entity/Entity.php @@ -54,31 +54,6 @@ } /** - * 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() { @@ -104,8 +79,6 @@ */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; - - return $this; } /** @@ -128,8 +101,8 @@ 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']}; @@ -192,7 +165,7 @@ $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']; } @@ -202,8 +175,8 @@ // 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. @@ -262,13 +235,13 @@ /** * {@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); } @@ -277,7 +250,7 @@ * {@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)); @@ -289,7 +262,7 @@ * {@inheritdoc} */ public function save() { + return \Drupal::entityManager()->getStorageController($this->entityType)->save($this); - return $this->getEntityManager()->getStorageController($this->entityType)->save($this); } /** @@ -297,7 +270,7 @@ */ 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)); } } @@ -311,7 +284,8 @@ // 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; } @@ -320,7 +294,7 @@ * {@inheritdoc} */ public function entityInfo() { + return \Drupal::entityManager()->getDefinition($this->entityType()); - return $this->getEntityManager()->getDefinition($this->entityType()); } /** @@ -389,8 +363,8 @@ } 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)); } } } unchanged: --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -62,6 +62,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 entityManager() { + return \Drupal::entityManager(); + } + + /** + * Returns the UUID generator. + * + * @return \Drupal\Component\Uuid\UuidInterface + */ + protected function uuidGenerator() { + return \Drupal::service('uuid'); + } + + /** * {@inheritdoc} */ public function id() { @@ -87,6 +112,8 @@ public function isNew() { */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; + + return $this; } /** @@ -109,8 +136,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']}; @@ -181,7 +208,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->entityManager()->getBundleInfo($this->entityType); if (isset($bundles[$bundle]['uri_callback'])) { $uri_callback = $bundles[$bundle]['uri_callback']; } @@ -191,8 +218,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. @@ -251,13 +278,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->entityManager() ->getAccessController($this->entityType) ->createAccess($this->bundle(), $account); } - return \Drupal::entityManager() + return $this->entityManager() ->getAccessController($this->entityType) ->access($this, $operation, Language::LANGCODE_DEFAULT, $account); } @@ -266,7 +293,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)); @@ -278,7 +305,7 @@ public function language() { * {@inheritdoc} */ public function save() { - return \Drupal::entityManager()->getStorageController($this->entityType)->save($this); + return $this->entityManager()->getStorageController($this->entityType)->save($this); } /** @@ -286,7 +313,7 @@ public function save() { */ public function delete() { if (!$this->isNew()) { - \Drupal::entityManager()->getStorageController($this->entityType)->delete(array($this->id() => $this)); + $this->entityManager()->getStorageController($this->entityType)->delete(array($this->id() => $this)); } } @@ -300,8 +327,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->uuidGenerator()->generate(); } return $duplicate; } @@ -310,7 +336,7 @@ public function createDuplicate() { * {@inheritdoc} */ public function entityInfo() { - return \Drupal::entityManager()->getDefinition($this->entityType()); + return $this->entityManager()->getDefinition($this->entityType()); } /** @@ -379,8 +405,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->entityManager()->hasController($entity_type, 'view_builder')) { + $this->entityManager()->getViewBuilder($entity_type)->resetCache(array_keys($entity_ids)); } } } diff -u b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php --- b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -93,17 +93,17 @@ $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityBase') ->setConstructorArgs(array($values, $this->entityType, $this->bundle)) - ->setMethods(array('checkPlain', 'formatString', 'getEntityManager', 'getTypedDataManager', 'getUuid', 'languageList', 'languageLoad')) + ->setMethods(array('checkPlain', 'entityManager', 'formatString', 'languageList', 'languageLoad', 'typedDataManager', 'uuidGenerator')) ->getMockForAbstractClass(); $this->entity->expects($this->any()) - ->method('getEntityManager') - ->will($this->returnValue($this->entityManager)); + ->method('checkPlain') + ->will($this->returnArgument(0)); $this->entity->expects($this->any()) - ->method('getUuid') - ->will($this->returnValue($this->uuid)); + ->method('entityManager') + ->will($this->returnValue($this->entityManager)); $this->entity->expects($this->any()) - ->method('getTypedDataManager') - ->will($this->returnValue($this->typedDataManager)); + ->method('formatString') + ->will($this->returnArgument(0)); $this->entity->expects($this->any()) ->method('languageList') ->will($this->returnValue(array())); @@ -111,11 +111,11 @@ ->method('languageLoad') ->will($this->returnValue(NULL)); $this->entity->expects($this->any()) - ->method('checkPlain') - ->will($this->returnArgument(0)); + ->method('typedDataManager') + ->will($this->returnValue($this->typedDataManager)); $this->entity->expects($this->any()) - ->method('formatString') - ->will($this->returnArgument(0)); + ->method('uuidGenerator') + ->will($this->returnValue($this->uuid)); } /** @@ -201,6 +201,8 @@ * Tests getValue(). */ public function testGetValue() { + // @todo Finish this test method. + $this->fail(); } /** @@ -365,7 +367,6 @@ * Tests getPropertyDefinition(). */ public function testGetPropertyDefinition() { - return; $this->entityManager->expects($this->once()) ->method('getFieldDefinitions') ->with($this->entityType, $this->bundle) @@ -380,9 +381,8 @@ */ public function testGetPropertyDefinitions() { $definitions = $this->entity->getPropertyDefinitions(); - $this->fail(print_r(array($this->entity->baseFieldDefinitions($this->entityType)), TRUE)); -// $this->assertInternalType('array', $definitions); -// $this->arrayHasKey('id', $definitions); + $this->assertInternalType('array', $definitions); + $this->arrayHasKey('id', $definitions); } /** diff -u b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php --- b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -36,6 +36,13 @@ protected $entityType; /** + * The route provider used for testing. + * + * @var \Drupal\Core\Routing\RouteProvider + */ + protected $routeProvider; + + /** * The UUID generator used for testing. * * @var \Drupal\Component\Uuid\UuidInterface @@ -62,21 +69,28 @@ $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); + $this->routeProvider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProvider') + ->disableOriginalConstructor() + ->getMock(); + $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')) + ->setMethods(array('entityManager', 'uuidGenerator', 'languageLoad', 'routeProvider')) ->getMock(); $this->entity->expects($this->any()) - ->method('getEntityManager') + ->method('entityManager') ->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)); + $this->entity->expects($this->any()) + ->method('routeProvider') + ->will($this->returnValue($this->routeProvider)); + $this->entity->expects($this->any()) + ->method('uuidGenerator') + ->will($this->returnValue($this->uuid)); } /** @@ -167,14 +181,26 @@ // Test link templates. $this->entity->id = $this->randomName(); $expected_template_path = 'foo/' . $this->entity->id(); + $route_name = 'foo.bar'; + $route_path = 'foo/{id}'; $this->entityManager->expects($this->at(0)) ->method('getDefinition') ->with($this->entityType) ->will($this->returnValue(array( 'links' => array( - 'canonical' => 'foo/{id}', + 'canonical' => $route_name, ), ))); + $route = $this->getMockBuilder('\Symfony\Component\Routing\Route') + ->disableOriginalConstructor() + ->getMock(); + $route->expects($this->once()) + ->method('getPath') + ->will($this->returnValue($route_path)); + $this->routeProvider->expects($this->once()) + ->method('getRouteByName') + ->with($route_name) + ->will($this->returnValue($route)); // 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 @@ -281,6 +307,8 @@ $duplicate = $this->entity->createDuplicate(); $this->assertInstanceOf('\Drupal\Core\Entity\Entity', $duplicate); $this->assertNotSame(spl_object_hash($this->entity), spl_object_hash($duplicate)); + $this->assertNull($duplicate->id()); + $this->assertNull($duplicate->uuid()); } /** only in patch2: unchanged: --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -57,6 +57,13 @@ public function __construct(array $values, $entity_type) { } /** + * Wraps format_string(). + */ + protected function formatString($string, array $args = array()) { + return format_string($string, $args); + } + + /** * {@inheritdoc} */ public function getOriginalId() { @@ -95,6 +102,8 @@ public function get($property_name) { */ public function set($property_name, $value) { $this->{$property_name} = $value; + + return $this; } /** @@ -131,6 +140,8 @@ public function status() { */ public function setSyncing($syncing) { $this->isSyncing = $syncing; + + return $this; } /** @@ -153,7 +164,7 @@ public function createDuplicate() { /** * Helper callback for uasort() to sort configuration entities by weight and label. */ - public static function sort($a, $b) { + public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) { $a_weight = isset($a->weight) ? $a->weight : 0; $b_weight = isset($b->weight) ? $b->weight : 0; if ($a_weight == $b_weight) { @@ -185,21 +196,24 @@ public function getExportProperties() { public function preSave(EntityStorageControllerInterface $storage_controller) { parent::preSave($storage_controller); - // Ensure this entity's UUID does not exist with a different ID, regardless - // of whether it's new or updated. - $matching_entities = $storage_controller->getQuery() - ->condition('uuid', $this->uuid()) - ->execute(); - $matched_entity = reset($matching_entities); - if (!empty($matched_entity) && ($matched_entity != $this->id())) { - throw new ConfigDuplicateUUIDException(format_string('Attempt to save a configuration entity %id with UUID %uuid when this UUID is already used for %matched', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%matched' => $matched_entity))); - } + // The getQuery() method only exists on ConfigStorageController. + if ($storage_controller instanceof ConfigStorageController) { + // Ensure this entity's UUID does not exist with a different ID, regardless + // of whether it's new or updated. + $matching_entities = $storage_controller->getQuery() + ->condition('uuid', $this->uuid()) + ->execute(); + $matched_entity = reset($matching_entities); + if (!empty($matched_entity) && ($matched_entity != $this->id())) { + throw new ConfigDuplicateUUIDException($this->formatString('Attempt to save a configuration entity %id with UUID %uuid when this UUID is already used for %matched', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%matched' => $matched_entity))); + } - if (!$this->isNew()) { - $original = $storage_controller->loadUnchanged($this->id()); - // Ensure that the UUID cannot be changed for an existing entity. - if ($original && ($original->uuid() != $this->uuid())) { - throw new ConfigDuplicateUUIDException(format_string('Attempt to save a configuration entity %id with UUID %uuid when this entity already exists with UUID %original_uuid', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%original_uuid' => $original->uuid()))); + if (!$this->isNew()) { + $original = $storage_controller->loadUnchanged($this->id()); + // Ensure that the UUID cannot be changed for an existing entity. + if ($original && ($original->uuid() != $this->uuid())) { + throw new ConfigDuplicateUUIDException($this->formatString('Attempt to save a configuration entity %id with UUID %uuid when this entity already exists with UUID %original_uuid', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%original_uuid' => $original->uuid()))); + } } } } only in patch2: unchanged: --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php @@ -64,6 +64,8 @@ public function setStatus($status); * * @param bool $status * The status of the sync flag. + * + * @return self */ public function setSyncing($status); @@ -109,6 +111,8 @@ public function get($property_name); * The name of the property that should be set. * @param mixed $value * The value the property should be set to. + * + * @return self */ public function set($property_name, $value); only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -0,0 +1,272 @@ + '', + 'name' => '\Drupal\Core\Config\Entity\ConfigEntityBase unit test', + 'group' => 'Entity', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $values = array(); + $this->entityType = $this->randomName(); + + $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); + + $this->routeProvider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProvider') + ->disableOriginalConstructor() + ->getMock(); + + $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); + + $this->entity = $this->getMockBuilder('\Drupal\Core\Config\Entity\ConfigEntityBase') + ->setConstructorArgs(array($values, $this->entityType)) + ->setMethods(array('entityManager', 'formatString', 'languageLoad', 'routeProvider', 'uuidGenerator')) + ->getMock(); + $this->entity->expects($this->any()) + ->method('formatString') + ->will($this->returnArgument(0)); + $this->entity->expects($this->any()) + ->method('entityManager') + ->will($this->returnValue($this->entityManager)); + $this->entity->expects($this->any()) + ->method('routeProvider') + ->will($this->returnValue($this->routeProvider)); + $this->entity->expects($this->any()) + ->method('uuidGenerator') + ->will($this->returnValue($this->uuid)); + $this->entity->expects($this->any()) + ->method('languageLoad') + ->will($this->returnValue(NULL)); + } + + /** + * Tests setOriginalId() and getOriginalId(). + */ + public function testGetOriginalId() { + $id = $this->randomName(); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->setOriginalId($id))); + $this->assertSame($id, $this->entity->getOriginalId()); + } + + /** + * Tests isNew(). + */ + public function testIsNew() { + $this->assertFalse($this->entity->isNew()); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->enforceIsNew())); + $this->assertTrue($this->entity->isNew()); + $this->entity->enforceIsNew(FALSE); + $this->assertFalse($this->entity->isNew()); + } + + /** + * Tests set() and get(). + */ + public function testGet() { + $name = $this->randomName(); + $value = $this->randomName(); + $this->assertNull($this->entity->get($name)); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->set($name, $value))); + $this->assertSame($value, $this->entity->get($name)); + } + + /** + * Tests setStatus() and status(). + */ + public function testSetStatus() { + $this->assertTrue($this->entity->status()); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->setStatus(FALSE))); + $this->assertFalse($this->entity->status()); + $this->entity->setStatus(TRUE); + $this->assertTrue($this->entity->status()); + } + + /** + * Tests enable(). + * + * @depends testSetStatus + */ + public function testEnable() { + $this->entity->setStatus(FALSE); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->enable())); + $this->assertTrue($this->entity->status()); + } + + /** + * Tests disable(). + * + * @depends testSetStatus + */ + public function testDisable() { + $this->entity->setStatus(TRUE); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->disable())); + $this->assertFalse($this->entity->status()); + } + + /** + * Tests setSyncing() and isSyncing(). + */ + public function testIsSyncing() { + $this->assertFalse($this->entity->isSyncing()); + $this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->setSyncing(TRUE))); + $this->assertTrue($this->entity->isSyncing()); + $this->entity->setSyncing(FALSE); + $this->assertFalse($this->entity->isSyncing()); + } + + /** + * 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', + ), + ))); + $this->entity->setOriginalId('foo'); + $duplicate = $this->entity->createDuplicate(); + $this->assertInstanceOf('\Drupal\Core\Entity\Entity', $duplicate); + $this->assertNotSame(spl_object_hash($this->entity), spl_object_hash($duplicate)); + $this->assertNull($duplicate->id()); + $this->assertNull($duplicate->getOriginalId()); + $this->assertNull($duplicate->uuid()); + } + + /** + * Tests sort(). + */ + public function testSort() { + $this->entityManager->expects($this->any()) + ->method('getDefinition') + ->with($this->entityType) + ->will($this->returnValue(array( + 'entity_keys' => array( + 'label' => 'label', + ), + ))); + $entity_a = $this->entity; + $entity_a->label = 'foo'; + $entity_b = clone $this->entity; + $entity_b->label = 'bar'; + $list = array($entity_a, $entity_b); + // Suppress errors because of https://bugs.php.net/bug.php?id=50688. + @usort($list, array('\Drupal\Core\Config\Entity\ConfigEntityBase', 'sort')); + $this->assertSame(spl_object_hash($entity_b), spl_object_hash($list[0])); + $entity_a->weight = 0; + $entity_b->weight = 1; + // Suppress errors because of https://bugs.php.net/bug.php?id=50688. + @usort($list, array($entity_a, 'sort')); + $this->assertSame(spl_object_hash($entity_a), spl_object_hash($list[0])); + } + + /** + * Tests getExportProperties(). + */ + public function testGetExportProperties() { + $properties = $this->entity->getExportProperties(); + $this->assertInternalType('array', $properties); + } + + /** + * 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 uri(). + */ + public function testUri() { + // The only difference between ConfigEntityBase::uri() and Entity::uri() is + // how link templates are processed, so only test those. + $this->entity->id = $this->randomName(); + $expected_template_path = 'foo/' . $this->entity->id(); + $route_name = 'foo.bar'; + $route_path = 'foo/{id}'; + $this->entityManager->expects($this->once()) + ->method('getDefinition') + ->with($this->entityType) + ->will($this->returnValue(array( + 'links' => array( + 'edit-form' => $route_name, + ), + ))); + $route = $this->getMockBuilder('\Symfony\Component\Routing\Route') + ->disableOriginalConstructor() + ->getMock(); + $route->expects($this->once()) + ->method('getPath') + ->will($this->returnValue($route_path)); + $this->routeProvider->expects($this->once()) + ->method('getRouteByName') + ->with($route_name) + ->will($this->returnValue($route)); + + $actual_template_uri = $this->entity->uri(); + $this->assertSame($expected_template_path, $actual_template_uri['path']); + } +}