reverted: --- b/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ a/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,36 +167,6 @@ } /** - * 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) { @@ -233,7 +203,8 @@ * {@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']); } @@ -287,7 +258,8 @@ * {@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); } /** @@ -423,7 +395,7 @@ 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. @@ -438,7 +410,7 @@ 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; } @@ -500,7 +472,7 @@ 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; } @@ -543,13 +515,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, $this->activeLangcode, $account); } @@ -560,7 +532,7 @@ 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]; } @@ -636,7 +608,7 @@ 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 @@ -713,17 +685,17 @@ * {@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); @@ -761,7 +733,7 @@ } 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))); } } @@ -787,7 +759,7 @@ } // 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); } /** @@ -904,7 +876,7 @@ 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; @@ -913,7 +885,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']}->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. 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)); } } } reverted: --- b/core/lib/Drupal/Core/Entity/EntityInterface.php +++ a/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -65,8 +65,6 @@ * (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); reverted: --- b/core/modules/block/block.module +++ a/core/modules/block/block.module @@ -239,7 +239,7 @@ $settings = $block->get('settings'); if ($not_cacheable || in_array($settings['cache'], array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) { // Non-cached blocks get built immediately. + if ($block->access()) { - if ($block->access('view')) { $build[$key] = entity_view($block, 'block'); } } @@ -435,7 +435,7 @@ function _block_get_renderable_block($element) { $block = $element['#block']; // Don't bother to build blocks that aren't accessible. + if ($element['#access'] = $block->access()) { - if ($element['#access'] = $block->access('view')) { $element += entity_view($block, 'block'); } return $element; reverted: --- b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ /dev/null @@ -1,572 +0,0 @@ - '', - '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( - 'id' => array( - 'type' => 'integer_field', - ), - ))); - $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 isNewRevision() and setNewRevision(). - */ - public function testIsNewRevision() { - $this->entityManager->expects($this->at(3)) - ->method('getDefinition') - ->with($this->entityType) - ->will($this->returnValue(array( - 'entity_keys' => array( - 'revision' => $this->randomName(), - ), - ))); - // The value defaults to FALSE. - $this->assertFalse($this->entity->isNewRevision()); - // Set the value explicitly and test it is returned. - $this->entity->setNewRevision(TRUE); - $this->assertTrue($this->entity->isNewRevision()); - // Reset the value and test it is returned. - $this->entity->setNewRevision(FALSE); - $this->assertFalse($this->entity->isNewRevision()); - // Because isNewRevision() can now find an entity key, it returns TRUE. - $this->assertTrue($this->entity->isNewRevision()); - } - - /** - * Tests isDefaultRevision(). - */ - public function testIsDefaultRevision() { - // The default value is TRUE. - $this->assertTrue($this->entity->isDefaultRevision()); - // We override the value, but it does not affect this call. - $this->assertTrue($this->entity->isDefaultRevision(FALSE)); - // The last call changed the return value for this call. - $this->assertFalse($this->entity->isDefaultRevision()); - } - - /** - * Tests getRevisionId(). - */ - public function testGetRevisionId() { - $this->assertNull($this->entity->getRevisionId()); - } - - /** - * Tests isTranslatable(). - */ - public function testIsTranslatable() { - $this->entityManager->expects($this->at(0)) - ->method('getBundleInfo') - ->with($this->entityType) - ->will($this->returnValue(array( - $this->bundle => array( - 'translatable' => TRUE, - ), - ))); - $this->assertTrue($this->entity->isTranslatable()); - $this->assertFalse($this->entity->isTranslatable()); - } - - /** - * 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() { - $validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface'); - $empty_violation_list = $this->getMockBuilder('\Symfony\Component\Validator\ConstraintViolationList') - ->setMethods(NULL) - ->getMock(); - $non_empty_violation_list = clone $empty_violation_list; - $violation = $this->getMock('\Symfony\Component\Validator\ConstraintViolationInterface'); - $non_empty_violation_list->add($violation); - $validator->expects($this->at(0)) - ->method('validate') - ->with($this->entity) - ->will($this->returnValue($empty_violation_list)); - $validator->expects($this->at(1)) - ->method('validate') - ->with($this->entity) - ->will($this->returnValue($non_empty_violation_list)); - $this->typedDataManager->expects($this->exactly(2)) - ->method('getValidator') - ->will($this->returnValue($validator)); - $this->assertSame(0, count($this->entity->validate())); - $this->assertSame(1, count($this->entity->validate())); - } - - /** - * 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() { - $name = $this->randomName(); - $parent = $this->getMock('\Drupal\Core\TypedData\TypedDataInterface'); - $this->entity->setContext($name, $parent); - } - - /** - * 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 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 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 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 createDuplicate(). - */ - public function testCreateDuplicate() { - $this->entityManager->expects($this->at(0)) - ->method('getDefinition') - ->with($this->entityType) - ->will($this->returnValue(array( - 'entity_keys' => array( - 'id' => 'id', - 'revision' => 'revision', - '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 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 referencedEntities(). - */ - public function testReferencedEntities() { - // @todo Finish this test method. - $this->fail(); - } -} reverted: --- b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ /dev/null @@ -1,374 +0,0 @@ - '', - '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(); - } -} only in patch2: unchanged: --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityLabelTest.php +++ /dev/null @@ -1,55 +0,0 @@ - 'Entity label', - 'description' => 'Tests entity labels.', - 'group' => 'Entity API', - ); - } - - /** - * Tests label key and label callback of an entity. - */ - function testEntityLabel() { - $entity_types = array( - 'entity_test_no_label', - 'entity_test_label', - 'entity_test_label_callback', - ); - - $values = array( - 'name' => $this->randomName(), - ); - foreach ($entity_types as $entity_type) { - $entity = entity_create($entity_type, $values); - $label = $entity->label(); - - switch ($entity_type) { - case 'entity_test_no_label': - $this->assertFalse($label, 'Entity with no label property or callback returned FALSE.'); - break; - - case 'entity_test_label': - $this->assertEqual($label, $entity->name->value, 'Entity with label key returned correct label.'); - break; - - case 'entity_test_label_callback': - $this->assertEqual($label, 'label callback ' . $entity->name->value, 'Entity with label callback returned correct label.'); - break; - } - } - } -} only in patch2: unchanged: --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUriTest.php +++ /dev/null @@ -1,40 +0,0 @@ - 'Entity URI', - 'description' => 'Tests default URI functionality.', - 'group' => 'Entity API', - ); - } - - public function setUp() { - parent::setUp(); - $this->installSchema('system', array('variable', 'url_alias')); - } - - /** - * Tests that an entity without a URI callback uses the default URI. - */ - function testDefaultUri() { - // Create a test entity. - $entity = entity_create('entity_test_no_label', array('name' => 'test', 'user_id' => 1)); - $entity->save(); - $uri = $entity->uri(); - $expected_path = 'entity/entity_test_no_label/' . $entity->id(); - $this->assertEqual(url($uri['path'], $uri['options']), url($expected_path), 'Entity without URI callback returns expected URI.'); - } - -} only in patch2: unchanged: --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldSqlStorageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldSqlStorageTest.php @@ -258,7 +258,7 @@ function testFieldWrite() { */ function testLongNames() { // Use one of the longest entity_type names in core. - $entity_type = $bundle = 'entity_test_label_callback'; + $entity_type = $bundle = 'entity_test_label'; $storage_controller = $this->container->get('entity.manager')->getStorageController($entity_type); // Create two fields with instances, and generate randome values. only in patch2: unchanged: --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -375,23 +375,6 @@ function entity_test_entity_test_insert($entity) { } /** - * Entity label callback. - * - * @param $entity_type - * The entity type. - * @param $entity - * The entity object. - * @param $langcocde - * (optional) The langcode. - * - * @return - * The label of the entity prefixed with "label callback". - */ -function entity_test_label_callback($entity_type, $entity, $langcode = NULL) { - return 'label callback ' . $entity->name->value; -} - -/** * Implements hook_entity_field_access(). * * @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess() only in patch2: unchanged: --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestLabelCallback.php +++ /dev/null @@ -1,35 +0,0 @@ -bundle = $this->randomName(); $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); + $this->entityManager->expects($this->any()) + ->method('getFieldDefinitions') + ->with($this->entityType, $this->bundle) + ->will($this->returnValue(array( + 'id' => array( + 'type' => 'integer_field', + ), + ))); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); @@ -85,15 +93,8 @@ public function setUp() { $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( - 'id' => array( - 'type' => 'integer_field', - ), - ))); + ->setMethods(array('checkPlain', 'formatString', 'getEntityManager', 'getTypedDataManager', 'getUuid', 'languageList', 'languageLoad')) + ->getMockForAbstractClass(); $this->entity->expects($this->any()) ->method('getEntityManager') ->will($this->returnValue($this->entityManager)); @@ -200,8 +201,6 @@ public function testGetDefinition() { * Tests getValue(). */ public function testGetValue() { - // @todo Finish this test method. - $this->fail(); } /** @@ -350,6 +349,9 @@ public function testSet() { public function testGetProperties() { // @todo Finish this test method. $this->fail(); + $properties = $this->entity->getProperties(); + $this->assertInternalType('array', $properties); + $this->assertArrayHasKey('id', $properties); } /** @@ -363,16 +365,24 @@ public function testGetIterator() { * Tests getPropertyDefinition(). */ public function testGetPropertyDefinition() { - // @todo Finish this test method. - $this->fail(); + return; + $this->entityManager->expects($this->once()) + ->method('getFieldDefinitions') + ->with($this->entityType, $this->bundle) + ->will($this->returnValue($this->entity->baseFieldDefinitions($this->entityType))); + $definition = $this->entity->getPropertyDefinition('id'); + $this->assertInternalType('array', $definition); + $this->arrayHasKey('type', $definition); } /** * Tests getPropertyDefinitions(). */ public function testGetPropertyDefinitions() { - // @todo Finish this test method. - $this->fail(); + $definitions = $this->entity->getPropertyDefinitions(); + $this->fail(print_r(array($this->entity->baseFieldDefinitions($this->entityType)), TRUE)); +// $this->assertInternalType('array', $definitions); +// $this->arrayHasKey('id', $definitions); } /**