Project

General

Profile

Paste
Download (4.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / entityreference / tests / entityreference.admin.test @ 56aebcb7

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains EntityReferenceHandlersTestCase
6
 */
7

    
8
/**
9
 * Test for Entity Reference admin UI.
10
 */
11
class EntityReferenceAdminTestCase extends DrupalWebTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Entity Reference UI',
15
      'description' => 'Tests for the administrative UI.',
16
      'group' => 'Entity Reference',
17
    );
18
  }
19

    
20
  public function setUp() {
21
    parent::setUp(array('field_ui', 'entity', 'ctools', 'entityreference'));
22

    
23
    // Create test user.
24
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer fields'));
25
    $this->drupalLogin($this->admin_user);
26

    
27
    // Create content type, with underscores.
28
    $type_name = strtolower($this->randomName(8)) . '_test';
29
    $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
30
    $this->type = $type->type;
31
    // Store a valid URL name, with hyphens instead of underscores.
32
    $this->hyphen_type = str_replace('_', '-', $this->type);
33
  }
34

    
35
  protected function assertFieldSelectOptions($name, $expected_options) {
36
    $xpath = $this->buildXPathQuery('//select[@name=:name]', array(':name' => $name));
37
    $fields = $this->xpath($xpath);
38
    if ($fields) {
39
      $field = $fields[0];
40
      $options = $this->getAllOptionsList($field);
41
      return $this->assertIdentical($options, $expected_options);
42
    }
43
    else {
44
      return $this->fail(t('Unable to find field @name', array('@name' => $name)));
45
    }
46
  }
47

    
48
  /**
49
   * Extract all the options of a select element.
50
   */
51
  protected function getAllOptionsList($element) {
52
    $options = array();
53
    // Add all options items.
54
    foreach ($element->option as $option) {
55
      $options[] = (string) $option['value'];
56
    }
57
    // TODO: support optgroup.
58
    return $options;
59
  }
60

    
61
  public function testFieldAdminHandler() {
62
    $bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;
63

    
64
    // First step: 'Add new field' on the 'Manage fields' page.
65
    $this->drupalPost($bundle_path . '/fields', array(
66
      'fields[_add_new_field][label]' => 'Test label',
67
      'fields[_add_new_field][field_name]' => 'test',
68
      'fields[_add_new_field][type]' => 'entityreference',
69
      'fields[_add_new_field][widget_type]' => 'entityreference_autocomplete',
70
    ), t('Save'));
71
    return;
72
    // Node should be selected by default.
73
    $this->assertFieldByName('field[settings][target_type]', 'node');
74
    // The base handler should be selected by default.
75
    $this->assertFieldByName('field[settings][handler]', 'base');
76

    
77
    // The base handler settings should be diplayed.
78
    $entity_type = 'node';
79
    $entity_info = entity_get_info($entity_type);
80
    foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
81
      $this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']');
82
    }
83

    
84
    // Test the sort settings.
85
    $options = array('none', 'property', 'field');
86
    $this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options);
87
    // Option 0: no sort.
88
    $this->assertFieldByName('field[settings][handler_settings][sort][type]', 'none');
89
    $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
90
    $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
91
    $this->assertNoFieldByName('field[settings][handler_settings][sort][direction]');
92
    // Option 1: sort by property.
93
    $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'property'), 'field[settings][handler_settings][sort][type]');
94
    $this->assertFieldByName('field[settings][handler_settings][sort][property]', '');
95
    $this->assertNoFieldByName('field[settings][handler_settings][sort][field]');
96
    $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
97
    // Option 2: sort by field.
98
    $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'field'), 'field[settings][handler_settings][sort][type]');
99
    $this->assertNoFieldByName('field[settings][handler_settings][sort][property]');
100
    $this->assertFieldByName('field[settings][handler_settings][sort][field]', '');
101
    $this->assertFieldByName('field[settings][handler_settings][sort][direction]', 'ASC');
102
    // Set back to no sort.
103
    $this->drupalPostAJAX(NULL, array('field[settings][handler_settings][sort][type]' => 'none'), 'field[settings][handler_settings][sort][type]');
104

    
105
    // Second step: 'Instance settings' form.
106
    $this->drupalPost(NULL, array(), t('Save field settings'));
107

    
108
    // Third step: confirm.
109
    $this->drupalPost(NULL, array(), t('Save settings'));
110

    
111
    // Check that the field appears in the overview form.
112
    $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', 'Test label', t('Field was created and appears in the overview page.'));
113
  }
114
}