diff --git a/field_permission_example/src/Tests/FieldNoteItemTest.php b/field_permission_example/src/Tests/FieldNoteItemTest.php index d461bf1..84e13ba 100644 --- a/field_permission_example/src/Tests/FieldNoteItemTest.php +++ b/field_permission_example/src/Tests/FieldNoteItemTest.php @@ -26,57 +26,100 @@ class FieldNoteItemTest extends FieldUnitTestBase { /** * {@inheritdoc} * - * This sets up the entity_test and user types to use our example field plugins. + * This sets up the entity_test and user types to use our example + * field plugins. */ protected function setUp() { array_unshift(self::$modules, 'field_permission_example'); parent::setUp(); + // Tests for SimpleTest do not support standard Drupal dependency + // injection, so use the KernelTest class' supplied container. + $type_manager = $this->container->get('entity_type.manager'); // Set up our entity_type and user type for our new field: - entity_create('field_storage_config', array( - 'field_name' => 'field_fieldnote', - 'entity_type' => 'entity_test', - 'type' => 'field_permission_example_fieldnote', - ))->save(); - entity_create('field_config', array( - 'entity_type' => 'entity_test', - 'field_name' => 'field_fieldnote', - 'bundle' => 'entity_test', - ))->save(); - - // Create a form display for the default form mode. - entity_get_form_display('entity_test', 'entity_test', 'default') - ->setComponent('field_fieldnote', array( + $type_manager + ->getStorage('field_storage_config') + ->create([ + 'field_name' => 'field_fieldnote', + 'entity_type' => 'entity_test', + 'type' => 'field_permission_example_fieldnote', + ])->save(); + + $type_manager + ->getStorage('field_config') + ->create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_fieldnote', + 'bundle' => 'entity_test', + ])->save(); + + // Create a form display for the default form mode, and + // add our field type. + $type_manager + ->getStorage('entity_form_display') + ->create([ + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'default', + 'status' => TRUE, + ]) + ->setComponent('field_fieldnote', [ 'type' => 'field_permission_example_widget', - )) + ]) ->save(); // Now do this for the user type. - entity_create('field_storage_config', array( - 'field_name' => 'user_fieldnote', - 'entity_type' => 'user', - 'type' => 'field_permission_example_fieldnote', - ))->save(); - entity_create('field_config', array( - 'entity_type' => 'user', - 'field_name' => 'user_fieldnote', - 'bundle' => 'user', - ))->save(); - - // Create a form display for the default form mode. - entity_get_form_display('user', 'user', 'default') - ->setComponent('user_fieldnote', array( - 'type' => 'field_permission_example_widget', - )) - ->save(); + $type_manager + ->getStorage('field_storage_config') + ->create([ + 'field_name' => 'user_fieldnote', + 'entity_type' => 'user', + 'type' => 'field_permission_example_fieldnote', + ])->save(); + + $type_manager + ->getStorage('field_config') + ->create([ + 'entity_type' => 'user', + 'field_name' => 'user_fieldnote', + 'bundle' => 'user', + ])->save(); + + // Fetch a form display for a user. + // Most likely, this will already exist, so check as Core does. + // @see https://api.drupal.org/api/drupal/core%21includes%21entity.inc/function/entity_get_form_display/8 + $entity_form_display + = $type_manager + ->getStorage('entity_form_display') + ->load('user.user.default'); + if (empty($entity_form_display)) { + $entity_form_display + = $type_manager + ->getStorage('entity_form_display') + ->create([ + 'targetEntityType' => 'user', + 'bundle' => 'user', + 'mode' => 'default', + 'status' => TRUE, + ]); + } + // And add our fancy field to that display: + $entity_form_display->setComponent('field_fieldnote', [ + 'type' => 'field_permission_example_widget', + ])->save(); + } /** - * Tests using entity fields of the field_permission_example_fieldnote field type. + * Test entity fields of the field_permission_example_fieldnote field type. */ public function testFieldNoteItem() { // Verify entity creation. - $entity = entity_create('entity_test'); + $type_manager = $this->container->get('entity_type.manager'); + $entity + = $type_manager + ->getStorage('entity_test') + ->create([]); $value = 'This is an epic entity'; $entity->field_fieldnote = $value; $entity->name->value = $this->randomMachineName(); @@ -84,7 +127,11 @@ class FieldNoteItemTest extends FieldUnitTestBase { // Verify entity has been created properly. $id = $entity->id(); - $entity = entity_load('entity_test', $id); + $entity + = $type_manager + ->getStorage('entity_test') + ->load($id); + $this->assertTrue($entity->field_fieldnote instanceof FieldItemListInterface, 'Field implements interface.'); $this->assertTrue($entity->field_fieldnote[0] instanceof FieldItemInterface, 'Field item implements interface.'); $this->assertEqual($entity->field_fieldnote->value, $value); @@ -97,11 +144,20 @@ class FieldNoteItemTest extends FieldUnitTestBase { // Read changed entity and assert changed values. $entity->save(); - $entity = entity_load('entity_test', $id); + + $entity + = $type_manager + ->getStorage('entity_test') + ->load($id); + $this->assertEqual($entity->field_fieldnote->value, $new_value); // Test sample item generation. - $entity = entity_create('entity_test'); + $entity + = $type_manager + ->getStorage('entity_test') + ->create([]); + $entity->field_fieldnote->generateSampleItems(); $this->entityValidateAndSave($entity); }