entityreference.admin.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Contains EntityReferenceHandlersTestCase
  5. */
  6. /**
  7. * Test for Entity Reference admin UI.
  8. */
  9. class EntityReferenceAdminTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Entity Reference UI',
  13. 'description' => 'Tests for the administrative UI.',
  14. 'group' => 'Entity Reference',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp(array('field_ui', 'entity', 'ctools', 'entityreference'));
  19. // Create test user.
  20. $this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types'));
  21. $this->drupalLogin($this->admin_user);
  22. // Create content type, with underscores.
  23. $type_name = strtolower($this->randomName(8)) . '_test';
  24. $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
  25. $this->type = $type->type;
  26. // Store a valid URL name, with hyphens instead of underscores.
  27. $this->hyphen_type = str_replace('_', '-', $this->type);
  28. }
  29. protected function assertFieldSelectOptions($name, $expected_options) {
  30. $xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
  31. $fields = $this->xpath($xpath);
  32. if ($fields) {
  33. $field = $fields[0];
  34. $options = $this->getAllOptionsList($field);
  35. return $this->assertIdentical($options, $expected_options);
  36. }
  37. else {
  38. return $this->fail(t('Unable to find field @name', array('@name' => $name)));
  39. }
  40. }
  41. /**
  42. * Extract all the options of a select element.
  43. */
  44. protected function getAllOptionsList($element) {
  45. $options = array();
  46. // Add all options items.
  47. foreach ($element->option as $option) {
  48. $options[] = (string) $option['value'];
  49. }
  50. // TODO: support optgroup.
  51. return $options;
  52. }
  53. public function testFieldAdminHandler() {
  54. $bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;
  55. // First step: 'Add new field' on the 'Manage fields' page.
  56. $this->drupalPost($bundle_path . '/fields', array(
  57. 'fields[_add_new_field][label]' => 'Test label',
  58. 'fields[_add_new_field][field_name]' => 'test',
  59. 'fields[_add_new_field][type]' => 'entityreference',
  60. 'fields[_add_new_field][widget_type]' => 'entityreference_autocomplete',
  61. ), t('Save'));
  62. // Node should be selected by default.
  63. $this->assertFieldByName('field[settings][target_type]', 'node');
  64. // The base handler should be selected by default.
  65. $this->assertFieldByName('field[settings][handler]', 'base');
  66. // The base handler settings should be diplayed.
  67. $entity_type = 'node';
  68. $entity_info = entity_get_info($entity_type);
  69. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  70. $this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']');
  71. }
  72. // Test the sort settings.
  73. $options = array('none', 'property', 'field');
  74. $this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options);
  75. // Option 0: no sort.
  76. $this->assertFieldByName('field[settings][handler_settings][sort][type]', 'none');
  77. $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  78. $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  79. $this->assertNoFieldByName('field[settings][handler_settings][sort][direction]');
  80. // Option 1: sort by property.
  81. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'property'), 'field[settings][handler_settings][sort][type]');
  82. $this->assertFieldByName('field[settings][handler_settings][sort][property]', '');
  83. $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  84. $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  85. // Option 2: sort by field.
  86. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'field'), 'field[settings][handler_settings][sort][type]');
  87. $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  88. $this->assertFieldByName('field[settings][handler_settings][sort][field]', '');
  89. $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  90. // Set back to no sort.
  91. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'none'), 'field[settings][handler_settings][sort][type]');
  92. // Second step: 'Instance settings' form.
  93. $this->drupalPost(NULL, array(), t('Save field settings'));
  94. // Third step: confirm.
  95. $this->drupalPost(NULL, array(), t('Save settings'));
  96. // Check that the field appears in the overview form.
  97. $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', 'Test label', t('Field was created and appears in the overview page.'));
  98. }
  99. }