Issue #2134857 by Xano, Wim Leers, Berdir, damiankloip: PHPUnit test the entity base classes.
parent
108789c919
commit
1370edf99f
|
@ -7,9 +7,10 @@
|
||||||
|
|
||||||
namespace Drupal\Core\Config\Entity;
|
namespace Drupal\Core\Config\Entity;
|
||||||
|
|
||||||
|
use Drupal\Component\Utility\String;
|
||||||
use Drupal\Core\Entity\Entity;
|
use Drupal\Core\Entity\Entity;
|
||||||
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
|
||||||
use Drupal\Core\Config\ConfigDuplicateUUIDException;
|
use Drupal\Core\Config\ConfigDuplicateUUIDException;
|
||||||
|
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a base configuration entity class.
|
* Defines a base configuration entity class.
|
||||||
|
@ -139,6 +140,8 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->{$property_name} = $value;
|
$this->{$property_name} = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -175,6 +178,8 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
|
||||||
*/
|
*/
|
||||||
public function setSyncing($syncing) {
|
public function setSyncing($syncing) {
|
||||||
$this->isSyncing = $syncing;
|
$this->isSyncing = $syncing;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -212,7 +217,7 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
|
||||||
/**
|
/**
|
||||||
* Helper callback for uasort() to sort configuration entities by weight and label.
|
* 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;
|
$a_weight = isset($a->weight) ? $a->weight : 0;
|
||||||
$b_weight = isset($b->weight) ? $b->weight : 0;
|
$b_weight = isset($b->weight) ? $b->weight : 0;
|
||||||
if ($a_weight == $b_weight) {
|
if ($a_weight == $b_weight) {
|
||||||
|
@ -262,14 +267,14 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
|
||||||
->execute();
|
->execute();
|
||||||
$matched_entity = reset($matching_entities);
|
$matched_entity = reset($matching_entities);
|
||||||
if (!empty($matched_entity) && ($matched_entity != $this->id()) && $matched_entity != $this->getOriginalId()) {
|
if (!empty($matched_entity) && ($matched_entity != $this->id()) && $matched_entity != $this->getOriginalId()) {
|
||||||
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)));
|
throw new ConfigDuplicateUUIDException(String::format('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()) {
|
if (!$this->isNew()) {
|
||||||
$original = $storage_controller->loadUnchanged($this->id());
|
$original = $storage_controller->loadUnchanged($this->id());
|
||||||
// Ensure that the UUID cannot be changed for an existing entity.
|
// Ensure that the UUID cannot be changed for an existing entity.
|
||||||
if ($original && ($original->uuid() != $this->uuid())) {
|
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())));
|
throw new ConfigDuplicateUUIDException(String::format('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->isSyncing()) {
|
if (!$this->isSyncing()) {
|
||||||
|
|
|
@ -17,16 +17,14 @@ interface ConfigEntityInterface extends EntityInterface {
|
||||||
/**
|
/**
|
||||||
* Enables the configuration entity.
|
* Enables the configuration entity.
|
||||||
*
|
*
|
||||||
* @return \Drupal\Core\Config\Entity\ConfigEntityInterface
|
* @return $this
|
||||||
* The configuration entity.
|
|
||||||
*/
|
*/
|
||||||
public function enable();
|
public function enable();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disables the configuration entity.
|
* Disables the configuration entity.
|
||||||
*
|
*
|
||||||
* @return \Drupal\Core\Config\Entity\ConfigEntityInterface
|
* @return $this
|
||||||
* The configuration entity.
|
|
||||||
*/
|
*/
|
||||||
public function disable();
|
public function disable();
|
||||||
|
|
||||||
|
@ -36,8 +34,7 @@ interface ConfigEntityInterface extends EntityInterface {
|
||||||
* @param bool $status
|
* @param bool $status
|
||||||
* The status of the configuration entity.
|
* The status of the configuration entity.
|
||||||
*
|
*
|
||||||
* @return \Drupal\Core\Config\Entity\ConfigEntityInterface
|
* @return $this
|
||||||
* The class instance that this method is called on.
|
|
||||||
*/
|
*/
|
||||||
public function setStatus($status);
|
public function setStatus($status);
|
||||||
|
|
||||||
|
@ -46,6 +43,8 @@ interface ConfigEntityInterface extends EntityInterface {
|
||||||
*
|
*
|
||||||
* @param bool $status
|
* @param bool $status
|
||||||
* The status of the sync flag.
|
* The status of the sync flag.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setSyncing($status);
|
public function setSyncing($status);
|
||||||
|
|
||||||
|
@ -110,6 +109,8 @@ interface ConfigEntityInterface extends EntityInterface {
|
||||||
* The name of the property that should be set.
|
* The name of the property that should be set.
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* The value the property should be set to.
|
* The value the property should be set to.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function set($property_name, $value);
|
public function set($property_name, $value);
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
|
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
|
||||||
$this->entityTypeId = $entity_type;
|
$this->entityTypeId = $entity_type;
|
||||||
$this->bundle = $bundle ? $bundle : $this->entityTypeId;
|
$this->bundle = $bundle ? $bundle : $this->entityTypeId;
|
||||||
$this->languages = language_list(Language::STATE_ALL);
|
$this->languages = $this->languageManager()->getLanguages(Language::STATE_ALL);
|
||||||
|
|
||||||
foreach ($values as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
// If the key matches an existing property set the value to the property
|
// If the key matches an existing property set the value to the property
|
||||||
|
@ -161,6 +161,15 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
$this->init();
|
$this->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the typed data manager.
|
||||||
|
*
|
||||||
|
* @return \Drupal\Core\TypedData\TypedDataManager
|
||||||
|
*/
|
||||||
|
protected function typedDataManager() {
|
||||||
|
return \Drupal::typedDataManager();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -197,8 +206,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function isTranslatable() {
|
public function isTranslatable() {
|
||||||
// @todo Inject the entity manager and retrieve bundle info from it.
|
$bundles = $this->entityManager()->getBundleInfo($this->entityTypeId);
|
||||||
$bundles = entity_get_bundles($this->entityTypeId);
|
|
||||||
return !empty($bundles[$this->bundle()]['translatable']);
|
return !empty($bundles[$this->bundle()]['translatable']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,15 +247,14 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getString() {
|
public function getString() {
|
||||||
return $this->label();
|
return (string) $this->label();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function validate() {
|
public function validate() {
|
||||||
// @todo: Add the typed data manager as proper dependency.
|
return $this->typedDataManager()->getValidator()->validate($this);
|
||||||
return \Drupal::typedDataManager()->getValidator()->validate($this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -333,10 +340,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
$this->fieldDefinitions = NULL;
|
$this->fieldDefinitions = NULL;
|
||||||
$this->clearTranslationCache();
|
$this->clearTranslationCache();
|
||||||
|
|
||||||
// Don't serialize the url generator.
|
return parent::__sleep();
|
||||||
$this->urlGenerator = NULL;
|
|
||||||
|
|
||||||
return array_keys(get_object_vars($this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -344,6 +348,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
* Magic __wakeup() implementation.
|
* Magic __wakeup() implementation.
|
||||||
*/
|
*/
|
||||||
public function __wakeup() {
|
public function __wakeup() {
|
||||||
|
parent::__wakeup();
|
||||||
$this->init();
|
$this->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +398,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
protected function getTranslatedField($name, $langcode) {
|
protected function getTranslatedField($name, $langcode) {
|
||||||
if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
||||||
$message = 'The entity object refers to a removed translation (@langcode) and cannot be manipulated.';
|
$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(String::format($message, array('@langcode' => $this->activeLangcode)));
|
||||||
}
|
}
|
||||||
// Populate $this->fields to speed-up further look-ups and to keep track of
|
// Populate $this->fields to speed-up further look-ups and to keep track of
|
||||||
// fields objects, possibly holding changes to field values.
|
// fields objects, possibly holding changes to field values.
|
||||||
|
@ -526,13 +531,13 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function access($operation = 'view', AccountInterface $account = NULL) {
|
public function access($operation, AccountInterface $account = NULL) {
|
||||||
if ($operation == 'create') {
|
if ($operation == 'create') {
|
||||||
return \Drupal::entityManager()
|
return $this->entityManager()
|
||||||
->getAccessController($this->entityTypeId)
|
->getAccessController($this->entityTypeId)
|
||||||
->createAccess($this->bundle(), $account);
|
->createAccess($this->bundle(), $account);
|
||||||
}
|
}
|
||||||
return \Drupal::entityManager()
|
return $this->entityManager()
|
||||||
->getAccessController($this->entityTypeId)
|
->getAccessController($this->entityTypeId)
|
||||||
->access($this, $operation, $this->activeLangcode, $account);
|
->access($this, $operation, $this->activeLangcode, $account);
|
||||||
}
|
}
|
||||||
|
@ -544,7 +549,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
$language = NULL;
|
$language = NULL;
|
||||||
if ($this->activeLangcode != Language::LANGCODE_DEFAULT) {
|
if ($this->activeLangcode != Language::LANGCODE_DEFAULT) {
|
||||||
if (!isset($this->languages[$this->activeLangcode])) {
|
if (!isset($this->languages[$this->activeLangcode])) {
|
||||||
$this->languages += language_list(Language::STATE_ALL);
|
$this->languages += $this->languageManager()->getLanguages(Language::STATE_ALL);
|
||||||
}
|
}
|
||||||
$language = $this->languages[$this->activeLangcode];
|
$language = $this->languages[$this->activeLangcode];
|
||||||
}
|
}
|
||||||
|
@ -594,7 +599,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
if ($name == 'langcode') {
|
if ($name == 'langcode') {
|
||||||
$this->setDefaultLangcode();
|
$this->setDefaultLangcode();
|
||||||
if (isset($this->translations[$this->defaultLangcode])) {
|
if (isset($this->translations[$this->defaultLangcode])) {
|
||||||
$message = format_string('A translation already exists for the specified language (@langcode).', array('@langcode' => $this->defaultLangcode));
|
$message = String::format('A translation already exists for the specified language (@langcode).', array('@langcode' => $this->defaultLangcode));
|
||||||
throw new \InvalidArgumentException($message);
|
throw new \InvalidArgumentException($message);
|
||||||
}
|
}
|
||||||
$this->updateFieldLangcodes($this->defaultLangcode);
|
$this->updateFieldLangcodes($this->defaultLangcode);
|
||||||
|
@ -643,7 +648,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
|
|
||||||
if (empty($translation)) {
|
if (empty($translation)) {
|
||||||
$message = 'Invalid translation language (@langcode) specified.';
|
$message = 'Invalid translation language (@langcode) specified.';
|
||||||
throw new \InvalidArgumentException(format_string($message, array('@langcode' => $langcode)));
|
throw new \InvalidArgumentException(String::format($message, array('@langcode' => $langcode)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $translation;
|
return $translation;
|
||||||
|
@ -708,14 +713,14 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
public function addTranslation($langcode, array $values = array()) {
|
public function addTranslation($langcode, array $values = array()) {
|
||||||
if (!isset($this->languages[$langcode]) || $this->hasTranslation($langcode)) {
|
if (!isset($this->languages[$langcode]) || $this->hasTranslation($langcode)) {
|
||||||
$message = 'Invalid translation language (@langcode) specified.';
|
$message = 'Invalid translation language (@langcode) specified.';
|
||||||
throw new \InvalidArgumentException(format_string($message, array('@langcode' => $langcode)));
|
throw new \InvalidArgumentException(String::format($message, array('@langcode' => $langcode)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate a new empty entity so default values will be populated in the
|
// Instantiate a new empty entity so default values will be populated in the
|
||||||
// specified language.
|
// specified language.
|
||||||
$entity_type = $this->getEntityType();
|
$entity_type = $this->getEntityType();
|
||||||
$default_values = array($entity_type->getKey('bundle') => $this->bundle, 'langcode' => $langcode);
|
$default_values = array($entity_type->getKey('bundle') => $this->bundle, 'langcode' => $langcode);
|
||||||
$entity = \Drupal::entityManager()
|
$entity = $this->entityManager()
|
||||||
->getStorageController($this->getEntityTypeId())
|
->getStorageController($this->getEntityTypeId())
|
||||||
->create($default_values);
|
->create($default_values);
|
||||||
|
|
||||||
|
@ -753,7 +758,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$message = 'The specified translation (@langcode) cannot be removed.';
|
$message = 'The specified translation (@langcode) cannot be removed.';
|
||||||
throw new \InvalidArgumentException(format_string($message, array('@langcode' => $langcode)));
|
throw new \InvalidArgumentException(String::format($message, array('@langcode' => $langcode)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -895,7 +900,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
public function createDuplicate() {
|
public function createDuplicate() {
|
||||||
if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
||||||
$message = 'The entity object refers to a removed translation (@langcode) and cannot be manipulated.';
|
$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(String::format($message, array('@langcode' => $this->activeLangcode)));
|
||||||
}
|
}
|
||||||
|
|
||||||
$duplicate = clone $this;
|
$duplicate = clone $this;
|
||||||
|
@ -904,8 +909,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
|
|
||||||
// Check if the entity type supports UUIDs and generate a new one if so.
|
// Check if the entity type supports UUIDs and generate a new one if so.
|
||||||
if ($entity_type->hasKey('uuid')) {
|
if ($entity_type->hasKey('uuid')) {
|
||||||
// @todo Inject the UUID service into the Entity class once possible.
|
$duplicate->{$entity_type->getKey('uuid')}->value = $this->uuidGenerator()->generate();
|
||||||
$duplicate->{$entity_type->getKey('uuid')}->value = \Drupal::service('uuid')->generate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether the entity type supports revisions and initialize it if so.
|
// Check whether the entity type supports revisions and initialize it if so.
|
||||||
|
@ -953,9 +957,8 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
public function label() {
|
public function label() {
|
||||||
$label = NULL;
|
$label = NULL;
|
||||||
$entity_type = $this->getEntityType();
|
$entity_type = $this->getEntityType();
|
||||||
// @todo Convert to is_callable() and call_user_func().
|
if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
|
||||||
if (($label_callback = $entity_type->getLabelCallback()) && function_exists($label_callback)) {
|
$label = call_user_func($label_callback, $this);
|
||||||
$label = $label_callback($this);
|
|
||||||
}
|
}
|
||||||
elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
||||||
$label = $this->{$label_key}->value;
|
$label = $this->{$label_key}->value;
|
||||||
|
|
|
@ -7,13 +7,14 @@
|
||||||
|
|
||||||
namespace Drupal\Core\Entity;
|
namespace Drupal\Core\Entity;
|
||||||
|
|
||||||
|
use Drupal\Core\DependencyInjection\DependencySerialization;
|
||||||
use Drupal\Core\Language\Language;
|
use Drupal\Core\Language\Language;
|
||||||
use Drupal\Core\Session\AccountInterface;
|
use Drupal\Core\Session\AccountInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a base entity class.
|
* Defines a base entity class.
|
||||||
*/
|
*/
|
||||||
abstract class Entity implements EntityInterface {
|
abstract class Entity extends DependencySerialization implements EntityInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The language code of the entity's default language.
|
* The language code of the entity's default language.
|
||||||
|
@ -60,6 +61,33 @@ abstract class Entity implements EntityInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the entity manager.
|
||||||
|
*
|
||||||
|
* @return \Drupal\Core\Entity\EntityManagerInterface
|
||||||
|
*/
|
||||||
|
protected function entityManager() {
|
||||||
|
return \Drupal::entityManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the language manager.
|
||||||
|
*
|
||||||
|
* @return \Drupal\Core\Language\LanguageManagerInterface
|
||||||
|
*/
|
||||||
|
protected function languageManager() {
|
||||||
|
return \Drupal::languageManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the UUID generator.
|
||||||
|
*
|
||||||
|
* @return \Drupal\Component\Uuid\UuidInterface
|
||||||
|
*/
|
||||||
|
protected function uuidGenerator() {
|
||||||
|
return \Drupal::service('uuid');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -86,6 +114,8 @@ abstract class Entity implements EntityInterface {
|
||||||
*/
|
*/
|
||||||
public function enforceIsNew($value = TRUE) {
|
public function enforceIsNew($value = TRUE) {
|
||||||
$this->enforceIsNew = $value;
|
$this->enforceIsNew = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,9 +138,8 @@ abstract class Entity implements EntityInterface {
|
||||||
public function label() {
|
public function label() {
|
||||||
$label = NULL;
|
$label = NULL;
|
||||||
$entity_type = $this->getEntityType();
|
$entity_type = $this->getEntityType();
|
||||||
// @todo Convert to is_callable() and call_user_func().
|
if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
|
||||||
if (($label_callback = $entity_type->getLabelCallback()) && function_exists($label_callback)) {
|
$label = call_user_func($label_callback, $this);
|
||||||
$label = $label_callback($this);
|
|
||||||
}
|
}
|
||||||
elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
elseif (($label_key = $entity_type->getKey('label')) && isset($this->{$label_key})) {
|
||||||
$label = $this->{$label_key};
|
$label = $this->{$label_key};
|
||||||
|
@ -138,7 +167,7 @@ abstract class Entity implements EntityInterface {
|
||||||
$bundle = $this->bundle();
|
$bundle = $this->bundle();
|
||||||
// A bundle-specific callback takes precedence over the generic one for
|
// A bundle-specific callback takes precedence over the generic one for
|
||||||
// the entity type.
|
// the entity type.
|
||||||
$bundles = \Drupal::entityManager()->getBundleInfo($this->getEntityTypeId());
|
$bundles = $this->entityManager()->getBundleInfo($this->getEntityTypeId());
|
||||||
if (isset($bundles[$bundle]['uri_callback'])) {
|
if (isset($bundles[$bundle]['uri_callback'])) {
|
||||||
$uri_callback = $bundles[$bundle]['uri_callback'];
|
$uri_callback = $bundles[$bundle]['uri_callback'];
|
||||||
}
|
}
|
||||||
|
@ -148,9 +177,8 @@ abstract class Entity implements EntityInterface {
|
||||||
|
|
||||||
// Invoke the callback to get the URI. If there is no callback, use the
|
// Invoke the callback to get the URI. If there is no callback, use the
|
||||||
// default URI format.
|
// default URI format.
|
||||||
// @todo Convert to is_callable() and call_user_func().
|
if (isset($uri_callback) && is_callable($uri_callback)) {
|
||||||
if (isset($uri_callback) && function_exists($uri_callback)) {
|
$uri = call_user_func($uri_callback, $this);
|
||||||
$uri = $uri_callback($this);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return array();
|
return array();
|
||||||
|
@ -247,13 +275,13 @@ abstract class Entity implements EntityInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function access($operation = 'view', AccountInterface $account = NULL) {
|
public function access($operation, AccountInterface $account = NULL) {
|
||||||
if ($operation == 'create') {
|
if ($operation == 'create') {
|
||||||
return \Drupal::entityManager()
|
return $this->entityManager()
|
||||||
->getAccessController($this->entityTypeId)
|
->getAccessController($this->entityTypeId)
|
||||||
->createAccess($this->bundle(), $account);
|
->createAccess($this->bundle(), $account);
|
||||||
}
|
}
|
||||||
return \Drupal::entityManager()
|
return $this->entityManager()
|
||||||
->getAccessController($this->entityTypeId)
|
->getAccessController($this->entityTypeId)
|
||||||
->access($this, $operation, Language::LANGCODE_DEFAULT, $account);
|
->access($this, $operation, Language::LANGCODE_DEFAULT, $account);
|
||||||
}
|
}
|
||||||
|
@ -262,7 +290,7 @@ abstract class Entity implements EntityInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function language() {
|
public function language() {
|
||||||
$language = language_load($this->langcode);
|
$language = $this->languageManager()->getLanguage($this->langcode);
|
||||||
if (!$language) {
|
if (!$language) {
|
||||||
// Make sure we return a proper language object.
|
// Make sure we return a proper language object.
|
||||||
$language = new Language(array('id' => Language::LANGCODE_NOT_SPECIFIED));
|
$language = new Language(array('id' => Language::LANGCODE_NOT_SPECIFIED));
|
||||||
|
@ -274,7 +302,7 @@ abstract class Entity implements EntityInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function save() {
|
public function save() {
|
||||||
return \Drupal::entityManager()->getStorageController($this->entityTypeId)->save($this);
|
return $this->entityManager()->getStorageController($this->entityTypeId)->save($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -282,7 +310,7 @@ abstract class Entity implements EntityInterface {
|
||||||
*/
|
*/
|
||||||
public function delete() {
|
public function delete() {
|
||||||
if (!$this->isNew()) {
|
if (!$this->isNew()) {
|
||||||
\Drupal::entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this));
|
$this->entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,8 +324,7 @@ abstract class Entity implements EntityInterface {
|
||||||
|
|
||||||
// Check if the entity type supports UUIDs and generate a new one if so.
|
// Check if the entity type supports UUIDs and generate a new one if so.
|
||||||
if ($entity_type->hasKey('uuid')) {
|
if ($entity_type->hasKey('uuid')) {
|
||||||
// @todo Inject the UUID service into the Entity class once possible.
|
$duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate();
|
||||||
$duplicate->{$entity_type->getKey('uuid')} = \Drupal::service('uuid')->generate();
|
|
||||||
}
|
}
|
||||||
return $duplicate;
|
return $duplicate;
|
||||||
}
|
}
|
||||||
|
@ -306,7 +333,7 @@ abstract class Entity implements EntityInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getEntityType() {
|
public function getEntityType() {
|
||||||
return \Drupal::entityManager()->getDefinition($this->getEntityTypeId());
|
return $this->entityManager()->getDefinition($this->getEntityTypeId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -378,8 +405,8 @@ abstract class Entity implements EntityInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($referenced_entities as $entity_type => $entities) {
|
foreach ($referenced_entities as $entity_type => $entities) {
|
||||||
if (\Drupal::entityManager()->hasController($entity_type, 'view_builder')) {
|
if ($this->entityManager()->hasController($entity_type, 'view_builder')) {
|
||||||
\Drupal::entityManager()->getViewBuilder($entity_type)->resetCache($entities);
|
$this->entityManager()->getViewBuilder($entity_type)->resetCache($entities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -412,16 +439,6 @@ abstract class Entity implements EntityInterface {
|
||||||
return $this->urlGenerator;
|
return $this->urlGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function __sleep() {
|
|
||||||
// Don't serialize the url generator.
|
|
||||||
$this->urlGenerator = NULL;
|
|
||||||
|
|
||||||
return array_keys(get_object_vars($this));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -65,6 +65,8 @@ interface EntityInterface extends AccessibleInterface {
|
||||||
* (optional) Whether the entity should be forced to be new. Defaults to
|
* (optional) Whether the entity should be forced to be new. Defaults to
|
||||||
* TRUE.
|
* TRUE.
|
||||||
*
|
*
|
||||||
|
* @return self
|
||||||
|
*
|
||||||
* @see \Drupal\Core\Entity\EntityInterface::isNew()
|
* @see \Drupal\Core\Entity\EntityInterface::isNew()
|
||||||
*/
|
*/
|
||||||
public function enforceIsNew($value = TRUE);
|
public function enforceIsNew($value = TRUE);
|
||||||
|
|
|
@ -63,7 +63,7 @@ interface ComplexDataInterface extends \Traversable, TypedDataInterface {
|
||||||
* @param bool $include_computed
|
* @param bool $include_computed
|
||||||
* If set to TRUE, computed properties are included. Defaults to FALSE.
|
* If set to TRUE, computed properties are included. Defaults to FALSE.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return \Drupal\Core\TypedData\TypedDataInterface[]
|
||||||
* An array of property objects implementing the TypedDataInterface, keyed
|
* An array of property objects implementing the TypedDataInterface, keyed
|
||||||
* by property name.
|
* by property name.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -170,7 +170,7 @@ function block_get_blocks_by_region($region) {
|
||||||
$build = array();
|
$build = array();
|
||||||
if ($list = block_list($region)) {
|
if ($list = block_list($region)) {
|
||||||
foreach ($list as $key => $block) {
|
foreach ($list as $key => $block) {
|
||||||
if ($block->access()) {
|
if ($block->access('view')) {
|
||||||
$build[$key] = entity_view($block, 'block');
|
$build[$key] = entity_view($block, 'block');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Drupal\Core\Cache\Cache;
|
||||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||||
use Drupal\block\BlockPluginBag;
|
use Drupal\block\BlockPluginBag;
|
||||||
use Drupal\block\BlockInterface;
|
use Drupal\block\BlockInterface;
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||||
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
||||||
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
||||||
|
|
||||||
|
@ -176,7 +177,7 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
|
||||||
/**
|
/**
|
||||||
* Sorts active blocks by weight; sorts inactive blocks by name.
|
* Sorts active blocks by weight; sorts inactive blocks by name.
|
||||||
*/
|
*/
|
||||||
public static function sort($a, $b) {
|
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||||
// Separate enabled from disabled.
|
// Separate enabled from disabled.
|
||||||
$status = $b->get('status') - $a->get('status');
|
$status = $b->get('status') - $a->get('status');
|
||||||
if ($status) {
|
if ($status) {
|
||||||
|
|
|
@ -97,14 +97,4 @@ class ConfigEntityUnitTest extends DrupalUnitTestBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests getOriginalId() and setOriginalId().
|
|
||||||
*/
|
|
||||||
protected function testGetOriginalId() {
|
|
||||||
$entity = $this->storage->create(array());
|
|
||||||
$id = $this->randomName();
|
|
||||||
$this->assertIdentical(spl_object_hash($entity->setOriginalId($id)), spl_object_hash($entity));
|
|
||||||
$this->assertIdentical($entity->getOriginalId(), $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace Drupal\config_test\Entity;
|
||||||
|
|
||||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||||
use Drupal\config_test\ConfigTestInterface;
|
use Drupal\config_test\ConfigTestInterface;
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the ConfigTest configuration entity.
|
* Defines the ConfigTest configuration entity.
|
||||||
|
@ -100,7 +101,7 @@ class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
|
||||||
/**
|
/**
|
||||||
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
|
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
|
||||||
*/
|
*/
|
||||||
public static function sort($a, $b) {
|
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||||
\Drupal::state()->set('config_entity_sort', TRUE);
|
\Drupal::state()->set('config_entity_sort', TRUE);
|
||||||
return parent::sort($a, $b);
|
return parent::sort($a, $b);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\entity;
|
namespace Drupal\entity;
|
||||||
|
|
||||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for config entity types that hold settings for form and view modes.
|
* Base class for config entity types that hold settings for form and view modes.
|
||||||
|
@ -60,7 +61,9 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function sort($a, $b) {
|
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||||
|
/** @var \Drupal\entity\EntityDisplayModeInterface $a */
|
||||||
|
/** @var \Drupal\entity\EntityDisplayModeInterface $b */
|
||||||
// Sort by the type of entity the view mode is used for.
|
// Sort by the type of entity the view mode is used for.
|
||||||
$a_type = $a->getTargetType();
|
$a_type = $a->getTargetType();
|
||||||
$b_type = $b->getTargetType();
|
$b_type = $b->getTargetType();
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\search\Entity;
|
namespace Drupal\search\Entity;
|
||||||
|
|
||||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||||
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
||||||
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
||||||
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
||||||
|
@ -213,7 +214,7 @@ class SearchPage extends ConfigEntityBase implements SearchPageInterface, Entity
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function sort($a, $b) {
|
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||||
/** @var $a \Drupal\search\SearchPageInterface */
|
/** @var $a \Drupal\search\SearchPageInterface */
|
||||||
/** @var $b \Drupal\search\SearchPageInterface */
|
/** @var $b \Drupal\search\SearchPageInterface */
|
||||||
$a_status = (int) $a->status();
|
$a_status = (int) $a->status();
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\system\Entity;
|
namespace Drupal\system\Entity;
|
||||||
|
|
||||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||||
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
|
||||||
use Drupal\system\ActionConfigEntityInterface;
|
use Drupal\system\ActionConfigEntityInterface;
|
||||||
use Drupal\Core\Action\ActionBag;
|
use Drupal\Core\Action\ActionBag;
|
||||||
|
@ -131,7 +132,9 @@ class Action extends ConfigEntityBase implements ActionConfigEntityInterface, En
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function sort($a, $b) {
|
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
||||||
|
/** @var \Drupal\system\ActionConfigEntityInterface $a */
|
||||||
|
/** @var \Drupal\system\ActionConfigEntityInterface $b */
|
||||||
$a_type = $a->getType();
|
$a_type = $a->getType();
|
||||||
$b_type = $b->getType();
|
$b_type = $b->getType();
|
||||||
if ($a_type != $b_type) {
|
if ($a_type != $b_type) {
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains \Drupal\system\Tests\Entity\EntityLabelTest.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\system\Tests\Entity;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests entity properties.
|
|
||||||
*/
|
|
||||||
class EntityLabelTest extends EntityUnitTestBase {
|
|
||||||
|
|
||||||
public static function getInfo() {
|
|
||||||
return array(
|
|
||||||
'name' => '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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -317,19 +317,6 @@ function entity_test_entity_test_insert($entity) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Entity label callback.
|
|
||||||
*
|
|
||||||
* @param $entity
|
|
||||||
* The entity object.
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
* The label of the entity prefixed with "label callback".
|
|
||||||
*/
|
|
||||||
function entity_test_label_callback($entity) {
|
|
||||||
return 'label callback ' . $entity->name->value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_entity_field_access().
|
* Implements hook_entity_field_access().
|
||||||
*
|
*
|
||||||
|
|
|
@ -10,6 +10,7 @@ namespace Drupal\Tests\Core\Config\Entity;
|
||||||
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
||||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||||
use Drupal\Component\Plugin\PluginBase;
|
use Drupal\Component\Plugin\PluginBase;
|
||||||
|
use Drupal\Core\Language\Language;
|
||||||
use Drupal\Tests\UnitTestCase;
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,6 +63,20 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
|
||||||
*/
|
*/
|
||||||
protected $provider;
|
protected $provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The language manager.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $languageManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity ID.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -77,7 +92,12 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function setUp() {
|
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->entityTypeId = $this->randomName();
|
||||||
$this->provider = $this->randomName();
|
$this->provider = $this->randomName();
|
||||||
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||||
|
@ -93,9 +113,16 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
|
||||||
|
|
||||||
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||||
|
|
||||||
|
$this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
|
||||||
|
$this->languageManager->expects($this->any())
|
||||||
|
->method('getLanguage')
|
||||||
|
->with('en')
|
||||||
|
->will($this->returnValue(new Language(array('id' => 'en'))));
|
||||||
|
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
$container->set('entity.manager', $this->entityManager);
|
$container->set('entity.manager', $this->entityManager);
|
||||||
$container->set('uuid', $this->uuid);
|
$container->set('uuid', $this->uuid);
|
||||||
|
$container->set('language_manager', $this->languageManager);
|
||||||
\Drupal::setContainer($container);
|
\Drupal::setContainer($container);
|
||||||
|
|
||||||
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', array($values, $this->entityTypeId));
|
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', array($values, $this->entityTypeId));
|
||||||
|
@ -236,6 +263,158 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
|
||||||
$this->assertEmpty($this->entity->calculateDependencies());
|
$this->assertEmpty($this->entity->calculateDependencies());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::setOriginalId
|
||||||
|
* @covers ::getOriginalId
|
||||||
|
*/
|
||||||
|
public function testGetOriginalId() {
|
||||||
|
$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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::isNew
|
||||||
|
*/
|
||||||
|
public function testIsNew() {
|
||||||
|
$this->assertFalse($this->entity->isNew());
|
||||||
|
$this->assertSame($this->entity, $this->entity->enforceIsNew());
|
||||||
|
$this->assertTrue($this->entity->isNew());
|
||||||
|
$this->entity->enforceIsNew(FALSE);
|
||||||
|
$this->assertFalse($this->entity->isNew());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::set
|
||||||
|
* @covers ::get
|
||||||
|
*/
|
||||||
|
public function testGet() {
|
||||||
|
$name = 'id';
|
||||||
|
$value = $this->randomName();
|
||||||
|
$this->assertSame($this->id, $this->entity->get($name));
|
||||||
|
$this->assertSame($this->entity, $this->entity->set($name, $value));
|
||||||
|
$this->assertSame($value, $this->entity->get($name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::setStatus
|
||||||
|
* @covers ::status
|
||||||
|
*/
|
||||||
|
public function testSetStatus() {
|
||||||
|
$this->assertTrue($this->entity->status());
|
||||||
|
$this->assertSame($this->entity, $this->entity->setStatus(FALSE));
|
||||||
|
$this->assertFalse($this->entity->status());
|
||||||
|
$this->entity->setStatus(TRUE);
|
||||||
|
$this->assertTrue($this->entity->status());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::enable
|
||||||
|
* @depends testSetStatus
|
||||||
|
*/
|
||||||
|
public function testEnable() {
|
||||||
|
$this->entity->setStatus(FALSE);
|
||||||
|
$this->assertSame($this->entity, $this->entity->enable());
|
||||||
|
$this->assertTrue($this->entity->status());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::disable
|
||||||
|
* @depends testSetStatus
|
||||||
|
*/
|
||||||
|
public function testDisable() {
|
||||||
|
$this->entity->setStatus(TRUE);
|
||||||
|
$this->assertSame($this->entity, $this->entity->disable());
|
||||||
|
$this->assertFalse($this->entity->status());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::setSyncing
|
||||||
|
* @covers ::isSyncing
|
||||||
|
*/
|
||||||
|
public function testIsSyncing() {
|
||||||
|
$this->assertFalse($this->entity->isSyncing());
|
||||||
|
$this->assertSame($this->entity, $this->entity->setSyncing(TRUE));
|
||||||
|
$this->assertTrue($this->entity->isSyncing());
|
||||||
|
$this->entity->setSyncing(FALSE);
|
||||||
|
$this->assertFalse($this->entity->isSyncing());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::createDuplicate
|
||||||
|
*/
|
||||||
|
public function testCreateDuplicate() {
|
||||||
|
$this->entityType->expects($this->at(0))
|
||||||
|
->method('getKey')
|
||||||
|
->with('id')
|
||||||
|
->will($this->returnValue('id'));
|
||||||
|
|
||||||
|
$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->assertNotEquals($this->entity->uuid(), $duplicate->uuid());
|
||||||
|
$this->assertSame($new_uuid, $duplicate->uuid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::sort
|
||||||
|
*/
|
||||||
|
public function testSort() {
|
||||||
|
$this->entityManager->expects($this->any())
|
||||||
|
->method('getDefinition')
|
||||||
|
->with($this->entityTypeId)
|
||||||
|
->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, '\Drupal\Core\Config\Entity\ConfigEntityBase::sort');
|
||||||
|
$this->assertSame($entity_b, $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($entity_a, $list[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::toArray
|
||||||
|
*/
|
||||||
|
public function testToArray() {
|
||||||
|
$properties = $this->entity->toArray();
|
||||||
|
$this->assertInternalType('array', $properties);
|
||||||
|
$class_info = new \ReflectionClass($this->entity);
|
||||||
|
foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
|
||||||
|
$name = $property->getName();
|
||||||
|
$this->assertArrayHasKey($name, $properties);
|
||||||
|
$this->assertSame($this->entity->get($name), $properties[$name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
|
class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
|
||||||
|
@ -260,5 +439,4 @@ class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInt
|
||||||
public function defaultConfiguration() {
|
public function defaultConfiguration() {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,351 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\Tests\Core\Entity;
|
||||||
|
|
||||||
|
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||||
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
use Drupal\Core\Language\Language;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @coversDefaultClass \Drupal\Core\Entity\ContentEntityBase
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
|
*/
|
||||||
|
class ContentEntityBaseUnitTest extends UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bundle of the entity under test.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $bundle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity under test.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity type used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entityType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity manager used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entityManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type ID of the entity under test.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $entityTypeId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The typed data manager used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $typedDataManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The language manager.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $languageManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The UUID generator used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $uuid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity ID.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function getInfo() {
|
||||||
|
return array(
|
||||||
|
'description' => '',
|
||||||
|
'name' => '\Drupal\Core\Entity\ContentEntityBase unit test',
|
||||||
|
'group' => 'Entity',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$this->id = 1;
|
||||||
|
$values = array(
|
||||||
|
'id' => $this->id,
|
||||||
|
'langcode' => 'en',
|
||||||
|
'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a',
|
||||||
|
);
|
||||||
|
$this->entityTypeId = $this->randomName();
|
||||||
|
$this->bundle = $this->randomName();
|
||||||
|
|
||||||
|
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||||
|
|
||||||
|
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||||
|
$this->entityManager->expects($this->any())
|
||||||
|
->method('getDefinition')
|
||||||
|
->with($this->entityTypeId)
|
||||||
|
->will($this->returnValue($this->entityType));
|
||||||
|
$this->entityManager->expects($this->any())
|
||||||
|
->method('getFieldDefinitions')
|
||||||
|
->with($this->entityTypeId, $this->bundle)
|
||||||
|
->will($this->returnValue(array(
|
||||||
|
'id' => array(
|
||||||
|
'type' => 'integer_field',
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
|
||||||
|
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||||
|
|
||||||
|
$this->typedDataManager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManager')
|
||||||
|
->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('en' => $language)));
|
||||||
|
$this->languageManager->expects($this->any())
|
||||||
|
->method('getLanguage')
|
||||||
|
->with('en')
|
||||||
|
->will($this->returnValue($language));
|
||||||
|
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->set('entity.manager', $this->entityManager);
|
||||||
|
$container->set('uuid', $this->uuid);
|
||||||
|
$container->set('typed_data_manager', $this->typedDataManager);
|
||||||
|
$container->set('language_manager', $this->languageManager);
|
||||||
|
\Drupal::setContainer($container);
|
||||||
|
|
||||||
|
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::isNewRevision
|
||||||
|
* @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')
|
||||||
|
->will($this->returnValue(FALSE));
|
||||||
|
$this->entityType->expects($this->at(1))
|
||||||
|
->method('hasKey')
|
||||||
|
->with('revision')
|
||||||
|
->will($this->returnValue(TRUE));
|
||||||
|
|
||||||
|
$this->assertFalse($this->entity->isNewRevision());
|
||||||
|
$this->assertTrue($this->entity->isNewRevision());
|
||||||
|
$this->entity->setNewRevision(TRUE);
|
||||||
|
$this->assertTRUE($this->entity->isNewRevision());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::isDefaultRevision
|
||||||
|
*/
|
||||||
|
public function testIsDefaultRevision() {
|
||||||
|
// The default value is TRUE.
|
||||||
|
$this->assertTrue($this->entity->isDefaultRevision());
|
||||||
|
// 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getRevisionId
|
||||||
|
*/
|
||||||
|
public function testGetRevisionId() {
|
||||||
|
// The default getRevisionId() implementation returns NULL.
|
||||||
|
$this->assertNull($this->entity->getRevisionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::isTranslatable
|
||||||
|
*/
|
||||||
|
public function testIsTranslatable() {
|
||||||
|
$this->entityManager->expects($this->at(0))
|
||||||
|
->method('getBundleInfo')
|
||||||
|
->with($this->entityTypeId)
|
||||||
|
->will($this->returnValue(array(
|
||||||
|
$this->bundle => array(
|
||||||
|
'translatable' => TRUE,
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
$this->assertTrue($this->entity->isTranslatable());
|
||||||
|
$this->assertFalse($this->entity->isTranslatable());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getString
|
||||||
|
*/
|
||||||
|
public function testGetString() {
|
||||||
|
$label = $this->randomName();
|
||||||
|
/** @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject $entity */
|
||||||
|
$entity = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityBase')
|
||||||
|
->setMethods(array('label'))
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMockForAbstractClass();
|
||||||
|
$entity->expects($this->once())
|
||||||
|
->method('label')
|
||||||
|
->will($this->returnValue($label));
|
||||||
|
|
||||||
|
$this->assertSame($label, $entity->getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::validate
|
||||||
|
*/
|
||||||
|
public function testValidate() {
|
||||||
|
$validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface');
|
||||||
|
/** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */
|
||||||
|
$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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getConstraints
|
||||||
|
*/
|
||||||
|
public function testGetConstraints() {
|
||||||
|
$this->assertInternalType('array', $this->entity->getConstraints());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getName
|
||||||
|
*/
|
||||||
|
public function testGetName() {
|
||||||
|
$this->assertNull($this->entity->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getRoot
|
||||||
|
*/
|
||||||
|
public function testGetRoot() {
|
||||||
|
$this->assertSame(spl_object_hash($this->entity), spl_object_hash($this->entity->getRoot()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getPropertyPath
|
||||||
|
*/
|
||||||
|
public function testGetPropertyPath() {
|
||||||
|
$this->assertSame('', $this->entity->getPropertyPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getParent
|
||||||
|
*/
|
||||||
|
public function testGetParent() {
|
||||||
|
$this->assertNull($this->entity->getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::setContext
|
||||||
|
*/
|
||||||
|
public function testSetContext() {
|
||||||
|
$name = $this->randomName();
|
||||||
|
$parent = $this->getMock('\Drupal\Core\TypedData\TypedDataInterface');
|
||||||
|
$this->entity->setContext($name, $parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::bundle
|
||||||
|
*/
|
||||||
|
public function testBundle() {
|
||||||
|
$this->assertSame($this->bundle, $this->entity->bundle());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::label
|
||||||
|
*/
|
||||||
|
public function testLabel() {
|
||||||
|
// Make a mock with one method that we use as the entity's label callback.
|
||||||
|
// We check that it is called, and that the entity's label is the callback's
|
||||||
|
// return value.
|
||||||
|
$callback_label = $this->randomName();
|
||||||
|
$callback_container = $this->getMock(get_class());
|
||||||
|
$callback_container->expects($this->once())
|
||||||
|
->method(__FUNCTION__)
|
||||||
|
->will($this->returnValue($callback_label));
|
||||||
|
$this->entityType->expects($this->once())
|
||||||
|
->method('getLabelCallback')
|
||||||
|
->will($this->returnValue(array($callback_container, __FUNCTION__)));
|
||||||
|
|
||||||
|
$this->assertSame($callback_label, $this->entity->label());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,348 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\Tests\Core\Entity\EntityUnitTest.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\Tests\Core\Entity;
|
||||||
|
|
||||||
|
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||||
|
use Drupal\Core\Language\Language;
|
||||||
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @coversDefaultClass \Drupal\Core\Entity\Entity
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
|
*/
|
||||||
|
class EntityUnitTest extends UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity under test.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity type used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entityType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity manager used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $entityManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of the type of the entity under test.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $entityTypeId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The route provider used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Routing\RouteProvider|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $routeProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The UUID generator used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $uuid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The language manager.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $languageManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity values.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $values;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function getInfo() {
|
||||||
|
return array(
|
||||||
|
'description' => '',
|
||||||
|
'name' => '\Drupal\Core\Entity\Entity unit test',
|
||||||
|
'group' => 'Entity',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
$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');
|
||||||
|
|
||||||
|
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||||
|
$this->entityManager->expects($this->any())
|
||||||
|
->method('getDefinition')
|
||||||
|
->with($this->entityTypeId)
|
||||||
|
->will($this->returnValue($this->entityType));
|
||||||
|
|
||||||
|
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||||
|
|
||||||
|
$this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
|
||||||
|
$this->languageManager->expects($this->any())
|
||||||
|
->method('getLanguage')
|
||||||
|
->with('en')
|
||||||
|
->will($this->returnValue(new Language(array('id' => 'en'))));
|
||||||
|
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->set('entity.manager', $this->entityManager);
|
||||||
|
$container->set('uuid', $this->uuid);
|
||||||
|
$container->set('language_manager', $this->languageManager);
|
||||||
|
|
||||||
|
\Drupal::setContainer($container);
|
||||||
|
|
||||||
|
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\Entity', array($this->values, $this->entityTypeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::id
|
||||||
|
*/
|
||||||
|
public function testId() {
|
||||||
|
$this->assertSame($this->values['id'], $this->entity->id());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::uuid
|
||||||
|
*/
|
||||||
|
public function testUuid() {
|
||||||
|
$this->assertSame($this->values['uuid'], $this->entity->uuid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::isNew
|
||||||
|
* @covers ::enforceIsNew
|
||||||
|
*/
|
||||||
|
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->assertSame($this->entityType, $this->entity->getEntityType());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::bundle
|
||||||
|
*/
|
||||||
|
public function testBundle() {
|
||||||
|
$this->assertSame($this->entityTypeId, $this->entity->bundle());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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->entityType->expects($this->at(0))
|
||||||
|
->method('getLabelCallback')
|
||||||
|
->will($this->returnValue(array($callback_container, __FUNCTION__)));
|
||||||
|
$this->entityType->expects($this->at(1))
|
||||||
|
->method('getLabelCallback')
|
||||||
|
->will($this->returnValue(NULL));
|
||||||
|
$this->entityType->expects($this->at(2))
|
||||||
|
->method('getKey')
|
||||||
|
->with('label')
|
||||||
|
->will($this->returnValue('label'));
|
||||||
|
|
||||||
|
// 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->entityTypeId)
|
||||||
|
->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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::language
|
||||||
|
*/
|
||||||
|
public function testLanguage() {
|
||||||
|
$this->assertSame('en', $this->entity->language()->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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->entityTypeId)
|
||||||
|
->will($this->returnValue($storage));
|
||||||
|
$this->entity->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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->entityTypeId)
|
||||||
|
->will($this->returnValue($storage));
|
||||||
|
$this->entity->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getEntityTypeId
|
||||||
|
*/
|
||||||
|
public function testGetEntityTypeId() {
|
||||||
|
$this->assertSame($this->entityTypeId, $this->entity->getEntityTypeId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::postDelete
|
||||||
|
*/
|
||||||
|
public function testPostDelete() {
|
||||||
|
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
|
||||||
|
|
||||||
|
$entity = $this->getMockBuilder('\Drupal\Core\Entity\Entity')
|
||||||
|
->setMethods(array('onSaveOrDelete'))
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$entity->expects($this->once())
|
||||||
|
->method('onSaveOrDelete');
|
||||||
|
|
||||||
|
$this->entity->postDelete($storage, array($entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::postLoad
|
||||||
|
*/
|
||||||
|
public function testPostLoad() {
|
||||||
|
// This method is internal, so check for errors on calling it only.
|
||||||
|
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageControllerInterface');
|
||||||
|
$entities = array($this->entity);
|
||||||
|
$this->entity->postLoad($storage, $entities);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::referencedEntities
|
||||||
|
*/
|
||||||
|
public function testReferencedEntities() {
|
||||||
|
$this->assertSame(array(), $this->entity->referencedEntities());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue