public function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); // Setup a field and instance. entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->referencedEntityType, 'default', array('target_bundles' => array($this->bundle)), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); }
/** * {@inheritdoc} */ function setUp() { parent::setUp(); $this->role1 = entity_create('user_role', array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8))); $this->role1->save(); $this->role2 = entity_create('user_role', array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8))); $this->role2->save(); entity_reference_create_instance('user', 'user', 'user_reference', 'User reference', 'user'); }
/** * Sets up the test. */ public function setUp() { parent::setUp(); $this->installEntitySchema('taxonomy_term'); $this->vocabulary = entity_create('taxonomy_vocabulary', array('name' => $this->randomMachineName(), 'vid' => drupal_strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->vocabulary->save(); $this->term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->term->save(); // Use the util to create an instance. entity_reference_create_instance('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term'); entity_reference_create_instance('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary'); }
public function setUp() { parent::setUp(); entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType); // Set up a field, so that the entity that'll be referenced bubbles up a // cache tag when rendering it entirely. entity_create('field_storage_config', array('name' => 'body', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save(); entity_create('field_instance_config', array('entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'body', 'label' => 'Body', 'settings' => array('text_processing' => TRUE)))->save(); entity_get_display($this->entityType, $this->bundle, 'default')->setComponent('body', array('type' => 'text_default', 'settings' => array()))->save(); entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML'))->save(); // Create the entity to be referenced. $this->referencedEntity = entity_create($this->entityType, array('name' => $this->randomMachineName())); $this->referencedEntity->body = array('value' => '<p>Hello, world!</p>', 'format' => 'full_html'); $this->referencedEntity->save(); }
public function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType); // Add the mapping. $mapping = rdf_get_mapping('entity_test', 'entity_test'); $mapping->setFieldMapping($this->fieldName, array('properties' => array('schema:knows')))->save(); // Create the entity to be referenced. $this->target_entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); $this->target_entity->save(); // Create the entity that will have the entity reference field. $this->entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); $this->entity->save(); $this->entity->{$this->fieldName}->entity = $this->target_entity; $this->entity->{$this->fieldName}->access = TRUE; $this->uri = $this->getAbsoluteUri($this->entity); }
/** * Tests the entity reference field with all its supported field widgets. */ public function testSupportedEntityTypesAndWidgets() { foreach ($this->getTestEntities() as $referenced_entities) { $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId(); // Create an Entity reference field. entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2); // Test the default 'entity_reference_autocomplete' widget. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save(); $entity_name = $this->randomMachineName(); $edit = array('name' => $entity_name, 'user_id' => mt_rand(0, 128), $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')', $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label()); $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); $this->assertFieldValues($entity_name, $referenced_entities); // Try to post the form again with no modification and check if the field // values remain the same. $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save')); $this->assertFieldValues($entity_name, $referenced_entities); // Test the 'entity_reference_autocomplete_tags' widget. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save(); $entity_name = $this->randomMachineName(); $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')'; // Test an input of the entity label without a ' (entity_id)' suffix. $target_id .= ', ' . $referenced_entities[1]->label(); $edit = array('name' => $entity_name, 'user_id' => mt_rand(0, 128), $this->fieldName . '[target_id]' => $target_id); $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); $this->assertFieldValues($entity_name, $referenced_entities); // Try to post the form again with no modification and check if the field // values remain the same. $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save')); $this->assertFieldValues($entity_name, $referenced_entities); // Test all the other widgets supported by the entity reference field. // Since we don't know the form structure for these widgets, just test // that editing and saving an already created entity works. $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference'); $supported_widget_types = array_diff(array_keys($supported_widgets), array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags')); foreach ($supported_widget_types as $widget_type) { entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => $widget_type))->save(); $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save')); $this->assertFieldValues($entity_name, $referenced_entities); } } }
/** * Tests entity render cache with references. */ public function testEntityViewBuilderCacheWithReferences() { // Force a request via GET so we can get drupal_render() cache working. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); $request->setMethod('GET'); // Create an entity reference field and an entity that will be referenced. entity_reference_create_instance('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test'); entity_get_display('entity_test', 'entity_test', 'full')->setComponent('reference_field')->save(); $entity_test_reference = $this->createTestEntity('entity_test'); $entity_test_reference->save(); // Get a fully built entity view render array for the referenced entity. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test_reference, 'full'); $cid_reference = drupal_render_cid_create($build); $bin_reference = $build['#cache']['bin']; // Mock the build array to not require the theme registry. unset($build['#theme']); $build['#markup'] = 'entity_render_test'; drupal_render($build); // Test that a cache entry was created for the referenced entity. $this->assertTrue($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render element for the referenced entity has been cached.'); // Create another entity that references the first one. $entity_test = $this->createTestEntity('entity_test'); $entity_test->reference_field->entity = $entity_test_reference; $entity_test->save(); // Get a fully built entity view render array. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full'); $cid = drupal_render_cid_create($build); $bin = $build['#cache']['bin']; // Mock the build array to not require the theme registry. unset($build['#theme']); $build['#markup'] = 'entity_render_test'; drupal_render($build); // Test that a cache entry is created. $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.'); // Save the entity and verify that both cache entries have been deleted. $entity_test->save(); $this->assertFalse($this->container->get('cache.' . $bin)->get($cid), 'The entity render cache has been cleared when the entity was deleted.'); $this->assertFalse($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render cache for the referenced entity has been cleared when the entity was deleted.'); // Restore the previous request method. $request->setMethod($request_method); }
function setUp() { parent::setUp(); entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType); }