123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- <?php
- class FieldUITestCase extends DrupalWebTestCase {
- function setUp() {
-
-
-
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules[] = 'field_test';
- parent::setUp($modules);
-
- $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy'));
- $this->drupalLogin($admin_user);
-
- $type_name = strtolower($this->randomName(8)) . '_test';
- $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
- $this->type = $type->type;
-
- $this->hyphen_type = str_replace('_', '-', $this->type);
- }
-
- function fieldUIAddNewField($bundle_path, $initial_edit, $field_edit = array(), $instance_edit = array()) {
-
- $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]'];
-
- $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.'));
-
- $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.'));
-
- $this->drupalPost(NULL, $instance_edit, t('Save settings'));
- $this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
-
- $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
- }
-
- function fieldUIAddExistingField($bundle_path, $initial_edit, $instance_edit = array()) {
-
- $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]'];
-
- $this->drupalPost("$bundle_path/fields", $initial_edit, t('Save'));
-
- $this->drupalPost(NULL, $instance_edit, t('Save settings'));
- $this->assertRaw(t('Saved %label configuration.', array('%label' => $label)), t('Redirected to "Manage fields" page.'));
-
- $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $label, t('Field was created and appears in the overview page.'));
- }
-
- function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
-
- $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.'));
-
- $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.'));
-
- $this->assertNoFieldByXPath('//table[@id="field-overview"]//span[@class="label-field"]', $label, t('Field does not appear in the overview page.'));
- }
- }
- class FieldUIManageFieldsTestCase extends FieldUITestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Manage fields',
- 'description' => 'Test the Field UI "Manage fields" screen.',
- 'group' => 'Field UI',
- );
- }
- function setUp() {
- parent::setUp();
-
- $this->field_label = $this->randomName(8);
- $this->field_name_input = strtolower($this->randomName(8));
- $this->field_name = 'field_'. $this->field_name_input;
- }
-
- function testCRUDFields() {
- $this->manageFieldsPage();
- $this->createField();
- $this->updateField();
- $this->addExistingField();
- }
-
- function manageFieldsPage() {
- $this->drupalGet('admin/structure/types/manage/' . $this->hyphen_type . '/fields');
-
- $table_headers = array(
- t('Label'),
- t('Machine name'),
- t('Field type'),
- t('Widget'),
- t('Operations'),
- );
- foreach ($table_headers as $table_header) {
-
- $this->assertRaw($table_header . '</th>', t('%table_header table header was found.', array('%table_header' => $table_header)));
- }
-
-
- foreach (array('Add new field', 'Add existing field') as $element) {
- $this->assertText($element, t('"@element" was found.', array('@element' => $element)));
- }
- }
-
- function createField() {
-
- $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->hyphen_type, $edit);
-
-
-
- $vocabulary = taxonomy_vocabulary_load(1);
- $this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/fields');
- $this->assertTrue($this->xpath('//select[@name="fields[_add_existing_field][field_name]"]//option[@value="' . $this->field_name . '"]'), t('Existing field was found in account settings.'));
- }
-
- function updateField() {
-
- $this->drupalGet('admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $this->field_name);
-
- $string = 'updated dummy test string';
- $edit = array(
- 'field[settings][test_field_setting]' => $string,
- 'instance[settings][test_instance_setting]' => $string,
- 'instance[widget][settings][test_widget_setting]' => $string,
- );
- $this->drupalPost(NULL, $edit, t('Save settings'));
-
- $this->assertFieldSettings($this->type, $this->field_name, $string);
-
- $this->assertText(t('Saved @label configuration.', array('@label' => $this->field_label)), t('Redirected to "Manage fields" page.'));
- }
-
- function addExistingField() {
-
- $this->drupalGet('admin/structure/types/manage/page/fields');
- $this->assertRaw(t('Add existing field'), t('"Add existing field" was found.'));
-
-
-
- $this->assertFalse($this->xpath('//select[@id="edit-add-existing-field-field-name"]//option[@value="comment"]'), t('The list of options respects entity type restrictions.'));
-
- $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);
- }
-
- function assertFieldSettings($bundle, $field_name, $string = 'dummy test string', $entity_type = 'node') {
-
- field_info_cache_clear();
-
- $field = field_info_field($field_name);
- $this->assertTrue($field['settings']['test_field_setting'] == $string, t('Field settings were found.'));
-
- $instance = field_info_instance($entity_type, $field_name, $bundle);
- $this->assertTrue($instance['settings']['test_instance_setting'] == $string, t('Field instance settings were found.'));
- $this->assertTrue($instance['widget']['settings']['test_widget_setting'] == $string, t('Field widget settings were found.'));
- }
-
- function testDefaultValue() {
-
- $field_name = 'test';
- $field = array(
- 'field_name' => $field_name,
- 'type' => 'test_field'
- );
- field_create_field($field);
- $instance = array(
- 'field_name' => $field_name,
- 'entity_type' => 'node',
- 'bundle' => $this->type,
- );
- field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
- $admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $field_name;
- $element_id = "edit-$field_name-$langcode-0-value";
- $element_name = "{$field_name}[$langcode][0][value]";
- $this->drupalGet($admin_path);
- $this->assertFieldById($element_id, '', t('The default value widget was empty.'));
-
- $edit = array($element_name => '-1');
- $this->drupalPost($admin_path, $edit, t('Save settings'));
- $this->assertText("$field_name does not accept the value -1", t('Form vaildation failed.'));
-
- $edit = array($element_name => '1');
- $this->drupalPost($admin_path, $edit, t('Save settings'));
- $this->assertText("Saved $field_name configuration", t('The form was successfully submitted.'));
- $instance = field_info_instance('node', $field_name, $this->type);
- $this->assertEqual($instance['default_value'], array(array('value' => 1)), t('The default value was correctly saved.'));
-
- $this->drupalGet($admin_path);
- $this->assertFieldById($element_id, '1', t('The default value widget was displayed with the correct value.'));
-
- $edit = array($element_name => '');
- $this->drupalPost(NULL, $edit, t('Save settings'));
- $this->assertText("Saved $field_name configuration", t('The form was successfully submitted.'));
- field_info_cache_clear();
- $instance = field_info_instance('node', $field_name, $this->type);
- $this->assertEqual($instance['default_value'], NULL, t('The default value was correctly saved.'));
- }
-
- function testDeleteField() {
-
- $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);
-
- $type_name2 = strtolower($this->randomName(8)) . '_test';
- $type2 = $this->drupalCreateContentType(array('name' => $type_name2, 'type' => $type_name2));
- $type_name2 = $type2->type;
- $hyphen_type2 = str_replace('_', '-', $type_name2);
-
- $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);
-
- $this->fieldUIDeleteField($bundle_path1, $this->field_name, $this->field_label, $this->type);
-
- field_info_cache_clear();
-
- $this->assertNull(field_info_instance('node', $this->field_name, $this->type), t('Field instance was deleted.'));
-
- $this->assertNotNull(field_info_field($this->field_name), t('Field was not deleted.'));
-
- $this->fieldUIDeleteField($bundle_path2, $this->field_name, $this->field_label, $type_name2);
-
- field_info_cache_clear();
-
- $this->assertNull(field_info_instance('node', $this->field_name, $type_name2), t('Field instance was deleted.'));
-
- $this->assertNull(field_info_field($this->field_name), t('Field was deleted.'));
- }
-
- function testHiddenFields() {
- $bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/';
-
- $this->drupalGet($bundle_path);
- $this->assertFalse($this->xpath('//select[@id="edit-add-new-field-type"]//option[@value="hidden_test_field"]'), t("The 'add new field' select respects field types 'no_ui' property."));
-
- $field_name = 'hidden_test_field';
- field_create_field(array('field_name' => $field_name, 'type' => $field_name));
- $instance = array(
- 'field_name' => $field_name,
- 'bundle' => $this->type,
- 'entity_type' => 'node',
- 'label' => t('Hidden field'),
- 'widget' => array('type' => 'test_field_widget'),
- );
- field_create_instance($instance);
- $this->assertTrue(field_read_instance('node', $field_name, $this->type), t('An instance of the field %field was created programmatically.', array('%field' => $field_name)));
-
-
- $this->drupalGet($bundle_path);
- $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', $instance['label'], t('Field was created and appears in the overview page.'));
-
-
- $bundle_path = 'admin/structure/types/manage/article/fields/';
- $this->drupalGet($bundle_path);
- $this->assertFalse($this->xpath('//select[@id="edit-add-existing-field-field-name"]//option[@value=:field_name]', array(':field_name' => $field_name)), t("The 'add existing field' select respects field types 'no_ui' property."));
- }
-
- function testRenameBundle() {
- $type2 = strtolower($this->randomName(8)) . '_' .'test';
- $hyphen_type2 = str_replace('_', '-', $type2);
- $options = array(
- 'type' => $type2,
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type, $options, t('Save content type'));
- $this->drupalGet('admin/structure/types/manage/' . $hyphen_type2 . '/fields');
- }
-
- function testDuplicateFieldName() {
-
-
- $edit = array(
- 'fields[_add_new_field][field_name]' => 'tags',
- 'fields[_add_new_field][label]' => $this->randomName(),
- 'fields[_add_new_field][type]' => 'taxonomy_term_reference',
- 'fields[_add_new_field][widget_type]' => 'options_select',
- );
- $url = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields';
- $this->drupalPost($url, $edit, t('Save'));
- $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
- $this->assertUrl($url, array(), 'Stayed on the same page.');
- }
- }
- class FieldUIManageDisplayTestCase extends FieldUITestCase {
- 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'));
- }
-
- function testFormatterUI() {
- $manage_fields = 'admin/structure/types/manage/' . $this->hyphen_type;
- $manage_display = $manage_fields . '/display';
-
- $edit = array(
- 'fields[_add_new_field][label]' => 'Test field',
- 'fields[_add_new_field][field_name]' => 'test',
- );
- $this->fieldUIAddNewField($manage_fields, $edit);
-
- 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];
-
-
- $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.'));
-
- $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.'));
-
- $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.'));
- }
-
- function testViewModeCustom() {
-
- $edit = array(
- 'fields[_add_new_field][label]' => 'Test field',
- 'fields[_add_new_field][field_name]' => 'test',
- );
- $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->hyphen_type, $edit);
-
-
-
- $value = 12345;
- $settings = array(
- 'type' => $this->type,
- 'field_test' => array(LANGUAGE_NONE => array(array('value' => $value))),
- );
- $node = $this->drupalCreateNode($settings);
-
- $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),
- );
-
-
- $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."));
-
-
- $edit = array(
- 'fields[field_test][type]' => 'field_test_with_prepare_view',
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_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."));
-
- $edit = array(
- "view_modes_custom[rss]" => TRUE,
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_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."));
-
-
- $edit = array(
- 'fields[field_test][type]' => 'hidden',
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display/rss', $edit, t('Save'));
- $this->assertNodeViewNoText($node, 'rss', $value, t("The field is hidden in 'rss' mode."));
-
-
- $edit = array(
- "view_modes_custom[rss]" => FALSE,
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_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."));
-
- $edit = array(
- "view_modes_custom[rss]" => TRUE,
- );
- $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display', $edit, t('Save'));
-
- $this->assertNodeViewNoText($node, 'rss', $value, t("The previous settings are kept when 'rss' mode is specialized again."));
- }
-
- function assertNodeViewText($node, $view_mode, $text, $message) {
- return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, FALSE);
- }
-
- function assertNodeViewNoText($node, $view_mode, $text, $message) {
- return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, TRUE);
- }
-
- function assertNodeViewTextHelper($node, $view_mode, $text, $message, $not_exists) {
-
-
- field_info_cache_clear();
-
- $old_content = $this->drupalGetContent();
-
- $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);
-
- $this->drupalSetContent($output);
- $method = ($not_exists ? 'assertNoText' : 'assertText');
- $return = $this->{$method}((string) $text, $message);
-
- $this->drupalSetContent($old_content);
- return $return;
- }
- }
- class FieldUIAlterTestCase extends DrupalWebTestCase {
- 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_test'));
-
- $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer users'));
- $this->drupalLogin($admin_user);
- }
-
- function testDefaultWidgetPropertiesAlter() {
-
- 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,
- ),
- ));
-
-
-
- $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().');
-
- field_create_field(array(
- 'field_name' => 'alter_test_options',
- 'type' => 'list_text'
- ));
-
- 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',
- )
- ));
-
-
-
- $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().');
-
- $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().');
- }
- }
|