You are here

name.test in Name Field 7

Same filename and directory in other branches
  1. 6 tests/name.test

Tests for the name module.

File

tests/name.test
View source
<?php

/**
 * @file
 * Tests for the name module.
 */

/**
 * Helper test class with some added functions for testing.
 */
class NameTestHelper extends DrupalWebTestCase {

  /**
   *
   */
  public function setUp() {

    // Call parent::setUp() allowing test cases to pass further modules.
    $modules = func_get_args();
    $modules = array_merge(array(
      'field',
      'field_ui',
      'name',
    ), $modules);
    parent::setUp($modules);

    // Base set up is done, we can call drupalCreateUser.
    $this->web_user = $this
      ->drupalCreateUser();
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'administer content types',
      'access content',
      'access administration pages',
      'administer fields',
    ));
    variable_set('clean_url', 0);
  }

  /**
   *
   */
  protected function assertNoFieldCheckedByName($name, $message = '') {
    $elements = $this
      ->xpath('//input[@name=:name]', array(
      ':name' => $name,
    ));
    return $this
      ->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : t('Checkbox field @name is not checked.', array(
      '@name' => $name,
    )), t('Browser'));
  }

  /**
   *
   */
  protected function assertFieldCheckedByName($name, $message = '') {
    $elements = $this
      ->xpath('//input[@name=:name]', array(
      ':name' => $name,
    ));
    return $this
      ->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : t('Checkbox field @name is checked.', array(
      '@name' => $name,
    )), t('Browser'));
  }

  /**
   *
   */
  public function assertNameFormat($name_components, $type, $object, $format, $expected, array $options = array()) {
    $this
      ->assertNameFormats($name_components, $type, $object, array(
      $format => $expected,
    ), $options);
  }

  /**
   *
   */
  public function assertNameFormats($name_components, $type, $object, array $names, array $options = array()) {
    foreach ($names as $format => $expected) {
      $value = name_format($name_components, $format, array(
        'object' => $object,
        'type' => $type,
      ));
      $this
        ->assertIdentical($value, $expected, t("Name value for '@name' was '@actual', expected value '@expected'. Components were: %components", array(
        '@name' => $format,
        '@actual' => $value,
        '@expected' => $expected,
        '%components' => implode(' ', $name_components),
      )));
    }
  }

}

/**
 *
 */
class NameUnitTestCase extends NameTestHelper {

  /**
   *
   */
  public static function getInfo() {
    return array(
      'name' => 'Name unit tests',
      'description' => 'Test basic, low-level name functions.',
      'group' => 'Name',
    );
  }

