You are here

multifield_table.test in Multifield table 7

File

multifield_table.test
View source
<?php

class MultifieldTableTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Multifield table',
      'description' => 'Test the Multifield table module.',
      'group' => 'Multifield',
    );
  }

  /**
   * Prepares the testing environment.
   */
  function setUp() {

    // Enable minimum required modules.
    parent::setUp('multifield', 'multifield_table');

    // Create user.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'bypass node access',
      'administer content types',
      'administer multifield',
    ));
    $this
      ->drupalLogin($this->privileged_user);

    // Create content type, with underscores.
    $type_name = strtolower($this
      ->randomName(8)) . '_test';
    $type = $this
      ->drupalCreateContentType(array(
      'name' => $type_name,
      'type' => $type_name,
    ));
    $this->type = $type->type;

    // Store a valid URL name, with hyphens instead of underscores.
    $this->hyphen_type = str_replace('_', '-', $this->type);
  }

  /**
   * Creates a new field through the Field UI.
   *
   * @param string $bundle_path
   *   Admin path of the bundle that the new field is to be attached to.
   * @param array $initial_edit
   *   $edit parameter for drupalPost() on the first step ('Manage fields'
   *   screen).
   * @param array $field_edit
   *   $edit parameter for drupalPost() on the second step ('Field settings'
   *   form).
   * @param array $instance_edit
   *   $edit parameter for drupalPost() on the third step ('Instance settings'
   *   form).
   */
  function fieldUIAddNewField($bundle_path, $initial_edit, $field_edit = array(), $instance_edit = array()) {

    // Use 'test_field' field type by default.
    $initial_edit += array(
      'fields[_add_new_field][type]' => 'test_field',
      'fields[_add_new_field][widget_type]' => 'test_field_widget',
    );
    $label = $initial_edit['fields[_add_new_field][label]'];
    $field_name = $initial_edit['fields[_add_new_field][field_name]'];

    // First step : 'Add new field' on the 'Manage fields' page.
    $this
      ->drupalPost("{$bundle_path}/fields", $initial_edit, t('Save'));
    $this
      ->assertRaw(t('These settings apply to the %label field everywhere it is used.', array(
      '%label' => $label,
    )), t('Field settings page was displayed.'));

    // Second step : 'Field settings' form.
    $this
      ->drupalPost(NULL, $field_edit, t('Save field settings'));
    $this
      ->assertRaw(t('Updated field %label field settings.', array(
      '%label' => $label,
    )), t('Redirected to instance and widget settings page.'));

    // Third step : 'Instance settings' form.
    $this
      ->drupalPost(NULL, $instance_edit, t('Save settings'));
    $this
      ->assertRaw(t('Saved %label configuration.', array(
      '%label' => $label,
    )), t('Redirected to "Manage fields" page.'));

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

  /**
   * Checks that multifield table has been generated successfully.
   *
   * @param array $fields
   *   Array containing fields to test
   */
  function checkMultifieldTable($fields) {
    $position = 0;
    foreach ($fields as $field) {
      $position++;
      $xpath = $this
        ->buildXPathQuery('//table/thead/tr/th[:position]', array(
        ':position' => $position,
      ));
      $this
        ->assertFieldByXPath($xpath, $field['label']);
      $xpath = $this
        ->buildXPathQuery('//table/thead/tr/th[contains(@class, :field-name)]', array(
        ':field-name' => drupal_clean_css_identifier($field['name']),
      ));
      $this
        ->assertFieldByXPath($xpath, $field['label']);
      $xpath = $this
        ->buildXPathQuery('//table/tbody/tr/td[:position]', array(
        ':position' => $position,
      ));
      $this
        ->assertFieldByXPath($xpath, $field['value']);
    }
  }

  /**
   * Shuffles an array preserving it keys.
   *
   * @param array $list
   *   Array to shuffle
   *
   * @return array
   *   The assoc shuffled array
   */
  function shuffleAssoc($list) {
    if (!is_array($list)) {
      return $list;
    }
    $keys = array_keys($list);
    shuffle($keys);
    $random = array();
    foreach ($keys as $key) {
      $random[$key] = $list[$key];
    }
    return $random;
  }

  /**
   * Performs the basic tests.
   */
  public function testMultifieldTableBasic() {
    $this->multifield = drupal_strtolower($this
      ->randomName());
    $edit = array(
      'label' => $this->multifield,
      'machine_name' => $this->multifield,
    );
    $this
      ->drupalPost('admin/structure/multifield/add', $edit, t('Save'));
    $this->multifield_path = 'admin/structure/multifield/manage/' . str_replace('_', '-', $this->multifield);

    // Define fields array.
    $fields = array(
      'text' => array(
        'type' => 'text',
        'widget' => 'text_textfield',
        'label' => $this
          ->randomName(),
        'name' => drupal_strtolower($this
          ->randomName()),
        'value' => $this
          ->randomName(),
      ),
      'longtext' => array(
        'type' => 'text_long',
        'widget' => 'text_textarea',
        'label' => $this
          ->randomName(),
        'name' => drupal_strtolower($this
          ->randomName()),
        'value' => $this
          ->randomName(32),
      ),
      'integer' => array(
        'type' => 'number_integer',
        'widget' => 'number',
        'label' => $this
          ->randomName(),
        'name' => drupal_strtolower($this
          ->randomName()),
        'value' => rand(0, 999),
      ),
    );

    // Generate fields.
    foreach ($fields as $field) {
      $edit = array(
        'fields[_add_new_field][type]' => $field['type'],
        'fields[_add_new_field][widget_type]' => $field['widget'],
        'fields[_add_new_field][label]' => $field['label'],
        'fields[_add_new_field][field_name]' => $field['name'],
      );
      $this
        ->fieldUIAddNewField($this->multifield_path, $edit);
    }

    // Create the multifield field.
    $bundle_path = 'admin/structure/types/manage/' . $this->hyphen_type;
    $edit = array(
      'fields[_add_new_field][type]' => $this->multifield,
      'fields[_add_new_field][widget_type]' => 'multifield_default',
      'fields[_add_new_field][label]' => 'Multifield',
      'fields[_add_new_field][field_name]' => 'multifield',
    );
    $this
      ->fieldUIAddNewField($bundle_path, $edit);

    // Set Multifield table as formatter.
    $manage_display = $bundle_path . '/display';
    $edit = array(
      'fields[field_multifield][type]' => 'multifield_table',
    );
    $this
      ->drupalPost($manage_display, $edit, t('Save'));

    // Go to node creation page.
    $edit = array(
      'title' => $this
        ->randomName(),
    );
    foreach ($fields as $field) {
      $edit["field_multifield[und][0][field_{$field['name']}][und][0][value]"] = $field['value'];
    }
    $this
      ->drupalPost('node/add/' . $this->hyphen_type, $edit, t('Save'));
    $this->node_url = $this
      ->getUrl();

    // Check table is generated successfully.
    $this
      ->checkMultifieldTable($fields);

    // Change multifield fields position.
    $shuffled_fields = $this
      ->shuffleAssoc($fields);
    $position = 0;
    $edit = array();
    foreach ($shuffled_fields as $field) {
      $position++;
      $edit["fields[field_{$field['name']}][weight]"] = $position;
    }
    $this
      ->drupalPost($this->multifield_path . '/display', $edit, t('Save'));

    // Check that table items respect multifield display weights.
    $this
      ->drupalGet($this->node_url);
    $this
      ->checkMultifieldTable($shuffled_fields);
  }

}

Classes