View source  
  <?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
class EntityQueryRelationshipTest extends EntityUnitTestBase {
  use EntityReferenceTestTrait;
  
  public static $modules = array(
    'taxonomy',
  );
  
  protected $factory;
  
  protected $terms;
  
  public $accounts;
  
  protected $entities;
  
  protected $fieldName;
  
  protected $queryResults;
  protected function setUp() {
    parent::setUp();
    $this
      ->installEntitySchema('taxonomy_term');
    
    $vocabulary = entity_create('taxonomy_vocabulary', array(
      'vid' => Unicode::strtolower($this
        ->randomMachineName()),
    ));
    $vocabulary
      ->save();
    
    entity_test_create_bundle('test_bundle');
    $this->fieldName = strtolower($this
      ->randomMachineName());
    $handler_settings = array(
      'target_bundles' => array(
        $vocabulary
          ->id() => $vocabulary
          ->id(),
      ),
      'auto_create' => TRUE,
    );
    $this
      ->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
    
    for ($i = 0; $i <= 1; $i++) {
      $term = entity_create('taxonomy_term', array(
        'name' => $this
          ->randomMachineName(),
        'vid' => $vocabulary
          ->id(),
      ));
      $term
        ->save();
      $this->terms[] = $term;
      $this->accounts[] = $this
        ->createUser();
    }
    
    for ($i = 0; $i <= 2; $i++) {
      $entity = entity_create('entity_test', array(
        'type' => 'test_bundle',
      ));
      $entity->name->value = $this
        ->randomMachineName();
      $index = $i ? 1 : 0;
      $entity->user_id->target_id = $this->accounts[$index]
        ->id();
      $entity->{$this->fieldName}->target_id = $this->terms[$index]
        ->id();
      $entity
        ->save();
      $this->entities[] = $entity;
    }
    $this->factory = \Drupal::service('entity.query');
  }
  
  public function testQuery() {
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->condition("user_id.entity.name", $this->accounts[0]
      ->getUsername())
      ->execute();
    $this
      ->assertResults(array(
      0,
    ));
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->condition("user_id.entity.name", $this->accounts[0]
      ->getUsername(), '<>')
      ->execute();
    $this
      ->assertResults(array(
      1,
      2,
    ));
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->exists("user_id.entity.name")
      ->execute();
    $this
      ->assertResults(array(
      0,
      1,
      2,
    ));
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->notExists("user_id.entity.name")
      ->execute();
    $this
      ->assertEqual(count($this->queryResults), 0);
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value)
      ->execute();
    $this
      ->assertResults(array(
      0,
    ));
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->condition("{$this->fieldName}.target_id.entity.name", $this->terms[0]->name->value)
      ->execute();
    $this
      ->assertResults(array(
      0,
    ));
    
    $this->queryResults = $this->factory
      ->get('entity_test')
      ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value, '<>')
      ->execute();
    $this
      ->assertResults(array(
      1,
      2,
    ));
  }
  
  protected function assertResults($expected) {
    $this
      ->assertEqual(count($this->queryResults), count($expected));
    foreach ($expected as $key) {
      $id = $this->entities[$key]
        ->id();
      $this
        ->assertEqual($this->queryResults[$id], $id);
    }
  }
}