protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('taxonomy_term');
     // We want a taxonomy term reference field. It needs a vocabulary, terms,
     // a field storage and a field. First, create the vocabulary.
     $vocabulary = entity_create('taxonomy_vocabulary', array('vid' => Unicode::strtolower($this->randomMachineName())));
     $vocabulary->save();
     // Second, create the field.
     $this->fieldName = strtolower($this->randomMachineName());
     entity_create('field_storage_config', array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'taxonomy_term_reference', 'settings' => array('allowed_values' => array(array('vocabulary' => $vocabulary->id())))))->save();
     entity_test_create_bundle('test_bundle');
     // Third, create the instance.
     entity_create('field_config', array('entity_type' => 'entity_test', 'field_name' => $this->fieldName, 'bundle' => 'test_bundle'))->save();
     // Create two terms and also two accounts.
     for ($i = 0; $i <= 1; $i++) {
         $term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $vocabulary->id()));
         $term->save();
         $this->terms[] = $term;
         $this->accounts[] = $this->createUser();
     }
     // Create three entity_test entities, the 0th entity will point to the
     // 0th account and 0th term, the 1st and 2nd entity will point to the
     // 1st account and 1st term.
     for ($i = 0; $i <= 2; $i++) {
         $entity = entity_create('entity_test', array('type' => 'test_bundle'));
         $entity->name->value = $this->randomMachineName();
         $index = $i ? 1 : 0;
         $entity->user_id->target_id = $this->accounts[$index]->id();
         $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
         $entity->save();
         $this->entities[] = $entity;
     }
     $this->factory = \Drupal::service('entity.query');
 }
 /**
  * Test hook_entity_display_build_alter().
  */
 public function testHookEntityDisplayBuildAlter()
 {
     entity_test_create_bundle('display_build_alter_bundle');
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = $this->container->get('renderer');
     $entity_ids = [];
     // Create some entities to test.
     for ($i = 0; $i < 5; $i++) {
         $entity = EntityTest::create(['name' => $this->randomMachineName(), 'type' => 'display_build_alter_bundle']);
         $entity->save();
         $entity_ids[] = $entity->id();
     }
     /** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
     $view_builder = $this->container->get('entity_type.manager')->getViewBuilder('entity_test');
     /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
     $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
     $storage->resetCache();
     $entities = $storage->loadMultiple($entity_ids);
     $build = $view_builder->viewMultiple($entities);
     $output = $renderer->renderRoot($build);
     $this->setRawContent($output->__toString());
     // Confirm that the content added in
     // entity_test_entity_display_build_alter() appears multiple times, not
     // just for the final entity.
     foreach ($entity_ids as $id) {
         $this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
     }
 }
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('taxonomy_term');
     // We want an entity reference field. It needs a vocabulary, terms, a field
     // storage and a field. First, create the vocabulary.
     $vocabulary = entity_create('taxonomy_vocabulary', array('vid' => Unicode::strtolower($this->randomMachineName())));
     $vocabulary->save();
     // Second, create the field.
     entity_test_create_bundle('test_bundle');
     $this->fieldName = strtolower($this->randomMachineName());
     $handler_settings = array('target_bundles' => array($vocabulary->id() => $vocabulary->id()), 'auto_create' => TRUE);
     $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
     // Create two terms and also two accounts.
     for ($i = 0; $i <= 1; $i++) {
         $term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $vocabulary->id()));
         $term->save();
         $this->terms[] = $term;
         $this->accounts[] = $this->createUser();
     }
     // Create three entity_test entities, the 0th entity will point to the
     // 0th account and 0th term, the 1st and 2nd entity will point to the
     // 1st account and 1st term.
     for ($i = 0; $i <= 2; $i++) {
         $entity = entity_create('entity_test', array('type' => 'test_bundle'));
         $entity->name->value = $this->randomMachineName();
         $index = $i ? 1 : 0;
         $entity->user_id->target_id = $this->accounts[$index]->id();
         $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
         $entity->save();
         $this->entities[] = $entity;
     }
     $this->factory = \Drupal::service('entity.query');
 }
Beispiel #4
0
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('entity_test_mulrev');
     $this->installConfig(array('language'));
     $figures = Unicode::strtolower($this->randomMachineName());
     $greetings = Unicode::strtolower($this->randomMachineName());
     foreach (array($figures => 'shape', $greetings => 'text') as $field_name => $field_type) {
         $field_storage = entity_create('field_storage_config', array('field_name' => $field_name, 'entity_type' => 'entity_test_mulrev', 'type' => $field_type, 'cardinality' => 2));
         $field_storage->save();
         $field_storages[] = $field_storage;
     }
     $bundles = array();
     for ($i = 0; $i < 2; $i++) {
         // For the sake of tablesort, make sure the second bundle is higher than
         // the first one. Beware: MySQL is not case sensitive.
         do {
             $bundle = $this->randomMachineName();
         } while ($bundles && strtolower($bundles[0]) >= strtolower($bundle));
         entity_test_create_bundle($bundle);
         foreach ($field_storages as $field_storage) {
             entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => $bundle))->save();
         }
         $bundles[] = $bundle;
     }
     // Each unit is a list of field name, langcode and a column-value array.
     $units[] = array($figures, 'en', array('color' => 'red', 'shape' => 'triangle'));
     $units[] = array($figures, 'en', array('color' => 'blue', 'shape' => 'circle'));
     // To make it easier to test sorting, the greetings get formats according
     // to their langcode.
     $units[] = array($greetings, 'tr', array('value' => 'merhaba', 'format' => 'format-tr'));
     $units[] = array($greetings, 'pl', array('value' => 'siema', 'format' => 'format-pl'));
     // Make these languages available to the greetings field.
     ConfigurableLanguage::createFromLangcode('tr')->save();
     ConfigurableLanguage::createFromLangcode('pl')->save();
     // Calculate the cartesian product of the unit array by looking at the
     // bits of $i and add the unit at the bits that are 1. For example,
     // decimal 13 is binary 1101 so unit 3,2 and 0 will be added to the
     // entity.
     for ($i = 1; $i <= 15; $i++) {
         $entity = EntityTestMulRev::create(array('type' => $bundles[$i & 1], 'name' => $this->randomMachineName(), 'langcode' => 'en'));
         // Make sure the name is set for every language that we might create.
         foreach (array('tr', 'pl') as $langcode) {
             $entity->addTranslation($langcode)->name = $this->randomMachineName();
         }
         foreach (array_reverse(str_split(decbin($i))) as $key => $bit) {
             if ($bit) {
                 list($field_name, $langcode, $values) = $units[$key];
                 $entity->getTranslation($langcode)->{$field_name}[] = $values;
             }
         }
         $entity->save();
     }
     $this->bundles = $bundles;
     $this->figures = $figures;
     $this->greetings = $greetings;
     $this->factory = \Drupal::service('entity.query');
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setup();
     $this->installEntitySchema('node');
     $this->installEntitySchema('taxonomy_term');
     $this->installEntitySchema('entity_test');
     $this->nodeType = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName()), 'name' => $this->randomString()]);
     $this->nodeType->save();
     $this->vocabulary = Vocabulary::create(['vid' => Unicode::strtolower($this->randomMachineName()), 'name' => $this->randomString()]);
     $this->vocabulary->save();
     // Create a custom bundle.
     $this->customBundle = 'test_bundle_' . Unicode::strtolower($this->randomMachineName());
     entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
 }
 /**
  * {@inheritdoc}
  */
 protected function createEntity()
 {
     // Create a "bar" bundle for the "entity_test" entity type and create.
     $bundle = 'bar';
     entity_test_create_bundle($bundle, NULL, 'entity_test');
     // Create a comment field on this bundle.
     \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar');
     // Create a "Camelids" test entity.
     $entity_test = entity_create('entity_test', array('name' => 'Camelids', 'type' => 'bar'));
     $entity_test->save();
     // Create a "Llama" comment.
     $comment = entity_create('comment', array('subject' => 'Llama', 'comment_body' => array('value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'format' => 'plain_text'), 'entity_id' => $entity_test->id(), 'entity_type' => 'entity_test', 'field_name' => 'comment', 'status' => \Drupal\comment\CommentInterface::PUBLISHED));
     $comment->save();
     return $comment;
 }
Beispiel #7
0
 /**
  * Tests making use of a custom bundle field.
  */
 public function testCustomBundleFieldUsage()
 {
     entity_test_create_bundle('custom');
     // Check that an entity with bundle entity_test does not have the custom
     // field.
     $storage = $this->entityManager->getStorage('entity_test');
     $entity = $storage->create(['type' => 'entity_test']);
     $this->assertFalse($entity->hasField('custom_bundle_field'));
     // Check that the custom bundle has the defined custom field and check
     // saving and deleting of custom field data.
     $entity = $storage->create(['type' => 'custom']);
     $this->assertTrue($entity->hasField('custom_bundle_field'));
     // Ensure that the field exists in the field map.
     $field_map = \Drupal::entityManager()->getFieldMap();
     $this->assertEqual($field_map['entity_test']['custom_bundle_field'], ['type' => 'string', 'bundles' => ['custom' => 'custom']]);
     $entity->custom_bundle_field->value = 'swanky';
     $entity->save();
     $storage->resetCache();
     $entity = $storage->load($entity->id());
     $this->assertEqual($entity->custom_bundle_field->value, 'swanky', 'Entity was saved correctly');
     $entity->custom_bundle_field->value = 'cozy';
     $entity->save();
     $storage->resetCache();
     $entity = $storage->load($entity->id());
     $this->assertEqual($entity->custom_bundle_field->value, 'cozy', 'Entity was updated correctly.');
     $entity->delete();
     /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
     $table_mapping = $storage->getTableMapping();
     $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'));
     $result = $this->database->select($table, 'f')->fields('f')->condition('f.entity_id', $entity->id())->execute();
     $this->assertFalse($result->fetchAssoc(), 'Field data has been deleted');
     // Create another entity to test that values are marked as deleted when a
     // bundle is deleted.
     $entity = $storage->create(['type' => 'custom', 'custom_bundle_field' => 'new']);
     $entity->save();
     entity_test_delete_bundle('custom');
     $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field'));
     $result = $this->database->select($table, 'f')->condition('f.entity_id', $entity->id())->condition('deleted', 1)->countQuery()->execute();
     $this->assertEqual(1, $result->fetchField(), 'Field data has been deleted');
     // Ensure that the field no longer exists in the field map.
     $field_map = \Drupal::entityManager()->getFieldMap();
     $this->assertFalse(isset($field_map['entity_test']['custom_bundle_field']));
     // @todo Test field purge and table deletion once supported. See
     //   https://www.drupal.org/node/2282119.
     // $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted');
 }
 /**
  * Tests the add page for an entity type not using bundle entities.
  */
 public function testAddPageWithoutBundleEntities()
 {
     entity_test_create_bundle('test', 'Test label', 'entity_test_mul');
     // Delete the default bundle, so that we can rely on our own.
     entity_test_delete_bundle('entity_test_mul', 'entity_test_mul');
     // One bundle exists, confirm redirection to the add-form.
     $this->drupalGet('/entity_test_mul/add');
     $this->assertUrl('/entity_test_mul/add/test');
     // Two bundles exist, confirm both are shown.
     entity_test_create_bundle('test2', 'Test2 label', 'entity_test_mul');
     $this->drupalGet('/entity_test_mul/add');
     $this->assertLink('Test label');
     $this->assertLink('Test2 label');
     $this->clickLink('Test2 label');
     $this->drupalGet('/entity_test_mul/add/test2');
     $this->drupalPostForm(NULL, ['name[0][value]' => 'test name'], t('Save'));
     $entity = EntityTestMul::load(1);
     $this->assertEqual('test name', $entity->label());
 }
 /**
  * {@inheritdoc}
  */
 protected function createEntity()
 {
     // Create a "bar" bundle for the "entity_test" entity type and create.
     $bundle = 'bar';
     entity_test_create_bundle($bundle, NULL, 'entity_test');
     // Create a comment field on this bundle.
     \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar', 'comment');
     // Display comments in a flat list; threaded comments are not render cached.
     $field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
     $field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT;
     $field->save();
     // Create a "Camelids" test entity.
     $entity_test = entity_create('entity_test', array('name' => 'Camelids', 'type' => 'bar'));
     $entity_test->save();
     // Create a "Llama" comment.
     $comment = entity_create('comment', array('subject' => 'Llama', 'comment_body' => array('value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'format' => 'plain_text'), 'entity_id' => $entity_test->id(), 'entity_type' => 'entity_test', 'field_name' => 'comment', 'status' => \Drupal\comment\CommentInterface::PUBLISHED));
     $comment->save();
     return $comment;
 }
 /**
  * Tests creating fields and instances during default config import.
  */
 function testImportCreateDefault()
 {
     $field_name = 'field_test_import';
     $field_id = "entity_test.{$field_name}";
     $instance_id = "entity_test.entity_test.{$field_name}";
     $field_name_2 = 'field_test_import_2';
     $field_id_2 = "entity_test.{$field_name_2}";
     $instance_id_2a = "entity_test.entity_test.{$field_name_2}";
     $instance_id_2b = "entity_test.test_bundle.{$field_name_2}";
     // Check that the fields and instances do not exist yet.
     $this->assertFalse(entity_load('field_storage_config', $field_id));
     $this->assertFalse(entity_load('field_instance_config', $instance_id));
     $this->assertFalse(entity_load('field_storage_config', $field_id_2));
     $this->assertFalse(entity_load('field_instance_config', $instance_id_2a));
     $this->assertFalse(entity_load('field_instance_config', $instance_id_2b));
     // Create a second bundle for the 'Entity test' entity type.
     entity_test_create_bundle('test_bundle');
     // Enable field_test_config module and check that the field and instance
     // shipped in the module's default config were created.
     \Drupal::moduleHandler()->install(array('field_test_config'));
     // A field with one instance.
     $field_storage = entity_load('field_storage_config', $field_id);
     $this->assertTrue($field_storage, 'The field was created.');
     $instance = entity_load('field_instance_config', $instance_id);
     $this->assertTrue($instance, 'The field instance was deleted.');
     // A field with multiple instances.
     $field_storage_2 = entity_load('field_storage_config', $field_id_2);
     $this->assertTrue($field_storage_2, 'The second field was created.');
     $this->assertTrue($instance->bundle, 'test_bundle', 'The second field instance was created on bundle test_bundle.');
     $this->assertTrue($instance->bundle, 'test_bundle_2', 'The second field instance was created on bundle test_bundle_2.');
     // Tests field instances.
     $ids = \Drupal::entityQuery('field_instance_config')->condition('entity_type', 'entity_test')->condition('bundle', 'entity_test')->execute();
     $this->assertEqual(count($ids), 2);
     $this->assertTrue(isset($ids['entity_test.entity_test.field_test_import']));
     $this->assertTrue(isset($ids['entity_test.entity_test.field_test_import_2']));
     $ids = \Drupal::entityQuery('field_instance_config')->condition('entity_type', 'entity_test')->condition('bundle', 'test_bundle')->execute();
     $this->assertEqual(count($ids), 1);
     $this->assertTrue(isset($ids['entity_test.test_bundle.field_test_import_2']));
 }
 /**
  * Tests creating field storages and fields during default config import.
  */
 function testImportCreateDefault()
 {
     $field_name = 'field_test_import';
     $field_storage_id = "entity_test.{$field_name}";
     $field_id = "entity_test.entity_test.{$field_name}";
     $field_name_2 = 'field_test_import_2';
     $field_storage_id_2 = "entity_test.{$field_name_2}";
     $field_id_2a = "entity_test.entity_test.{$field_name_2}";
     $field_id_2b = "entity_test.test_bundle.{$field_name_2}";
     // Check that the field storages and fields do not exist yet.
     $this->assertFalse(FieldStorageConfig::load($field_storage_id));
     $this->assertFalse(FieldConfig::load($field_id));
     $this->assertFalse(FieldStorageConfig::load($field_storage_id_2));
     $this->assertFalse(FieldConfig::load($field_id_2a));
     $this->assertFalse(FieldConfig::load($field_id_2b));
     // Create a second bundle for the 'Entity test' entity type.
     entity_test_create_bundle('test_bundle');
     // Enable field_test_config module and check that the field and storage
     // shipped in the module's default config were created.
     \Drupal::service('module_installer')->install(array('field_test_config'));
     // A field storage with one single field.
     $field_storage = FieldStorageConfig::load($field_storage_id);
     $this->assertTrue($field_storage, 'The field was created.');
     $field = FieldConfig::load($field_id);
     $this->assertTrue($field, 'The field was deleted.');
     // A field storage with two fields.
     $field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
     $this->assertTrue($field_storage_2, 'The second field was created.');
     $this->assertTrue($field->getTargetBundle(), 'test_bundle', 'The second field was created on bundle test_bundle.');
     $this->assertTrue($field->getTargetBundle(), 'test_bundle_2', 'The second field was created on bundle test_bundle_2.');
     // Tests fields.
     $ids = \Drupal::entityQuery('field_config')->condition('entity_type', 'entity_test')->condition('bundle', 'entity_test')->execute();
     $this->assertEqual(count($ids), 2);
     $this->assertTrue(isset($ids['entity_test.entity_test.field_test_import']));
     $this->assertTrue(isset($ids['entity_test.entity_test.field_test_import_2']));
     $ids = \Drupal::entityQuery('field_config')->condition('entity_type', 'entity_test')->condition('bundle', 'test_bundle')->execute();
     $this->assertEqual(count($ids), 1);
     $this->assertTrue(isset($ids['entity_test.test_bundle.field_test_import_2']));
 }
 /**
  * Tests reference field validation.
  */
 public function testEntityReferenceFieldValidation()
 {
     // Test a valid reference.
     $referenced_entity = $this->container->get('entity_type.manager')->getStorage($this->referencedEntityType)->create(array('type' => $this->bundle));
     $referenced_entity->save();
     $entity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('type' => $this->bundle));
     $entity->{$this->fieldName}->target_id = $referenced_entity->id();
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 0, 'Validation passes.');
     // Test an invalid reference.
     $entity->{$this->fieldName}->target_id = 9999;
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
     $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->referencedEntityType, '%id' => 9999)));
     // Test a non-referenceable bundle.
     entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType);
     $referenced_entity = entity_create($this->referencedEntityType, array('type' => 'non_referenceable'));
     $referenced_entity->save();
     $entity->{$this->fieldName}->target_id = $referenced_entity->id();
     $violations = $entity->{$this->fieldName}->validate();
     $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
     $this->assertEqual($violations[0]->getMessage(), t('This entity (%type: %id) cannot be referenced.', array('%type' => $this->referencedEntityType, '%id' => $referenced_entity->id())));
 }
Beispiel #13
0
 /**
  * Tests that a field not available for every bundle is rendered as empty.
  */
 public function testMissingBundleFieldRender()
 {
     // Create a new bundle not having the test field attached.
     $bundle = $this->randomMachineName();
     entity_test_create_bundle($bundle);
     $entity = EntityTest::create(['type' => $bundle, 'name' => $this->randomString(), 'user_id' => $this->testUsers[0]->id()]);
     $entity->save();
     $executable = Views::getView('test_field_field_test');
     $executable->execute();
     $this->assertEqual('', $executable->getStyle()->getField(1, 'field_test'));
 }
 /**
  * Tests the translation of a boolean field settings.
  */
 public function testBooleanFieldConfigTranslation()
 {
     // Add a test boolean field.
     $field_name = strtolower($this->randomMachineName());
     FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'boolean'])->save();
     $bundle = strtolower($this->randomMachineName());
     entity_test_create_bundle($bundle);
     $field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => $bundle]);
     $on_label = 'On label (with <em>HTML</em> & things)';
     $field->setSetting('on_label', $on_label);
     $off_label = 'Off label (with <em>HTML</em> & things)';
     $field->setSetting('off_label', $off_label);
     $field->save();
     $this->drupalLogin($this->translatorUser);
     $this->drupalGet("/entity_test/structure/{$bundle}/fields/entity_test.{$bundle}.{$field_name}/translate");
     $this->clickLink('Add');
     // Checks the text of details summary element that surrounds the translation
     // options.
     $this->assertText(Html::escape(strip_tags($on_label)) . ' Boolean settings');
     // Checks that the correct on and off labels appear on the form.
     $this->assertEscaped($on_label);
     $this->assertEscaped($off_label);
 }
 /**
  * Tests the translation of field and field storage configuration.
  */
 public function testFieldConfigTranslation()
 {
     // Add a test field which has a translatable field setting and a
     // translatable field storage setting.
     $field_name = strtolower($this->randomMachineName());
     $field_storage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'test_field']);
     $translatable_storage_setting = $this->randomString();
     $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
     $field_storage->save();
     $bundle = strtolower($this->randomMachineName());
     entity_test_create_bundle($bundle);
     $field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => $bundle]);
     $translatable_field_setting = $this->randomString();
     $field->setSetting('translatable_field_setting', $translatable_field_setting);
     $field->save();
     $this->drupalLogin($this->translatorUser);
     $this->drupalGet("/entity_test/structure/{$bundle}/fields/entity_test.{$bundle}.{$field_name}/translate");
     $this->clickLink('Add');
     $this->assertText('Translatable field setting');
     $this->assertEscaped($translatable_field_setting);
     $this->assertText('Translatable storage setting');
     $this->assertEscaped($translatable_storage_setting);
 }
 /**
  * Tests updating a bundle field when it has existing data.
  */
 public function testBundleFieldUpdateWithExistingData()
 {
     // Add the bundle field and run the update.
     $this->addBundleField();
     $this->entityDefinitionUpdateManager->applyUpdates();
     // Save an entity with the bundle field populated.
     entity_test_create_bundle('custom');
     $this->entityManager->getStorage('entity_test_update')->create(array('type' => 'test_bundle', 'new_bundle_field' => 'foo'))->save();
     // Change the field's field type and apply updates. It's expected to
     // throw an exception.
     $this->modifyBundleField();
     try {
         $this->entityDefinitionUpdateManager->applyUpdates();
         $this->fail('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
     } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
         $this->pass('FieldStorageDefinitionUpdateForbiddenException thrown when trying to update a field schema that has data.');
     }
 }
