Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / Views / TaxonomyRelationshipTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests\Views;
4
5 use Drupal\node\NodeInterface;
6 use Drupal\taxonomy\TermInterface;
7 use Drupal\views\Views;
8
9 /**
10  * Tests taxonomy relationships with parent term and node.
11  *
12  * @group taxonomy
13  */
14 class TaxonomyRelationshipTest extends TaxonomyTestBase {
15
16   /**
17    * Stores the terms used in the tests.
18    *
19    * @var \Drupal\taxonomy\TermInterface[]
20    */
21   protected $terms = [];
22
23   /**
24    * Views used by this test.
25    *
26    * @var array
27    */
28   public static $testViews = ['test_taxonomy_term_relationship'];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     // Make term2 parent of term1.
37     $this->term1->set('parent', $this->term2->id());
38     $this->term1->save();
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();
47
48     Views::viewsData()->clear();
49
50   }
51
52   /**
53    * Tests the taxonomy parent plugin UI.
54    */
55   public function testTaxonomyRelationships() {
56
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_hierarchy']['left_field'], 'tid');
65     $this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['field'], 'tid');
66
67     // Check the generated views data of taxonomy_term_hierarchy.
68     $views_data = Views::viewsData()->get('taxonomy_term_hierarchy');
69     // Check the table join data.
70     $this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['left_field'], 'tid');
71     $this->assertEqual($views_data['table']['join']['taxonomy_term_hierarchy']['field'], 'parent');
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'], 'tid');
74     // Check the parent relationship data.
75     $this->assertEqual($views_data['parent']['relationship']['base'], 'taxonomy_term_field_data');
76     $this->assertEqual($views_data['parent']['relationship']['field'], 'parent');
77     $this->assertEqual($views_data['parent']['relationship']['label'], t('Parent'));
78     $this->assertEqual($views_data['parent']['relationship']['id'], 'standard');
79     // Check the parent filter and argument data.
80     $this->assertEqual($views_data['parent']['filter']['id'], 'numeric');
81     $this->assertEqual($views_data['parent']['argument']['id'], 'taxonomy');
82
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());
90
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);
94
95       if (!$index) {
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_hierarchy_tid, $this->term2->id());
99       }
100       $this->assertTrue($row->_relationship_entities['nid'] instanceof NodeInterface);
101       $this->assertEqual($row->_relationship_entities['nid']->id(), $this->nodes[$index]->id());
102     }
103   }
104
105 }