You are here

function PhoneNumberUITest::testPhoneNumberCreate in Phone Number 6

Creates a cck_phone field for the "page" type and creates a page with a cck_phone.

File

tests/cck_phone.crud_input.test, line 79
Testing CRUD API for user input.

Class

PhoneNumberUITest
Testing that users can not input bad phone number or country code

Code

function testPhoneNumberCreate() {
  $account = $this
    ->drupalCreateUser(array(
    'administer content types',
    'access content',
    'create page content',
  ));
  $this
    ->drupalLogin($account);

  // create field
  $name = drupal_strtolower($this
    ->randomName());
  $edit = array(
    '_add_new_field[label]' => $name,
    '_add_new_field[field_name]' => $name,
    '_add_new_field[type]' => 'phone_number',
    '_add_new_field[widget_type]' => 'phone_number',
  );
  $this
    ->drupalPost('admin/content/node-type/page/fields', $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(
    'all_country_codes' => TRUE,
    'enable_custom_country' => TRUE,
  ), t('Save field settings'));

  // Is field created?
  $this
    ->assertRaw(t('Added field %label.', array(
    '%label' => $name,
  )), 'Field added');

  // Create page form
  $this
    ->drupalGet('node/add/page');
  $field_name = 'field_' . $name;
  $this
    ->assertField($field_name . '[0][number]', 'Number found');
  $this
    ->assertField($field_name . '[0][country_codes]', 'Country codes found');

  // Select a random country code
  $countrycodes = cck_phone_countrycodes();
  $cc_keys = array_keys($countrycodes);
  $cc = $cc_keys[mt_rand(0, count($cc_keys))];
  $bad_cc = $this
    ->randomString(2);
  while (in_array($bad_cc, $cc_keys)) {
    $bad_cc = $this
      ->randomString(2);
  }
  $input_test_cases = array(
    array(
      'number' => strval(mt_rand(10000, 9999999999)),
      'cc' => $cc,
      'msg' => 'Number found',
      'type' => self::PHONENUMBER_INPUT_TYPE_GOOD,
    ),
    array(
      'number' => strval(mt_rand(100, 999)) . '-' . strval(mt_rand(1000, 9999)),
      'cc' => $cc,
      'msg' => 'Number found',
      'type' => self::PHONENUMBER_INPUT_TYPE_GOOD,
    ),
    array(
      'number' => '(' . strval(mt_rand(100, 999)) . ') ' . strval(mt_rand(1000, 9999)),
      'cc' => $cc,
      'msg' => 'Number found',
      'type' => self::PHONENUMBER_INPUT_TYPE_GOOD,
    ),
    array(
      'number' => strval(mt_rand(100, 999)) . ' ' . strval(mt_rand(1000, 9999)),
      'cc' => $cc,
      'msg' => 'Number found',
      'type' => self::PHONENUMBER_INPUT_TYPE_GOOD,
    ),
    array(
      'number' => strval(mt_rand(1000000000000000, 99999999999999999)),
      'cc' => $cc,
      'msg' => 'Number too long',
      'type' => self::PHONENUMBER_INPUT_TYPE_BAD_NUMBER,
    ),
    array(
      'number' => $this
        ->randomString(8),
      'cc' => $cc,
      'msg' => 'non digit',
      'type' => self::PHONENUMBER_INPUT_TYPE_BAD_NUMBER,
    ),
  );
  foreach ($input_test_cases as $input) {
    $this
      ->drupalLogin($account);
    $this
      ->drupalGet('node/add/page');
    $edit = array(
      'title' => $input['number'],
      $field_name . '[0][number]' => $input['number'],
      $field_name . '[0][country_codes]' => $input['cc'],
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    if ($input['type'] == self::PHONENUMBER_INPUT_TYPE_BAD_NUMBER) {
      $this
        ->assertRaw(t('number must be digit only.', array(
        '%input' => $input['number'],
      )), 'Not a valid number: ' . $input['number']);
      continue;
    }
    else {
      $this
        ->assertRaw(t('@type %title has been created.', array(
        '@type' => 'Page',
        '%title' => $edit['title'],
      )), 'Page created: ' . $input['number']);
    }
    $url = $this
      ->getUrl();

    // Change to anonymous user
    $this
      ->drupalLogout();
    $this
      ->drupalGet($url);
    if ($input['type'] == self::PHONENUMBER_INPUT_TYPE_GOOD) {
      $phone_output = $countrycodes[$input['cc']]['code'] . '-' . cck_phone_clean_number($input['number']);
      $this
        ->assertRaw($phone_output);
    }
  }
}