TagTest.php 2.29 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\views\tests\UI\TagTest.
 */

namespace Drupal\views\Tests\UI;

10
use Drupal\views\Tests\ViewUnitTestBase;
11
use Drupal\views_ui\Routing\ViewsUIController;
12

13 14 15
/**
 * Tests the views ui tagging functionality.
 */
16
class TagTest extends ViewUnitTestBase {
17

18 19 20 21 22 23 24
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('views', 'views_ui');

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  public static function getInfo() {
    return array(
      'name' => 'Tag',
      'description' => 'Tests the views ui tagging functionality.',
      'group' => 'Views UI',
    );
  }

  /**
   * Tests the views_ui_autocomplete_tag function.
   */
  public function testViewsUiAutocompleteTag() {
    module_load_include('inc', 'views_ui', '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;
45
      entity_create('view', array('tag' => $tag, 'id' => $this->randomName()))->save();
46 47 48
    }

    // Make sure just ten results are returns.
49
    $controller = ViewsUIController::create($this->container);
50 51 52
    $request = $this->container->get('request');
    $request->query->set('q', 'autocomplete_tag_test');
    $result = $controller->autocompleteTag($request);
53 54 55 56
    $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.
57 58
    $request->query->set('q', 'autocomplete_tag_test_even');
    $result = $controller->autocompleteTag($request);
59 60 61
    $matches = (array) json_decode($result->getContent());
    $this->assertEqual(count($matches), 8, 'Make sure that only a subset is returned.');
    foreach ($matches as $tag) {
62
      $this->assertTrue(array_search($tag, $tags) !== FALSE, format_string('Make sure the returned tag @tag actually exists.', array('@tag' => $tag)));
63 64 65
    }

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

}