EntityReferenceRelationshipTest.php 10.3 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\field\Kernel\EntityReference\Views;
4

5
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
6
7
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMul;
8
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9
10
11
12
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;

/**
13
 * Tests entity reference relationship data.
14
 *
15
 * @group entity_reference
16
17
 *
 * @see core_field_views_data()
18
 */
19
class EntityReferenceRelationshipTest extends ViewsKernelTestBase {
20

21
22
  use EntityReferenceTestTrait;

23
24
25
26
27
  /**
   * Views used by this test.
   *
   * @var array
   */
28
29
30
31
32
33
  public static $testViews = array(
    'test_entity_reference_entity_test_view',
    'test_entity_reference_reverse_entity_test_view',
    'test_entity_reference_entity_test_mul_view',
    'test_entity_reference_reverse_entity_test_mul_view',
    );
34
35

  /**
36
   * Modules to install.
37
38
39
   *
   * @var array
   */
40
  public static $modules = ['user', 'field', 'entity_test', 'views', 'entity_reference_test_views'];
41
42
43
44
45
46
47
48
49
50
51

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

  /**
   * {@inheritdoc}
   */
52
  protected function setUp($import_test_views = TRUE) {
53
54
    parent::setUp();

55
    $this->installEntitySchema('user');
56
    $this->installEntitySchema('entity_test');
57
58
59
    $this->installEntitySchema('entity_test_mul');

    // Create reference from entity_test to entity_test_mul.
60
    $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_data', 'field_test_data', 'entity_test_mul');
61

62
63
    // Create reference from entity_test_mul to entity_test.
    $this->createEntityReferenceField('entity_test_mul', 'entity_test_mul', 'field_data_test', 'field_data_test', 'entity_test');
64

65
    ViewTestData::createTestViews(get_class($this), array('entity_reference_test_views'));
66
67
68
69
70
71
  }

