Commit 16f0ab81 authored by alexpott's avatar alexpott

Issue #2167603 by damiankloip, tim.plunkett: EntityType should put unknown properties into an array

parent 6367b690
......@@ -232,6 +232,13 @@ class EntityType implements EntityTypeInterface {
*/
protected $constraints = array();
/**
* Any additional properties and values.
*
* @var array
*/
protected $additional = [];
/**
* Constructs a new EntityType.
*
......@@ -248,7 +255,7 @@ public function __construct($definition) {
}
foreach ($definition as $property => $value) {
$this->{$property} = $value;
$this->set($property, $value);
}
// Ensure defaults.
......@@ -279,14 +286,25 @@ public function __construct($definition) {
* {@inheritdoc}
*/
public function get($property) {
return isset($this->{$property}) ? $this->{$property} : NULL;
if (property_exists($this, $property)) {
$value = isset($this->{$property}) ? $this->{$property} : NULL;
}
else {
$value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
}
return $value;
}
/**
* {@inheritdoc}
*/
public function set($property, $value) {
$this->{$property} = $value;
if (property_exists($this, $property)) {
$this->{$property} = $value;
}
else {
$this->additional[$property] = $value;
}
return $this;
}
......
......@@ -20,7 +20,7 @@
* @ConfigEntityType(
* id = "base_field_override",
* label = @Translation("Base field override"),
* controllers = {
* handlers = {
* "storage" = "Drupal\Core\Field\BaseFieldOverrideStorage"
* },
* config_prefix = "base_field_override",
......
......@@ -24,7 +24,6 @@
* @ConfigEntityType(
* id = "migration",
* label = @Translation("Migration"),
* module = "migrate",
* handlers = {
* "storage" = "Drupal\migrate\MigrationStorage"
* },
......
......@@ -26,7 +26,6 @@
* "duplicate" = "Drupal\responsive_image\ResponsiveImageStyleForm"
* }
* },
* list_path = "admin/config/media/responsive-image-style",
* admin_permission = "administer responsive images",
* config_prefix = "styles",
* entity_keys = {
......
......@@ -23,7 +23,6 @@
* },
* base_table = "entity_test_update",
* revision_table = "entity_test_update_revision",
* fieldable = TRUE,
* persistent_cache = FALSE,
* entity_keys = {
* "id" = "id",
......
......@@ -8,6 +8,7 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Tests\UnitTestCase;
/**
......@@ -31,6 +32,29 @@ protected function setUpEntityType($definition) {
return new EntityType($definition);
}
/**
* @covers ::get
*
* @dataProvider providerTestGet
*/
public function testGet(array $defintion, $key, $expected) {
$entity_type = $this->setUpEntityType($defintion);
$this->assertSame($expected, $entity_type->get($key));
}
/**
* @covers ::set
* @covers ::get
*
* @dataProvider providerTestSet
*/
public function testSet($key, $value) {
$entity_type = $this->setUpEntityType([]);
$this->assertInstanceOf('Drupal\Core\Entity\EntityTypeInterface', $entity_type->set($key, $value));
$this->assertSame($value, $entity_type->get($key));
$this->assertNoPublicProperties($entity_type);
}
/**
* Tests the getKeys() method.
*
......@@ -64,6 +88,34 @@ public function testHasKey($entity_keys, $expected) {
$this->assertSame(FALSE, $entity_type->hasKey('bananas'));
}
/**
* Provides test data for testGet.
*/
public function providerTestGet() {
return [
[[], 'provider', NULL],
[['provider' => ''], 'provider', ''],
[['provider' => 'test'], 'provider', 'test'],
[[], 'something_additional', NULL],
[['something_additional' => ''], 'something_additional', ''],
[['something_additional' => 'additional'], 'something_additional', 'additional'],
];
}
/**
* Provides test data for testSet.
*/
public function providerTestSet() {
return [
['provider', NULL],
['provider', ''],
['provider', 'test'],
['something_additional', NULL],
['something_additional', ''],
['something_additional', 'additional'],
];
}
/**
* Provides test data.
*/
......@@ -272,4 +324,14 @@ public function testConstraintMethods() {
$this->assertEquals([], $entity_type->getConstraints());
}
/**
* Asserts there on no public properties on the object instance.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
*/
protected function assertNoPublicProperties(EntityTypeInterface $entity_type) {
$reflection = new \ReflectionObject($entity_type);
$this->assertEmpty($reflection->getProperties(\ReflectionProperty::IS_PUBLIC));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment