Commit a6a4adc9 authored by alexpott's avatar alexpott

Issue #1981316 by aspilicious, andypost, pcambra, swentel: Drop procedural...

Issue #1981316 by aspilicious, andypost, pcambra, swentel: Drop procedural usage of fields in [f-l] modules.
parent db0e9117
......@@ -67,14 +67,15 @@ function getLastFileId() {
* A list of widget settings that will be added to the widget defaults.
*/
function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
$field = array(
$field_definition = array(
'field_name' => $name,
'type' => 'file',
'settings' => array(),
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
);
$field['settings'] = array_merge($field['settings'], $field_settings);
$field = field_create_field($field);
$field_definition['settings'] = array_merge($field_definition['settings'], $field_settings);
$field = entity_create('field_entity', $field_definition);
$field->save();
$this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
return $field;
......@@ -106,7 +107,7 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra
'settings' => array(),
);
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
field_create_instance($instance);
entity_create('field_instance', $instance)->save();
entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($name, array(
......@@ -123,7 +124,7 @@ function updateFileField($name, $type_name, $instance_settings = array(), $widge
$instance = field_info_instance('node', $name, $type_name);
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
field_update_instance($instance);
$instance->save();
entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($instance['field_name'], array(
......@@ -238,4 +239,5 @@ function assertFileIsPermanent(FileInterface $file, $message = NULL) {
$message = isset($message) ? $message : t('File %file is permanent.', array('%file' => $file->getFileUri()));
$this->assertTrue($file->isPermanent(), $message);
}
}
......@@ -52,7 +52,7 @@ function testRequired() {
$this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.'));
// Try again with a multiple value field.
field_delete_field($field_name);
$field->delete();
$this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));
// Try to post a new node without uploading a file in the multivalue field.
......@@ -153,4 +153,5 @@ function testFileExtension() {
$this->assertFileExists($node_file, t('File exists after uploading a file with extension checking.'));
$this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.'));
}
}
......@@ -44,18 +44,16 @@ public function setUp() {
$this->installSchema('file', 'file_managed');
$this->installSchema('file', 'file_usage');
$field = array(
entity_create('field_entity', array(
'field_name' => 'file_test',
'type' => 'file',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
$instance = array(
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'file_test',
'bundle' => 'entity_test',
);
field_create_instance($instance);
))->save();
file_put_contents('public://example.txt', $this->randomName());
$this->file = entity_create('file', array(
'uri' => 'public://example.txt',
......
......@@ -53,7 +53,7 @@ function forum_enable() {
// is being enabled at the same time as taxonomy after both modules have been
// enabled, the field might exist but still be marked inactive.
if (!field_read_field('taxonomy_forums', array('include_inactive' => TRUE))) {
$field = array(
entity_create('field_entity', array(
'field_name' => 'taxonomy_forums',
'type' => 'taxonomy_term_reference',
'settings' => array(
......@@ -64,8 +64,7 @@ function forum_enable() {
),
),
),
);
field_create_field($field);
))->save();
// Create a default forum so forum posts can be created.
$term = entity_create('taxonomy_term', array(
......@@ -78,14 +77,13 @@ function forum_enable() {
$term->save();
// Create the instance on the bundle.
$instance = array(
entity_create('field_instance', array(
'field_name' => 'taxonomy_forums',
'entity_type' => 'node',
'label' => $vocabulary->name,
'bundle' => 'forum',
'required' => TRUE,
);
field_create_instance($instance);
))->save();
// Assign form display settings for the 'default' form mode.
entity_get_form_display('node', 'forum', 'default')
......@@ -124,7 +122,7 @@ function forum_uninstall() {
variable_del('node_options_forum');
field_delete_field('taxonomy_forums');
field_info_field('taxonomy_forums')->delete();
// Purge field data now to allow taxonomy module to be uninstalled
// if this is the only field remaining.
field_purge_batch(10);
......
......@@ -78,49 +78,43 @@ function setUp() {
language_save($german);
// Create the test text field.
$field = array(
entity_create('field_entity', array(
'field_name' => 'field_test_text',
'type' => 'text',
'translatable' => FALSE,
);
field_create_field($field);
$instance = array(
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test_text',
'bundle' => 'entity_test',
);
field_create_instance($instance);
))->save();
// Create the test translatable field.
$field = array(
entity_create('field_entity', array(
'field_name' => 'field_test_translatable_text',
'type' => 'text',
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test_translatable_text',
'bundle' => 'entity_test',
);
field_create_instance($instance);
))->save();
// Create the test entity reference field.
$field = array(
entity_create('field_entity', array(
'translatable' => TRUE,
'settings' => array(
'target_type' => 'entity_test',
),
'field_name' => 'field_test_entity_reference',
'type' => 'entity_reference',
);
field_create_field($field);
$instance = array(
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test_entity_reference',
'bundle' => 'entity_test',
);
field_create_instance($instance);
))->save();
// Set up the mock serializer.
$normalizers = array(
......
......@@ -51,30 +51,28 @@ function testDefaultImages() {
$widget_settings = array(
'preview_image_style' => 'medium',
);
$this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
$instance = $this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, 'article');
// Add another instance with another default image to the page content type.
$instance2 = array(
'field_name' => $field['field_name'],
$instance2 = entity_create('field_instance', array(
'field_name' => $field->id(),
'entity_type' => 'node',
'bundle' => 'page',
'label' => $instance['label'],
'required' => $instance['required'],
'label' => $instance->label(),
'required' => $instance->required,
'settings' => array(
'default_image' => $default_images['instance2']->id(),
),
);
field_create_instance($instance2);
$instance2 = field_info_instance('node', $field_name, 'page');
));
$instance2->save();
$widget_settings = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($field['field_name']);
entity_get_form_display('node', 'page', 'default')
->setComponent($field['field_name'], $widget_settings)
->setComponent($field->id(), $widget_settings)
->save();
entity_get_display('node', 'page', 'default')
->setComponent($field['field_name'])
->setComponent($field->id())
->save();
// Confirm the defaults are present on the article field settings form.
......@@ -145,7 +143,7 @@ function testDefaultImages() {
// Upload a new default for the field.
$field['settings']['default_image'] = array($default_images['field_new']->id());
field_update_field($field);
$field->save();
// Confirm that the new default is used on the article field settings form.
$this->drupalGet("admin/structure/types/manage/article/fields/$instance->id/field");
......@@ -180,7 +178,7 @@ function testDefaultImages() {
// Upload a new default for the article's field instance.
$instance['settings']['default_image'] = $default_images['instance_new']->id();
field_update_instance($instance);
$instance->save();
// Confirm the new field instance default is used on the article field
// admin form.
......@@ -219,7 +217,7 @@ function testDefaultImages() {
// Remove the instance default from articles.
$instance['settings']['default_image'] = 0;
field_update_instance($instance);
$instance->save();
// Confirm the article field instance default has been removed.
$this->drupalGet("admin/structure/types/manage/article/fields/$instance->id");
......@@ -251,4 +249,5 @@ function testDefaultImages() {
)
);
}
}
......@@ -76,7 +76,7 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
);
$field['settings'] = array_merge($field['settings'], $field_settings);
field_create_field($field);
entity_create('field_entity', $field)->save();
$instance = array(
'field_name' => $field['field_name'],
......@@ -88,7 +88,8 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc
'settings' => array(),
);
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
$field_instance = field_create_instance($instance);
$field_instance = entity_create('field_instance', $instance);
$field_instance->save();
entity_get_form_display('node', $type_name, 'default')
->setComponent($field['field_name'], array(
......@@ -127,4 +128,5 @@ function uploadNodeImage($image, $field_name, $type) {
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
return isset($matches[1]) ? $matches[1] : FALSE;
}
}
......@@ -43,18 +43,16 @@ public function setUp() {
$this->installSchema('file', array('file_managed', 'file_usage'));
$field = array(
entity_create('field_entity', array(
'field_name' => 'image_test',
'type' => 'image',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
$instance = array(
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'bundle' => 'entity_test',
);
field_create_instance($instance);
))->save();
file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = entity_create('file', array(
'uri' => 'public://example.jpg',
......
......@@ -22,6 +22,27 @@ class LinkFieldTest extends WebTestBase {
*/
public static $modules = array('entity_test', 'link');
/**
* A field to use in this test class.
*
* @var \Drupal\field\Plugin\Core\Entity\Field
*/
protected $field;
/**
* The instance used in this test class.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
/**
* A user with permission to view and manage test entities.
*
* @var object
*/
protected $web_user;
public static function getInfo() {
return array(
'name' => 'Link field',
......@@ -44,23 +65,23 @@ function setUp() {
* Tests link field URL validation.
*/
function testURLValidation() {
$field_name = drupal_strtolower($this->randomName());
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
$this->field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
));
$this->field->save();
entity_create('field_instance', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_DISABLED,
),
);
field_create_instance($this->instance);
))->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
->setComponent($field_name, array(
'type' => 'link_default',
'settings' => array(
'placeholder_url' => 'http://example.com',
......@@ -68,7 +89,7 @@ function testURLValidation() {
))
->save();
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
->setComponent($field_name, array(
'type' => 'link',
))
->save();
......@@ -77,7 +98,7 @@ function testURLValidation() {
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'Link URL field is displayed');
$this->assertFieldByName("{$field_name}[$langcode][0][url]", '', 'Link URL field is displayed');
$this->assertRaw('placeholder="http://example.com"');
// Verify that a valid URL can be submitted.
......@@ -85,7 +106,7 @@ function testURLValidation() {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $value,
"{$field_name}[$langcode][0][url]" => $value,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
......@@ -107,7 +128,7 @@ function testURLValidation() {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $invalid_value,
"{$field_name}[$langcode][0][url]" => $invalid_value,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The URL @url is not valid.', array('@url' => $invalid_value)));
......@@ -118,24 +139,25 @@ function testURLValidation() {
* Tests the link title settings of a link field.
*/
function testLinkTitle() {
$field_name = drupal_strtolower($this->randomName());
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
$this->field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'link',
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
));
$this->field->save();
$this->instance = entity_create('field_instance', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Read more about this entity',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
);
field_create_instance($this->instance);
));
$this->instance->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
->setComponent($field_name, array(
'type' => 'link_default',
'settings' => array(
'placeholder_url' => 'http://example.com',
......@@ -144,7 +166,7 @@ function testLinkTitle() {
))
->save();
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
->setComponent($field_name, array(
'type' => 'link',
'label' => 'hidden',
))
......@@ -155,35 +177,35 @@ function testLinkTitle() {
// Verify that the link text field works according to the field setting.
foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) {
// Update the link title field setting.
$this->instance['settings']['title'] = $title_setting;
field_update_instance($this->instance);
$this->instance->settings['title'] = $title_setting;
$this->instance->save();
// Display creation form.
$this->drupalGet('entity_test/add');
// Assert label is shown.
$this->assertText('Read more about this entity');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][url]", '', 'URL field found.');
$this->assertFieldByName("{$field_name}[$langcode][0][url]", '', 'URL field found.');
$this->assertRaw('placeholder="http://example.com"');
if ($title_setting === DRUPAL_DISABLED) {
$this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Link text field not found.');
$this->assertNoFieldByName("{$field_name}[$langcode][0][title]", '', 'Link text field not found.');
$this->assertNoRaw('placeholder="Enter the text for this link"');
}
else {
$this->assertRaw('placeholder="Enter the text for this link"');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][title]", '', 'Link text field found.');
$this->assertFieldByName("{$field_name}[$langcode][0][title]", '', 'Link text field found.');
if ($title_setting === DRUPAL_REQUIRED) {
// Verify that the link text is required, if the URL is non-empty.
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com',
"{$field_name}[$langcode][0][url]" => 'http://www.example.com',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('!name field is required.', array('!name' => t('Link text'))));
// Verify that the link text is not required, if the URL is empty.
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => '',
"{$field_name}[$langcode][0][url]" => '',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('!name field is required.', array('!name' => t('Link text'))));
......@@ -191,8 +213,8 @@ function testLinkTitle() {
// Verify that a URL and link text meets requirements.
$this->drupalGet('entity_test/add');
$edit = array(
"{$this->field['field_name']}[$langcode][0][url]" => 'http://www.example.com',
"{$this->field['field_name']}[$langcode][0][title]" => 'Example',
"{$field_name}[$langcode][0][url]" => 'http://www.example.com',
"{$field_name}[$langcode][0][title]" => 'Example',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('!name field is required.', array('!name' => t('Link text'))));
......@@ -205,8 +227,8 @@ function testLinkTitle() {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $value,
"{$this->field['field_name']}[$langcode][0][title]" => '',
"{$field_name}[$langcode][0][url]" => $value,
"{$field_name}[$langcode][0][title]" => '',
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
......@@ -222,7 +244,7 @@ function testLinkTitle() {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][title]" => $title,
"{$field_name}[$langcode][0][title]" => $title,
);
$this->drupalPost("entity_test/manage/$id/edit", $edit, t('Save'));
$this->assertText(t('entity_test @id has been updated.', array('@id' => $id)));
......@@ -236,34 +258,34 @@ function testLinkTitle() {
* Tests the default 'link' formatter.
*/
function testLinkFormatter() {
$field_name = drupal_strtolower($this->randomName());
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
$this->field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'link',
'cardinality' => 2,
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
));
$this->field->save();
entity_create('field_instance', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'label' => 'Read more about this entity',
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
);
$display_options = array(
'type' => 'link',
'label' => 'hidden',
);
field_create_instance($this->instance);
))->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
->setComponent($field_name, array(
'type' => 'link_default',
))
->save();
$display_options = array(
'type' => 'link',
'label' => 'hidden',
);
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], $display_options)
->setComponent($field_name, $display_options)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
......@@ -282,11 +304,11 @@ function testLinkFormatter() {
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][url]" => $url1,
"{$field_name}[$langcode][0][url]" => $url1,
// Note that $title1 is not submitted.
"{$this->field['field_name']}[$langcode][0][title]" => '',
"{$this->field['field_name']}[$langcode][1][url]" => $url2,
"{$this->field['field_name']}[$langcode][1][title]" => $title2,
"{$field_name}[$langcode][0][title]" => '',
"{$field_name}[$langcode][1][url]" => $url2,
"{$field_name}[$langcode][1][title]" => $title2,
);
// Assert label is shown.
$this->assertText('Read more about this entity');
......@@ -321,7 +343,7 @@ function testLinkFormatter() {
$display_options['settings'] = $new_value;
}
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], $display_options)
->setComponent($field_name, $display_options)
->save();
$this->renderTestEntity($id);
......@@ -379,33 +401,33 @@ function testLinkFormatter() {
* merged, since they involve different configuration and output.
*/
function testLinkSeparateFormatter() {
$field_name = drupal_strtolower($this->randomName());
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
$this->field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'link',
'cardinality' => 2,
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
));
$this->field->save();
entity_create('field_instance', array(
'field_name' => $field_name,