  /**
   * Test name_format().
   */
  public function testGetInvalidTokens() {
    $names = array(
      'given' => array(
        'components' => array(
          'given' => 'John',
        ),
        'tests' => array(
          // Test that only the given name creates a entry.
          // Title.
          't' => '',
          // Given name.
          'g' => 'John',
          // Escaped letter.
          '\\g' => 'g',
          // Preferred name.
          'p' => 'John',
          // Preferred name without fallback.
          'q' => '',
          // Middle name(s)
          'm' => '',
          // Family name.
          'f' => '',
          // Credentials.
          'c' => '',
          // Generational suffix.
          's' => '',
          // First letter of the preferred or given.
          'w' => 'J',
          // First letter of the preferred without fallback.
          'v' => '',
          // First letter given.
          'x' => 'J',
          // First letter middle.
          'y' => '',
          // First letter family.
          'z' => '',
          // Either the given or family name. Given name is given preference.
          'e' => 'John',
          // Either the given or family name. Family name is given preference.
          'E' => 'John',
          // Combination test.
          // Using a single space.
          'g f' => 'John ',
          // Separator 1.
          'gif' => 'John ',
          // Separator 2.
          'gjf' => 'John, ',
          // Separator 3.
          'gkf' => 'John',
          'f g' => ' John',
          'fig' => ' John',
          'fjg' => ', John',
          'fkg' => 'John',
          't g t' => ' John ',
          'tigit' => ' John ',
          'tjgjt' => ', John, ',
          'tkgkt' => 'John',
          // Modifier entries.
          // Lowercase.
          'Lg' => 'john',
          // Uppercase.
          'Ug' => 'JOHN',
          // First letter to uppercase.
          'Fg' => 'John',
          // First letter of all words to uppercase.
          'Gg' => 'John',
          // lowercase, first letter to uppercase.
          'LF(g)' => 'John',
          // lowercase, first letter of all words to uppercase.
          'LG(g)' => 'John',
          // lowercase, first letter to uppercase.
          'LFg' => 'John',
          // lowercase, first letter of all words to uppercase.
          'LGg' => 'John',
          // Trims whitespace around the next token.
          'Tg' => 'John',
          // check_plain.
          'Sg' => 'John',
          // Conditional entries.
          // Brackets.
          '(((g)))' => 'John',
          // Brackets - mismatched.
          '(g))()(' => 'John)(',
          // Insert the token if both the surrounding tokens are not empty.
          'g+ f' => 'John',
          // Insert the token, if and only if the next token after it is not empty.
          'g= f' => 'John',
          // Skip the token, if and only if the next token after it is not empty.
          'g^ f' => 'John ',
          // Uses only the first one.
          's|c|g|m|f|t' => 'John',
          // Uses the previous token unless empty, otherwise it uses this token.
          'g|f' => 'John',
          // Real world examples.
          // Full name with a comma-space before credentials.
          'L(t= g= m= f= s=,(= c))' => ' john',
          // Full name with a comma-space before credentials. ucfirst does not work on a whitespace.
          'TS(LF(t= g= m= f= s)=,(= c))' => 'john',
          // Full name with a comma-space before credentials.
          'L(t+ g+ m+ f+ s+,(= c))' => 'john',
          // Full name with a comma-space before credentials.
          'TS(LF(t+ g+ m+ f+ s)+,(= c))' => 'John',
        ),
      ),
      'full' => array(
        'components' => array(
          'title' => 'MR.',
          'given' => 'JoHn',
          'middle' => 'pEter',
          'family' => 'dOE',
          'generational' => 'sR',
          'credentials' => 'b.Sc, pHd',
          'preferred' => 'peTe',
        ),
        // MR. JoHn pEter dOE sR b.Sc, pHd.
        'tests' => array(
          // Test that only the given name creates a entry.
          // Title.
          't' => 'MR.',
          // Given name.
          'g' => 'JoHn',
          // Preferred name.
          'p' => 'peTe',
          // Preferred name without fallback.
          'q' => 'peTe',
          // Middle name(s)
          'm' => 'pEter',
          // Family name.
          'f' => 'dOE',
          // Credentials.
          'c' => 'b.Sc, pHd',
          // Generational suffix.
          's' => 'sR',
          // First letter of the preferred or given.
          'w' => 'p',
          // First letter of the preferred without fallback.
          'v' => 'p',
          // First letter given.
          'x' => 'J',
          // First letter middle.
          'y' => 'p',
          // First letter family.
          'z' => 'd',
          // Either the given or family name. Given name is given preference.
          'e' => 'JoHn',
          // Either the given or family name. Family name is given preference.
          'E' => 'dOE',
          // Combination test.
          // Using a single space.
          'g f' => 'JoHn dOE',
          // Separator 1.
          'gif' => 'JoHn dOE',
          // Separator 2.
          'gjf' => 'JoHn, dOE',
          // Separator 3.
          'gkf' => 'JoHndOE',
          'f g' => 'dOE JoHn',
          'fig' => 'dOE JoHn',
          'fjg' => 'dOE, JoHn',
          'fkg' => 'dOEJoHn',
          't g t' => 'MR. JoHn MR.',
          'tigit' => 'MR. JoHn MR.',
          'tjgjt' => 'MR., JoHn, MR.',
          'tkgkt' => 'MR.JoHnMR.',
          // Modifier entries.
          // Lowercase.
          'L(t g m f s c)' => 'mr. john peter doe sr b.sc, phd',
          // Uppercase.
          'U(t g m f s c)' => 'MR. JOHN PETER DOE SR B.SC, PHD',
          // First letter to uppercase.
          'F(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // First letter of all words to uppercase.
          'G(t g m f s c)' => 'MR. JoHn PEter DOE SR B.Sc, PHd',
          // First letter to uppercase.
          'LF(t g m f s c)' => 'Mr. john peter doe sr b.sc, phd',
          // First letter of all words to uppercase.
          'LG(t g m f s c)' => 'Mr. John Peter Doe Sr B.sc, Phd',
          // Trims whitespace around the next token.
          'T(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // check_plain.
          'S(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // Conditional entries.
          // Brackets.
          '(((t g m f s c)))' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // Brackets - mismatched.
          '(t g m f s c))()(' => 'MR. JoHn pEter dOE sR b.Sc, pHd)(',
          // Insert the token, if and only if the next token after it is not empty.
          't= g= m= f= s= c' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // Uses the previous token unless empty, otherwise it uses this token.
          'g|m|f' => 'JoHn',
          // Uses the previous token unless empty, otherwise it uses this token.
          'm|f|g' => 'pEter',
          // Uses only the first one.
          's|c|g|m|f|t' => 'sR',
          // Real world examples.
          // Full name with a comma-space before credentials.
          'L(t= g= m= f= s=,(= c))' => 'mr. john peter doe sr, b.sc, phd',
          // Full name with a comma-space before credentials.
          'TS(LG(t= g= m= f= s)=,LG(= c))' => 'Mr. John Peter Doe Sr, B.sc, Phd',
          // Full name including preferred name (nickname).
          // Note G only works on a space boundary.
          'TS(LG(((t+ig+i(=\\(q-\\)))+im)+if)+iLG(s))' => 'Mr. John (pete) Peter Doe Sr',
        ),
      ),
    );
    foreach ($names as $title => $info) {
      $this
        ->assertNameFormats($info['components'], NULL, NULL, $info['tests']);
    }
  }

}

