You are here

name.test in Name Field 6

Same filename and directory in other branches
  1. 7 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 {
  function setUp() {

    // Call parent::setUp() allowing test cases to pass further modules.
    $modules = func_get_args();
    $modules = array_merge(array(
      'content',
      'name',
    ), $modules);
    $parent_callback = 'parent::setUp';
    if (version_compare(PHP_VERSION, '5.3.0', '<')) {
      $parent_callback = array(
        $this,
        'parent::setUp',
      );
    }
    call_user_func_array($parent_callback, $modules);
  }
  function assertNameFormat($name_components, $type, $object, $format, $expected, array $options = array()) {
    $this
      ->assertNameFormats($name_components, $type, $object, array(
      $format => $expected,
    ), $options);
  }
  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
          't' => '',
          // Title
          'g' => 'John',
          // Given name
          '\\g' => 'g',
          // Escaped letter
          'm' => '',
          // Middle name(s)
          'f' => '',
          // Family name
          'c' => '',
          // Credentials
          's' => '',
          // Generational suffix
          'x' => 'J',
          // First letter given
          'y' => '',
          // First letter middle
          'z' => '',
          // First letter family
          'e' => 'John',
          // Either the given or family name. Given name is given preference
          'E' => 'John',
          // Either the given or family name. Family name is given preference
          // Combination test
          'g f' => 'John ',
          // Using a single space
          'gif' => 'John ',
          // Separator 1
          'gjf' => 'John, ',
          // Separator 2
          'gkf' => 'John',
          // Separator 3
          'f g' => ' John',
          'fig' => ' John',
          'fjg' => ', John',
          'fkg' => 'John',
          't g t' => ' John ',
          'tigit' => ' John ',
          'tjgjt' => ', John, ',
          'tkgkt' => 'John',
          // Modifier entries
          'Lg' => 'john',
          // lowercase
          'Ug' => 'JOHN',
          // uppercase
          'Fg' => 'John',
          // first letter to uppercase
          'Gg' => 'John',
          // first letter of all words to uppercase
          'LF(g)' => 'John',
          // lowercase, first letter to uppercase
          'LG(g)' => 'John',
          // lowercase, first letter of all words to uppercase
          'LFg' => 'John',
          // lowercase, first letter to uppercase
          'LGg' => 'John',
          // lowercase, first letter of all words to uppercase
          'Tg' => 'John',
          // Trims whitespace around the next token
          'Sg' => 'John',
          // check_plain
          // Conditional entries
          '(((g)))' => 'John',
          // brackets
          '(g))()(' => 'John)(',
          // brackets - mismatched
          'g+ f' => '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
          's|c|g|m|f|t' => 'John',
          // Uses only the first one.
          'g|f' => 'John',
          // Uses the previous token unless empty, otherwise it uses this token
          // Real world examples
          '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 name with a comma-space before credentials. ucfirst does not work on a whitespace
          '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',
        ),
        //MR. JoHn pEter dOE sR b.Sc, pHd
        'tests' => array(
          // Test that only the given name creates a entry
          't' => 'MR.',
          // Title
          'g' => 'JoHn',
          // Given name
          'm' => 'pEter',
          // Middle name(s)
          'f' => 'dOE',
          // Family name
          'c' => 'b.Sc, pHd',
          // Credentials
          's' => 'sR',
          // Generational suffix
          'x' => 'J',
          // First letter given
          'y' => 'p',
          // First letter middle
          'z' => 'd',
          // First letter family
          'e' => 'JoHn',
          // Either the given or family name. Given name is given preference
          'E' => 'dOE',
          // Either the given or family name. Family name is given preference
          // Combination test
          'g f' => 'JoHn dOE',
          // Using a single space
          'gif' => 'JoHn dOE',
          // Separator 1
          'gjf' => 'JoHn, dOE',
          // Separator 2
          'gkf' => 'JoHndOE',
          // Separator 3
          '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
          'L(t g m f s c)' => 'mr. john peter doe sr b.sc, phd',
          // lowercase
          'U(t g m f s c)' => 'MR. JOHN PETER DOE SR B.SC, PHD',
          // uppercase
          'F(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // first letter to uppercase
          'G(t g m f s c)' => 'MR. JoHn PEter DOE SR B.Sc, PHd',
          // first letter of all words to uppercase
          'LF(t g m f s c)' => 'Mr. john peter doe sr b.sc, phd',
          // first letter to uppercase
          'LG(t g m f s c)' => 'Mr. John Peter Doe Sr B.sc, Phd',
          // first letter of all words to uppercase
          'T(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // Trims whitespace around the next token
          'S(t g m f s c)' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // check_plain
          // Conditional entries
          '(((t g m f s c)))' => 'MR. JoHn pEter dOE sR b.Sc, pHd',
          // 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
          'g|m|f' => 'JoHn',
          // Uses the previous token unless empty, otherwise it uses this token
          'm|f|g' => 'pEter',
          // Uses the previous token unless empty, otherwise it uses this token
          's|c|g|m|f|t' => 'sR',
          // Uses only the first one.
          // Real world examples
          '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',
        ),
      ),
    );

    /*
     // Placeholders for token support insertion on the [object / key | entity / bundle].
        '1' => t('Token placeholder 1'),
        '2' => t('Token placeholder 2'),
        '3' => t('Token placeholder 3'),
        '4' => t('Token placeholder 4'),
        '5' => t('Token placeholder 5'),
        '6' => t('Token placeholder 6'),
        '7' => t('Token placeholder 7'),
        '8' => t('Token placeholder 8'),
        '9' => t('Token placeholder 9'),
    */
    foreach ($names as $title => $info) {
      $this
        ->assertNameFormats($info['components'], NULL, NULL, $info['tests']);
    }
  }

}

Classes

Namesort descending Description
NameTestHelper Helper test class with some added functions for testing.
NameUnitTestCase