EntityReferenceRelationshipTest.php 5.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Tests\Views\EntityReferenceRelationshipTest.
 */

namespace Drupal\entity_reference\Tests\Views;

use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\field\Entity\FieldStorageConfig;
12 13 14 15 16 17
use Drupal\field\Entity\FieldInstanceConfig;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Tests\ViewUnitTestBase;
use Drupal\views\Views;

/**
18
 * Tests entity reference relationship data.
19
 *
20
 * @group entity_reference
21 22 23 24 25 26 27 28 29 30 31 32
 * @see entity_reference_field_views_data()
 */
class EntityReferenceRelationshipTest extends ViewUnitTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_entity_reference_view');

  /**
33
   * Modules to install.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   *
   * @var array
   */
  public static $modules = array('user', 'field', 'entity_test', 'options', 'entity_reference', 'views', 'entity_reference_test_views');

  /**
   * The entity_test entities used by the test.
   *
   * @var array
   */
  protected $entities = array();

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('entity_test');

    ViewTestData::createTestViews(get_class($this), array('entity_reference_test_views'));

56
    $field_storage = FieldStorageConfig::create(array(
57 58 59 60 61 62 63 64
      'settings' => array(
        'target_type' => 'entity_test',
      ),
      'entity_type' => 'entity_test',
      'name' => 'field_test',
      'type' => 'entity_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ));
65
    $field_storage->save();
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    $instance = FieldInstanceConfig::create(array(
      'entity_type' => 'entity_test',
      'field_name' => 'field_test',
      'bundle' => 'entity_test',
      'settings' => array(
        'handler' => 'default',
        'handler_settings' => array(),
      ),
    ));
    $instance->save();

    // Create some test entities which link each other.
    $entity_storage= \Drupal::entityManager()->getStorage('entity_test');
    $referenced_entity = $entity_storage->create(array());
    $referenced_entity->save();
    $this->entities[$referenced_entity->id()] = $referenced_entity;

    $entity = $entity_storage->create(array());
    $entity->field_test->target_id = $referenced_entity->id();
    $entity->save();
87
    $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
88 89
    $this->entities[$entity->id()] = $entity;

90
    $entity = $entity_storage->create(array());
91 92
    $entity->field_test->target_id = $referenced_entity->id();
    $entity->save();
93
    $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    $this->entities[$entity->id()] = $entity;
  }

  /**
   * Tests using the views relationship.
   */
  public function testRelationship() {
    // Check just the generated views data.
    $views_data_field_test = Views::viewsData()->get('entity_test__field_test');
    $this->assertEqual($views_data_field_test['field_test']['relationship']['id'], 'standard');
    $this->assertEqual($views_data_field_test['field_test']['relationship']['base'], 'entity_test');
    $this->assertEqual($views_data_field_test['field_test']['relationship']['base field'], 'id');
    $this->assertEqual($views_data_field_test['field_test']['relationship']['relationship field'], 'field_test_target_id');

    // Check the backwards reference.
    $views_data_entity_test = Views::viewsData()->get('entity_test');
    $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['id'], 'entity_reverse');
    $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base'], 'entity_test');
    $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base field'], 'id');
    $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field table'], 'entity_test__field_test');
    $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field field'], 'field_test_target_id');


    // Check an actual test view.
    $view = Views::getView('test_entity_reference_view');
    $this->executeView($view);

    foreach (array_keys($view->result) as $index) {
      // Just check that the actual ID of the entity is the expected one.
      $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id());
      // Test the forward relationship.
      // The second and third entity refer to the first one.
      // The value key on the result will be in the format
      // BASE_TABLE_FIELD_NAME.
      $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1);

      if ($index > 0) {
        // Test that the correct relationship entity is on the row.
        $this->assertEqual($view->result[$index]->_relationship_entities['test_relationship']->id(), 1);
      }
    }

    $view->destroy();
    $this->executeView($view, 'embed_1');

    foreach (array_keys($view->result) as $index) {
      $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id());
      // The second and third entity refer to the first one.
      $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1);
    }
  }

}