Beispiel #17
0
 /**
  * Sets up the necessary bundles on the test entity type.
  */
 protected function setUpExampleStructure() {
   entity_test_create_bundle('item');
   entity_test_create_bundle('article');
 }
 /**
  * Tests the cross deletion behavior between field storages and fields.
  */
 function testDeleteFieldCrossDeletion()
 {
     $field_definition_2 = $this->fieldDefinition;
     $field_definition_2['bundle'] .= '_another_bundle';
     entity_test_create_bundle($field_definition_2['bundle']);
     // Check that deletion of a field storage deletes its fields.
     $field_storage = $this->fieldStorage;
     FieldConfig::create($this->fieldDefinition)->save();
     FieldConfig::create($field_definition_2)->save();
     $field_storage->delete();
     $this->assertFalse(FieldConfig::loadByName('entity_test', $this->fieldDefinition['bundle'], $field_storage->getName()));
     $this->assertFalse(FieldConfig::loadByName('entity_test', $field_definition_2['bundle'], $field_storage->getName()));
     // Check that deletion of the last field deletes the storage.
     $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
     $field_storage->save();
     $field = FieldConfig::create($this->fieldDefinition);
     $field->save();
     $field_2 = FieldConfig::create($field_definition_2);
     $field_2->save();
     $field->delete();
     $this->assertTrue(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
     $field_2->delete();
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
     // Check that deletion of all fields using a storage simultaneously deletes
     // the storage.
     $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
     $field_storage->save();
     $field = FieldConfig::create($this->fieldDefinition);
     $field->save();
     $field_2 = FieldConfig::create($field_definition_2);
     $field_2->save();
     $this->container->get('entity.manager')->getStorage('field_config')->delete(array($field, $field_2));
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
 }
Beispiel #19
0
 protected function setUp()
 {
     parent::setUp();
     $this->fieldStorages = array();
     $this->entities = array();
     $this->entitiesByBundles = array();
     // Create two bundles.
     $this->bundles = array('bb_1' => 'bb_1', 'bb_2' => 'bb_2');
     foreach ($this->bundles as $name => $desc) {
         entity_test_create_bundle($name, $desc);
     }
     // Create two field storages.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'bf_1', 'entity_type' => $this->entityTypeId, 'type' => 'test_field', 'cardinality' => 1));
     $field_storage->save();
     $this->fieldStorages[] = $field_storage;
     $field_storage = FieldStorageConfig::create(array('field_name' => 'bf_2', 'entity_type' => $this->entityTypeId, 'type' => 'test_field', 'cardinality' => 4));
     $field_storage->save();
     $this->fieldStorages[] = $field_storage;
     // For each bundle, create each field, and 10 entities with values for the
     // fields.
     foreach ($this->bundles as $bundle) {
         foreach ($this->fieldStorages as $field_storage) {
             FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $bundle])->save();
         }
         for ($i = 0; $i < 10; $i++) {
             $entity = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId)->create(array('type' => $bundle));
             foreach ($this->fieldStorages as $field_storage) {
                 $entity->{$field_storage->getName()}->setValue($this->_generateTestFieldValues($field_storage->getCardinality()));
             }
             $entity->save();
         }
     }
     $this->entities = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId)->loadMultiple();
     foreach ($this->entities as $entity) {
         // This test relies on the entities having stale field definitions
         // so that the deleted field can be accessed on them. Access the field
         // now, so that they are always loaded.
         $entity->bf_1->value;
         // Also keep track of the entities per bundle.
         $this->entitiesByBundles[$entity->bundle()][$entity->id()] = $entity;
     }
 }
 protected function setUp()
 {
     parent::setUp();
     $this->fieldStorages = array();
     $this->entities = array();
     $this->entities_by_bundles = array();
     // Create two bundles.
     $this->bundles = array('bb_1' => 'bb_1', 'bb_2' => 'bb_2');
     foreach ($this->bundles as $name => $desc) {
         entity_test_create_bundle($name, $desc);
     }
     // Create two field storages.
     $field_storage = entity_create('field_storage_config', array('field_name' => 'bf_1', 'entity_type' => $this->entity_type, 'type' => 'test_field', 'cardinality' => 1));
     $field_storage->save();
     $this->fieldStorages[] = $field_storage;
     $field_storage = entity_create('field_storage_config', array('field_name' => 'bf_2', 'entity_type' => $this->entity_type, 'type' => 'test_field', 'cardinality' => 4));
     $field_storage->save();
     $this->fieldStorages[] = $field_storage;
     // For each bundle, create each field, and 10 entities with values for the
     // fields.
     foreach ($this->bundles as $bundle) {
         foreach ($this->fieldStorages as $field_storage) {
             entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => $bundle))->save();
         }
         for ($i = 0; $i < 10; $i++) {
             $entity = entity_create($this->entity_type, array('type' => $bundle));
             foreach ($this->fieldStorages as $field_storage) {
                 $entity->{$field_storage->getName()}->setValue($this->_generateTestFieldValues($field_storage->getCardinality()));
             }
             $entity->save();
         }
     }
     $this->entities = entity_load_multiple($this->entity_type);
     foreach ($this->entities as $entity) {
         // Also keep track of the entities per bundle.
         $this->entities_by_bundles[$entity->bundle()][$entity->id()] = $entity;
     }
 }
