diff -u b/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 --- b/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 @@ -57 +57 @@ -} \ No newline at end of file +} diff -u b/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 --- b/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 @@ -23,40 +23,33 @@ return $items; - break; + case 'valid_computed_entity_reference': $items = []; $parent = $this->getEntity(); if (!$parent->isNew()) { $items[] = $this->createItem(0, [ - 'target_id' => $parent->id() == 1 ? 2 : 1, + 'target_id' => $parent->id() === 1 ? 2 : 1, ]); } return $items; - break; + case 'non_valid_computed_entity_reference': $items = []; $parent = $this->getEntity(); if (!$parent->isNew()) { $items[] = $this->createItem(0, [ - 'target_id' => '3', // Non existing entity reference. + 'target_id' => '3', ]); } return $items; - break; + case 'non_valid_computed_timestamp': $items = []; $items[] = $this->createItem(0, [ 'value' => 'A', ]); return $items; - break; - case 'non_valid_computed_entity_reference': - $items = []; - $items[] = $this->createItem(0, [ - 'target_id' => 3, - ]); - break; } + return []; } - } diff -u b/core/modules/field/tests/src/Kernel/FieldComputedTest.php b/core/modules/field/tests/src/Kernel/FieldComputedTest.php --- b/core/modules/field/tests/src/Kernel/FieldComputedTest.php +++ b/core/modules/field/tests/src/Kernel/FieldComputedTest.php @@ -1,12 +1,11 @@ entities[0]->get('valid_computed_entity_reference')->entity; - $this->assertInstanceOf(EntityTest::class, $referencedEntity); - $this->assertEquals($this->entities[1]->id(), $referencedEntity->id()); + /** @var \Drupal\Core\Entity\EntityInterface $referenced_entity */ + $referenced_entity = $this->entities[0]->get('valid_computed_entity_reference')->entity; + $this->assertInstanceOf(EntityTest::class, $referenced_entity); + $this->assertEquals($this->entities[1]->id(), $referenced_entity->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); + public function testNonValidComputedEntityReference() { + $referenced_entity = $this->entities[0]->get('non_valid_computed_entity_reference')->entity; + $this->assertNull($referenced_entity); } /** - * @expectedException \Drupal\Core\Field\FieldException - * @expectedExceptionMessage This value should be a valid number. + * Test that a non valid computed timestamp returns throws an exception. */ public function testNonValidComputedTimestamp() { - $data = [ + $this->setExpectedException(FieldException::class, 'This value should be a valid number.'); + $entities_data = [ [ 'type' => 'non_valid_computed_timestamp', 'title' => 'Entity with a non valid computed timestamp', ], ]; - $this->createTestEntities($data); + $this->createTestEntities($entities_data); } /** @@ -83,22 +85,23 @@ $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); - $data = $this->getTestDataForEntities($this->getName()); - - $this->createTestEntities($data); + $entities_data = $this->getTestDataForEntities($this->getName()); + $this->createTestEntities($entities_data); } /** * Get an array of test data for the creation of entities. * - * @param $methodName + * @param string $method_name + * The name of the method to test. * - * @return array Data for creation of test entities. + * @return array + * Data for creation of test entities. */ - private function getTestDataForEntities($methodName){ + protected function getTestDataForEntities($method_name) { $data = []; - switch ($methodName) { - case "testValidComputedTimestamp": + switch ($method_name) { + case 'testValidComputedTimestamp': $data = [ [ 'type' => 'valid_computed_timestamp', @@ -106,7 +109,8 @@ ], ]; break; - case "testNonValidComputedEntityReference": + + case 'testNonValidComputedEntityReference': $data = [ [ 'type' => 'non_valid_computed_entity_reference', @@ -118,7 +122,8 @@ ], ]; break; - case "testValidComputedEntityReference": + + case 'testValidComputedEntityReference': $data = [ [ 'type' => 'valid_computed_entity_reference', @@ -133,11 +138,15 @@ } return $data; } + /** - * Create some Test Entities based on an array of data. + * Create some test entities based on an array of data. + * + * @param array $entities_data + * An array containing the data to create entities from. */ - private function createTestEntities($data) { - foreach ($data as $item) { + protected function createTestEntities(array $entities_data) { + foreach ($entities_data as $item) { $entity = EntityTest::create([ 'type' => $item['type'], 'title' => $item['title'], @@ -150 +159 @@ -} \ No newline at end of file +}