You are here

public function CountryFieldTest::testCountryField in Country Field 7

Tests adding and editing values using a country field.

File

./country_field.test, line 73
Contains tests for country field functionality.

Class

CountryFieldTest
Ensures that country field works correctly.

Code

public function testCountryField() {
  $this
    ->drupalLogin($this->adminUser);

  // Add a new country field.
  $this
    ->drupalGet('admin/structure/types/manage/ponies/fields');
  $edit = array(
    'fields[_add_new_field][label]' => 'Foobar',
    'fields[_add_new_field][field_name]' => 'foobar',
    'fields[_add_new_field][type]' => 'country',
    'fields[_add_new_field][widget_type]' => 'options_select',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(), t('Save field settings'));
  $this
    ->drupalPost(NULL, array(
    'instance[settings][countries][]' => array(
      'NZ',
      'AU',
      'DE',
      'BG',
    ),
    'field[cardinality]' => '-1',
  ), t('Save settings'));
  $this
    ->assertRaw(t('Saved %name configuration', array(
    '%name' => 'Foobar',
  )));
  $this
    ->drupalPost('admin/structure/types/manage/ponies/display', array(
    'fields[field_foobar][type]' => 'list_default',
  ), t('Save'));

  // Test the fields exist.
  $this
    ->drupalGet('node/add/ponies');
  $this
    ->assertField('field_foobar[und][]', 'Found foobar country field');
  $this
    ->assertNoRaw('<option value="RO">Romania</option>', 'The list is limited. "RO" was not included.');

  // Submit data.
  $edit = array(
    'field_foobar[und][]' => array(
      'NZ',
      'AU',
      'DE',
    ),
    'title' => 'Barfoo',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $entities = entity_load('node', FALSE, array(
    'title' => 'Barfoo',
  ));
  $this
    ->assertEqual(1, count($entities), 'Entity was saved');
  $node = reset($entities);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText('Barfoo');
  $this
    ->assertText('New Zealand');
  $this
    ->assertText('Australia');
  $this
    ->assertText('Germany');
  $this
    ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 3, 'Three items in field');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][0]['value'], 'AU');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][1]['value'], 'DE');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][2]['value'], 'NZ');
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $edit = array(
    'title' => 'Bazbar',
    // Remove one child.
    'field_foobar[und][]' => array(
      'NZ',
      'AU',
    ),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText('Bazbar');

  // Reload entity.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 2, 'Two values in field');
}