You are here

function RestfulAutoCompleteTestCase::testAutocomplete in RESTful 7

Same name and namespace in other branches
  1. 7.2 tests/RestfulAutoCompleteTestCase.test \RestfulAutoCompleteTestCase::testAutocomplete()

Test the autocomplete functionality.

File

tests/RestfulAutoCompleteTestCase.test, line 34
Contains RestfulAutoCompleteTestCase

Class

RestfulAutoCompleteTestCase
@file Contains RestfulAutoCompleteTestCase

Code

function testAutocomplete() {

  // Create terms.
  restful_test_create_vocabulary_and_terms('tags', FALSE);
  $handler = restful_get_restful_handler('test_tags');

  // "CONTAINS" operator.
  $request = array(
    'autocomplete' => array(
      'string' => 'ter',
    ),
  );
  $result = $handler
    ->get('', $request);
  $expected_result = array(
    1 => 'term1',
    2 => 'term2',
    3 => 'term3',
  );
  $this
    ->assertEqual($result, $expected_result, 'Autocomplete with "CONTAINS" operator returned expected results.');

  // "STARTS_WITH" operator.
  $request = array(
    'autocomplete' => array(
      'string' => 'term1',
      'operator' => 'STARTS_WITH',
    ),
  );
  $result = $handler
    ->get('', $request);
  $expected_result = array(
    1 => 'term1',
  );
  $this
    ->assertEqual($result, $expected_result, 'Autocomplete with "CONTAINS" operator returned expected results.');

  // Empty query.
  $request = array(
    'autocomplete' => array(
      'string' => 'non-existing',
    ),
  );
  $result = $handler
    ->get('', $request);
  $this
    ->assertFalse($result, 'No values returned from empty query.');

  // Test autocomplete for users.
  $handler = restful_get_restful_handler('users');
  $request = array(
    'autocomplete' => array(
      'string' => substr($this->user->name, 0, 3),
    ),
  );
  $result = $handler
    ->get('', $request);
  $this
    ->assertTrue($result, 'Results has returned for entity without a bundle key.');
}