Newer
Older
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityQueryAggregateTest.
*/
namespace Drupal\system\Tests\Entity;
/**
* Defines a test for testing aggregation support for entity query.
*
* @see \Drupal\entity_test\Entity\EntityTest
*/
class EntityQueryAggregateTest extends EntityUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array();
/**
* The entity_test storage controller to create the test entities.
*
* @var \Drupal\entity_test\EntityTestStorageController
*/
protected $entityStorageController;
/**
* The actual query result, to compare later.
*
* @var array
*/
protected $queryResult;
/**
* The query factory to create entity queries.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
public $factory;
public static function getInfo() {
return array(
'name' => 'Entity Query aggregation',
'description' => 'Tests the Entity Query Aggregation API',
'group' => 'Entity API',
);
}
public function setUp() {
parent::setUp();
$this->entityStorageController = $this->container->get('entity.manager')->getStorageController('entity_test');
$this->factory = $this->container->get('entity.query');
// Add some fieldapi fields to be used in the test.
for ($i = 1; $i <= 2; $i++) {
$field_name = 'field_test_' . $i;
entity_create('field_config', array(
'name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'integer',
'cardinality' => 2,
))->save();
entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
))->save();
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
}
$entity = $this->entityStorageController->create(array(
'id' => 1,
'user_id' => 1,
'field_test_1' => 1,
'field_test_2' => 2,
));
$entity->enforceIsNew();
$entity->save();
$entity = $this->entityStorageController->create(array(
'id' => 2,
'user_id' => 2,
'field_test_1' => 1,
'field_test_2' => 7,
));
$entity->enforceIsNew();
$entity->save();
$entity = $this->entityStorageController->create(array(
'id' => 3,
'user_id' => 2,
'field_test_1' => 2,
'field_test_2' => 1,
));
$entity->enforceIsNew();
$entity->save();
$entity = $this->entityStorageController->create(array(
'id' => 4,
'user_id' => 2,
'field_test_1' => 2,
'field_test_2' => 8,
));
$entity->enforceIsNew();
$entity->save();
$entity = $this->entityStorageController->create(array(
'id' => 5,
'user_id' => 3,
'field_test_1' => 2,
'field_test_2' => 2,
));
$entity->enforceIsNew();
$entity->save();
$entity = $this->entityStorageController->create(array(
'id' => 6,
'user_id' => 3,
'field_test_1' => 3,
'field_test_2' => 8,
));
$entity->enforceIsNew();
$entity->save();
}
/**
* Test aggregation support.
*/
public function testAggregation() {
// Apply a simple groupby.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1),
array('user_id' => 2),
array('user_id' => 3),
));
$function_expected = array();
$function_expected['count'] = array(array('id_count' => 6));
$function_expected['min'] = array(array('id_min' => 1));
$function_expected['max'] = array(array('id_max' => 6));
$function_expected['sum'] = array(array('id_sum' => 21));
$function_expected['avg'] = array(array('id_avg' => (21.0/6.0)));
// Apply a simple aggregation for different aggregation functions.
foreach ($function_expected as $aggregation_function => $expected) {
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', $aggregation_function)
->execute();
$this->assertEqual($this->queryResult, $expected);
}
// Apply aggregation and groupby on the same query.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1, 'id_count' => 1),
array('user_id' => 2, 'id_count' => 3),
array('user_id' => 3, 'id_count' => 2),
));
// Apply aggregation and a condition which matches.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->conditionAggregate('id', 'COUNT', 8)
->execute();
$this->assertResults(array());
// Don't call aggregate to test the implicit aggregate call.
$this->queryResult = $this->factory->getAggregate('entity_test')
->conditionAggregate('id', 'COUNT', 8)
->execute();
$this->assertResults(array());
// Apply aggregation and a condition which matches.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'count')
->conditionAggregate('id', 'COUNT', 6)
->execute();
$this->assertResults(array(array('id_count' => 6)));
// Apply aggregation, a groupby and a condition which matches partially via
// the operator '='.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'count')
->conditionAggregate('id', 'count', 2)
->groupBy('user_id')
->execute();
$this->assertResults(array(array('id_count' => 2, 'user_id' => 3)));
// Apply aggregation, a groupby and a condition which matches partially via
// the operator '>'.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'count')
->conditionAggregate('id', 'COUNT', 1, '>')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('id_count' => 2, 'user_id' => 3),
array('id_count' => 3, 'user_id' => 2),
));
// Apply aggregation and a sort. This might not be useful, but have a proper
// test coverage.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->sortAggregate('id', 'COUNT')
->execute();
$this->assertSortedResults(array(array('id_count' => 6)));
// Don't call aggregate to test the implicit aggregate call.
$this->queryResult = $this->factory->getAggregate('entity_test')
->sortAggregate('id', 'COUNT')
->execute();
$this->assertSortedResults(array(array('id_count' => 6)));
// Apply aggregation, groupby and a sort descending.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->sortAggregate('id', 'COUNT', 'DESC')
->execute();
$this->assertSortedResults(array(
array('user_id' => 2, 'id_count' => 3),
array('user_id' => 3, 'id_count' => 2),
array('user_id' => 1, 'id_count' => 1),
));
// Apply aggregation, groupby and a sort ascending.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->sortAggregate('id', 'COUNT', 'ASC')
->execute();
$this->assertSortedResults(array(
array('user_id' => 1, 'id_count' => 1),
array('user_id' => 3, 'id_count' => 2),
array('user_id' => 2, 'id_count' => 3),
));
// Apply aggregation, groupby, an aggregation condition and a sort with the
// operator '='.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->sortAggregate('id', 'COUNT')
->conditionAggregate('id', 'COUNT', 2)
->execute();
$this->assertSortedResults(array(array('id_count' => 2, 'user_id' => 3)));
// Apply aggregation, groupby, an aggregation condition and a sort with the
// operator '<' and order ASC.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->sortAggregate('id', 'COUNT', 'ASC')
->conditionAggregate('id', 'COUNT', 3, '<')
->execute();
$this->assertSortedResults(array(
array('id_count' => 1, 'user_id' => 1),
array('id_count' => 2, 'user_id' => 3),
));
// Apply aggregation, groupby, an aggregation condition and a sort with the
// operator '<' and order DESC.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('id', 'COUNT')
->groupBy('user_id')
->sortAggregate('id', 'COUNT', 'DESC')
->conditionAggregate('id', 'COUNT', 3, '<')
->execute();
$this->assertSortedResults(array(
array('id_count' => 2, 'user_id' => 3),
array('id_count' => 1, 'user_id' => 1),
));
// Test aggregation/groupby support for fieldapi fields.
// Just group by a fieldapi field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->execute();
$this->assertResults(array(
array('field_test_1' => 1),
array('field_test_1' => 2),
array('field_test_1' => 3),
));
// Group by a fieldapi field and aggregate a normal property.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('user_id', 'COUNT')
->groupBy('field_test_1')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'user_id_count' => 2),
array('field_test_1' => 2, 'user_id_count' => 3),
array('field_test_1' => 3, 'user_id_count' => 1),
));
// Group by a normal property and aggregate a fieldapi field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'COUNT')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1, 'field_test_1_count' => 1),
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_count' => 2),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'SUM')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1, 'field_test_1_sum' => 1),
array('user_id' => 2, 'field_test_1_sum' => 5),
array('user_id' => 3, 'field_test_1_sum' => 5),
));
// Aggregate by two different fieldapi fields.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'SUM')
->aggregate('field_test_2', 'SUM')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1, 'field_test_1_sum' => 1, 'field_test_2_sum' => 2),
array('user_id' => 2, 'field_test_1_sum' => 5, 'field_test_2_sum' => 16),
array('user_id' => 3, 'field_test_1_sum' => 5, 'field_test_2_sum' => 10),
));
// This time aggregate the same field twice.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'SUM')
->aggregate('field_test_1', 'COUNT')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 1, 'field_test_1_sum' => 1, 'field_test_1_count' => 1),
array('user_id' => 2, 'field_test_1_sum' => 5, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_sum' => 5, 'field_test_1_count' => 2),
));
// Group by and aggregate by a fieldapi field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->aggregate('field_test_2', 'COUNT')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2),
array('field_test_1' => 2, 'field_test_2_count' => 3),
array('field_test_1' => 3, 'field_test_2_count' => 1),
));
// Group by and aggregate by a fieldapi field and use multiple aggregate
// functions.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->aggregate('field_test_2', 'COUNT')
->aggregate('field_test_2', 'SUM')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2, 'field_test_2_sum' => 9),
array('field_test_1' => 2, 'field_test_2_count' => 3, 'field_test_2_sum' => 11),
array('field_test_1' => 3, 'field_test_2_count' => 1, 'field_test_2_sum' => 8),
));
// Apply an aggregate condition for a fieldapi field and group by a simple
// property.
$this->queryResult = $this->factory->getAggregate('entity_test')
->conditionAggregate('field_test_1', 'COUNT', 3)
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_count' => 2),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'SUM')
->conditionAggregate('field_test_1', 'COUNT', 2, '>')
->groupBy('user_id')
->execute();
$this->assertResults(array(
array('user_id' => 2, 'field_test_1_sum' => 5, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_sum' => 5, 'field_test_1_count' => 2),
));
// Apply an aggregate condition for a simple property and a group by a
// fieldapi field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->conditionAggregate('user_id', 'COUNT', 2)
->groupBy('field_test_1')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'user_id_count' => 2),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->conditionAggregate('user_id', 'COUNT', 2, '>')
->groupBy('field_test_1')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'user_id_count' => 2),
array('field_test_1' => 2, 'user_id_count' => 3),
));
// Apply an aggregate condition and a group by fieldapi fields.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->conditionAggregate('field_test_2', 'COUNT', 2)
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->conditionAggregate('field_test_2', 'COUNT', 2, '>')
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2),
array('field_test_1' => 2, 'field_test_2_count' => 3),
));
// Apply an aggregate condition and a group by fieldapi fields with multiple
// conditions via AND.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->conditionAggregate('field_test_2', 'COUNT', 2)
->conditionAggregate('field_test_2', 'SUM', 8)
->execute();
$this->assertResults(array());
// Apply an aggregate condition and a group by fieldapi fields with multiple
// conditions via OR.
$this->queryResult = $this->factory->getAggregate('entity_test', 'OR')
->groupBy('field_test_1')
->conditionAggregate('field_test_2', 'COUNT', 2)
->conditionAggregate('field_test_2', 'SUM', 8)
->execute();
$this->assertResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2, 'field_test_2_sum' => 9),
array('field_test_1' => 3, 'field_test_2_count' => 1, 'field_test_2_sum' => 8),
));
// Group by a normal property and aggregate a fieldapi field and sort by the
// groupby field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'COUNT')
->groupBy('user_id')
->sort('user_id', 'DESC')
->execute();
$this->assertSortedResults(array(
array('user_id' => 3, 'field_test_1_count' => 2),
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 1, 'field_test_1_count' => 1),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->aggregate('field_test_1', 'COUNT')
->groupBy('user_id')
->sort('user_id', 'ASC')
->execute();
$this->assertSortedResults(array(
array('user_id' => 1, 'field_test_1_count' => 1),
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_count' => 2),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->conditionAggregate('field_test_1', 'COUNT', 2, '>')
->groupBy('user_id')
->sort('user_id', 'ASC')
->execute();
$this->assertSortedResults(array(
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_count' => 2),
));
// Group by a normal property, aggregate a fieldapi field, and sort by the
// aggregated field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->sortAggregate('field_test_1', 'COUNT', 'DESC')
->groupBy('user_id')
->execute();
$this->assertSortedResults(array(
array('user_id' => 2, 'field_test_1_count' => 3),
array('user_id' => 3, 'field_test_1_count' => 2),
array('user_id' => 1, 'field_test_1_count' => 1),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->sortAggregate('field_test_1', 'COUNT', 'ASC')
->groupBy('user_id')
->execute();
$this->assertSortedResults(array(
array('user_id' => 1, 'field_test_1_count' => 1),
array('user_id' => 3, 'field_test_1_count' => 2),
array('user_id' => 2, 'field_test_1_count' => 3),
));
// Group by and aggregate by fieldapi field, and sort by the groupby field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->aggregate('field_test_2', 'COUNT')
->sort('field_test_1', 'ASC')
->execute();
$this->assertSortedResults(array(
array('field_test_1' => 1, 'field_test_2_count' => 2),
array('field_test_1' => 2, 'field_test_2_count' => 3),
array('field_test_1' => 3, 'field_test_2_count' => 1),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->aggregate('field_test_2', 'COUNT')
->sort('field_test_1', 'DESC')
->execute();
$this->assertSortedResults(array(
array('field_test_1' => 3, 'field_test_2_count' => 1),
array('field_test_1' => 2, 'field_test_2_count' => 3),
array('field_test_1' => 1, 'field_test_2_count' => 2),
));
// Groupy and aggregate by fieldapi field, and sort by the aggregated field.
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->sortAggregate('field_test_2', 'COUNT', 'DESC')
->execute();
$this->assertSortedResults(array(
array('field_test_1' => 2, 'field_test_2_count' => 3),
array('field_test_1' => 1, 'field_test_2_count' => 2),
array('field_test_1' => 3, 'field_test_2_count' => 1),
));
$this->queryResult = $this->factory->getAggregate('entity_test')
->groupBy('field_test_1')
->sortAggregate('field_test_2', 'COUNT', 'ASC')
->execute();
$this->assertSortedResults(array(
array('field_test_1' => 3, 'field_test_2_count' => 1),
array('field_test_1' => 1, 'field_test_2_count' => 2),
array('field_test_1' => 2, 'field_test_2_count' => 3),
));
}
/**
* Asserts the results as expected regardless of order between and in rows.
*
* @param array $expected
* An array of the expected results.
*/
protected function assertResults($expected, $sorted = FALSE) {
$found = TRUE;
$expected_keys = array_keys($expected);
foreach ($this->queryResult as $key => $row) {
$keys = $sorted ? array($key) : $expected_keys;
foreach ($keys as $key) {
$expected_row = $expected[$key];
if (!array_diff_assoc($row, $expected_row) && !array_diff_assoc($expected_row, $row)) {
continue 2;
}
}
$found = FALSE;
break;
}
return $this->assertTrue($found, strtr('!expected expected, !found found', array('!expected' => print_r($expected, TRUE), '!found' => print_r($this->queryResult, TRUE))));
}
/**
* Asserts the results as expected regardless of order in rows.
*
* @param array $expected
* An array of the expected results.
*/
protected function assertSortedResults($expected) {
return $this->assertResults($expected, TRUE);
}
}