Tests term autocompletion edge cases with slashes in the names.
function testTermAutocompletion() {
// Add a term with a slash in the name.
$first_term = $this
->createTerm($this->vocabulary);
$first_term->name = '10/16/2011';
$first_term
->save();
// Add another term that differs after the slash character.
$second_term = $this
->createTerm($this->vocabulary);
$second_term->name = '10/17/2011';
$second_term
->save();
// Add another term that has both a comma and a slash character.
$third_term = $this
->createTerm($this->vocabulary);
$third_term->name = 'term with, a comma and / a slash';
$third_term
->save();
// Try to autocomplete a term name that matches both terms.
// We should get both term in a json encoded string.
$input = '10/';
$path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary
->id();
// The result order is not guaranteed, so check each term separately.
$result = $this
->drupalGet($path, array(
'query' => array(
'q' => $input,
),
));
$data = drupal_json_decode($result);
$this
->assertEqual($data[$first_term
->label()], check_plain($first_term
->label()), 'Autocomplete returned the first matching term');
$this
->assertEqual($data[$second_term
->label()], check_plain($second_term
->label()), 'Autocomplete returned the second matching term');
// Try to autocomplete a term name that matches first term.
// We should only get the first term in a json encoded string.
$input = '10/16';
$path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary
->id();
$this
->drupalGet($path, array(
'query' => array(
'q' => $input,
),
));
$target = array(
$first_term
->label() => check_plain($first_term
->label()),
);
$this
->assertRaw(drupal_json_encode($target), 'Autocomplete returns only the expected matching term.');
// Try to autocomplete a term name with both a comma and a slash.
$input = '"term with, comma and / a';
$path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary
->id();
$this
->drupalGet($path, array(
'query' => array(
'q' => $input,
),
));
$n = $third_term
->label();
// Term names containing commas or quotes must be wrapped in quotes.
if (strpos($third_term
->label(), ',') !== FALSE || strpos($third_term
->label(), '"') !== FALSE) {
$n = '"' . str_replace('"', '""', $third_term
->label()) . '"';
}
$target = array(
$n => check_plain($third_term
->label()),
);
$this
->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.');
}