You are here

country_field.test in Country Field 7

Contains tests for country field functionality.

File

country_field.test
View source
<?php

/**
 * @file
 * Contains tests for country field functionality.
 */

/**
 * Ensures that country field works correctly.
 */
class CountryFieldTest extends DrupalWebTestCase {

  /**
   * Profile to use.
   */
  protected $profile = 'testing';

  /**
   * Admin user
   *
   * @var \StdClass
   */
  protected $adminUser;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'country_field',
    'list',
    'node',
    'field_ui',
  );

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Country Field',
      'description' => 'Test country field functionality.',
      'group' => 'Country Field',
    );
  }

  /**
   * Permissions to grant admin user.
   *
   * @var array
   */
  protected $permissions = array(
    'access administration pages',
    'administer content types',
    'create ponies content',
    'edit any ponies content',
    'administer nodes',
  );

  /**
   * Sets up the test.
   */
  protected function setUp() {
    parent::setUp(self::$modules);
    $this
      ->drupalCreateContentType(array(
      'type' => 'ponies',
    ));
    $this->adminUser = $this
      ->drupalCreateUser($this->permissions);
  }

  /**
   * Tests adding and editing values using a country field.
   */
  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');
  }

}

Classes

Namesort descending Description
CountryFieldTest Ensures that country field works correctly.