  /**
   * Tests using the views relationship.
   */
  public function testNoDataTableRelationship() {
72

73
    // Create some test entities which link each other.
74
    $referenced_entity = EntityTestMul::create();
75
76
    $referenced_entity->save();

77
78
    $entity = EntityTest::create();
    $entity->field_test_data->target_id = $referenced_entity->id();
79
    $entity->save();
80
81
    $this->assertEqual($entity->field_test_data[0]->entity->id(), $referenced_entity->id());
    $this->entities[] = $entity;
82

83
84
    $entity = EntityTest::create();
    $entity->field_test_data->target_id = $referenced_entity->id();
85
    $entity->save();
86
87
    $this->assertEqual($entity->field_test_data[0]->entity->id(), $referenced_entity->id());
    $this->entities[] = $entity;
88
89

    Views::viewsData()->clear();
90
91
92
93
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

    // Check the generated views data.
    $views_data = Views::viewsData()->get('entity_test__field_test_data');
    $this->assertEqual($views_data['field_test_data']['relationship']['id'], 'standard');
    $this->assertEqual($views_data['field_test_data']['relationship']['base'], 'entity_test_mul_property_data');
    $this->assertEqual($views_data['field_test_data']['relationship']['base field'], 'id');
    $this->assertEqual($views_data['field_test_data']['relationship']['relationship field'], 'field_test_data_target_id');
    $this->assertEqual($views_data['field_test_data']['relationship']['entity type'], 'entity_test_mul');

    // Check the backwards reference.
    $views_data = Views::viewsData()->get('entity_test_mul_property_data');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['id'], 'entity_reverse');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['base'], 'entity_test');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['base field'], 'id');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field table'], 'entity_test__field_test_data');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field field'], 'field_test_data_target_id');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['field_name'], 'field_test_data');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['entity_type'], 'entity_test');
    $this->assertEqual($views_data['reverse__entity_test__field_test_data']['relationship']['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]);

    // Check an actual test view.
    $view = Views::getView('test_entity_reference_entity_test_view');
    $this->executeView($view);
    /** @var \Drupal\views\ResultRow $row */
    foreach ($view->result as $index => $row) {
      // Check that the actual ID of the entity is the expected one.
      $this->assertEqual($row->id, $this->entities[$index]->id());

      // Also check that we have the correct result entity.
      $this->assertEqual($row->_entity->id(), $this->entities[$index]->id());

      // Test the forward relationship.
      $this->assertEqual($row->entity_test_mul_property_data_entity_test__field_test_data_i, 1);

      // Test that the correct relationship entity is on the row.
      $this->assertEqual($row->_relationship_entities['field_test_data']->id(), 1);
      $this->assertEqual($row->_relationship_entities['field_test_data']->bundle(), 'entity_test_mul');

    }

    // Check the backwards reference view.
    $view = Views::getView('test_entity_reference_reverse_entity_test_view');
    $this->executeView($view);
    /** @var \Drupal\views\ResultRow $row */
    foreach ($view->result as $index => $row) {
      $this->assertEqual($row->id, 1);
      $this->assertEqual($row->_entity->id(), 1);

      // Test the backwards relationship.
      $this->assertEqual($row->field_test_data_entity_test_mul_property_data_id, $this->entities[$index]->id());

      // Test that the correct relationship entity is on the row.
      $this->assertEqual($row->_relationship_entities['reverse__entity_test__field_test_data']->id(), $this->entities[$index]->id());
      $this->assertEqual($row->_relationship_entities['reverse__entity_test__field_test_data']->bundle(), 'entity_test');
    }
145
146
147
  }

  /**
148
149
150
   * Tests views data generated for relationship.
   *
   * @see entity_reference_field_views_data()
151
   */
152
  public function testDataTableRelationship() {
153

154
155
156
157
158
159
160
161
162
    // Create some test entities which link each other.
    $referenced_entity = EntityTest::create();
    $referenced_entity->save();

    $entity = EntityTestMul::create();
    $entity->field_data_test->target_id = $referenced_entity->id();
    $entity->save();
    $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id());
    $this->entities[] = $entity;
163

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    $entity = EntityTestMul::create();
    $entity->field_data_test->target_id = $referenced_entity->id();
    $entity->save();
    $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id());
    $this->entities[] = $entity;

    Views::viewsData()->clear();

    // Check the generated views data.
    $views_data = Views::viewsData()->get('entity_test_mul__field_data_test');
    $this->assertEqual($views_data['field_data_test']['relationship']['id'], 'standard');
    $this->assertEqual($views_data['field_data_test']['relationship']['base'], 'entity_test');
    $this->assertEqual($views_data['field_data_test']['relationship']['base field'], 'id');
    $this->assertEqual($views_data['field_data_test']['relationship']['relationship field'], 'field_data_test_target_id');
    $this->assertEqual($views_data['field_data_test']['relationship']['entity type'], 'entity_test');

    // Check the backwards reference.
    $views_data = Views::viewsData()->get('entity_test');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['id'], 'entity_reverse');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base'], 'entity_test_mul_property_data');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base field'], 'id');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field table'], 'entity_test_mul__field_data_test');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field field'], 'field_data_test_target_id');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field_name'], 'field_data_test');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['entity_type'], 'entity_test_mul');
    $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]);
190
191

    // Check an actual test view.
192
    $view = Views::getView('test_entity_reference_entity_test_mul_view');
193
    $this->executeView($view);
194
195
    /** @var \Drupal\views\ResultRow $row */
    foreach ($view->result as $index => $row) {
196
      // Check that the actual ID of the entity is the expected one.
197
      $this->assertEqual($row->id, $this->entities[$index]->id());
198
199

      // Also check that we have the correct result entity.
200
      $this->assertEqual($row->_entity->id(), $this->entities[$index]->id());
201

202
      // Test the forward relationship.
203
204
205
206
207
208
      $this->assertEqual($row->entity_test_entity_test_mul__field_data_test_id, 1);

      // Test that the correct relationship entity is on the row.
      $this->assertEqual($row->_relationship_entities['field_data_test']->id(), 1);
      $this->assertEqual($row->_relationship_entities['field_data_test']->bundle(), 'entity_test');

209
210
    }

211
212
213
214
215
216
217
218
219
220
    // Check the backwards reference view.
    $view = Views::getView('test_entity_reference_reverse_entity_test_mul_view');
    $this->executeView($view);
    /** @var \Drupal\views\ResultRow $row */
    foreach ($view->result as $index => $row) {
      $this->assertEqual($row->id, 1);
      $this->assertEqual($row->_entity->id(), 1);

      // Test the backwards relationship.
      $this->assertEqual($row->field_data_test_entity_test_id, $this->entities[$index]->id());
221

222
223
224
      // Test that the correct relationship entity is on the row.
      $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->id(), $this->entities[$index]->id());
      $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->bundle(), 'entity_test_mul');
225
226
227
228
    }
  }

}