/**
 * Tests for the admin settings and custom format page.
 */
class Name_AdminTestCase extends NameTestHelper {

  /**
   *
   */
  public static function getInfo() {
    return array(
      'name' => 'Admin Setting Pages',
      'description' => 'Various tests on the admin area settings.',
      'group' => 'Name',
    );
  }

  /**
   * The most basic test. This should only fail if there is a change to the
   * Drupal API.
   */
  public function testAdminSettings() {
    global $base_path;

    // Default settings and system settings.
    $this
      ->drupalLogin($this->admin_user);

    // The default installed formats.
    $this
      ->drupalGet('admin/config/content/name');
    $row_template = array(
      'title href' => '//tr[@id="name-id"]/td[1]/a/@href',
      'title' => '//tr[@id="name-id"]/td[1]/a',
      'machine' => '//tr[@id="name-id"]/td[2]',
      'code' => '//tr[@id="name-id"]/td[3]',
      'formatted' => '//tr[@id="name-id"]/td[4]',
      'edit' => '//tr[@id="name-id"]/td[5]/a',
      'edit link' => '//tr[@id="name-id"]/td[5]/a/@href',
      'delete' => '//tr[@id="name-id"]/td[5]/a',
      'delete link' => '//tr[@id="name-id"]/td[5]/a/@href',
    );
    $all_values = array(
      0 => array(
        'title href' => $base_path . '?q=admin/config/content/name/settings',
        'title' => t('Default'),
        'machine' => 'default',
        'code' => '((((t+ig)+im)+if)+is)+jc',
        'formatted' => 'Mr Joe John Peter Mark Doe Jnr., B.Sc., Ph.D. JOAN SUE DOE Prince ',
      ),
      1 => array(
        'title href' => $base_path . '?q=admin/config/content/name/1',
        'title' => t('Full'),
        'machine' => 'full',
        'code' => '((((t+ig)+im)+if)+is)+jc',
        'formatted' => 'Mr Joe John Peter Mark Doe Jnr., B.Sc., Ph.D. JOAN SUE DOE Prince ',
        'edit' => t('Edit'),
        'edit link' => $base_path . '?q=admin/config/content/name/1',
        'delete' => t('Delete'),
        'delete link' => $base_path . '?q=admin/config/content/name/1/delete',
      ),
      2 => array(
        'title href' => $base_path . '?q=admin/config/content/name/2',
        'title' => t('Given'),
        'machine' => 'given',
        'code' => 'g',
        'formatted' => 'Joe JOAN Prince ',
        'edit' => t('Edit'),
        'edit link' => $base_path . '?q=admin/config/content/name/2',
        'delete' => t('Delete'),
        'delete link' => $base_path . '?q=admin/config/content/name/2/delete',
      ),
      3 => array(
        'title href' => $base_path . '?q=admin/config/content/name/3',
        'title' => t('Family'),
        'machine' => 'family',
        'code' => 'f',
        'formatted' => 'Doe DOE  ',
        'edit link' => $base_path . '?q=admin/config/content/name/3',
        'delete link' => $base_path . '?q=admin/config/content/name/3/delete',
      ),
      4 => array(
        'title href' => $base_path . '?q=admin/config/content/name/4',
        'title' => t('Title Family'),
        'machine' => 'formal',
        'code' => 't+if',
        'formatted' => 'Mr Doe DOE  ',
        'edit link' => $base_path . '?q=admin/config/content/name/4',
        'delete link' => $base_path . '?q=admin/config/content/name/4/delete',
      ),
      5 => array(
        'title href' => $base_path . '?q=admin/config/content/name/5',
        'title' => t('Given Family'),
        'machine' => 'short_full',
        'code' => 'g+if',
        'formatted' => 'Joe Doe JOAN DOE Prince ',
        'edit link' => $base_path . '?q=admin/config/content/name/5',
        'delete link' => $base_path . '?q=admin/config/content/name/5/delete',
      ),
    );
    foreach ($all_values as $id => $row) {
      foreach ($row as $cell_code => $value) {
        $xpath = str_replace('name-id', 'name-' . $id, $row_template[$cell_code]);
        $raw_xpath = $this
          ->xpath($xpath);
        if (!is_array($raw_xpath)) {
          $results = '__MISSING__';
        }
        elseif ($cell_code == 'delete' || $cell_code == 'delete link') {
          $results = $raw_xpath[1];
        }
        else {
          $results = current($raw_xpath);
        }
        $this
          ->assertEqual($results, $value, "Testing {$cell_code} on row {$id} using '{$xpath}' and expecting '" . check_plain($value) . "', got '" . check_plain($results) . "'.");
      }
    }
    $raw_xpath = $this
      ->xpath('//tr[@id="name-0"]/td[5]/a');
    $results = $raw_xpath ? current($raw_xpath) : '__MISSING__';
    $this
      ->assertEqual($results, t('Edit'), "Testing edit on row 0 using '//tr[@id=\"name-id\"]/td[1]/a' and expecting 'Edit', got '{$results}'.");
    $raw_xpath = $this
      ->xpath('//tr[@id="name-0"]/td[5]/a/@href');
    $results = $raw_xpath ? current($raw_xpath) : '__MISSING__';
    $this
      ->assertEqual($results, $base_path . '?q=admin/config/content/name/settings', "Testing edit link on row 0 using '//tr[@id=\"name-id\"]/td[1]/a/@href' and expecting {$base_path} . '?q=admin/config/content/name/settings', got '{$results}'.");
    $this
      ->drupalGet('admin/config/content/name/settings');

    // Fieldset rendering check.
    $this
      ->assertRaw('<legend><span class="fieldset-legend">Format string help</span></legend><div class="fieldset-wrapper">', 'Testing the help fieldgroup');
    $default_values = array(
      'name_settings[default_format]' => 't+ig+im+if+is+kc',
      'name_settings[sep1]' => ' ',
      'name_settings[sep2]' => ', ',
      'name_settings[sep3]' => '',
    );
    foreach ($default_values as $name => $value) {
      $this
        ->assertField($name, $value);
    }

    // ID example.
    $this
      ->assertFieldById('edit-name-settings-sep1', ' ', t('Sep 3 default value.'));
    $post_values = $default_values;
    $post_values['name_settings[default_format]'] = '';
    $this
      ->drupalPost('admin/config/content/name/settings', $post_values, t('Save configuration'));
    $this
      ->assertText(t('Default format field is required.'));
    $post_values['name_settings[default_format]'] = '     ';
    $this
      ->drupalPost('admin/config/content/name/settings', $post_values, t('Save configuration'));
    $this
      ->assertText(t('Default format field is required.'));
    $test_values = array(
      'name_settings[default_format]' => 'c+ks+if+im+ig+t',
      'name_settings[sep1]' => '~',
      'name_settings[sep2]' => '^',
      'name_settings[sep3]' => '-',
    );
    $this
      ->drupalPost('admin/config/content/name/settings', $test_values, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));
    foreach ($test_values as $name => $value) {
      $this
        ->assertField($name, $value);
    }

