Commit f9c39902 authored by webchick's avatar webchick

Issue #1591432 by aspilicious: Convert field UI tests to PSR-0.

parent 33ef742c
......@@ -4,4 +4,3 @@ package = Core
version = VERSION
core = 8.x
dependencies[] = field
files[] = field_ui.test
<?php
/**
* @file
* Definition of Drupal\field_ui\Tests\AlterTest.
*/
namespace Drupal\field_ui\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests custom widget hooks and callbacks on the field administration pages.
*/
class AlterTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Widget customization',
'description' => 'Test custom field widget hooks and callbacks on field administration pages.',
'group' => 'Field UI',
);
}
function setUp() {
parent::setUp(array('field_ui', 'field_test', 'text', 'list'));
// Create Article node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
// Create test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer users'));
$this->drupalLogin($admin_user);
}
/**
* Tests hook_field_widget_properties_alter() on the default field widget.
*
* @see field_test_field_widget_properties_alter()
* @see field_test_field_widget_properties_user_alter()
* @see field_test_field_widget_form_alter()
*/
function testDefaultWidgetPropertiesAlter() {
// Create the alter_test_text field and an instance on article nodes.
field_create_field(array(
'field_name' => 'alter_test_text',
'type' => 'text',
));
field_create_instance(array(
'field_name' => 'alter_test_text',
'entity_type' => 'node',
'bundle' => 'article',
'widget' => array(
'type' => 'text_textfield',
'size' => 60,
),
));
// Test that field_test_field_widget_properties_alter() sets the size to
// 42 and that field_test_field_widget_form_alter() reports the correct
// size when the form is displayed.
$this->drupalGet('admin/structure/types/manage/article/fields/alter_test_text');
$this->assertText('Field size: 42', 'Altered field size is found in hook_field_widget_form_alter().');
// Create the alter_test_options field.
field_create_field(array(
'field_name' => 'alter_test_options',
'type' => 'list_text'
));
// Create instances on users and page nodes.
field_create_instance(array(
'field_name' => 'alter_test_options',
'entity_type' => 'user',
'bundle' => 'user',
'widget' => array(
'type' => 'options_select',
)
));
field_create_instance(array(
'field_name' => 'alter_test_options',
'entity_type' => 'node',
'bundle' => 'page',
'widget' => array(
'type' => 'options_select',
)
));
// Test that field_test_field_widget_properties_user_alter() replaces
// the widget and that field_test_field_widget_form_alter() reports the
// correct widget name when the form is displayed.
$this->drupalGet('admin/config/people/accounts/fields/alter_test_options');
$this->assertText('Widget type: options_buttons', 'Widget type is altered for users in hook_field_widget_form_alter().');
// Test that the widget is not altered on page nodes.
$this->drupalGet('admin/structure/types/manage/page/fields/alter_test_options');
$this->assertText('Widget type: options_select', 'Widget type is not altered for pages in hook_field_widget_form_alter().');
}
}
<?php
/**
* @file
* Definition of Drupal\field_ui\Tests\FieldUiTestBase.
*/
namespace Drupal\field_ui\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Provides common functionality for the Field UI test classes.
*/
class FieldUiTestBase extends WebTestBase {
function setUp() {
// Since this is a base class for many test cases, support the same
// flexibility that Drupal\simpletest\WebTestBase::setUp() has for the
// modules to be passed in as either an array or a variable number of string
// arguments.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'node';
$modules[] = 'field_ui';
$modules[] = 'field_test';
$modules[] = 'taxonomy';
parent::setUp($modules);
// Create test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
}
/**
* Creates a new field through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the new field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPost() on the first step ('Manage fields'
* screen).
* @param $field_edit
* $edit parameter for drupalPost() on the second step ('Field settings'
* form).
* @param $instance_edit
* $edit parameter for drupalPost() on the third step ('Instance settings'
* form).
*/
function fieldUIAddNewField($bundle_path, $initial_edit, $field_edit = array(), $instance_edit = array()) {
// Use 'test_field' field type by default.
$initial_edit += array(
'fields[_add_new_field][type]' => 'test_field',
'fields[_add_new_field][widget_type]' => 'test_field_widget',
);
$label = $initial_edit['fields[_add_new_field][label]'];
$field_name = $initial_edit['fields[_add_new_field][field_name]'];
// First step : 'Add new field' on the 'Manage fields' page.
$this->drupalPost("$bundle_path/fields", $initial_edit, t('Save'));
$this->assertRaw(t('These settings apply to the %label field everywhere it is used.', array('%label' => $label)), t('Field settings page was displayed.'));
// Second step : 'Field settings' form.
$this->drupalPost(NULL, $field_edit, t('Save field settings'));
$this->assertRaw(t('Updated field %label field settings.', array('%label' => $label)), t('Redirected to instance and widget settings page.'));
// Third step : 'Instance settings' form.
$this->drupalPost(NULL, $instance_edit, t('Save settings'));
$this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
// Check that the field appears in the overview form.
$this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
}
/**
* Adds an existing field through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPost() on the first step ('Manage fields'
* screen).
* @param $instance_edit
* $edit parameter for drupalPost() on the second step ('Instance settings'
* form).
*/
function fieldUIAddExistingField($bundle_path, $initial_edit, $instance_edit = array()) {
// Use 'test_field_widget' by default.
$initial_edit += array(
'fields[_add_existing_field][widget_type]' => 'test_field_widget',
);
$label = $initial_edit['fields[_add_existing_field][label]'];
$field_name = $initial_edit['fields[_add_existing_field][field_name]'];
// First step : 'Add existing field' on the 'Manage fields' page.
$this->drupalPost("$bundle_path/fields", $initial_edit, t('Save'));
// Second step : 'Instance settings' form.
$this->drupalPost(NULL, $instance_edit, t('Save settings'));
$this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
// Check that the field appears in the overview form.
$this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
}
/**
* Deletes a field instance through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the field instance is to be deleted from.
* @param $field_name
* The name of the field.
* @param $label
* The label of the field.
* @param $bundle_label
* The label of the bundle.
*/
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)), t('Delete confirmation was found.'));
// Submit confirmation form.
$this->drupalPost(NULL, array(), t('Delete'));
$this->assertRaw(t('The field %label has been deleted from the %type content type.', array('%label' => $label, '%type' => $bundle_label)), t('Delete message was found.'));
// Check that the field does not appear in the overview form.
$this->assertNoFieldByXPath('//table[@id="field-overview"]//span[@class="label-field"]', $label, t('Field does not appear in the overview page.'));
}
}
<?php
/**
* @file
* Definition of Drupal\field_ui\Tests\ManageDisplayTest.
*/
namespace Drupal\field_ui\Tests;
use Drupal\node\Node;
/**
* Tests the functionality of the 'Manage display' screens.
*/
class ManageDisplayTest extends FieldUiTestBase {
public static function getInfo() {
return array(
'name' => 'Manage display',
'description' => 'Test the Field UI "Manage display" screens.',
'group' => 'Field UI',
);
}
function setUp() {
parent::setUp(array('search'));
}
/**
* Tests formatter settings.
*/
function testFormatterUI() {
$manage_fields = 'admin/structure/types/manage/' . $this->type;
$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);
// Clear the test-side cache and get the saved field instance.
field_info_cache_clear();
$instance = field_info_instance('node', 'field_test', $this->type);
$format = $instance['display']['default']['type'];
$default_settings = field_info_formatter_settings($format);
$setting_name = key($default_settings);
$setting_value = $instance['display']['default']['settings'][$setting_name];
// Display the "Manage display" screen and check that the expected formatter is
// selected.
$this->drupalGet($manage_display);
$this->assertFieldByName('fields[field_test][type]', $format, t('The expected formatter is selected.'));
$this->assertText("$setting_name: $setting_value", t('The expected summary is displayed.'));
// Change the formatter and check that the summary is updated.
$edit = array('fields[field_test][type]' => 'field_test_multiple', 'refresh_rows' => 'field_test');
$this->drupalPostAJAX(NULL, $edit, array('op' => t('Refresh')));
$format = 'field_test_multiple';
$default_settings = field_info_formatter_settings($format);
$setting_name = key($default_settings);
$setting_value = $default_settings[$setting_name];
$this->assertFieldByName('fields[field_test][type]', $format, t('The expected formatter is selected.'));
$this->assertText("$setting_name: $setting_value", t('The expected summary is displayed.'));
// Submit the form and check that the instance is updated.
$this->drupalPost(NULL, array(), t('Save'));
field_info_cache_clear();
$instance = field_info_instance('node', 'field_test', $this->type);
$current_format = $instance['display']['default']['type'];
$current_setting_value = $instance['display']['default']['settings'][$setting_name];
$this->assertEqual($current_format, $format, t('The formatter was updated.'));
$this->assertEqual($current_setting_value, $setting_value, t('The setting was updated.'));
}
/**
* Tests switching view modes to use custom or 'default' settings'.
*/
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);
// 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
// (for example, unlikely to be part of the "Submitted by ... on ..." line).
$value = 12345;
$settings = array(
'type' => $this->type,
'field_test' => array(LANGUAGE_NOT_SPECIFIED => array(array('value' => $value))),
);
$node = $this->drupalCreateNode($settings);
// Gather expected output values with the various formatters.
$formatters = field_info_formatter_types();
$output = array(
'field_test_default' => $formatters['field_test_default']['settings']['test_formatter_setting'] . '|' . $value,
'field_test_with_prepare_view' => $formatters['field_test_with_prepare_view']['settings']['test_formatter_setting_additional'] . '|' . $value. '|' . ($value + 1),
);
// Check that the field is displayed with the default formatter in 'rss'
// mode (uses 'default'), and hidden in 'teaser' mode (uses custom settings).
$this->assertNodeViewText($node, 'rss', $output['field_test_default'], t("The field is displayed as expected in view modes that use 'default' settings."));
$this->assertNodeViewNoText($node, 'teaser', $value, t("The field is hidden in view modes that use custom settings."));
// Change fomatter for 'default' mode, check that the field is displayed
// accordingly in 'rss' mode.
$edit = array(
'fields[field_test][type]' => 'field_test_with_prepare_view',
);
$this->drupalPost('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected in view modes that use 'default' settings."));
// Specialize the 'rss' mode, check that the field is displayed the same.
$edit = array(
"view_modes_custom[rss]" => TRUE,
);
$this->drupalPost('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected in newly specialized 'rss' mode."));
// Set the field to 'hidden' in the view mode, check that the field is
// hidden.
$edit = array(
'fields[field_test][type]' => 'hidden',
);
$this->drupalPost('admin/structure/types/manage/' . $this->type . '/display/rss', $edit, t('Save'));
$this->assertNodeViewNoText($node, 'rss', $value, t("The field is hidden in 'rss' mode."));
// Set the view mode back to 'default', check that the field is displayed
// accordingly.
$edit = array(
"view_modes_custom[rss]" => FALSE,
);
$this->drupalPost('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected when 'rss' mode is set back to 'default' settings."));
// Specialize the view mode again.
$edit = array(
"view_modes_custom[rss]" => TRUE,
);
$this->drupalPost('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
// Check that the previous settings for the view mode have been kept.
$this->assertNodeViewNoText($node, 'rss', $value, t("The previous settings are kept when 'rss' mode is specialized again."));
}
/**
* Asserts that a string is found in the rendered node in a view mode.
*
* @param Node $node
* The node.
* @param $view_mode
* The view mode in which the node should be displayed.
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
*
* @return
* TRUE on pass, FALSE on fail.
*/
function assertNodeViewText(Node $node, $view_mode, $text, $message) {
return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, FALSE);
}
/**
* Asserts that a string is not found in the rendered node in a view mode.
*
* @param Node $node
* The node.
* @param $view_mode
* The view mode in which the node should be displayed.
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @return
* TRUE on pass, FALSE on fail.
*/
function assertNodeViewNoText(Node $node, $view_mode, $text, $message) {
return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, TRUE);
}
/**
* Asserts that a string is (not) found in the rendered nodein a view mode.
*
* This helper function is used by assertNodeViewText() and
* assertNodeViewNoText().
*
* @param Node $node
* The node.
* @param $view_mode
* The view mode in which the node should be displayed.
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $not_exists
* TRUE if this text should not exist, FALSE if it should.
*
* @return
* TRUE on pass, FALSE on fail.
*/
function assertNodeViewTextHelper(Node $node, $view_mode, $text, $message, $not_exists) {
// Make sure caches on the tester side are refreshed after changes
// submitted on the tested side.
field_info_cache_clear();
// Save current content so that we can restore it when we're done.
$old_content = $this->drupalGetContent();
// Render a cloned node, so that we do not alter the original.
$clone = clone $node;
$element = node_view($clone, $view_mode);
$output = drupal_render($element);
$this->verbose(t('Rendered node - view mode: @view_mode', array('@view_mode' => $view_mode)) . '<hr />'. $output);
// Assign content so that WebTestBase functions can be used.
$this->drupalSetContent($output);
$method = ($not_exists ? 'assertNoText' : 'assertText');
$return = $this->{$method}((string) $text, $message);
// Restore previous content.
$this->drupalSetContent($old_content);
return $return;
}
}
......@@ -2,143 +2,15 @@
/**
* @file
* Tests for field_ui.module.
* Definition of Drupal\field_ui\Tests\ManageFieldsTest.
*/
use Drupal\node\Node;
use Drupal\simpletest\WebTestBase;
/**
* Provides common functionality for the Field UI test classes.
*/
class FieldUITestCase extends WebTestBase {
function setUp() {
// Since this is a base class for many test cases, support the same
// flexibility that Drupal\simpletest\WebTestBase::setUp() has for the
// modules to be passed in as either an array or a variable number of string
// arguments.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'node';
$modules[] = 'field_ui';
$modules[] = 'field_test';
$modules[] = 'taxonomy';
parent::setUp($modules);
// Create test user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$type_name = strtolower($this->randomName(8)) . '_test';
$type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
$this->type = $type->type;
}
/**
* Creates a new field through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the new field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPost() on the first step ('Manage fields'
* screen).
* @param $field_edit
* $edit parameter for drupalPost() on the second step ('Field settings'
* form).
* @param $instance_edit
* $edit parameter for drupalPost() on the third step ('Instance settings'
* form).
*/
function fieldUIAddNewField($bundle_path, $initial_edit, $field_edit = array(), $instance_edit = array()) {
// Use 'test_field' field type by default.
$initial_edit += array(
'fields[_add_new_field][type]' => 'test_field',
'fields[_add_new_field][widget_type]' => 'test_field_widget',
);
$label = $initial_edit['fields[_add_new_field][label]'];
$field_name = $initial_edit['fields[_add_new_field][field_name]'];
// First step : 'Add new field' on the 'Manage fields' page.
$this->drupalPost("$bundle_path/fields", $initial_edit, t('Save'));
$this->assertRaw(t('These settings apply to the %label field everywhere it is used.', array('%label' => $label)), t('Field settings page was displayed.'));
// Second step : 'Field settings' form.
$this->drupalPost(NULL, $field_edit, t('Save field settings'));
$this->assertRaw(t('Updated field %label field settings.', array('%label' => $label)), t('Redirected to instance and widget settings page.'));
// Third step : 'Instance settings' form.
$this->drupalPost(NULL, $instance_edit, t('Save settings'));
$this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
// Check that the field appears in the overview form.
$this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
}
/**
* Adds an existing field through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the field is to be attached to.
* @param $initial_edit
* $edit parameter for drupalPost() on the first step ('Manage fields'
* screen).
* @param $instance_edit
* $edit parameter for drupalPost() on the second step ('Instance settings'
* form).
*/
function fieldUIAddExistingField($bundle_path, $initial_edit, $instance_edit = array()) {
// Use 'test_field_widget' by default.
$initial_edit += array(
'fields[_add_existing_field][widget_type]' => 'test_field_widget',
);
$label = $initial_edit['fields[_add_existing_field][label]'];
$field_name = $initial_edit['fields[_add_existing_field][field_name]'];
// First step : 'Add existing field' on the 'Manage fields' page.
$this->drupalPost("$bundle_path/fields", $initial_edit, t('Save'));
// Second step : 'Instance settings' form.
$this->drupalPost(NULL, $instance_edit, t('Save settings'));
$this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
// Check that the field appears in the overview form.
$this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
}
/**
* Deletes a field instance through the Field UI.
*
* @param $bundle_path
* Admin path of the bundle that the field instance is to be deleted from.
* @param $field_name
* The name of the field.
* @param $label