You are here

function ReferenceOptionLimitTaxonomyTestCase::testNodeCreateForm in Reference field option limit 7

Test the module's functionality.

File

tests/reference_option_limit.test, line 42
Contains tests for the Reference option limit module.

Class

ReferenceOptionLimitTaxonomyTestCase
Test use of the module with term reference fields.

Code

function testNodeCreateForm() {
  $this
    ->drupalGet('node/add/test-rol-node-taxo');

  // Get a country term.
  $terms = taxonomy_get_term_by_name('France', 'countries');
  $country_term = array_pop($terms);
  $edit = array(
    'test_rol_country[und]' => $country_term->tid,
  );
  $this
    ->drupalPostAJAX(NULL, $edit, 'test_rol_country[und]');

  // The AJAX post updates the content our assertions test against.
  // Check each term: all the cities in France should be present; all the
  // others should not.
  foreach (reference_option_limit_test_taxonomy_cities() as $city_name => $country_name) {
    if ($country_name == 'France') {
      $this
        ->assertText($city_name, "The {$city_name} term was found in the form.");
    }
    else {
      $this
        ->assertNoText($city_name, "The {$city_name} term was not found in the form.");
    }
  }

  // Get a country term.
  $terms = taxonomy_get_term_by_name('Italy', 'countries');
  $country_term = array_pop($terms);
  $edit = array(
    'test_rol_country[und]' => $country_term->tid,
  );

  // Change the country we have selected.
  $this
    ->drupalPostAJAX(NULL, $edit, 'test_rol_country[und]');
  foreach (reference_option_limit_test_taxonomy_cities() as $city_name => $country_name) {
    if ($country_name == 'Italy') {
      $this
        ->assertText($city_name, "The {$city_name} term was found in the form.");
    }
    else {
      $this
        ->assertNoText($city_name, "The {$city_name} term was not found in the form.");
    }
  }

  // Save the node.
  $terms = taxonomy_get_term_by_name('Firenze', 'cities');
  $city_term = array_pop($terms);
  $edit = array(
    'title' => $this
      ->randomName(),
    // Use the most recent country term.
    'test_rol_country[und]' => $country_term->tid,
    "test_rol_city[und][{$city_term->tid}]" => 1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // The URL is of the form http://example.com/node/NID.
  $url = $this
    ->getUrl();
  $pieces = explode('/', $url);
  $nid = array_pop($pieces);
  $node = node_load($nid);
  $this
    ->assertEqual($node->test_rol_country[LANGUAGE_NONE][0]['tid'], $country_term->tid, "The node has its country field value set.");
  $this
    ->assertEqual($node->test_rol_city[LANGUAGE_NONE][0]['tid'], $city_term->tid, "The node has its city field value set.");
}