diff --git a/core/lib/Drupal/Core/Field/ComputedFieldItemListBase.php b/core/lib/Drupal/Core/Field/ComputedFieldItemListBase.php new file mode 100644 index 0000000..8bd789d --- /dev/null +++ b/core/lib/Drupal/Core/Field/ComputedFieldItemListBase.php @@ -0,0 +1,31 @@ +computeFieldItemListValues(); + $errors = $this->validate()->getIterator(); + if ($errors->current()) { + throw new FieldException($errors->current()->getMessage()); + } + return isset($this->list[$index]) ? $this->list[$index] : NULL; + } + + /** + * {@inheritdoc} + */ + public function getIterator() { + $this->computeFieldItemListValues(); + return parent::getIterator(); + } +} diff --git a/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml b/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml new file mode 100644 index 0000000..42f6216 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml @@ -0,0 +1,6 @@ +name: 'Field Computed Test' +type: module +description: 'Support module for the computed field tests.' +core: 8.x +package: Testing +version: VERSION diff --git a/core/modules/field/tests/modules/field_computed_test/field_computed_test.module b/core/modules/field/tests/modules/field_computed_test/field_computed_test.module new file mode 100644 index 0000000..6a29dd7 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/field_computed_test.module @@ -0,0 +1,63 @@ +id() === 'entity_test') { + + // Add fields to separate bundles so we can test them individually. + switch ($bundle) { + case 'valid_computed_timestamp': + $fields['valid_computed_timestamp'] = BaseFieldDefinition::create('timestamp') + ->setComputed(TRUE) + ->setClass(ComputedValuesItemList::class) + ->setSetting('field', 'valid_computed_timestamp') + ->setLabel(t('Request Time')); + break; + case 'valid_computed_entity_reference': + $fields['valid_computed_entity_reference'] = BaseFieldDefinition::create('entity_reference') + ->setComputed(TRUE) + ->setSetting('target_type', 'entity_test') + ->setClass(ComputedValuesItemList::class) + ->setSetting('field', 'valid_computed_entity_reference') + ->setLabel(t('Valid Computed Entity Reference')); + break; + case 'non_valid_computed_timestamp': + $fields['non_valid_computed_timestamp'] = BaseFieldDefinition::create('timestamp') + ->setComputed(TRUE) + ->setClass(ComputedValuesItemList::class) + ->setSetting('field', 'non_valid_computed_timestamp') + ->setLabel(t('Non valid computed integer')); + break; + case 'non_valid_computed_entity_reference': + $fields['non_valid_computed_entity_reference'] = BaseFieldDefinition::create('entity_reference') + ->setComputed(TRUE) + ->setSetting('target_type', 'entity_test') + ->setClass(ComputedValuesItemList::class) + ->setSetting('field', 'non_valid_computed_entity_reference') + ->setLabel(t('Valid Computed Entity Reference')); + break; + case 'multiplier': + $fields['multiplier'] = BaseFieldDefinition::create('multiplier') + ->setLabel(t('Multiplied integer')) + ->setSetting('factor', 3); + break; + } + } + return $fields; +} \ No newline at end of file diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/ComputedValuesItemList.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/ComputedValuesItemList.php new file mode 100644 index 0000000..d24d325 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/ComputedValuesItemList.php @@ -0,0 +1,41 @@ +list[$index]) || $this->list[$index]->isEmpty()) { + + switch ($this->getSetting('field')) { + case 'valid_computed_timestamp': + $this->list[$index] = $this->createItem($index, \Drupal::time()->getRequestTime()); + break; + case 'valid_computed_entity_reference': + $parent = $this->getEntity(); + if (!$parent->isNew()) { + $this->list[$index] = $this->createItem($index, $parent->id() == 1 ? 2 : 1); + } + break; + case 'non_valid_computed_entity_reference': + $parent = $this->getEntity(); + if (!$parent->isNew()) { + $this->list[$index] = $this->createItem($index, 3); + } + break; + case 'non_valid_computed_timestamp': + $this->list[$index] = $this->createItem($index, 'A'); + break; + case 'non_valid_computed_entity_reference': + $this->list[$index] = $this->createItem($index, 'A'); + break; + } + } + } + +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/Multiplier.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/Multiplier.php new file mode 100644 index 0000000..7d24a19 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/Multiplier.php @@ -0,0 +1,50 @@ +setLabel(t('Original value')); + $properties['multipliedValue'] = DataDefinition::create('integer') + ->setLabel(t('Multiplied value')) + ->setComputed(TRUE) + ->setClass(MultiplierProcessor::class); + return $properties; + } + + + /** + * {@inheritdoc} + */ + public static function schema(FieldStorageDefinitionInterface $field_definition) { + return [ + 'columns' => [ + 'value' => [ + 'type' => 'int', + ], + ], + 'indexes' => [ + 'format' => ['value'], + ], + ]; + } +} \ No newline at end of file diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/MultiplierProcessor.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/MultiplierProcessor.php new file mode 100644 index 0000000..0a20ada --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/MultiplierProcessor.php @@ -0,0 +1,31 @@ +multipliedValue !== NULL && $this->value !== NULL) { + return $this->multipliedValue; + } + $item = $this->getParent(); + $factor = $item->getDataDefinition()->getSetting('factor'); + + $this->multipliedValue = $item->value * $factor; + return $this->multipliedValue; + } + +} \ No newline at end of file diff --git a/core/modules/field/tests/src/Kernel/FieldComputedTest.php b/core/modules/field/tests/src/Kernel/FieldComputedTest.php new file mode 100644 index 0000000..9211b1c --- /dev/null +++ b/core/modules/field/tests/src/Kernel/FieldComputedTest.php @@ -0,0 +1,175 @@ +installSchema('system', ['sequences', 'key_value']); + $this->installConfig(['field', 'system']); + $this->installEntitySchema('entity_test'); + $this->installEntitySchema('user'); + + $data = $this->getTestDataForEntities($this->getName()); + + $this->createTestEntities($data); + } + + /** + * Test a valid computed 'timestamp' field. + */ + public function testValidComputedTimestamp() { + $request_time = \Drupal::time()->getRequestTime(); + $value = $this->entities[0]->get('valid_computed_timestamp')->value; + $this->assertEquals($request_time, $value); + } + + /** + * Test a valid computed entityreference field. + */ + public function testValidComputedEntityReference() { + $referencedEntity = $this->entities[0]->get('valid_computed_entity_reference')->entity; + $this->assertInstanceOf(EntityTest::class, $referencedEntity); + $this->assertEquals($this->entities[1]->id(), $referencedEntity->id()); + } + + /** + * Test that a non existing entity reference returns NULL. + */ + public function testNonValidComputedEntityReference(){ + $referencedEntity = $this->entities[0]->get('non_valid_computed_entity_reference')->entity; + $this->assertNull($referencedEntity); + } + + /** + * @expectedException \Drupal\Core\Field\FieldException + * @expectedExceptionMessage This value should be a valid number. + */ + public function testNonValidComputedTimestamp() { + $data = [ + [ + 'type' => 'non_valid_computed_timestamp', + 'title' => 'Entity with a non valid computed timestamp', + ], + ]; + $this->createTestEntities($data); + } + + + /** + * Test that the processed Multiplier works correctly. + */ + public function testMultiplierFieldType(){ + $originalValue = 9; + $factor = 3; + $multipliedValue = $this->entities[0]->get('multiplier')->multipliedValue; + $this->assertEquals($originalValue*$factor, $multipliedValue); + } + + + + /** + * Get an array of test data for the creation of entities. + * + * @param $methodName + * + * @return array Data for creation of test entities. + */ + private function getTestDataForEntities($methodName){ + $data = []; + switch ($methodName) { + case "testValidComputedTimestamp": + $data = [ + [ + 'type' => 'valid_computed_timestamp', + 'title' => 'Entity with valid computed timestamp', + ], + ]; + break; + case "testNonValidComputedEntityReference": + $data = [ + [ + 'type' => 'non_valid_computed_entity_reference', + 'title' => 'Entity A', + ], + [ + 'type' => 'non_valid_computed_entity_reference', + 'title' => 'Entity B', + ], + ]; + break; + case "testValidComputedEntityReference": + $data = [ + [ + 'type' => 'valid_computed_entity_reference', + 'title' => 'Entity A', + ], + [ + 'type' => 'valid_computed_entity_reference', + 'title' => 'Entity B', + ], + ]; + break; + case "testMultiplierFieldType": + $data = [ + [ + 'type' => 'multiplier', + 'title' => 'Entity with multiplier field' + ], + ]; + break; + } + return $data; + } + + /** + * Create some Test Entities based on an array of data. + */ + private function createTestEntities($data) { + foreach ($data as $item) { + $entity = EntityTest::create([ + 'type' => $item['type'], + 'title' => $item['title'], + ]); + if($entity->bundle() == 'multiplier'){ + $entity->set('multiplier', 9); + } + $entity->save(); + $this->entities[] = $entity; + } + } +} \ No newline at end of file diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php index 2b929f3..1f4bfa4 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php @@ -83,6 +83,13 @@ protected function setUp() { ->method('getDefaultFieldSettings') ->willReturn([]); + $typed_data_manager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); + // @todo: maybe use a reasonable argument + $typed_data_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->anything()) + ->will($this->returnValue(['list_class' => '\Drupal\Core\Field\FieldItemList'])); + $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->renderer = $this->getMock('Drupal\Core\Render\RendererInterface'); @@ -94,6 +101,7 @@ protected function setUp() { $this->container = new ContainerBuilder(); $this->container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); + $this->container->set('typed_data_manager', $typed_data_manager); \Drupal::setContainer($this->container); } diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index 616f6ba..1f2cab1 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -64,8 +64,16 @@ protected function setUp() { ->with($this->fieldType) ->will($this->returnValue($this->fieldTypeDefinition['field_settings'])); + $typed_data_manager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); + // @todo: maybe use a reasonable argument + $typed_data_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->anything()) + ->will($this->returnValue(['list_class' => '\Drupal\Core\Field\FieldItemList'])); + $container = new ContainerBuilder(); $container->set('plugin.manager.field.field_type', $field_type_manager); + $container->set('typed_data_manager', $typed_data_manager); \Drupal::setContainer($container); } diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php index 541ec88..5a511cd 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -472,9 +472,12 @@ public function testLabel() { * - Fields array for $fields. */ public function providerGet() { + $items = $this->getMockBuilder('Drupal\Core\Field\FieldItemList') + ->disableOriginalConstructor() + ->getMock(); return [ // Populated fields array. - ['result', 'field_name', 'langcode', ['field_name' => ['langcode' => 'result']]], + [$items, 'field_name', 'langcode', ['field_name' => ['langcode' => $items]]], // Incomplete fields array. ['getTranslatedField_result', 'field_name', 'langcode', ['field_name' => 'no_langcode']], // Empty fields array. diff --git a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php index 953daac..d5832d7 100644 --- a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php @@ -141,8 +141,15 @@ protected function setUp() { $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class); $this->typedDataManager->expects($this->any()) ->method('getDefinition') - ->with('entity') - ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter'])); + ->will($this->returnCallback(function($plugin_id){ + switch ($plugin_id) { + case 'entity': + return ['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']; + default: + // @todo: maybe use a reasonable argument value + return ['list_class' => '\Drupal\Core\Field\FieldItemList']; + } + })); $this->typedDataManager->expects($this->any()) ->method('getDefaultConstraints') ->willReturn([]);