You are here

function ReferenceOptionLimitEntityreferenceTestCase::helperTestNodeCreateForm in Reference field option limit 7

Helper to run tests.

This allows us to run the same tests with different field settings.

Parameters

$settings: An array of key settings for the field and instance, with the following properties:

  • 'widget': The widget type used by the the field instance. One of either 'options_select' or 'options_buttons'.
  • 'cardinality': The cardinality on the field. One of either 1 or FIELD_CARDINALITY_UNLIMITED.
  • 'default': The default value on the field instance. An array of values (without the FieldAPI nesting for language and delta). For no default value, an empty array.
  • 'empty_behaviour': Whether the limited field shows all options (FALSE) or none (TRUE) when the matching field is initially empty.
1 call to ReferenceOptionLimitEntityreferenceTestCase::helperTestNodeCreateForm()
ReferenceOptionLimitEntityreferenceTestCase::testNodeCreateForm in tests/reference_option_limit.test
Test the functionality on a node create form.

File

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

Class

ReferenceOptionLimitEntityreferenceTestCase
Test use of the module with entityreference fields.

Code

function helperTestNodeCreateForm($settings) {

  // Sanity check that the field settings are what we expect then to be.
  // This also helps make the test result more readable, as it marks the start
  // of a new round.
  $country_field_info = field_info_field('test_rol_er_country');
  $country_instance_info = field_info_instance('node', 'test_rol_er_country', 'test_rol_node_article');
  $city_instance_info = field_info_instance('node', 'test_rol_er_city', 'test_rol_node_article');
  $this
    ->assertTrue($country_instance_info['widget']['type'] == $settings['widget'] && $country_field_info['cardinality'] == $settings['cardinality'] && $city_instance_info['options_limit_empty_behaviour'] == $settings['empty_behaviour'], format_string("The field settings are correctly set: widget type !widget, cardinality !card, default value !default, default value behaviour: !empty-default.", array(
    '!widget' => $settings['widget'],
    '!card' => $settings['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 'unlimited' : $settings['cardinality'],
    '!default' => empty($settings['default']) ? 'none' : implode(', ', $settings['default']),
    '!empty-default' => $settings['empty_behaviour'] ? 'hide options for an empty default' : "don't hide options for an empty default",
  )));
  $this
    ->drupalGet('node/add/test-rol-node-article');

  // Check that the city field is limited by the default value of the country
  // field. (We can assume that FieldAPI works properly and that the default
  // value is set!)
  // The the empty behaviour determines what should happen to the city field
  // when the country field default is empty:
  //  - empty_behaviour TRUE: no city options shown.
  //  - empty_behaviour FALSE: all city options shown.
  $expect_no_cities = $expect_all_cities = FALSE;
  if (empty($settings['default'])) {
    if ($settings['empty_behaviour']) {
      $expect_no_cities = TRUE;
    }
    else {
      $expect_all_cities = TRUE;
    }
  }
  foreach (reference_option_limit_test_entityreference_cities() as $city_name => $country_name) {

    // We expect to find the city listed if one of the following holds:
    //  - its country is in the default
    //  - the default is empty, and the empty behaviour is not to limit
    //    options.
    if ($expect_no_cities) {
      $this
        ->assertNoText($city_name, "The {$city_name} node was not found in the initial node add form.");
      continue;
    }
    if ($expect_all_cities) {
      $this
        ->assertText($city_name, "The {$city_name} node was found in the initial node add form.");
      continue;
    }
    $country_of_city_in_default = in_array($country_name, $settings['default']);
    if ($country_of_city_in_default) {
      $this
        ->assertText($city_name, "The {$city_name} node was found in the initial node add form.");
    }
    else {
      $this
        ->assertNoText($city_name, "The {$city_name} node was not found in the initial node add form.");
    }
  }

  // Change the country we have selected.
  $this
    ->rolPostAJAXCountryField('France', $settings);

  // 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_entityreference_cities() as $city_name => $country_name) {
    if ($country_name == 'France') {
      $this
        ->assertText($city_name, "The {$city_name} node was found in the form.");
    }
    else {
      $this
        ->assertNoText($city_name, "The {$city_name} node was not found in the form.");
    }
  }

  // Change the country we have selected.
  $this
    ->rolPostAJAXCountryField('Italy', $settings);
  foreach (reference_option_limit_test_entityreference_cities() as $city_name => $country_name) {
    if ($country_name == 'Italy') {
      $this
        ->assertText($city_name, "The {$city_name} node was found in the form.");
    }
    else {
      $this
        ->assertNoText($city_name, "The {$city_name} node was not found in the form.");
    }
  }

  // Save the node.
  $city_node = $this
    ->getNodeByTitle('Firenze');
  $country_node = $this
    ->getNodeByTitle('Italy');
  $post_data = $this
    ->getEditArray('Italy', $settings);
  $post_data['edit']['title'] = $this
    ->randomName();
  $post_data['edit']["test_rol_er_city[und][{$city_node->nid}]"] = 1;
  $this
    ->drupalPost(NULL, $post_data['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_er_country[LANGUAGE_NONE][0]['target_id'], $country_node->nid, "The node has its country field value set.");
  $this
    ->assertEqual($node->test_rol_er_city[LANGUAGE_NONE][0]['target_id'], $city_node->nid, "The node has its city field value set.");
}