3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\node\NodeInterface;
6 use Drupal\taxonomy\TermInterface;
7 use Drupal\views\Views;
10 * Tests taxonomy relationships with parent term and node.
14 class TaxonomyRelationshipTest extends TaxonomyTestBase {
17 * Stores the terms used in the tests.
19 * @var \Drupal\taxonomy\TermInterface[]
21 protected $terms = [];
24 * Views used by this test.
28 public static $testViews = ['test_taxonomy_term_relationship'];
33 protected function setUp($import_test_views = TRUE) {
34 parent::setUp($import_test_views);
36 // Make term2 parent of term1.
37 $this->term1->set('parent', $this->term2->id());
39 // Store terms in an array for testing.
40 $this->terms[] = $this->term1;
41 $this->terms[] = $this->term2;
42 // Only set term1 on node1 and term2 on node2 for testing.
43 unset($this->nodes[0]->field_views_testing_tags[1]);
44 $this->nodes[0]->save();
45 unset($this->nodes[1]->field_views_testing_tags[0]);
46 $this->nodes[1]->save();
48 Views::viewsData()->clear();
53 * Tests the taxonomy parent plugin UI.
55 public function testTaxonomyRelationships() {
57 // Check the generated views data of taxonomy_index.
58 $views_data = Views::viewsData()->get('taxonomy_index');
59 // Check the table join data.
60 $this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
61 $this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'tid');
62 $this->assertEqual($views_data['table']['join']['node_field_data']['left_field'], 'nid');
63 $this->assertEqual($views_data['table']['join']['node_field_data']['field'], 'nid');
64 $this->assertEqual($views_data['table']['join']['taxonomy_term__parent']['left_field'], 'entity_id');
65 $this->assertEqual($views_data['table']['join']['taxonomy_term__parent']['field'], 'tid');
67 // Check the generated views data of taxonomy_term__parent.
68 $views_data = Views::viewsData()->get('taxonomy_term__parent');
69 // Check the table join data.
70 $this->assertEqual($views_data['table']['join']['taxonomy_term__parent']['left_field'], 'entity_id');
71 $this->assertEqual($views_data['table']['join']['taxonomy_term__parent']['field'], 'parent_target_id');
72 $this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
73 $this->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'entity_id');
74 // Check the parent relationship data.
75 $this->assertEqual($views_data['parent_target_id']['relationship']['base'], 'taxonomy_term_field_data');
76 $this->assertEqual($views_data['parent_target_id']['relationship']['base field'], 'tid');
77 $this->assertEqual($views_data['parent_target_id']['relationship']['label'], t('Parent'));
78 $this->assertEqual($views_data['parent_target_id']['relationship']['id'], 'standard');
79 // Check the parent filter and argument data.
80 $this->assertEqual($views_data['parent_target_id']['filter']['id'], 'numeric');
81 $this->assertEqual($views_data['parent_target_id']['argument']['id'], 'taxonomy');
83 // Check an actual test view.
84 $view = Views::getView('test_taxonomy_term_relationship');
85 $this->executeView($view);
86 /** @var \Drupal\views\ResultRow $row */
87 foreach ($view->result as $index => $row) {
88 // Check that the actual ID of the entity is the expected one.
89 $this->assertEqual($row->tid, $this->terms[$index]->id());
91 // Also check that we have the correct result entity.
92 $this->assertEqual($row->_entity->id(), $this->terms[$index]->id());
93 $this->assertTrue($row->_entity instanceof TermInterface);
96 $this->assertTrue($row->_relationship_entities['parent'] instanceof TermInterface);
97 $this->assertEqual($row->_relationship_entities['parent']->id(), $this->term2->id());
98 $this->assertEqual($row->taxonomy_term_field_data_taxonomy_term__parent_tid, $this->term2->id());
100 $this->assertTrue($row->_relationship_entities['nid'] instanceof NodeInterface);
101 $this->assertEqual($row->_relationship_entities['nid']->id(), $this->nodes[$index]->id());