Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / Views / TaxonomyTermFilterDepthTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests\Views;
4
5 use Drupal\views\Views;
6
7 /**
8  * Test the taxonomy term with depth filter.
9  *
10  * @group taxonomy
11  */
12 class TaxonomyTermFilterDepthTest extends TaxonomyTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['taxonomy', 'taxonomy_test_views', 'views', 'node'];
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $testViews = ['test_filter_taxonomy_index_tid_depth'];
23
24   /**
25    * @var \Drupal\taxonomy\TermInterface[]
26    */
27   protected $terms = [];
28
29   /**
30    * @var \Drupal\views\ViewExecutable
31    */
32   protected $view;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     // Create a hierarchy 3 deep. Note the parent setup function creates two
41     // top-level terms w/o children.
42     $first = $this->createTerm(['name' => 'First']);
43     $second = $this->createTerm(['name' => 'Second', 'parent' => $first->id()]);
44     $third = $this->createTerm(['name' => 'Third', 'parent' => $second->id()]);
45
46     // Create a node w/o any terms.
47     $settings = ['type' => 'article'];
48     $this->nodes[] = $this->drupalCreateNode($settings);
49
50     // Create a node with only the top level term.
51     $settings['field_views_testing_tags'][0]['target_id'] = $first->id();
52     $this->nodes[] = $this->drupalCreateNode($settings);
53
54     // Create a node with only the third level term.
55     $settings['field_views_testing_tags'][0]['target_id'] = $third->id();
56     $this->nodes[] = $this->drupalCreateNode($settings);
57
58     $this->terms[0] = $first;
59     $this->terms[1] = $second;
60     $this->terms[2] = $third;
61
62     $this->view = Views::getView('test_filter_taxonomy_index_tid_depth');
63   }
64
65   /**
66    * Tests the terms with depth filter.
67    */
68   public function testTermWithDepthFilter() {
69     $column_map = ['nid' => 'nid'];
70     $assert_method = 'assertIdentical';
71
72     // Default view has an empty value for this filter, so all nodes should be
73     // returned.
74     $expected = [
75       ['nid' => 1],
76       ['nid' => 2],
77       ['nid' => 3],
78       ['nid' => 4],
79       ['nid' => 5],
80     ];
81     $this->executeView($this->view);
82     $this->assertIdenticalResultsetHelper($this->view, $expected, $column_map, $assert_method);
83
84     // Set filter to search on top-level term, with depth 0.
85     $expected = [['nid' => 4]];
86     $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);
87
88     // Top-level term, depth 1.
89     $expected = [['nid' => 4]];
90     $this->assertTermWithDepthResult($this->terms[0]->id(), 0, $expected);
91
92     // Top-level term, depth 2.
93     $expected = [['nid' => 4], ['nid' => 5]];
94     $this->assertTermWithDepthResult($this->terms[0]->id(), 2, $expected);
95
96     // Second-level term, depth 1.
97     $expected = [['nid' => 5]];
98     $this->assertTermWithDepthResult($this->terms[1]->id(), 1, $expected);
99
100     // Third-level term, depth 0.
101     $expected = [['nid' => 5]];
102     $this->assertTermWithDepthResult($this->terms[2]->id(), 0, $expected);
103
104     // Third-level term, depth 1.
105     $expected = [['nid' => 5]];
106     $this->assertTermWithDepthResult($this->terms[2]->id(), 1, $expected);
107
108     // Third-level term, depth -2.
109     $expected = [['nid' => 4], ['nid' => 5]];
110     $this->assertTermWithDepthResult($this->terms[2]->id(), -2, $expected);
111   }
112
113   /**
114    * Changes the tid filter to given term and depth.
115    *
116    * @param int $tid
117    *   The term ID to filter on.
118    * @param int $depth
119    *   The depth to search.
120    * @param array $expected
121    *   The expected views result.
122    */
123   protected function assertTermWithDepthResult($tid, $depth, array $expected) {
124     $this->view->destroy();
125     $this->view->initDisplay();
126     $filters = $this->view->displayHandlers->get('default')
127       ->getOption('filters');
128     $filters['tid_depth']['depth'] = $depth;
129     $filters['tid_depth']['value'] = [$tid];
130     $this->view->displayHandlers->get('default')
131       ->setOption('filters', $filters);
132     $this->executeView($this->view);
133     $this->assertIdenticalResultsetHelper($this->view, $expected, ['nid' => 'nid'], 'assertIdentical');
134   }
135
136 }