You are here

public function EntityreferenceReferenceTableFormatterTest::testEntityreferenceTableFormatter in Reference Table Formatter 7

Test the field collection integration.

File

tests/entityreference_reference_table_formatter.test, line 38

Class

EntityreferenceReferenceTableFormatterTest

Code

public function testEntityreferenceTableFormatter() {
  $this
    ->drupalLogin($this->adminUser);
  $type = $this
    ->drupalCreateContentType(array(
    'name' => 'Employment History',
    'type' => 'employment_history',
  ));
  $manage_fields = 'admin/structure/types/manage/' . $this->hyphen_type;
  $manage_display = $manage_fields . '/display';

  // Create a field called "employment_history" with unlimited cardinality.
  $this
    ->fieldUIAddNewField($manage_fields, array(
    'fields[_add_new_field][label]' => 'Employment History',
    'fields[_add_new_field][field_name]' => 'employment_history',
    'fields[_add_new_field][widget_type]' => 'options_buttons',
    'fields[_add_new_field][type]' => 'entityreference',
  ), array(
    'field[settings][target_type]' => 'node',
    'field[settings][handler]' => 'base',
    'field[settings][handler_settings][target_bundles][employment_history]' => 'employment_history',
  ), array(
    'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
  ));

  // Configure field to use table formatter.
  $this
    ->drupalGet($manage_display);

  // Use the table formatter.
  $edit = array(
    'fields[field_employment_history][type]' => 'reference_table_formatter',
    'refresh_rows' => 'field_employment_history',
  );
  $this
    ->drupalPostAJAX(NULL, $edit, array(
    'op' => t('Refresh'),
  ));
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $this
    ->assertText('Showing all fields.');
  $this
    ->assertText('Showing a header.');
  $this
    ->assertText('Using the "default" view mode.');
  $this
    ->assertText('Showing empty columns.');

  // Add fields to the new field collection bundle.
  $fields = array(
    'company' => 'Company',
    'position' => 'Position',
    'years_employed' => 'Years Employed',
  );
  foreach ($fields as $name => $label) {
    $this
      ->fieldUIAddNewField('admin/structure/types/manage/employment-history', array(
      'fields[_add_new_field][label]' => $label,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'text',
      'fields[_add_new_field][widget_type]' => 'text_textfield',
    ));
  }
  $test_data = array(
    array(
      'Ice cream King',
      'Cheif Salesman',
      '2.5 years',
    ),
    array(
      'London Zoo',
      'Bird Handler',
      '1 year',
    ),
    array(
      'Gregs discount carpets',
      'Delivery Guy',
      '1 year',
    ),
    array(
      'Tonys Hotdog Stand',
      'Mustard Application Officer',
      '1 year',
    ),
  );
  $test_data_ids = array();
  foreach ($test_data as $delta => $data) {
    $node = (object) array(
      'title' => 'Test Item ' . $delta,
      'type' => 'employment_history',
      'field_company' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $data[0],
          ),
        ),
      ),
      'field_position' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $data[1],
          ),
        ),
      ),
      'field_years_employed' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $data[2],
          ),
        ),
      ),
    );
    node_object_prepare($node);
    node_save($node);
    $test_data_ids[] = $node->nid;
  }
  $form_array = array(
    'title' => 'My Test Node',
  );
  foreach ($test_data_ids as $delta => $nid) {
    $form_array['field_employment_history[und][' . ($delta + 1) . ']'] = $nid;
  }
  $this
    ->drupalPost('node/add/' . $this->hyphen_type, $form_array, 'Save');
  $node_url = $this
    ->getUrl();

  // Assert the header is rendered.
  $this
    ->assertRaw('<th>Title</th><th>Body</th><th>Company</th><th>Position</th><th>Years Employed</th>');

  // Assert all our data exists.
  $this
    ->assertRaw('<td>Test Item 0</td><td></td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Ice cream King</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Cheif Salesman</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">2.5 years</div></div></div></td>');
  $this
    ->assertRaw('<td>Test Item 1</td><td></td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">London Zoo</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Bird Handler</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">1 year</div></div></div></td>');
  $this
    ->assertRaw('<td>Test Item 2</td><td></td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Gregs discount carpets</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Delivery Guy</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">1 year</div></div></div></td>');
  $this
    ->assertRaw('<td>Test Item 3</td><td></td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Tonys Hotdog Stand</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Mustard Application Officer</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">1 year</div></div></div></td>');

  // Change some settings to exclude the body field, turn off the header.
  $this
    ->drupalGet($manage_display);
  $this
    ->drupalPostAJAX(NULL, array(), 'field_employment_history_formatter_settings_edit');
  $fields = array(
    // Disable the header.
    'fields[field_employment_history][settings_edit_form][settings][show_header]' => FALSE,
    // Select certain fields to render.
    'fields[field_employment_history][settings_edit_form][settings][fields][title]' => 'title',
    'fields[field_employment_history][settings_edit_form][settings][fields][field_company]' => 'field_company',
    'fields[field_employment_history][settings_edit_form][settings][fields][field_position]' => 'field_position',
    'fields[field_employment_history][settings_edit_form][settings][fields][field_years_employed]' => 'field_years_employed',
  );
  $this
    ->drupalPostAJAX(NULL, $fields, 'field_employment_history_formatter_settings_update');
  $this
    ->drupalPost(NULL, array(), 'Save');
  $this
    ->assertText('Showing title, field_company, field_position, field_years_employed fields.');
  $this
    ->assertText('Not showing a header');
  $this
    ->drupalGet($node_url);

  // No body field should be shown.
  $this
    ->assertRaw('<td>Test Item 0</td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Ice cream King</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Cheif Salesman</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">2.5 years</div></div></div></td>');

  // No table header.
  $this
    ->assertNoRaw('thead');

  // Change the settings to show all fields and then "hide empty columns".
  $this
    ->drupalGet($manage_display);
  $this
    ->drupalPostAJAX(NULL, array(), 'field_employment_history_formatter_settings_edit');
  $fields = array(
    'fields[field_employment_history][settings_edit_form][settings][show_header]' => TRUE,
    'fields[field_employment_history][settings_edit_form][settings][fields][title]' => FALSE,
    'fields[field_employment_history][settings_edit_form][settings][fields][field_company]' => FALSE,
    'fields[field_employment_history][settings_edit_form][settings][fields][field_position]' => FALSE,
    'fields[field_employment_history][settings_edit_form][settings][fields][field_years_employed]' => FALSE,
    'fields[field_employment_history][settings_edit_form][settings][hide_empty]' => TRUE,
  );
  $this
    ->drupalPostAJAX(NULL, $fields, 'field_employment_history_formatter_settings_update');
  $this
    ->drupalPost(NULL, array(), 'Save');
  $this
    ->assertText('Showing all fields.');
  $this
    ->assertText('Showing a header.');
  $this
    ->assertText('Hiding empty columns.');

  // The body field should be filtered out.
  $this
    ->drupalGet($node_url);
  $this
    ->assertRaw('<td>Test Item 0</td><td><div class="field field-name-field-company field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Ice cream King</div></div></div></td><td><div class="field field-name-field-position field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">Cheif Salesman</div></div></div></td><td><div class="field field-name-field-years-employed field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">2.5 years</div></div></div></td>');
}