You are here

public function AbstractAutocompleteSynonymsWebTestCase::testAutocompleteMenuPath in Synonyms 7

Test autocomplete menu path.

Feed all known "buggy" input to synonym friendly autocomplete menu path, in order to test its performance.

File

./synonyms.test, line 423
Tests for the Synonyms module.

Class

AbstractAutocompleteSynonymsWebTestCase
Test "Synonyms friendly autocomplete" widget of Synonyms module.

Code

public function testAutocompleteMenuPath() {
  $this
    ->assertAutocompleteMenuPath('', array(), 'Submitting empty string into autocomplete path returns empty result.');
  $this
    ->assertAutocompleteMenuPath($this
    ->randomName(), array(), 'Submitting a non existing name into autocomplete path returns empty result.');
  $this
    ->assertAutocompleteMenuPath($this->terms['term1']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(), 'Submitting a value for a field with disabled autocomplete behavior yields empty result.');
  $this
    ->assertAutocompleteMenuPath(drupal_strtoupper(drupal_substr($this
    ->entityLabel($this->terms['term1']), 1, -1)), array(
    $this
      ->entityLabel($this->terms['term1']) => $this
      ->entityLabel($this->terms['term1']),
    $this
      ->entityLabel($this->terms['term1_longer_name']) => $this
      ->entityLabel($this->terms['term1_longer_name']),
  ), 'Submitting a name similar to 2 existing term names yields both terms included in the autocomplete response.');
  $this
    ->assertAutocompleteMenuPath($this
    ->entityLabel($this->terms['term1']) . ', ' . drupal_strtoupper(drupal_substr($this
    ->entityLabel($this->terms['term1']), 1, -1)), array(
    $this
      ->entityLabel($this->terms['term1']) . ', ' . $this
      ->entityLabel($this->terms['term1_longer_name']) => $this
      ->entityLabel($this->terms['term1_longer_name']),
  ), 'Submitting one term already chosen along with a name similar to 2 existing term names yields only suggested a new term.');
  $this
    ->assertAutocompleteMenuPath(drupal_strtoupper(drupal_substr($this
    ->entityLabel($this->terms['no_synonyms']), 1, -1)), array(
    $this
      ->entityLabel($this->terms['no_synonyms']) => $this
      ->entityLabel($this->terms['no_synonyms']),
  ), 'Submitting a name similar to one existing term name into autocomplete path yields that term included.');
  $this
    ->assertAutocompleteMenuPath(drupal_strtolower($this
    ->entityLabel($this->terms['no_synonyms'])) . ', ' . drupal_strtoupper(drupal_substr($this
    ->entityLabel($this->terms['no_synonyms']), 1, -1)), array(), 'Submitting the same term over again into autocomplete path yields no results.');
  $this
    ->assertAutocompleteMenuPath($this
    ->entityLabel($this->terms['one_synonym']) . ', ' . $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(), 'Submitting a synonym of a term over again into autocomplete path yields no results.');
  foreach (array(
    'no_synonyms',
    'one_synonym',
    'two_synonyms',
  ) as $k) {
    $this
      ->assertAutocompleteMenuPath(drupal_strtolower(drupal_substr($this
      ->entityLabel($this->terms[$k]), 1, -1)), array(
      $this
        ->entityLabel($this->terms[$k]) => $this
        ->entityLabel($this->terms[$k]),
    ), 'Submitting a name similar to ' . $k . ' term into autocomplete path yields the term included.');
    $synonyms = field_get_items($this->behavior_implementation['entity_type'], $this->terms[$k], $this->fields['enabled']['field']['field_name']);
    if (is_array($synonyms)) {
      foreach ($synonyms as $delta => $item) {
        $this
          ->assertAutocompleteMenuPath(drupal_strtolower(drupal_substr($item['value'], 1, -1)), array(
          $this
            ->entityLabel($this->terms[$k]) => $this
            ->synonymAutocompleteResult($this->terms[$k], $item['value']),
        ), 'Submitting a name similar to synonym#' . $delta . ' of the term ' . $k . ' into autocomplete path yields the term included.');
      }
    }
  }
  $this
    ->assertAutocompleteMenuPath('one_term_name_another_synonym_', array(
    $this
      ->entityLabel($this->terms['name_another_synonym']) => $this
      ->entityLabel($this->terms['name_another_synonym']),
    $this
      ->entityLabel($this->terms['synonym_another_name']) => $this
      ->synonymAutocompleteResult($this->terms['synonym_another_name'], $this->terms['synonym_another_name']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  ), 'Submitting a name similar to name of one term and synonym of another into autocomplete path yields both terms included.');

  // Enabling another field in the autocomplete suggestions to make sure 2 and
  // more fields can participate in the action.
  $behavior_implementation = $this->behavior_implementation;
  $behavior_implementation['provider'] = synonyms_provider_field_provider_name($this->fields['disabled']['field']);
  synonyms_behavior_implementation_save($behavior_implementation);
  $this->terms['one_synonym']->{$this->fields['disabled']['field']['field_name']} = $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']};
  entity_save($this->behavior_implementation['entity_type'], $this->terms['one_synonym']);
  $this
    ->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
    $this
      ->entityLabel($this->terms['one_synonym']) => $this
      ->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $behavior_implementation),
    $this
      ->entityLabel($this->terms['one_synonym']) . ' ' => $this
      ->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  ), 'Autocomplete works correctly when more than 1 field participates in the autocomplete behavior.');
  synonyms_behavior_implementation_delete($behavior_implementation);

  // Submit a name that is similar to one of our terms and is similar to a
  // a term from another vocabulary, which should not participate in the
  // autocomplete. We do this trick in different flavors, all with the idea
  // to make sure the bundles that should not participate in the field values
  // are not suggested by the autocomplete menu path. The different flavors
  // are:
  // - non valid term similar to valid term
  // - non valid term similar to valid synonym
  // - non valid synonym similar to valid term
  // - non valid synonym similar to valid synonym
  $instance = field_create_instance(array(
    'field_name' => $this->fields['enabled']['field']['field_name'],
    'entity_type' => $this->fields['enabled']['instance']['entity_type'],
    'bundle' => 'another_vocabulary',
  ));
  $instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  $behavior_implementation = $this->behavior_implementation;
  $behavior_implementation['bundle'] = $instance['bundle'];
  synonyms_behavior_implementation_save($behavior_implementation);
  $this
    ->assertAutocompleteMenuPath($this
    ->entityLabel($this->terms['no_synonyms']), array(
    $this
      ->entityLabel($this->terms['no_synonyms']) => $this
      ->entityLabel($this->terms['no_synonyms']),
  ), 'Submitting term name similar to term from another bundle does not include the term from another bundle.');
  $this
    ->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
    $this
      ->entityLabel($this->terms['one_synonym']) => $this
      ->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  ), 'Submitting synonym similar to term from another bundle does not include the term from another bundle.');
  $this
    ->assertAutocompleteMenuPath($this
    ->entityLabel($this->terms['no_synonyms']), array(
    $this
      ->entityLabel($this->terms['no_synonyms']) => $this
      ->entityLabel($this->terms['no_synonyms']),
  ), 'Submitting term name similar to a synonym of a term from another bundle does not include the term from another bundle.');
  $this
    ->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
    $this
      ->entityLabel($this->terms['one_synonym']) => $this
      ->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  ), 'Submitting synonym similar to a synonym of a term from another bundle does not include the term from another bundle.');
}