1.20.x field_ui.test FieldUIManageFieldsTestCase::testDeleteField()

Tests that deletion removes fields and instances as expected.

File

modules/field_ui/tests/field_ui.test, line 421
Tests for field_ui.module.

Class

FieldUIManageFieldsTestCase
Tests the functionality of the 'Manage fields' screen.

Code

function testDeleteField() {
  // Create a new field.
  $bundle_path1 = 'admin/structure/types/manage/' . $this->hyphen_type;
  $edit1 = array(
    'fields[_add_new_field][label]' => $this->field_label,
    'fields[_add_new_field][field_name]' => $this->field_name_input,
  );
  $this->fieldUIAddNewField($bundle_path1, $edit1);

  // Create an additional node type.
  $type_name2 = strtolower($this->randomName(8)) . '_test';
  $type2 = $this->backdropCreateContentType(array('name' => $type_name2, 'type' => $type_name2));
  $type_name2 = $type2->type;
  $hyphen_type2 = str_replace('_', '-', $type_name2);

  // Add an instance to the second node type.
  $bundle_path2 = 'admin/structure/types/manage/' . $hyphen_type2;
  $edit2 = array(
    'fields[_add_existing_field][label]' => $this->field_label,
    'fields[_add_existing_field][field_name]' => $this->field_name,
  );
  $this->fieldUIAddExistingField($bundle_path2, $edit2);

  // Delete the first instance.
  $this->fieldUIDeleteField($bundle_path1, $this->field_name, $this->field_label, $this->type);

  // Reset the fields info.
  _field_info_collate_fields_reset();
  // Check that the field instance was deleted.
  $this->assertNull(field_info_instance('node', $this->field_name, $this->type), 'Field instance was deleted.');
  // Check that the field was not deleted
  $this->assertNotNull(field_info_field($this->field_name), 'Field was not deleted.');

  // Delete the second instance.
  $this->fieldUIDeleteField($bundle_path2, $this->field_name, $this->field_label, $type_name2);

  // Reset the fields info.
  _field_info_collate_fields_reset();
  // Check that the field instance was deleted.
  $this->assertNull(field_info_instance('node', $this->field_name, $type_name2), 'Field instance was deleted.');
  // Check that the field was deleted too.
  $this->assertNull(field_info_field($this->field_name), 'Field was deleted.');
}