SearchApiDateTest.php 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace Drupal\Tests\facets\Kernel\Plugin\query_type;

use Drupal\KernelTests\KernelTestBase;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\query_type\SearchApiDate;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;

/**
 * Kernel test for date query type.
 *
 * @group facets
 */
class SearchApiDateTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

23
24
    // This is the default set by Drupal as well, but to be explicit we set it
    // here as well. The raw value is the UTC, the displayed value is calculated
25
26
27
28
29
30
31
32
33
34
35
36
37
    // by the PHP timezone - presently.
    date_default_timezone_set('Australia/Sydney');
  }

  /**
   * Tests string query type without executing the query with an "AND" operator.
   *
   * @dataProvider resultsProvider
   */
  public function testQueryTypeAnd($granularity, $original_results, $grouped_results) {
    $query = new SearchApiQuery([], 'search_api_query', []);
    $facetReflection = new \ReflectionClass('Drupal\facets\Entity\Facet');
    $facet = new Facet(
borisson_'s avatar
borisson_ committed
38
      ['query_operator' => 'AND', 'widget' => 'links'],
39
40
41
42
43
      'facets_facet'
    );
    $widget = $this->getMockBuilder('Drupal\facets\Widget\WidgetPluginInterface')
      ->disableOriginalConstructor()
      ->getMock();
44
45
    $widget->method('getConfiguration')
      ->will($this->returnValue([
borisson_'s avatar
borisson_ committed
46
47
48
49
        'granularity' => $granularity,
        'date_display' => '',
        'display_relative' => FALSE,
      ]));
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    $widget_instance = $facetReflection->getProperty('widgetInstance');
    $widget_instance->setAccessible(TRUE);
    $widget_instance->setValue($facet, $widget);

    $query_type = new SearchApiDate(
      [
        'facet' => $facet,
        'query' => $query,
        'results' => $original_results,
      ],
      'search_api_string',
      []
    );

    $built_facet = $query_type->build();
    $this->assertInstanceOf('\Drupal\facets\FacetInterface', $built_facet);

    $results = $built_facet->getResults();
    $this->assertInternalType('array', $results);

    foreach ($grouped_results as $k => $result) {
      $this->assertInstanceOf('\Drupal\facets\Result\ResultInterface', $results[$k]);
      $this->assertEquals($result['count'], $results[$k]->getCount());
      $this->assertEquals($result['filter'], $results[$k]->getDisplayValue());
    }
  }

  /**
   * Data provider for date results and different groupings.
   */
  public function resultsProvider() {
    return [
      'Year' => [
        SearchApiDate::FACETAPI_DATE_YEAR,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001' => ['count' => 1, 'filter' => 2001],
          '2010' => ['count' => 2, 'filter' => 2010],
          '2011' => ['count' => 1, 'filter' => 2011],
          '2016' => ['count' => 10, 'filter' => 2016],
        ],
      ],
      'Month' => [
        SearchApiDate::FACETAPI_DATE_MONTH,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001-03' => ['count' => 1, 'filter' => 'March 2001'],
          '2010-03' => ['count' => 2, 'filter' => 'March 2010'],
          '2011-05' => ['count' => 1, 'filter' => 'May 2011'],
          '2016-05' => ['count' => 8, 'filter' => 'May 2016'],
          '2016-06' => ['count' => 2, 'filter' => 'June 2016'],
        ],
      ],
      'Day' => [
        SearchApiDate::FACETAPI_DATE_DAY,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001-03-16' => ['count' => 1, 'filter' => '16 March 2001'],
          '2010-03-18' => ['count' => 1, 'filter' => '18 March 2010'],
          '2010-03-31' => ['count' => 1, 'filter' => '31 March 2010'],
          '2011-05-25' => ['count' => 1, 'filter' => '25 May 2011'],
          '2016-05-25' => ['count' => 5, 'filter' => '25 May 2016'],
          '2016-05-26' => ['count' => 3, 'filter' => '26 May 2016'],
          '2016-06-03' => ['count' => 1, 'filter' => '03 June 2016'],
          '2016-06-15' => ['count' => 1, 'filter' => '15 June 2016'],
        ],
      ],
      'Hour' => [
        SearchApiDate::FACETAPI_DATE_HOUR,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001-03-16T14' => ['count' => 1, 'filter' => '16/03/2001 14h'],
          '2010-03-18T19' => ['count' => 1, 'filter' => '18/03/2010 19h'],
          '2010-03-31T02' => ['count' => 1, 'filter' => '31/03/2010 02h'],
          '2011-05-25T19' => ['count' => 1, 'filter' => '25/05/2011 19h'],
          '2016-05-25T19' => ['count' => 3, 'filter' => '25/05/2016 19h'],
          '2016-05-25T20' => ['count' => 1, 'filter' => '25/05/2016 20h'],
          '2016-05-25T21' => ['count' => 1, 'filter' => '25/05/2016 21h'],
          '2016-05-26T05' => ['count' => 1, 'filter' => '26/05/2016 05h'],
          '2016-05-26T18' => ['count' => 2, 'filter' => '26/05/2016 18h'],
          '2016-06-03T14' => ['count' => 1, 'filter' => '03/06/2016 14h'],
          '2016-06-15T04' => ['count' => 1, 'filter' => '15/06/2016 04h'],
        ],
      ],
      'Minute' => [
        SearchApiDate::FACETAPI_DATE_MINUTE,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001-03-16T14:02' => ['count' => 1, 'filter' => '16/03/2001 14:02'],
          '2010-03-18T19:22' => ['count' => 1, 'filter' => '18/03/2010 19:22'],
          '2010-03-31T02:32' => ['count' => 1, 'filter' => '31/03/2010 02:32'],
          '2011-05-25T19:12' => ['count' => 1, 'filter' => '25/05/2011 19:12'],
          '2016-05-25T19:12' => ['count' => 3, 'filter' => '25/05/2016 19:12'],
          '2016-05-25T20:30' => ['count' => 1, 'filter' => '25/05/2016 20:30'],
          '2016-05-25T21:12' => ['count' => 1, 'filter' => '25/05/2016 21:12'],
          '2016-05-26T05:00' => ['count' => 1, 'filter' => '26/05/2016 05:00'],
          '2016-05-26T18:10' => ['count' => 2, 'filter' => '26/05/2016 18:10'],
          '2016-06-03T14:05' => ['count' => 1, 'filter' => '03/06/2016 14:05'],
          '2016-06-15T04:54' => ['count' => 1, 'filter' => '15/06/2016 04:54'],
        ],
      ],
      'Second' => [
        SearchApiDate::FACETAPI_DATE_SECOND,
        [
          ['count' => 1, 'filter' => '984711763'],
          ['count' => 1, 'filter' => '1268900542'],
          ['count' => 1, 'filter' => '1269963121'],
          ['count' => 1, 'filter' => '1306314733'],
          ['count' => 1, 'filter' => '1464167533'],
          ['count' => 2, 'filter' => '1464167534'],
          ['count' => 1, 'filter' => '1464172214'],
          ['count' => 1, 'filter' => '1464174734'],
          ['count' => 1, 'filter' => '1464202800'],
          ['count' => 1, 'filter' => '1464250210'],
          ['count' => 1, 'filter' => '1464250230'],
          ['count' => 1, 'filter' => '1464926723'],
          ['count' => 1, 'filter' => '1465930475'],
        ],
        [
          '2001-03-16T14:02:43' => ['count' => 1, 'filter' => '16/03/2001 14:02:43'],
          '2010-03-18T19:22:22' => ['count' => 1, 'filter' => '18/03/2010 19:22:22'],
          '2010-03-31T02:32:01' => ['count' => 1, 'filter' => '31/03/2010 02:32:01'],
          '2011-05-25T19:12:13' => ['count' => 1, 'filter' => '25/05/2011 19:12:13'],
          '2016-05-25T19:12:13' => ['count' => 1, 'filter' => '25/05/2016 19:12:13'],
          '2016-05-25T19:12:14' => ['count' => 2, 'filter' => '25/05/2016 19:12:14'],
          '2016-05-25T20:30:14' => ['count' => 1, 'filter' => '25/05/2016 20:30:14'],
          '2016-05-25T21:12:14' => ['count' => 1, 'filter' => '25/05/2016 21:12:14'],
          '2016-05-26T05:00:00' => ['count' => 1, 'filter' => '26/05/2016 05:00:00'],
          '2016-05-26T18:10:10' => ['count' => 1, 'filter' => '26/05/2016 18:10:10'],
          '2016-05-26T18:10:30' => ['count' => 1, 'filter' => '26/05/2016 18:10:30'],
          '2016-06-03T14:05:23' => ['count' => 1, 'filter' => '03/06/2016 14:05:23'],
          '2016-06-15T04:54:35' => ['count' => 1, 'filter' => '15/06/2016 04:54:35'],
        ],
      ],
    ];
  }

  /**
   * Tests string query type without results.
   */
  public function testEmptyResults() {
    $query = new SearchApiQuery([], 'search_api_query', []);
    $facet = new Facet([], 'facets_facet');

    $query_type = new SearchApiDate(
      [
        'facet' => $facet,
        'query' => $query,
      ],
      'search_api_string',
      []
    );

    $built_facet = $query_type->build();
    $this->assertInstanceOf('\Drupal\facets\FacetInterface', $built_facet);

    $results = $built_facet->getResults();
    $this->assertInternalType('array', $results);
    $this->assertEmpty($results);
  }

}