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

/**
 * @file
 * Contains \Drupal\entity_reference\Tests\EntityReferenceAutocompleteTest.
 */

namespace Drupal\entity_reference\Tests;

10
use Drupal\Component\Serialization\Json;
11
use Drupal\Component\Utility\String;
12
use Drupal\Component\Utility\Tags;
13 14 15
use Drupal\entity_reference\EntityReferenceController;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17

18
/**
19 20 21
 * Tests the autocomplete functionality.
 *
 * @group entity_reference
22 23 24 25 26 27 28 29
 */
class EntityReferenceAutocompleteTest extends EntityUnitTestBase {

  /**
   * The entity type used in this test.
   *
   * @var string
   */
30
  protected $entityType = 'entity_test';
31 32 33 34 35 36

  /**
   * The bundle used in this test.
   *
   * @var string
   */
37
  protected $bundle = 'entity_test';
38 39 40 41 42 43 44 45 46

  /**
   * The name of the field used in this test.
   *
   * @var string
   */
  protected $fieldName = 'field_test';

  /**
47
   * Modules to install.
48 49 50
   *
   * @var array
   */
51
  public static $modules = array('entity_reference', 'entity_reference_test');
52

53
  protected function setUp() {
54 55
    parent::setUp();

56
    entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
57 58 59 60 61
  }

  /**
   * Tests autocompletion edge cases with slashes in the names.
   */
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  function testEntityReferenceAutocompletion() {
    // Add an entity with a slash in its name.
    $entity_1 = entity_create($this->entityType, array('name' => '10/16/2011', $this->fieldName => NULL));
    $entity_1->save();

    // Add another entity that differs after the slash character.
    $entity_2 = entity_create($this->entityType, array('name' => '10/17/2011', $this->fieldName => NULL));
    $entity_2->save();

    // Add another entity that has both a comma and a slash character.
    $entity_3 = entity_create($this->entityType, array('name' => 'label with, and / test', $this->fieldName => NULL));
    $entity_3->save();

    // Try to autocomplete a entity label that matches both entities.
    // We should get both entities in a JSON encoded string.
77
    $input = '10/';
78
    $data = $this->getAutocompleteResult('single', $input);
79 80
    $this->assertIdentical($data[0]['label'], String::checkPlain($entity_1->name->value), 'Autocomplete returned the first matching entity');
    $this->assertIdentical($data[1]['label'], String::checkPlain($entity_2->name->value), 'Autocomplete returned the second matching entity');
81

82 83
    // Try to autocomplete a entity label that matches the first entity.
    // We should only get the first entity in a JSON encoded string.
84
    $input = '10/16';
85
    $data = $this->getAutocompleteResult('single', $input);
86 87
    $target = array(
      'value' => $entity_1->name->value . ' (1)',
88
      'label' => String::checkPlain($entity_1->name->value),
89 90
    );
    $this->assertIdentical(reset($data), $target, 'Autocomplete returns only the expected matching entity.');
91 92 93 94 95

    // Try to autocomplete a entity label that matches the second entity, and
    // the first entity  is already typed in the autocomplete (tags) widget.
    $input = $entity_1->name->value . ' (1), 10/17';
    $data = $this->getAutocompleteResult('tags', $input);
96
    $this->assertIdentical($data[0]['label'], String::checkPlain($entity_2->name->value), 'Autocomplete returned the second matching entity');
97 98 99 100

    // Try to autocomplete a entity label with both a comma and a slash.
    $input = '"label with, and / t';
    $data = $this->getAutocompleteResult('single', $input);
101
    $n = $entity_3->name->value . ' (3)';
102
    // Entity labels containing commas or quotes must be wrapped in quotes.
103
    $n = Tags::encode($n);
104 105
    $target = array(
      'value' => $n,
106
      'label' => String::checkPlain($entity_3->name->value),
107 108
    );
    $this->assertIdentical(reset($data), $target, 'Autocomplete returns an entity label containing a comma and a slash.');
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  }

  /**
   * Returns the result of an Entity reference autocomplete request.
   *
   * @param string $type
   *   The Entity reference autocomplete type (e.g. 'single', 'tags').
   * @param string $input
   *   The label of the entity to query by.
   *
   * @return mixed
   *  The JSON value encoded in its appropriate PHP type.
   */
  protected function getAutocompleteResult($type, $input) {
    $request = Request::create('entity_reference/autocomplete/' . $type . '/' . $this->fieldName . '/node/article/NULL');
    $request->query->set('q', $input);

    $entity_reference_controller = EntityReferenceController::create($this->container);
    $result = $entity_reference_controller->handleAutocomplete($request, $type, $this->fieldName, $this->entityType, $this->bundle, 'NULL')->getContent();

129
    return Json::decode($result);
130
  }
131 132 133 134 135 136

  /**
   * Tests autocomplete for entity base fields.
   */
  public function testBaseField() {
    // Add two users.
137
    $user_1 = entity_create('user', array('name' => 'auto1', 'status' => TRUE));
138
    $user_1->save();
139
    $user_2 = entity_create('user', array('name' => 'auto2', 'status' => TRUE));
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    $user_2->save();

    $request = Request::create('entity_reference/autocomplete/single/user_id/entity_test/entity_test/NULL');
    $request->query->set('q', 'auto');

    $entity_reference_controller = EntityReferenceController::create($this->container);
    $result = $entity_reference_controller->handleAutocomplete($request, 'single', 'user_id', 'entity_test', 'entity_test', 'NULL')->getContent();

    $data = Json::decode($result);
    $this->assertIdentical($data[0]['label'], String::checkPlain($user_1->getUsername()), 'Autocomplete returned the first matching entity');
    $this->assertIdentical($data[1]['label'], String::checkPlain($user_2->getUsername()), 'Autocomplete returned the second matching entity');

    // Checks that exception thrown for unknown field.
    try {
      $entity_reference_controller->handleAutocomplete($request, 'single', 'unknown_field', 'entity_test', 'entity_test', 'NULL')->getContent();
      $this->fail('Autocomplete throws exception for unknown field.');
    }
    catch (AccessDeniedHttpException $e) {
      $this->pass('Autocomplete throws exception for unknown field.');
    }
  }

162
}