public function TagTest::testViewsUiAutocompleteTag

Tests the views_ui_autocomplete_tag function.

File

drupal/core/modules/views_ui/lib/Drupal/views_ui/Tests/TagTest.php, line 36
Contains \Drupal\views_ui\tests\TagTest.

Class

TagTest
Tests the views ui tagging functionality.

Namespace

Drupal\views_ui\Tests

Code

public function testViewsUiAutocompleteTag() {
  \Drupal::moduleHandler()
    ->loadInclude('views_ui', 'inc', 'admin');

  // Save 15 views with a tag.
  $tags = array();
  for ($i = 0; $i < 16; $i++) {
    $suffix = $i % 2 ? 'odd' : 'even';
    $tag = 'autocomplete_tag_test_' . $suffix . $this
      ->randomName();
    $tags[] = $tag;
    entity_create('view', array(
      'tag' => $tag,
      'id' => $this
        ->randomName(),
    ))
      ->save();
  }

  // Make sure just ten results are returns.
  $controller = ViewsUIController::create($this->container);
  $request = $this->container
    ->get('request');
  $request->query
    ->set('q', 'autocomplete_tag_test');
  $result = $controller
    ->autocompleteTag($request);
  $matches = (array) json_decode($result
    ->getContent());
  $this
    ->assertEqual(count($matches), 10, 'Make sure the maximum amount of tag results is 10.');

  // Make sure that matching by a certain prefix works.
  $request->query
    ->set('q', 'autocomplete_tag_test_even');
  $result = $controller
    ->autocompleteTag($request);
  $matches = (array) json_decode($result
    ->getContent());
  $this
    ->assertEqual(count($matches), 8, 'Make sure that only a subset is returned.');
  foreach ($matches as $tag) {
    $this
      ->assertTrue(array_search($tag, $tags) !== FALSE, format_string('Make sure the returned tag @tag actually exists.', array(
      '@tag' => $tag,
    )));
  }

  // Make sure an invalid result doesn't return anything.
  $request->query
    ->set('q', $this
    ->randomName());
  $result = $controller
    ->autocompleteTag($request);
  $matches = (array) json_decode($result
    ->getContent());
  $this
    ->assertEqual(count($matches), 0, "Make sure an invalid tag doesn't return anything.");
}