summaryrefslogtreecommitdiffstats
path: root/tests/src/Unit/Plugin/Condition/OtherFacetTest.php
blob: 821f0750ad02bfd06d850f6ff8968f3e6857de4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
190
<?php

/**
 * @file
 * Contains \Drupal\Tests\facets\Plugin\Condition\OtherFacetTest.
 */

namespace Drupal\Tests\facets\Unit\Plugin\Condition;

use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\Condition\OtherFacet;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;

/**
 * Unit test for the 'other facet' condition plugin.
 *
 * @group facets
 */
class OtherFacetTest extends UnitTestCase {

  /**
   * Tests what happens when no values are passed on to the plugin.
   */
  public function testNoValue() {
    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
    $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager')
      ->disableOriginalConstructor()
      ->getMock();
    $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
    $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager')
      ->disableOriginalConstructor()
      ->getMock();
    $sut = new OtherFacet($storage, $manager, $user, $facet_manager, [], 'other_facet', '');

    $evaluation = $sut->evaluate();
    $this->assertTrue($evaluation);
  }

  /**
   * Tests the return value of the plugin for a displayed facet.
   */
  public function testDisplayedFacet() {
    $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock')
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->exactly(1))
      ->method('access')
      ->willReturn(TRUE);
    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
    $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager')
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->exactly(1))
      ->method('createInstance')
      ->willReturn($block);
    $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
    $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager')
      ->disableOriginalConstructor()
      ->getMock();
    $sut = new OtherFacet($storage, $manager, $user, $facet_manager, ['facets' => 'test'], 'other_facet', '');

    $evaluation = $sut->evaluate();
    $this->assertTrue($evaluation);
  }

  /**
   * Tests the return value of the plugin for a hidden facet.
   */
  public function testHiddenFacet() {
    $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock')
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->exactly(1))
      ->method('access')
      ->willReturn(FALSE);
    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
    $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager')
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->exactly(1))
      ->method('createInstance')
      ->willReturn($block);
    $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
    $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager')
      ->disableOriginalConstructor()
      ->getMock();
    $sut = new OtherFacet($storage, $manager, $user, $facet_manager, ['facets' => 'test'], 'other_facet', '');

    $evaluation = $sut->evaluate();
    $this->assertFalse($evaluation);
  }

  /**
   * Tests the return value of the plugin for an active facet value.
   */
  public function testActiveFacetValue() {
    $facet = new Facet([], 'facets_facet');
    /** @var \Drupal\facets\Result\ResultInterface[] $results */
    $results = [
      new Result('llama', 'Llama', 1),
      new Result('kitten', 'Kitten', 5),
      new Result('puppy', 'Puppy', 3),
    ];
    $results[0]->setActiveState(TRUE);
    $facet->setResults($results);

    $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock')
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->exactly(0))->method('access');
    $block->expects($this->exactly(1))
      ->method('getPluginId')
      ->willReturn('block:id');
    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
    $storage->expects($this->exactly(1))
      ->method('load')
      ->with('id')
      ->willReturn($facet);
    $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager')
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->exactly(1))
      ->method('createInstance')
      ->willReturn($block);
    $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
    $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager')
      ->disableOriginalConstructor()
      ->getMock();
    $facet_manager->expects($this->exactly(1))
      ->method('returnProcessedFacet')
      ->with('id')
      ->willReturn($facet);

    $configuration = ['facets' => 'test', 'facet_value' => 'llama'];
    $sut = new OtherFacet($storage, $manager, $user, $facet_manager, $configuration, 'other_facet', '');

    $evaluation = $sut->evaluate();
    $this->assertTrue($evaluation);
  }

  /**
   * Tests the return value of the plugin for an inactive facet value.
   */
  public function testInactiveFacetValue() {
    $facet = new Facet([], 'facets_facet');
    /** @var \Drupal\facets\Result\ResultInterface[] $results */
    $results = [
      new Result('llama', 'Llama', 1),
      new Result('kitten', 'Kitten', 5),
      new Result('puppy', 'Puppy', 3),
    ];
    $results[1]->setActiveState(TRUE);
    $facet->setResults($results);

    $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock')
      ->disableOriginalConstructor()
      ->getMock();
    $block->expects($this->exactly(0))->method('access');
    $block->expects($this->exactly(1))
      ->method('getPluginId')
      ->willReturn('block:id');
    $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
    $storage->expects($this->exactly(1))
      ->method('load')
      ->with('id')
      ->willReturn($facet);
    $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager')
      ->disableOriginalConstructor()
      ->getMock();
    $manager->expects($this->exactly(1))
      ->method('createInstance')
      ->willReturn($block);
    $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
    $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager')
      ->disableOriginalConstructor()
      ->getMock();
    $facet_manager->expects($this->exactly(1))
      ->method('returnProcessedFacet')
      ->with('id')
      ->willReturn($facet);

    $configuration = ['facets' => 'test', 'facet_value' => 'llama'];
    $sut = new OtherFacet($storage, $manager, $user, $facet_manager, $configuration, 'other_facet', '');

    $evaluation = $sut->evaluate();
    $this->assertFalse($evaluation);
  }

}