Ejemplo n.º 1
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');
 }
Ejemplo n.º 2
0
 /**
  * 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());
 }
Ejemplo n.º 3
0
 /**
  * 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");
 }
Ejemplo n.º 4
0
 /**
  * Tests making use of a custom bundle field.
  */
 public function testCustomBundleFieldUsage()
 {
     // Check that an entity with bundle entity_test does not have the custom
     // field.
     $this->moduleHandler->install(array('entity_bundle_field_test'), FALSE);
     $storage = $this->entityManager->getStorage('entity_test');
     $entity = $storage->create(['type' => 'entity_test']);
     $this->assertFalse($entity->hasField('custom_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_field'));
     $entity->custom_field->value = 'swanky';
     $entity->save();
     $storage->resetCache();
     $entity = $storage->load($entity->id());
     $this->assertEqual($entity->custom_field->value, 'swanky', 'Entity was saved correct.y');
     $entity->custom_field->value = 'cozy';
     $entity->save();
     $storage->resetCache();
     $entity = $storage->load($entity->id());
     $this->assertEqual($entity->custom_field->value, 'cozy', 'Entity was updated correctly.');
     $entity->delete();
     $table = $storage->_fieldTableName($entity->getFieldDefinition('custom_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_field' => 'new']);
     $entity->save();
     entity_test_delete_bundle('custom');
     $table = $storage->_fieldTableName($entity->getFieldDefinition('custom_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');
     // @todo Test field purge and table deletion once supported.
     // $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted');
 }
Ejemplo n.º 5
0
 /**
  * Tests that deletions of custom bundles are mirrored in field settings.
  */
 public function testCustomTargetBundleDeletion()
 {
     // Attach an entity reference field to $this->nodeType.
     $name = Unicode::strtolower($this->randomMachineName());
     $label = $this->randomString();
     $handler_settings = ['target_bundles' => [$this->customBundle => $this->customBundle]];
     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'entity_test', 'default', $handler_settings);
     // Check that the 'target_bundle' setting contains the custom bundle.
     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
     $actual_handler_settings = $field_config->getSetting('handler_settings');
     $this->assertEqual($handler_settings, $actual_handler_settings);
     // Delete the custom bundle.
     entity_test_delete_bundle($this->customBundle, 'entity_test');
     // Check that the deleted bundle is no longer present in the
     // 'target_bundles' field setting.
     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
     $handler_settings = $field_config->getSetting('handler_settings');
     $this->assertTrue(empty($handler_settings['target_bundles']));
 }