You are here

double_field.test in Double Field 7

Tests for double_field.module.

File

tests/double_field.test
View source
<?php

/**
 * @file
 * Tests for double_field.module.
 */

/**
 * Tests for double field types.
 */
class Double_fieldTestCase extends DrupalWebTestCase {
  protected $instance;
  protected $admin_user;
  protected $web_user;
  public static function getInfo() {
    return array(
      'name' => 'Double field',
      'description' => 'Test the creation of double fields.',
      'group' => 'Field types',
    );
  }
  function setUp() {
    parent::setUp('double_field');

    // Create test user.
    $admin_user = $this
      ->drupalCreateUser(array(
      'access administration pages',
      'administer content types',
    ));
    $this
      ->drupalLogin($admin_user);

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

  /**
   * Test admin UI.
   */
  function testAdminUI() {
    $field_name = strtolower($this
      ->randomName(8)) . '_test';
    $field_UI_path = "admin/structure/types/manage/{$this->type}/";
    $this
      ->drupalGet($field_UI_path . 'fields');
    $subfields = array(
      'first',
      'second',
    );

    // Check whether double field type is accessible.
    $this
      ->assertFieldByXPath('//select[@id="edit-fields-add-new-field-type"]/option', 'double_field', t('Element «edit-fields-add-new-field-type» exists.'));
    $this
      ->assertFieldByXPath('//select[@id="edit-fields-add-new-field-widget-type"]/optgroup[@label="Double field"]/option', 'double_textfield', t('Element «add new field widget type» exists.'));
    $this
      ->assertFieldByXPath('//select[@id="edit-fields-add-existing-field-widget-type"]/optgroup[@label="Double field"]/option', 'double_textfield', t('Element «add existing field widget type» exists.'));

    // Add new field via admin UI.
    $edit = array(
      'fields[_add_new_field][label]' => 'Test',
      'fields[_add_new_field][field_name]' => $field_name,
      'fields[_add_new_field][type]' => 'double_field',
      'fields[_add_new_field][widget_type]' => 'double_textfield',
    );
    $this
      ->drupalPost($field_UI_path . 'fields', $edit, t('Save'));

    // Drupal adds «field» prefix to all custom fields.
    $field_name = 'field_' . $field_name;

    // Check widget settings.
    foreach ($subfields as $subfield) {
      $settings['field[settings][' . $subfield . '_maxlength]'] = mt_rand(1, 255);
      $settings['instance[widget][settings][' . $subfield . '][size]'] = mt_rand(1, 50);
      $settings['instance[widget][settings][' . $subfield . '][prefix]'] = $this
        ->randomName(mt_rand(1, 50));
      $settings['instance[widget][settings][' . $subfield . '][suffix]'] = $this
        ->randomName(mt_rand(1, 50));
    }
    $this
      ->drupalPost($field_UI_path . 'fields/' . $field_name, $settings, t('Save settings'));
    $this
      ->drupalGet($field_UI_path . 'fields/' . $field_name);
    foreach ($settings as $name => $value) {
      $real_value = $this
        ->xpath('//input[@name="' . $name . '"]/@value');
      $this
        ->assertTrue(isset($real_value[0]) && $value == $real_value[0], t('Element «%name» has correct value.', array(
        '%name' => $name,
      )));
    }

    // Check formatter settings.
    $this
      ->drupalGet($field_UI_path . 'display');
    foreach (array_keys(double_field_field_formatter_info()) as $formatter) {
      $this
        ->assertFieldByXPath('//select[@name="fields[' . $field_name . '][type]"]/option', $formatter, t('Formatter «!formatter» exists.', array(
        '!formatter' => $formatter,
      )));
    }
    $instance = field_info_instance('node', $field_name, $this->type);

    // Check default formatter settings.
    $this
      ->assertText('First suffix', t('Default_summary is displayed.'));

    // Change formatter settings.
    $instance['display']['default']['settings'] = array(
      'first' => array(
        'prefix' => $this
          ->randomName(8),
        'suffix' => $this
          ->randomName(8),
      ),
      'second' => array(
        'prefix' => $this
          ->randomName(8),
        'suffix' => $this
          ->randomName(8),
      ),
    );
    field_update_instance($instance);

    // Check formatter settings again.
    $this
      ->drupalGet($field_UI_path . 'display');
    foreach ($subfields as $subfield) {
      $this
        ->assertText(ucfirst($subfield) . ' prefix: ' . $instance['display']['default']['settings'][$subfield]['prefix'], t('The expected summary is displayed.'));
    }
  }

}

Classes

Namesort descending Description
Double_fieldTestCase Tests for double field types.