Commit 0df31af2 authored by alexpott's avatar alexpott

Issue #2384357 by amateescu: Simplify Field UI testing

parent 43e265b9
......@@ -11,6 +11,7 @@
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -21,6 +22,8 @@
*/
class CommentNonNodeTest extends WebTestBase {
use FieldUiTestTrait;
public static $modules = array('comment', 'user', 'field_ui', 'entity_test');
/**
......@@ -367,34 +370,13 @@ function testCommentFunctionality() {
$bundle->save();
// Add a new comment field.
$this->drupalGet('entity_test/structure/entity_test/fields');
$edit = array(
'fields[_add_new_field][label]' => 'Foobar',
'fields[_add_new_field][field_name]' => 'foobar',
'fields[_add_new_field][type]' => 'comment',
);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm(NULL, array(
$storage_edit = array(
'field_storage[settings][comment_type]' => 'foobar',
), t('Save field settings'));
$this->drupalPostForm(NULL, array(), t('Save settings'));
$this->assertRaw(t('Saved %name configuration', array('%name' => 'Foobar')));
);
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
// Add a third comment field.
$this->drupalGet('entity_test/structure/entity_test/fields');
$edit = array(
'fields[_add_new_field][label]' => 'Barfoo',
'fields[_add_new_field][field_name]' => 'barfoo',
'fields[_add_new_field][type]' => 'comment',
);
$this->drupalPostForm(NULL, $edit, t('Save'));
// Re-use another comment type.
$this->drupalPostForm(NULL, array(
'field_storage[settings][comment_type]' => 'foobar',
), t('Save field settings'));
$this->drupalPostForm(NULL, array(), t('Save settings'));
$this->assertRaw(t('Saved %name configuration', array('%name' => 'Barfoo')));
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);
// Check the field contains the correct comment type.
$field_storage = entity_load('field_storage_config', 'entity_test.field_barfoo');
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\Unicode;
use Drupal\contact\Entity\ContactForm;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Entity\EntityTypeInterface;
......@@ -20,6 +21,8 @@
*/
class ContactSitewideTest extends WebTestBase {
use FieldUiTestTrait;
/**
* Set to TRUE to strict check all configuration saved.
*
......@@ -255,15 +258,10 @@ function testSiteWideContact() {
$this->assertResponse(200);
// Create a simple textfield.
$edit = array(
'fields[_add_new_field][label]' => $field_label = $this->randomMachineName(),
'fields[_add_new_field][field_name]' => Unicode::strtolower($this->randomMachineName()),
'fields[_add_new_field][type]' => 'text',
);
$field_name = 'field_' . $edit['fields[_add_new_field][field_name]'];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm(NULL, array(), t('Save field settings'));
$this->drupalPostForm(NULL, array(), t('Save settings'));
$field_name = Unicode::strtolower($this->randomMachineName());
$field_label = $this->randomMachineName();
$this->fieldUIAddNewField(NULL, $field_name, $field_label, 'text');
$field_name = 'field_' . $field_name;
// Check that the field is displayed.
$this->drupalGet('contact/' . $contact_form);
......
......@@ -2,77 +2,51 @@
/**
* @file
* Contains \Drupal\field_ui\Tests\FieldUiTestBase.
* Contains \Drupal\field_ui\Tests\FieldUiTestTrait.
*/
namespace Drupal\field_ui\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
/**
* Provides common functionality for the Field UI test classes.
*/
abstract class FieldUiTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'field_ui', 'field_test', 'taxonomy', 'image');
protected function setUp() {
parent::setUp();
// Create test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer taxonomy', 'administer taxonomy_term fields', 'administer taxonomy_term display', 'administer users', 'administer account settings', 'administer user display', 'bypass node access'));
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomMachineName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
// Create a default vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => Unicode::strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'help' => '',
'nodes' => array('article' => 'article'),
'weight' => mt_rand(0, 10),
));
$vocabulary->save();
$this->vocabulary = $vocabulary->id();
}
trait FieldUiTestTrait {
/**
* Creates a new field through the Field UI.
*
* @param $bundle_path
* @param string $bundle_path
* Admin path of the bundle that the new field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPostForm() on the first step ('Manage fields'
* screen).
* @param $storage_edit
* $edit parameter for drupalPostForm() on the second step ('Storage
* @param string $field_name
* The field name of the new field storage.
* @param string $label
* (optional) The label of the new field. Defaults to a random string.
* @param string $field_type
* (optional) The field type of the new field storage. Defaults to
* 'test_field'.
* @param array $storage_edit
* (optional) $edit parameter for drupalPostForm() on the second step
* ('Storage settings' form).
* @param array $field_edit
* (optional) $edit parameter for drupalPostForm() on the third step ('Field
* settings' form).
* @param $field_edit
* $edit parameter for drupalPostForm() on the third step ('Field settings'
* form).
*/
function fieldUIAddNewField($bundle_path, $initial_edit, $storage_edit = array(), $field_edit = array()) {
// Use 'test_field' field type by default.
$initial_edit += array(
'fields[_add_new_field][type]' => 'test_field',
public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = array(), array $field_edit = array()) {
$label = $label ?: $this->randomString();
$initial_edit = array(
'fields[_add_new_field][field_name]' => $field_name,
'fields[_add_new_field][type]' => $field_type,
'fields[_add_new_field][label]' => $label,
);
$label = $initial_edit['fields[_add_new_field][label]'];
// Allow the caller to set a NULL path in case they navigated to the right
// page before calling this method.
if ($bundle_path !== NULL) {
$bundle_path = "$bundle_path/fields";
}
// First step : 'Add new field' on the 'Manage fields' page.
$this->drupalPostForm("$bundle_path/fields", $initial_edit, t('Save'));
$this->drupalPostForm($bundle_path, $initial_edit, t('Save'));
$this->assertRaw(t('These settings apply to the %label field everywhere it is used.', array('%label' => $label)), 'Storage settings page was displayed.');
// Test Breadcrumbs.
$this->assertLink($label, 0, 'Field label is correct in the breadcrumb of the storage settings page.');
......@@ -92,17 +66,23 @@ function fieldUIAddNewField($bundle_path, $initial_edit, $storage_edit = array()
/**
* Adds an existing field through the Field UI.
*
* @param $bundle_path
* @param string $bundle_path
* Admin path of the bundle that the field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPostForm() on the first step ('Manage fields'
* screen).
* @param $field_edit
* $edit parameter for drupalPostForm() on the second step ('Field settings'
* form).
* @param string $existing_field_name
* The name of the existing field storage for which we want to add a new
* field.
* @param string $label
* (optional) The label of the new field. Defaults to a random string.
* @param array $field_edit
* (optional) $edit parameter for drupalPostForm() on the second step
* ('Field settings' form).
*/
function fieldUIAddExistingField($bundle_path, $initial_edit, $field_edit = array()) {
$label = $initial_edit['fields[_add_existing_field][label]'];
public function fieldUIAddExistingField($bundle_path, $existing_field_name, $label = NULL, array $field_edit = array()) {
$label = $label ?: $this->randomString();
$initial_edit = array(
'fields[_add_existing_field][label]' => $label,
'fields[_add_existing_field][field_name]' => $existing_field_name,
);
// First step : 'Re-use existing field' on the 'Manage fields' page.
$this->drupalPostForm("$bundle_path/fields", $initial_edit, t('Save'));
......@@ -119,16 +99,16 @@ function fieldUIAddExistingField($bundle_path, $initial_edit, $field_edit = arra
/**
* Deletes a field through the Field UI.
*
* @param $bundle_path
* @param string $bundle_path
* Admin path of the bundle that the field is to be deleted from.
* @param $field_name
* @param string $field_name
* The name of the field.
* @param $label
* @param string $label
* The label of the field.
* @param $bundle_label
* @param string $bundle_label
* The label of the bundle.
*/
function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
// Display confirmation form.
$this->drupalGet("$bundle_path/fields/$field_name/delete");
$this->assertRaw(t('Are you sure you want to delete the field %label', array('%label' => $label)), 'Delete confirmation was found.');
......@@ -143,4 +123,5 @@ function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
// Check that the field does not appear in the overview form.
$this->assertNoFieldByXPath('//table[@id="field-overview"]//span[@class="label-field"]', $label, 'Field does not appear in the overview page.');
}
}
......@@ -7,24 +7,57 @@
namespace Drupal\field_ui\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
/**
* Tests the Field UI "Manage display" and "Manage form display" screens.
*
* @group field_ui
*/
class ManageDisplayTest extends FieldUiTestBase {
class ManageDisplayTest extends WebTestBase {
use SchemaCheckTestTrait;
use FieldUiTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('search', 'field_test', 'field_third_party_test');
public static $modules = array('node', 'field_ui', 'taxonomy', 'search', 'field_test', 'field_third_party_test');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer taxonomy', 'administer taxonomy_term fields', 'administer taxonomy_term display', 'administer users', 'administer account settings', 'administer user display', 'bypass node access'));
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomMachineName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
// Create a default vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => Unicode::strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'help' => '',
'nodes' => array('article' => 'article'),
'weight' => mt_rand(0, 10),
));
$vocabulary->save();
$this->vocabulary = $vocabulary->id();
}
/**
* Tests formatter settings.
......@@ -34,11 +67,7 @@ function testFormatterUI() {
$manage_display = $manage_fields . '/display';
// Create a field, and a node with some data for the field.
$edit = array(
'fields[_add_new_field][label]' => 'Test field',
'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField($manage_fields, $edit);
$this->fieldUIAddNewField($manage_fields, 'test', 'Test field');
// Get the display options (formatter and settings) that were automatically
// assigned for the 'default' display.
......@@ -152,11 +181,7 @@ public function testWidgetUI() {
// Creates a new field that can be used with multiple formatters.
// Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable().
$edit = array(
'fields[_add_new_field][label]' => 'Test field',
'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField($manage_fields, $edit);
$this->fieldUIAddNewField($manage_fields, 'test', 'Test field');
// Get the display options (formatter and settings) that were automatically
// assigned for the 'default' display.
......@@ -231,11 +256,7 @@ public function testWidgetUI() {
// Creates a new field that can not be used with the multiple formatter.
// Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable().
$edit = array(
'fields[_add_new_field][label]' => 'One Widget Field',
'fields[_add_new_field][field_name]' => 'onewidgetfield',
);
$this->fieldUIAddNewField($manage_fields, $edit);
$this->fieldUIAddNewField($manage_fields, 'onewidgetfield', 'One Widget Field');
// Go to the Manage Form Display.
$this->drupalGet($manage_display);
......@@ -250,11 +271,7 @@ public function testWidgetUI() {
*/
function testViewModeCustom() {
// Create a field, and a node with some data for the field.
$edit = array(
'fields[_add_new_field][label]' => 'Test field',
'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $edit);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test field');
\Drupal::entityManager()->clearCachedFieldDefinitions();
// For this test, use a formatter setting value that is an integer unlikely
// to appear in a rendered node other than as part of the field being tested
......@@ -336,11 +353,7 @@ public function testViewModeLocalTasks() {
*/
function testNonInitializedFields() {
// Create a test field.
$edit = array(
'fields[_add_new_field][label]' => 'Test',
'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $edit);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test');
// Check that the field appears as 'hidden' on the 'Manage display' page
// for the 'teaser' mode.
......
......@@ -12,17 +12,39 @@
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
/**
* Tests the Field UI "Manage fields" screen.
*
* @group field_ui
*/
class ManageFieldsTest extends FieldUiTestBase {
class ManageFieldsTest extends WebTestBase {
use FieldUiTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'field_ui', 'field_test', 'taxonomy', 'image');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer taxonomy', 'administer taxonomy_term fields', 'administer taxonomy_term display', 'administer users', 'administer account settings', 'administer user display', 'bypass node access'));
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomMachineName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
// Create random field name.
$this->field_label = $this->randomMachineName(8);
$this->field_name_input = strtolower($this->randomMachineName(8));
......@@ -118,11 +140,7 @@ function manageFieldsPage($type = '') {
*/
function createField() {
// Create a test field.
$edit = array(
'fields[_add_new_field][label]' => $this->field_label,
'fields[_add_new_field][field_name]' => $this->field_name_input,
);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $edit);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $this->field_name_input, $this->field_label);
}
/**
......@@ -168,11 +186,7 @@ function addExistingField() {
$this->assertFalse($this->xpath('//select[@id="edit-add-existing-field-field-name"]//option[@value="comment"]'), 'The list of options respects entity type restrictions.');
// Add a new field based on an existing field.
$edit = array(
'fields[_add_existing_field][label]' => $this->field_label . '_2',
'fields[_add_existing_field][field_name]' => $this->field_name,
);
$this->fieldUIAddExistingField("admin/structure/types/manage/page", $edit);
$this->fieldUIAddExistingField("admin/structure/types/manage/page", $this->field_name, $this->field_label . '_2');
}
/**
......@@ -243,11 +257,7 @@ protected function addPersistentFieldStorage() {
$this->drupalGet('admin/structure/types/manage/page/fields');
$this->assertRaw(t('Re-use existing field'), '"Re-use existing field" was found.');
// Add a new field for the orphaned storage.
$edit = array(
'fields[_add_existing_field][label]' => $this->randomMachineName(),
'fields[_add_existing_field][field_name]' => $this->field_name,
);
$this->fieldUIAddExistingField("admin/structure/types/manage/page", $edit);
$this->fieldUIAddExistingField("admin/structure/types/manage/page", $this->field_name);
}
/**
......@@ -293,11 +303,7 @@ function testFieldPrefix() {
$this->assertText('New field name cannot be longer than 22 characters but is currently 23 characters long.');
// Create a valid field.
$edit = array(
'fields[_add_new_field][label]' => $this->field_label,
'fields[_add_new_field][field_name]' => $this->field_name_input,
);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $edit);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $this->field_name_input, $this->field_label);
$this->drupalGet('admin/structure/types/manage/' . $this->type . '/fields/node.' . $this->type . '.' . $field_prefix . $this->field_name_input);
$this->assertText(format_string('@label settings for @type', array('@label' => $this->field_label, '@type' => $this->type)));
}
......@@ -366,11 +372,7 @@ function testDefaultValue() {
function testDeleteField() {
// Create a new field.
$bundle_path1 = 'admin/structure/types/manage/' . $this->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);
$this->fieldUIAddNewField($bundle_path1, $this->field_name_input, $this->field_label);
// Create an additional node type.
$type_name2 = strtolower($this->randomMachineName(8)) . '_test';
......@@ -379,11 +381,7 @@ function testDeleteField() {
// Add a field to the second node type.
$bundle_path2 = 'admin/structure/types/manage/' . $type_name2;
$edit2 = array(
'fields[_add_existing_field][label]' => $this->field_label,
'fields[_add_existing_field][field_name]' => $this->field_name,
);
$this->fieldUIAddExistingField($bundle_path2, $edit2);
$this->fieldUIAddExistingField($bundle_path2, $this->field_name, $this->field_label);
// Delete the first field.
$this->fieldUIDeleteField($bundle_path1, "node.$this->type.$this->field_name", $this->field_label, $this->type);
......@@ -554,11 +552,8 @@ function testDuplicateFieldName() {
function testDeleteTaxonomyField() {
// Create a new field.
$bundle_path = 'admin/structure/taxonomy/manage/tags/overview';
$edit1 = array(
'fields[_add_new_field][label]' => $this->field_label,
'fields[_add_new_field][field_name]' => $this->field_name_input,
);
$this->fieldUIAddNewField($bundle_path, $edit1);
$this->fieldUIAddNewField($bundle_path, $this->field_name_input, $this->field_label);
// Delete the field.
$this->fieldUIDeleteField($bundle_path, "taxonomy_term.tags.$this->field_name", $this->field_label, 'Tags');
......
......@@ -9,6 +9,7 @@
use Drupal\comment\Entity\Comment;
use Drupal\field\Entity\FieldConfig;
use Drupal\field_ui\Tests\FieldUiTestTrait;
/**
* Tests the file field widget, single and multi-valued, with and without AJAX,
......@@ -18,6 +19,8 @@
*/
class FileFieldWidgetTest extends FileFieldTestBase {
use FieldUiTestTrait;
/**
* Modules to enable.
*
......@@ -257,15 +260,11 @@ function testPrivateFileComment() {
// Create a new field.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
$edit = array(
'fields[_add_new_field][label]' => $label = $this->randomMachineName(),
'fields[_add_new_field][field_name]' => $name = strtolower($this->randomMachineName()),
'fields[_add_new_field][type]' => 'file',
);
$this->drupalPostForm('admin/structure/comment/manage/comment/fields', $edit, t('Save'));
$edit = array('field_storage[settings][uri_scheme]' => 'private');
$this->drupalPostForm(NULL, $edit, t('Save field settings'));
$this->drupalPostForm(NULL, array(), t('Save settings'));
$name = strtolower($this->randomMachineName());
$label = $this->randomMachineName();
$storage_edit = array('field_storage[settings][uri_scheme]' => 'private');
$this->fieldUIAddNewField('admin/structure/comment/manage/comment', $name, $label, 'file', $storage_edit);
// Manually clear cache on the tester side.
\Drupal::entityManager()->clearCachedFieldDefinitions();
......
......@@ -8,6 +8,7 @@
namespace Drupal\link\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\simpletest\WebTestBase;
/**
......@@ -17,6 +18,8 @@
*/
class LinkFieldUITest extends WebTestBase {
use FieldUiTestTrait;
/**
* Modules to enable.
*
......@@ -42,16 +45,7 @@ function testFieldUI() {
// Add a link field to the newly-created type.
$label = $this->randomMachineName();
$field_name = Unicode::strtolower($label);
$edit = array(
'fields[_add_new_field][label]' => $label,
'fields[_add_new_field][field_name]' => $field_name,
'fields[_add_new_field][type]' => 'link',
);
$this->drupalPostForm("$type_path/fields", $edit, t('Save'));
// Proceed to the Edit (field settings) page.
$this->drupalPostForm(NULL, array(), t('Save field settings'));
// Proceed to the Manage fields overview page.
$this->drupalPostForm(NULL, array(), t('Save settings'));
$this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
// Load the formatter page to check that the settings summary does not
// generate warnings.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment