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', 'administer fields'));
  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. return;
  63. // Node should be selected by default.
  64. $this->assertFieldByName('field[settings][target_type]', 'node');
  65. // The base handler should be selected by default.
  66. $this->assertFieldByName('field[settings][handler]', 'base');
  67. // The base handler settings should be diplayed.
  68. $entity_type = 'node';
  69. $entity_info = entity_get_info($entity_type);
  70. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  71. $this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']');
  72. }
  73. // Test the sort settings.
  74. $options = array('none', 'property', 'field');
  75. $this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options);
  76. // Option 0: no sort.
  77. $this->assertFieldByName('field[settings][handler_settings][sort][type]', 'none');
  78. $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  79. $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  80. $this->assertNoFieldByName('field[settings][handler_settings][sort][direction]');
  81. // Option 1: sort by property.
  82. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'property'), 'field[settings][handler_settings][sort][type]');
  83. $this->assertFieldByName('field[settings][handler_settings][sort][property]', '');
  84. $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
  85. $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  86. // Option 2: sort by field.
  87. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'field'), 'field[settings][handler_settings][sort][type]');
  88. $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
  89. $this->assertFieldByName('field[settings][handler_settings][sort][field]', '');
  90. $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
  91. // Set back to no sort.
  92. $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'none'), 'field[settings][handler_settings][sort][type]');
  93. // Second step: 'Instance settings' form.
  94. $this->drupalPost(NULL, array(), t('Save field settings'));
  95. // Third step: confirm.
  96. $this->drupalPost(NULL, array(), t('Save settings'));
  97. // Check that the field appears in the overview form.
  98. $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', 'Test label', t('Field was created and appears in the overview page.'));
  99. }
  100. }