Beispiel #21
0
 /**
  * Makes sure data types are correctly derived for all entity types.
  */
 public function testDataTypes()
 {
     $types = \Drupal::typedDataManager()->getDefinitions();
     foreach (entity_test_entity_types() as $entity_type) {
         $this->assertTrue($types['entity:' . $entity_type]['class'], 'Entity data type registed.');
     }
     // Check bundle types are provided as well.
     entity_test_create_bundle('bundle');
     $types = \Drupal::typedDataManager()->getDefinitions();
     $this->assertTrue($types['entity:entity_test:bundle']['class'], 'Entity bundle data type registed.');
 }
Beispiel #22
0
 /**
  * Tests the behavior of a field component within an entity display object.
  */
 public function testExtraFieldComponent()
 {
     entity_test_create_bundle('bundle_with_extra_fields');
     $display = EntityViewDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'bundle_with_extra_fields', 'mode' => 'default'));
     // Check that the default visibility taken into account for extra fields
     // unknown in the display.
     $this->assertEqual($display->getComponent('display_extra_field'), array('weight' => 5));
     $this->assertNull($display->getComponent('display_extra_field_hidden'));
     // Check that setting explicit options overrides the defaults.
     $display->removeComponent('display_extra_field');
     $display->setComponent('display_extra_field_hidden', array('weight' => 10));
     $this->assertNull($display->getComponent('display_extra_field'));
     $this->assertEqual($display->getComponent('display_extra_field_hidden'), array('weight' => 10, 'settings' => array(), 'third_party_settings' => array()));
 }
 /**
  * Creates a referencing and a non-referencing entity for testing purposes.
  *
  * @param \Drupal\Core\Entity\EntityInterface $referenced_entity
  *  The entity that the referencing entity should reference.
  *
  * @return \Drupal\Core\Entity\EntityInterface[]
  *  An array containing a referencing entity and a non-referencing entity.
  */
 protected function createReferenceTestEntities($referenced_entity)
 {
     // All referencing entities should be of the type 'entity_test'.
     $entity_type = 'entity_test';
     // Create a "foo" bundle for the given entity type.
     $bundle = 'foo';
     entity_test_create_bundle($bundle, NULL, $entity_type);
     // Add a field of the given type to the given entity type's "foo" bundle.
     $field_name = $referenced_entity->getEntityTypeId() . '_reference';
     entity_create('field_storage_config', array('field_name' => $field_name, 'entity_type' => $entity_type, 'type' => 'entity_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('target_type' => $referenced_entity->getEntityTypeId())))->save();
     entity_create('field_config', array('field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'settings' => array('handler' => 'default', 'handler_settings' => array('target_bundles' => array($referenced_entity->bundle() => $referenced_entity->bundle()), 'sort' => array('field' => '_none'), 'auto_create' => FALSE))))->save();
     if (!$this->entity->getEntityType()->hasHandlerClass('view_builder')) {
         entity_get_display($entity_type, $bundle, 'full')->setComponent($field_name, array('type' => 'entity_reference_label'))->save();
     } else {
         $referenced_entity_view_mode = $this->selectViewMode($this->entity->getEntityTypeId());
         entity_get_display($entity_type, $bundle, 'full')->setComponent($field_name, array('type' => 'entity_reference_entity_view', 'settings' => array('view_mode' => $referenced_entity_view_mode)))->save();
     }
     // Create an entity that does reference the entity being tested.
     $label_key = \Drupal::entityManager()->getDefinition($entity_type)->getKey('label');
     $referencing_entity = entity_create($entity_type, array($label_key => 'Referencing ' . $entity_type, 'status' => 1, 'type' => $bundle, $field_name => array('target_id' => $referenced_entity->id())));
     $referencing_entity->save();
     // Create an entity that does not reference the entity being tested.
     $non_referencing_entity = entity_create($entity_type, array($label_key => 'Non-referencing ' . $entity_type, 'status' => 1, 'type' => $bundle));
     $non_referencing_entity->save();
     return array($referencing_entity, $non_referencing_entity);
 }
 /**
  * Tests the cross deletion behavior between fields and instances.
  */
 function testDeleteFieldInstanceCrossDeletion()
 {
     $instance_definition_2 = $this->instanceDefinition;
     $instance_definition_2['bundle'] .= '_another_bundle';
     entity_test_create_bundle($instance_definition_2['bundle']);
     // Check that deletion of a field deletes its instances.
     $field_storage = $this->fieldStorage;
     entity_create('field_instance_config', $this->instanceDefinition)->save();
     entity_create('field_instance_config', $instance_definition_2)->save();
     $field_storage->delete();
     $this->assertFalse(FieldInstanceConfig::loadByName('entity_test', $this->instanceDefinition['bundle'], $field_storage->name));
     $this->assertFalse(FieldInstanceConfig::loadByName('entity_test', $instance_definition_2['bundle'], $field_storage->name));
     // Chack that deletion of the last instance deletes the field.
     $field_storage = entity_create('field_storage_config', $this->fieldStorageDefinition);
     $field_storage->save();
     $instance = entity_create('field_instance_config', $this->instanceDefinition);
     $instance->save();
     $instance_2 = entity_create('field_instance_config', $instance_definition_2);
     $instance_2->save();
     $instance->delete();
     $this->assertTrue(FieldStorageConfig::loadByName('entity_test', $field_storage->name));
     $instance_2->delete();
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->name));
     // Check that deletion of all instances of the same field simultaneously
     // deletes the field.
     $field_storage = entity_create('field_storage_config', $this->fieldStorageDefinition);
     $field_storage->save();
     $instance = entity_create('field_instance_config', $this->instanceDefinition);
     $instance->save();
     $instance_2 = entity_create('field_instance_config', $instance_definition_2);
     $instance_2->save();
     $this->container->get('entity.manager')->getStorage('field_instance_config')->delete(array($instance, $instance_2));
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->name));
 }
 /**
  * Tests creating a field override config for a bundle field.
  *
  * @see entity_test_entity_base_field_info_alter()
  */
 public function testFieldOverrideBundleField()
 {
     // First make sure the bundle field override in code, which is provided by
     // the test entity works.
     entity_test_create_bundle('some_test_bundle', 'Some test bundle', 'entity_test_field_override');
     $field_definitions = $this->entityManager->getFieldDefinitions('entity_test_field_override', 'entity_test_field_override');
     $this->assertEqual($field_definitions['name']->getDescription(), 'The default description.');
     $this->assertNull($field_definitions['name']->getTargetBundle());
     $field_definitions = $this->entityManager->getFieldDefinitions('entity_test_field_override', 'some_test_bundle');
     $this->assertEqual($field_definitions['name']->getDescription(), 'Custom description.');
     $this->assertEqual($field_definitions['name']->getTargetBundle(), 'some_test_bundle');
     // Now create a config override of the bundle field.
     $field_config = $field_definitions['name']->getConfig('some_test_bundle');
     $field_config->setTranslatable(FALSE);
     $field_config->save();
     // Make sure both overrides are present.
     $this->entityManager->clearCachedFieldDefinitions();
     $field_definitions = $this->entityManager->getFieldDefinitions('entity_test_field_override', 'some_test_bundle');
     $this->assertEqual($field_definitions['name']->getDescription(), 'Custom description.');
     $this->assertEqual($field_definitions['name']->getTargetBundle(), 'some_test_bundle');
     $this->assertFalse($field_definitions['name']->isTranslatable());
 }
 /**
  * Tests comment fields cannot be added to entity types without integer IDs.
  */
 public function testsNonIntegerIdEntities()
 {
     // Create a bundle for entity_test_string_id.
     entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
     $limited_user = $this->drupalCreateUser(array('administer entity_test_string_id fields'));
     $this->drupalLogin($limited_user);
     // Visit the Field UI field add page.
     $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
     // Ensure field isn't shown for string IDs.
     $this->assertNoOption('edit-new-storage-type', 'comment');
     // Ensure a core field type shown.
     $this->assertOption('edit-new-storage-type', 'boolean');
     // Create a bundle for entity_test_no_id.
     entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
     $this->drupalLogin($this->drupalCreateUser(array('administer entity_test_no_id fields')));
     // Visit the Field UI field add page.
     $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
     // Ensure field isn't shown for empty IDs.
     $this->assertNoOption('edit-new-storage-type', 'comment');
     // Ensure a core field type shown.
     $this->assertOption('edit-new-storage-type', 'boolean');
 }
 /**
  * Tests deleting field storages and fields as part of config import.
  */
 public function testImportDelete()
 {
     $this->installConfig(['field_test_config']);
     // At this point there are 5 field configuration objects in the active
     // storage.
     // - field.storage.entity_test.field_test_import
     // - field.storage.entity_test.field_test_import_2
     // - field.field.entity_test.entity_test.field_test_import
     // - field.field.entity_test.entity_test.field_test_import_2
     // - field.field.entity_test.test_bundle.field_test_import_2
     $field_name = 'field_test_import';
     $field_storage_id = "entity_test.{$field_name}";
     $field_name_2 = 'field_test_import_2';
     $field_storage_id_2 = "entity_test.{$field_name_2}";
     $field_id = "entity_test.entity_test.{$field_name}";
     $field_id_2a = "entity_test.entity_test.{$field_name_2}";
     $field_id_2b = "entity_test.test_bundle.{$field_name_2}";
     $field_storage_config_name = "field.storage.{$field_storage_id}";
     $field_storage_config_name_2 = "field.storage.{$field_storage_id_2}";
     $field_config_name = "field.field.{$field_id}";
     $field_config_name_2a = "field.field.{$field_id_2a}";
     $field_config_name_2b = "field.field.{$field_id_2b}";
     // Create a second bundle for the 'Entity test' entity type.
     entity_test_create_bundle('test_bundle');
     // Get the uuid's for the field storages.
     $field_storage_uuid = FieldStorageConfig::load($field_storage_id)->uuid();
     $field_storage_uuid_2 = FieldStorageConfig::load($field_storage_id_2)->uuid();
     $active = $this->container->get('config.storage');
     $staging = $this->container->get('config.storage.staging');
     $this->copyConfig($active, $staging);
     $this->assertTrue($staging->delete($field_storage_config_name), String::format('Deleted field storage: !field_storage', array('!field_storage' => $field_storage_config_name)));
     $this->assertTrue($staging->delete($field_storage_config_name_2), String::format('Deleted field storage: !field_storage', array('!field_storage' => $field_storage_config_name_2)));
     $this->assertTrue($staging->delete($field_config_name), String::format('Deleted field: !field', array('!field' => $field_config_name)));
     $this->assertTrue($staging->delete($field_config_name_2a), String::format('Deleted field: !field', array('!field' => $field_config_name_2a)));
     $this->assertTrue($staging->delete($field_config_name_2b), String::format('Deleted field: !field', array('!field' => $field_config_name_2b)));
     $deletes = $this->configImporter()->getUnprocessedConfiguration('delete');
     $this->assertEqual(count($deletes), 5, 'Importing configuration will delete 3 fields and 2 field storages.');
     // Import the content of the staging directory.
     $this->configImporter()->import();
     // Check that the field storages and fields are gone.
     \Drupal::entityManager()->getStorage('field_storage_config')->resetCache(array($field_storage_id));
     $field_storage = FieldStorageConfig::load($field_storage_id);
     $this->assertFalse($field_storage, 'The field storage was deleted.');
     \Drupal::entityManager()->getStorage('field_storage_config')->resetCache(array($field_storage_id_2));
     $field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
     $this->assertFalse($field_storage_2, 'The second field storage was deleted.');
     \Drupal::entityManager()->getStorage('field_config')->resetCache(array($field_id));
     $field = FieldConfig::load($field_id);
     $this->assertFalse($field, 'The field was deleted.');
     \Drupal::entityManager()->getStorage('field_config')->resetCache(array($field_id_2a));
     $field_2a = FieldConfig::load($field_id_2a);
     $this->assertFalse($field_2a, 'The second field on test bundle was deleted.');
     \Drupal::entityManager()->getStorage('field_config')->resetCache(array($field_id_2b));
     $field_2b = FieldConfig::load($field_id_2b);
     $this->assertFalse($field_2b, 'The second field on test bundle 2 was deleted.');
     // Check that all config files are gone.
     $active = $this->container->get('config.storage');
     $this->assertIdentical($active->listAll($field_storage_config_name), array());
     $this->assertIdentical($active->listAll($field_storage_config_name_2), array());
     $this->assertIdentical($active->listAll($field_config_name), array());
     $this->assertIdentical($active->listAll($field_config_name_2a), array());
     $this->assertIdentical($active->listAll($field_config_name_2b), array());
     // Check that the storage definition is preserved in state.
     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: array();
     $this->assertTrue(isset($deleted_storages[$field_storage_uuid]));
     $this->assertTrue(isset($deleted_storages[$field_storage_uuid_2]));
     // Purge field data, and check that the storage definition has been
     // completely removed once the data is purged.
     field_purge_batch(10);
     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: array();
     $this->assertTrue(empty($deleted_storages), 'Fields are deleted');
 }
 /**
  * Tests \Drupal\views\Plugin\views\field\Field::getValue
  */
 public function testGetValueMethod()
 {
     $bundle = 'test_bundle';
     entity_test_create_bundle($bundle);
     $field_multiple = FieldConfig::create(['field_name' => 'field_test_multiple', 'entity_type' => 'entity_test', 'bundle' => 'test_bundle']);
     $field_multiple->save();
     foreach ($this->entities as $entity) {
         $entity->delete();
     }
     $this->entities = [];
     $this->entities[] = $entity = EntityTest::create(['type' => 'entity_test', 'name' => 'test name', 'user_id' => $this->testUsers[0]->id()]);
     $entity->save();
     $this->entities[] = $entity = EntityTest::create(['type' => 'entity_test', 'name' => 'test name 2', 'user_id' => $this->testUsers[0]->id()]);
     $entity->save();
     $this->entities[] = $entity = EntityTest::create(['type' => $bundle, 'name' => 'test name 3', 'user_id' => $this->testUsers[0]->id(), 'field_test_multiple' => [1, 2, 3]]);
     $entity->save();
     $executable = Views::getView('test_field_field_test');
     $executable->execute();
     $field_normal = $executable->field['field_test'];
     $field_entity_reference = $executable->field['user_id'];
     $field_multi_cardinality = $executable->field['field_test_multiple'];
     $this->assertEquals($this->entities[0]->field_test->value, $field_normal->getValue($executable->result[0]));
     $this->assertEquals($this->entities[0]->user_id->target_id, $field_entity_reference->getValue($executable->result[0]));
     $this->assertEquals($this->entities[1]->field_test->value, $field_normal->getValue($executable->result[1]));
     $this->assertEquals($this->entities[1]->user_id->target_id, $field_entity_reference->getValue($executable->result[1]));
     $this->assertEquals([], $field_multi_cardinality->getValue($executable->result[0]));
     $this->assertEquals([], $field_multi_cardinality->getValue($executable->result[1]));
     $this->assertEquals([1, 2, 3], $field_multi_cardinality->getValue($executable->result[2]));
 }
 /**
  * Test entity_bundle_delete().
  */
 function testEntityDeleteBundle()
 {
     $entity_type = 'entity_test_rev';
     $this->createFieldWithInstance('', $entity_type);
     // Create a new bundle.
     $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName());
     entity_test_create_bundle($new_bundle, NULL, $entity_type);
     // Add an instance to that bundle.
     $this->instance_definition['bundle'] = $new_bundle;
     entity_create('field_instance_config', $this->instance_definition)->save();
     // Create a second field for the test bundle
     $field_name = drupal_strtolower($this->randomName() . '_field_name');
     $field_storage = array('name' => $field_name, 'entity_type' => $entity_type, 'type' => 'test_field', 'cardinality' => 1);
     entity_create('field_storage_config', $field_storage)->save();
     $instance = array('field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $this->instance->bundle, 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', 'weight' => mt_rand(0, 127));
     entity_create('field_instance_config', $instance)->save();
     // Save an entity with data for both fields
     $entity = entity_create($entity_type, array('type' => $this->instance->bundle));
     $values = $this->_generateTestFieldValues($this->field_storage->getCardinality());
     $entity->{$this->field_name} = $values;
     $entity->{$field_name} = $this->_generateTestFieldValues(1);
     $entity = $this->entitySaveReload($entity);
     // Verify the fields are present on load
     $this->assertEqual(count($entity->{$this->field_name}), 4, 'First field got loaded');
     $this->assertEqual(count($entity->{$field_name}), 1, 'Second field got loaded');
     // Delete the bundle.
     entity_test_delete_bundle($this->instance->bundle, $entity_type);
     // Verify no data gets loaded
     $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId());
     $controller->resetCache();
     $entity = $controller->load($entity->id());
     $this->assertTrue(empty($entity->{$this->field_name}), 'No data for first field');
     $this->assertTrue(empty($entity->{$field_name}), 'No data for second field');
     // Verify that the instances are gone.
     $this->assertFalse(entity_load('field_instance_config', 'entity_test.' . $this->instance->bundle . '.' . $this->field_name), "First field is deleted");
     $this->assertFalse(entity_load('field_instance_config', 'entity_test.' . $instance['bundle'] . '.' . $field_name), "Second field is deleted");
 }
 /**
  * Tests file listing usage page for entities with no canonical link template.
  */
 function testFileListingUsageNoLink()
 {
     // Login with user with right permissions and test listing.
     $this->drupalLogin($this->adminUser);
     // Create a bundle and attach a File field to the bundle.
     $bundle = $this->randomMachineName();
     entity_test_create_bundle($bundle, NULL, 'entity_test_constraints');
     $this->createFileField('field_test_file', 'entity_test_constraints', $bundle, array(), array('file_extensions' => 'txt png'));
     // Create file to attach to entity.
     $file = File::create(['filename' => 'druplicon.txt', 'uri' => 'public://druplicon.txt', 'filemime' => 'text/plain']);
     $file->setPermanent();
     file_put_contents($file->getFileUri(), 'hello world');
     $file->save();
     // Create entity and attach the created file.
     $entity_name = $this->randomMachineName();
     $entity = EntityTestConstraints::create(array('uid' => 1, 'name' => $entity_name, 'type' => $bundle, 'field_test_file' => array('target_id' => $file->id())));
     $entity->save();
     // Create node entity and attach the created file.
     $node = $this->drupalCreateNode(array('type' => 'article', 'file' => $file));
     $node->save();
     // Load the file usage page for the created and attached file.
     $this->drupalGet('admin/content/files/usage/' . $file->id());
     $this->assertResponse(200);
     // Entity name should be displayed, but not linked if Entity::toUrl
     // throws an exception
     $this->assertText($entity_name, 'Entity name is added to file usage listing.');
     $this->assertNoLink($entity_name, 'Linked entity name not added to file usage listing.');
     $this->assertLink($node->getTitle());
 }