    // The default installed formats and the updated default format.
    $this
      ->drupalGet('admin/config/content/name');
    $xpath = '//tr[@id="name-0"]/td[3]';
    $this
      ->assertEqual(current($this
      ->xpath($xpath)), 'c+ks+if+im+ig+t', 'Default is equal to set default.');
    $this
      ->drupalGet('admin/config/content/name/add');
    $this
      ->assertRaw('<legend><span class="fieldset-legend">Format string help</span></legend><div class="fieldset-wrapper">', 'Testing the help fieldgroup');
    $values = array(
      'name' => '',
      'machine_name' => '',
      'format' => '',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    foreach (array(
      t('Name'),
      t('Machine-readable name'),
      t('Format'),
    ) as $title) {
      $this
        ->assertText(t('!field field is required', array(
        '!field' => $title,
      )));
    }
    $values = array(
      'name' => 'given',
      'machine_name' => '1234567890abcdefghijklmnopqrstuvwxyz_',
      'format' => 'a',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    $this
      ->assertText(t('The name you have chosen is already in use.'));
    $this
      ->assertNoText(t('!field field is required', array(
      '!field' => t('Format'),
    )));
    $this
      ->assertNoText(t('!field field is required', array(
      '!field' => t('Machine-readable name'),
    )));
    $values = array(
      'name' => 'given',
      'machine_name' => '%&*(',
      'format' => 'a',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    $this
      ->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
    $values = array(
      'name' => 'given',
      'machine_name' => 'given',
      'format' => 'a',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    $this
      ->assertText(t('The machine-readable name you have chosen is already in use.'));
    $values = array(
      'name' => 'given',
      'machine_name' => 'default',
      'format' => 'a',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    $this
      ->assertText(t('The machine-readable name you have chosen is reserved.'));
    $values = array(
      'name' => 'Test',
      'machine_name' => 'test',
      'format' => 'abc',
    );
    $this
      ->drupalPost('admin/config/content/name/add', $values, t('Save'));
    $this
      ->assertText(t('Custom format Test has been created.'));
    $new_rows = array(
      6 => array(
        'title href' => $base_path . '?q=admin/config/content/name/6',
        'title' => 'Test',
        'machine' => 'test',
        'code' => 'abc',
        'formatted' => 'abB.Sc., Ph.D. ab ab ',
        'edit link' => $base_path . '?q=admin/config/content/name/6',
        'delete link' => $base_path . '?q=admin/config/content/name/6/delete',
      ),
    );
    foreach ($new_rows as $id => $row) {
      foreach ($row as $cell_code => $value) {
        $xpath = str_replace('name-id', 'name-' . $id, $row_template[$cell_code]);
        $raw_xpath = $this
          ->xpath($xpath);
        if (!is_array($raw_xpath)) {
          $results = '__MISSING__';
        }
        elseif ($cell_code == 'delete' || $cell_code == 'delete link') {
          $results = count($raw_xpath) > 1 ? $raw_xpath[1] : '__MISSING__';
        }
        else {
          $results = current($raw_xpath);
        }
        $this
          ->assertEqual($results, $value, "Testing {$cell_code} on row {$id} using '{$xpath}' and expecting '{$value}', got '{$results}'.");
      }
    }
    $values = array(
      'name' => 'new name',
      'machine_name' => 'bob',
      'format' => 'f+g',
    );
    $this
      ->drupalPost('admin/config/content/name/6', $values, t('Save'));
    $this
      ->assertText(t('Custom format new name has been updated.'));
    $new_rows = array(
      6 => array(
        'title' => $values['name'],
        'machine' => $values['machine_name'],
        'code' => $values['format'],
      ),
    );
    foreach ($new_rows as $id => $row) {
      foreach ($row as $cell_code => $value) {
        $xpath = str_replace('name-id', 'name-' . $id, $row_template[$cell_code]);
        $raw_xpath = $this
          ->xpath($xpath);
        if (!is_array($raw_xpath)) {
          $results = '__MISSING__';
        }
        elseif ($cell_code == 'delete' || $cell_code == 'delete link') {
          $results = count($raw_xpath) > 1 ? $raw_xpath[1] : '__MISSING__';
        }
        else {
          $results = current($raw_xpath);
        }
        $this
          ->assertEqual($results, $value, "Testing {$cell_code} on row {$id} using '{$xpath}' and expecting '{$value}', got '{$results}'.");
      }
    }
    $this
      ->drupalGet('admin/config/content/name/60');
    $this
      ->assertText(t('The custom format could not be found.'));
    $this
      ->drupalGet('admin/config/content/name/60/delete');
    $this
      ->assertText(t('The custom format could not be found.'));
    $this
      ->drupalPost('admin/config/content/name/4', array(), t('Delete'));
    $this
      ->assertText(t('Are you sure you want to delete the custom format !title ("t+if")?', array(
      '!title' => check_plain(t('Title Family')),
    )));
    $this
      ->drupalPost(NULL, array(
      'confirm' => 1,
    ), t('Delete'));
    $this
      ->assertText(t('The custom name format !title was deleted.', array(
      '!title' => check_plain('Title Family'),
    )));
  }

}

/**
 * Tests for the admin settings and custom format page.
 */
class Name_FieldTestCase extends NameTestHelper {

  /**
   *
   */
  public static function getInfo() {
    return array(
      'name' => 'Node Name Field',
      'description' => 'Various tests on creating a name field on a node.',
      'group' => 'Name',
    );
  }

  /**
   * The most basic test. This should only fail if there is a change to the
   * Drupal API.
   */
  public function testFieldEntry() {
    $this
      ->drupalLogin($this->admin_user);
    $new_name_field = array(
      'fields[_add_new_field][label]' => 'Test name',
      'fields[_add_new_field][field_name]' => 'name_test',
      'fields[_add_new_field][type]' => 'name',
      'fields[_add_new_field][widget_type]' => 'name_widget',
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields', $new_name_field, t('Save'));

    // Required test.
    $field_settings = array();
    foreach ($this
      ->name_getFieldSettings() as $key => $value) {
      $field_settings[$key] = '';
    }
    foreach ($this
      ->name_getFieldSettingsCheckboxes() as $key => $value) {
      $field_settings[$key] = FALSE;
    }
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $field_settings, t('Save field settings'));
    $n = _name_translations();
    $required_messages = array(
      t('Label for !field field is required.', array(
        '!field' => $n['title'],
      )),
      t('Label for !field field is required.', array(
        '!field' => $n['given'],
      )),
      t('Label for !field field is required.', array(
        '!field' => $n['middle'],
      )),
      t('Label for !field field is required.', array(
        '!field' => $n['family'],
      )),
      t('Label for !field field is required.', array(
        '!field' => $n['generational'],
      )),
      t('Label for !field field is required.', array(
        '!field' => $n['credentials'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['title'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['given'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['middle'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['family'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['generational'],
      )),
      t('Maximum length for !field field is required.', array(
        '!field' => $n['credentials'],
      )),
      t('!field options field is required.', array(
        '!field' => $n['title'],
      )),
      t('!field options field is required.', array(
        '!field' => $n['generational'],
      )),
      // There is now empty tests on both of these fields, so the second
      // message will not show if the required message is shown.
      t('!field field is required.', array(
        '!field' => t('Components'),
      )),
      t('!field field is required.', array(
        '!field' => t('Minimum components'),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }
    $field_settings = array(
      'field[settings][components][title]' => FALSE,
      'field[settings][components][given]' => TRUE,
      'field[settings][components][middle]' => FALSE,
      'field[settings][components][family]' => TRUE,
      'field[settings][components][generational]' => FALSE,
      'field[settings][components][credentials]' => FALSE,
      'field[settings][minimum_components][title]' => TRUE,
      'field[settings][minimum_components][given]' => FALSE,
      'field[settings][minimum_components][middle]' => FALSE,
      'field[settings][minimum_components][family]' => FALSE,
      'field[settings][minimum_components][generational]' => TRUE,
      'field[settings][minimum_components][credentials]' => TRUE,
      'field[settings][max_length][title]' => 0,
      'field[settings][max_length][given]' => -456,
      'field[settings][max_length][middle]' => 'asdf',
      'field[settings][max_length][family]' => 3454,
      'field[settings][max_length][generational]' => 4.5,
      'field[settings][max_length][credentials]' => 'NULL',
      'field[settings][title_options]' => "-- --\nMr.\nMrs.\nMiss\nMs.\nDr.\nProf.\n[vocabulary:machine]",
      'field[settings][generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\n[vocabulary:123]",
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $field_settings, t('Save field settings'));
    $required_messages = array(
      t('!components can not be selected for !label when they are not selected for !label2.', array(
        '!label' => t('Minimum components'),
        '!label2' => t('Components'),
        '!components' => check_plain(implode(', ', array(
          $n['title'],
          $n['generational'],
          $n['credentials'],
        ))),
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['title'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['given'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['middle'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['family'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['generational'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['credentials'],
      )),
      t('!field must have one of the following components: !components', array(
        '!field' => t('Minimum components'),
        '!components' => check_plain(implode(', ', array(
          $n['given'],
          $n['family'],
        ))),
      )),
      t("The vocabulary 'machine' in !field could not be found.", array(
        '!field' => t('!title options', array(
          '!title' => $n['title'],
        )),
      )),
      t("The vocabulary '123' in !field could not be found.", array(
        '!field' => t('!generational options', array(
          '!generational' => $n['generational'],
        )),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }

    // Make sure option lengths do not exceed the title lengths.
    $field_settings = array(
      'field[settings][max_length][title]' => 5,
      'field[settings][max_length][generational]' => 3,
      'field[settings][title_options]' => "Aaaaa.\n-- --\nMr.\nMrs.\nBbbbbbbb\nMiss\nMs.\nDr.\nProf.\nCcccc.",
      'field[settings][generational_options]' => "AAAA\n-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\nBBBB",
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $field_settings, t('Save field settings'));
    $required_messages = array(
      t('The following options exceed the maximun allowed !field length: Aaaaa., Bbbbbbbb, Ccccc.', array(
        '!field' => t('!title options', array(
          '!title' => $n['title'],
        )),
      )),
      t('The following options exceed the maximun allowed !field length: AAAA, VIII, BBBB', array(
        '!field' => t('!generational options', array(
          '!generational' => $n['generational'],
        )),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }

    // Make sure option have at least one valid option.
    $field_settings = array(
      'field[settings][title_options]' => " \n-- --\n ",
      'field[settings][generational_options]' => " \n-- --\n ",
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $field_settings, t('Save field settings'));
    $required_messages = array(
      t('!field are required.', array(
        '!field' => t('!title options', array(
          '!title' => $n['title'],
        )),
      )),
      t('!field are required.', array(
        '!field' => t('!generational options', array(
          '!generational' => $n['generational'],
        )),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }

    // Make sure option have at least one valid only have one default value.
    $field_settings = array(
      'field[settings][title_options]' => "-- --\nMr.\nMrs.\nMiss\n-- Bob\nDr.\nProf.",
      'field[settings][generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\n--",
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $field_settings, t('Save field settings'));
    $required_messages = array(
      t('!field can only have one blank value assigned to it.', array(
        '!field' => t('!title options', array(
          '!title' => $n['title'],
        )),
      )),
      t('!field can only have one blank value assigned to it.', array(
        '!field' => t('!generational options', array(
          '!generational' => $n['generational'],
        )),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }

    // Save the field again with the default values.
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test/field-settings', $this
      ->name_getFieldSettings(), t('Save field settings'));
    $this
      ->assertText(t('Updated field Test name field settings.'));

    // Now the widget settings...
    // First, check that field validation is working... cut n paste from above test.
    $field_settings = array(
      'field[settings][components][title]' => FALSE,
      'field[settings][components][given]' => TRUE,
      'field[settings][components][middle]' => FALSE,
      'field[settings][components][family]' => TRUE,
      'field[settings][components][generational]' => FALSE,
      'field[settings][components][credentials]' => FALSE,
      'field[settings][minimum_components][title]' => TRUE,
      'field[settings][minimum_components][given]' => FALSE,
      'field[settings][minimum_components][middle]' => FALSE,
      'field[settings][minimum_components][family]' => FALSE,
      'field[settings][minimum_components][generational]' => TRUE,
      'field[settings][minimum_components][credentials]' => TRUE,
      'field[settings][max_length][title]' => 0,
      'field[settings][max_length][given]' => -456,
      'field[settings][max_length][middle]' => 'asdf',
      'field[settings][max_length][family]' => 3454,
      'field[settings][max_length][generational]' => 4.5,
      'field[settings][max_length][credentials]' => 'NULL',
      'field[settings][title_options]' => "-- --\nMr.\nMrs.\nMiss\nMs.\nDr.\nProf.\n[vocabulary:machine]",
      'field[settings][generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX\n[vocabulary:123]",
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/field_name_test', $field_settings, t('Save settings'));
    $required_messages = array(
      t('!components can not be selected for !label when they are not selected for !label2.', array(
        '!label' => t('Minimum components'),
        '!label2' => t('Components'),
        '!components' => check_plain(implode(', ', array(
          $n['title'],
          $n['generational'],
          $n['credentials'],
        ))),
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['title'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['given'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['middle'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['family'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['generational'],
      )),
      t('!field must be a positive integer between 1 and 255.', array(
        '!field' => $n['credentials'],
      )),
      t('!field must have one of the following components: !components', array(
        '!field' => t('Minimum components'),
        '!components' => check_plain(implode(', ', array(
          $n['given'],
          $n['family'],
        ))),
      )),
      t("The vocabulary 'machine' in !field could not be found.", array(
        '!field' => t('!title options', array(
          '!title' => $n['title'],
        )),
      )),
      t("The vocabulary '123' in !field could not be found.", array(
        '!field' => t('!generational options', array(
          '!generational' => $n['generational'],
        )),
      )),
    );
    foreach ($required_messages as $message) {
      $this
        ->assertText($message);
    }
    $widget_settings = array(
      // title, description, none.
      'instance[settings][title_display][title]' => 'description',
      'instance[settings][title_display][given]' => 'description',
      'instance[settings][title_display][middle]' => 'description',
      'instance[settings][title_display][family]' => 'description',
      'instance[settings][title_display][generational]' => 'description',
      'instance[settings][title_display][credentials]' => 'description',
      'instance[settings][size][title]' => 6,
      'instance[settings][size][given]' => 20,
      'instance[settings][size][middle]' => 20,
      'instance[settings][size][family]' => 20,
      'instance[settings][size][generational]' => 5,
      'instance[settings][size][credentials]' => 35,
      'instance[settings][inline_css][title]' => '',
      'instance[settings][inline_css][given]' => '',
      'instance[settings][inline_css][middle]' => '',
      'instance[settings][inline_css][family]' => '',
      'instance[settings][inline_css][generational]' => '',
      'instance[settings][inline_css][credentials]' => '',
    );
    $this
      ->drupalGet('admin/structure/types/manage/page/fields/field_name_test');
    foreach ($widget_settings as $name => $value) {
      $this
        ->assertFieldByName($name, $value);
    }
  }

  /**
   * Tests that enabled components are shown on the field widget.
   */
  public function testNameWidget() {

    // Create a field for the user entity type. Enable all components.
    // For minimum components, only enable 'family'.
    field_create_field(array(
      'field_name' => 'name_test',
      'type' => 'name',
      'settings' => array(
        'allow_family_or_given' => 0,
        'autocomplete_separator' => array(
          'credentials' => ', ',
          'family' => ' -',
          'generational' => ' ',
          'given' => ' -',
          'middle' => ' -',
          'title' => ' ',
        ),
        'autocomplete_source' => array(
          'credentials' => array(),
          'family' => array(),
          'generational' => array(
            'generational' => 0,
          ),
          'given' => array(),
          'middle' => array(),
          'title' => array(
            'title' => 0,
          ),
        ),
        'components' => array(
          'credentials' => 'credentials',
          'family' => 'family',
          'generational' => 'generational',
          'given' => 'given',
          'middle' => 'middle',
          'title' => 'title',
        ),
        'max_length' => array(
          'credentials' => 255,
          'family' => 63,
          'generational' => 15,
          'given' => 63,
          'middle' => 20,
          'title' => 31,
        ),
        'minimum_components' => array(
          'credentials' => 0,
          'family' => 'family',
          'generational' => 0,
          'given' => 0,
          'middle' => 0,
          'title' => 0,
        ),
        'sort_options' => array(
          'generational' => 'generational',
          'title' => 'title',
        ),
      ),
    ));

    // Create a field instance. Don't override component settings.
    field_create_instance(array(
      'field_name' => 'name_test',
      'entity_type' => 'user',
      'bundle' => 'user',
      'settings' => array(
        'component_css' => '',
        'component_layout' => 'default',
        'components' => array(
          'credentials' => 0,
          'family' => 0,
          'generational' => 0,
          'given' => 0,
          'middle' => 0,
          'title' => 0,
        ),
        'credentials_inline' => 0,
        'generational_field' => 'select',
        'field_type' => array(
          'credentials' => 'text',
          'family' => 'text',
          'generational' => 'select',
          'given' => 'text',
          'middle' => 'text',
          'title' => 'select',
        ),
        'minimum_components' => array(
          'credentials' => 0,
          'family' => 'family',
          'generational' => 0,
          'given' => 0,
          'middle' => 0,
          'title' => 0,
        ),
        'name_user_preferred' => 0,
        'override_format' => 'default',
        'preferred_field_reference' => '',
        'show_component_required_marker' => 1,
        'size' => array(
          'credentials' => 35,
          'family' => 20,
          'generational' => 5,
          'given' => 20,
          'middle' => 20,
          'title' => 6,
        ),
        'title_display' => array(
          'credentials' => 'title',
          'family' => 'title',
          'generational' => 'title',
          'given' => 'title',
          'middle' => 'title',
          'title' => 'title',
        ),
        'title_field' => 'select',
        'user_register_form' => 1,
      ),
      'widget' => array(
        'type' => 'name_widget',
      ),
      'display' => array(
        'default' => array(
          'type' => 'name_formatter',
        ),
      ),
    ));

    // Login as webuser.
    $web_user = $this
      ->drupalCreateUser(array(
      'access content',
    ));
    $this
      ->drupalLogin($web_user);

    // Go to own edit account page.
    $this
      ->drupalGet('user/' . $web_user->uid . '/edit');

    // Assert expected fields.
    $expected_fields = array(
      'name_test[und][0][credentials]',
      'name_test[und][0][family]',
      'name_test[und][0][generational]',
      'name_test[und][0][given]',
      'name_test[und][0][middle]',
      'name_test[und][0][title]',
    );
    foreach ($expected_fields as $name) {
      $this
        ->assertFieldByName($name);
    }
  }

  /**
   *
   */
  public function name_getFieldSettings() {
    $field_settings = array(
      'field[settings][components][title]' => TRUE,
      'field[settings][components][given]' => TRUE,
      'field[settings][components][middle]' => TRUE,
      'field[settings][components][family]' => TRUE,
      'field[settings][components][generational]' => TRUE,
      'field[settings][components][credentials]' => TRUE,
      'field[settings][minimum_components][title]' => FALSE,
      'field[settings][minimum_components][given]' => TRUE,
      'field[settings][minimum_components][middle]' => FALSE,
      'field[settings][minimum_components][family]' => TRUE,
      'field[settings][minimum_components][generational]' => FALSE,
      'field[settings][minimum_components][credentials]' => FALSE,
      'field[settings][max_length][title]' => 31,
      'field[settings][max_length][given]' => 63,
      'field[settings][max_length][middle]' => 127,
      'field[settings][max_length][family]' => 63,
      'field[settings][max_length][generational]' => 15,
      'field[settings][max_length][credentials]' => 255,
      'field[settings][labels][title]' => t('Title'),
      'field[settings][labels][given]' => t('Given'),
      'field[settings][labels][middle]' => t('Middle name(s)'),
      'field[settings][labels][family]' => t('Family'),
      'field[settings][labels][generational]' => t('Generational'),
      'field[settings][labels][credentials]' => t('Credentials'),
      'field[settings][sort_options][title]' => TRUE,
      'field[settings][sort_options][generational]' => FALSE,
      'field[settings][title_options]' => "-- --\nMr.\nMrs.\nMiss\nMs.\nDr.\nProf.",
      'field[settings][generational_options]' => "-- --\nJr.\nSr.\nI\nII\nIII\nIV\nV\nVI\nVII\nVIII\nIX\nX",
    );
    return $field_settings;
  }

  /**
   *
   */
  public function name_getFieldSettingsCheckboxes() {
    $field_settings = array(
      'field[settings][components][title]' => TRUE,
      'field[settings][components][given]' => TRUE,
      'field[settings][components][middle]' => TRUE,
      'field[settings][components][family]' => TRUE,
      'field[settings][components][generational]' => TRUE,
      'field[settings][components][credentials]' => TRUE,
      'field[settings][minimum_components][title]' => FALSE,
      'field[settings][minimum_components][given]' => TRUE,
      'field[settings][minimum_components][middle]' => FALSE,
      'field[settings][minimum_components][family]' => TRUE,
      'field[settings][minimum_components][generational]' => FALSE,
      'field[settings][minimum_components][credentials]' => FALSE,
      'field[settings][sort_options][title]' => TRUE,
      'field[settings][sort_options][generational]' => FALSE,
    );
    return $field_settings;
  }

}

Classes

Namesort descending Description
NameTestHelper Helper test class with some added functions for testing.
NameUnitTestCase
Name_AdminTestCase Tests for the admin settings and custom format page.
Name_FieldTestCase Tests for the admin settings and custom format page.