You are here

field_collection_reference_table_formatter.test in Reference Table Formatter 7

File

tests/field_collection_reference_table_formatter.test
View source
<?php

class FieldCollectionReferenceTableFormatterTest extends FieldUITestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Reference Table Formatter Field Collection',
      'description' => 'Test the table formatter functionality.',
      'group' => 'Reference Table Formatter',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setup() {
    parent::setUp(array(
      'reference_table_formatter',
      'ctools',
      'field_ui',
      'node',
      'field_collection',
    ));
    $this->adminUser = $this
      ->drupalCreateUser(array(
      'access content',
      'administer nodes',
      'bypass node access',
      'administer content types',
      'administer taxonomy',
      'administer field collections',
    ));
  }

  /**
   * Test the field collection integration.
   */
  public function testFieldCollectionTableFormatter() {
    $this
      ->drupalLogin($this->adminUser);
    $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]' => 'field_collection_embed',
      'fields[_add_new_field][type]' => 'field_collection',
    ), array(), 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 "full" view mode.');

    // 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/field-collections/field-employment-history/manage', 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',
      ));
    }
    $this
      ->drupalGet('node/add/' . $this->hyphen_type);
    $node_add_fields = array(
      'title' => 'Joe Bloggs',
    );
    $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',
      ),
    );
    foreach ($test_data as $delta => $data) {
      $field_array = array(
        'field_employment_history[und][' . $delta . '][field_company][und][0][value]' => $data[0],
        'field_employment_history[und][' . $delta . '][field_position][und][0][value]' => $data[1],
        'field_employment_history[und][' . $delta . '][field_years_employed][und][0][value]' => $data[2],
      );
      $node_add_fields += $field_array;
      if ($delta < count($test_data) - 1) {
        $this
          ->drupalPostAJAX(NULL, $node_add_fields, 'field_employment_history_add_more');
      }
    }
    $this
      ->drupalPost(NULL, $node_add_fields, 'Save');

    // Ensure the header as appears at the top of the table.
    $this
      ->assertRaw('<th>Company</th><th>Position</th><th>Years Employed</th>');

    // Assert all of the rows appear as expected.
    $this
      ->assertRaw('<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><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><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><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>');
  }

}