|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Framework\View\Element\UiComponent\DataProvider; |
| 10 | + |
| 11 | +use Magento\Framework\Api\Filter; |
| 12 | +use Magento\Framework\Api\Search\FilterGroup; |
| 13 | +use Magento\Framework\Api\Search\SearchCriteriaInterface; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Represents FilterPool methods test class |
| 20 | + */ |
| 21 | +class ReportingTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManagerInterface |
| 25 | + */ |
| 26 | + private $objectManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritdoc |
| 30 | + */ |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php |
| 38 | + * @magentoDbIsolation disabled |
| 39 | + * @dataProvider filtersDataProvider |
| 40 | + * @param array $filters |
| 41 | + * @param int $expectedCount |
| 42 | + */ |
| 43 | + public function testSearchItemsByOrCondition(array $filters, int $expectedCount): void |
| 44 | + { |
| 45 | + $filterGroups = []; |
| 46 | + $filterGroups[] = $this->objectManager->create(FilterGroup::class) |
| 47 | + ->setFilters( |
| 48 | + [ |
| 49 | + $this->objectManager->create(Filter::class, ['data' => $filters[0]]), |
| 50 | + $this->objectManager->create(Filter::class, ['data' => $filters[1]]), |
| 51 | + ] |
| 52 | + ); |
| 53 | + $filterGroups[] = $this->objectManager->create(FilterGroup::class) |
| 54 | + ->setFilters([$this->objectManager->create(Filter::class, ['data' => $filters[2]])]); |
| 55 | + if (isset($filters[3], $filters[4])) { |
| 56 | + $filterGroups[] = $this->objectManager->create(FilterGroup::class) |
| 57 | + ->setFilters( |
| 58 | + [ |
| 59 | + $this->objectManager->create(Filter::class, ['data' => $filters[3]]), |
| 60 | + $this->objectManager->create(Filter::class, ['data' => $filters[4]]), |
| 61 | + ] |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** @var SearchCriteriaInterface $searchCriteria */ |
| 66 | + $searchCriteria = $this->objectManager->get(SearchCriteriaInterface::class); |
| 67 | + $searchCriteria->setFilterGroups($filterGroups); |
| 68 | + $searchCriteria->setRequestName('customer_listing_data_source'); |
| 69 | + $searchCriteria->setSortOrders([]); |
| 70 | + |
| 71 | + /** @var Reporting $reporting */ |
| 72 | + $reporting = $this->objectManager->get(Reporting::class); |
| 73 | + $collection = $reporting->search($searchCriteria); |
| 74 | + self::assertCount($expectedCount, $collection->getItems(), 'Wrong collection filters applied'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return array[] |
| 79 | + */ |
| 80 | + public function filtersDataProvider() |
| 81 | + { |
| 82 | + return [ |
| 83 | + 'variation 1 (filter OR filter) AND filter' => [ |
| 84 | + 'filters' => [ |
| 85 | + [ |
| 86 | + 'field' => 'email', |
| 87 | + 'value' => '%1%', |
| 88 | + 'condition_type' => 'like', |
| 89 | + ], |
| 90 | + [ |
| 91 | + 'field' => 'email', |
| 92 | + 'value' => '%2%', |
| 93 | + 'condition_type' => 'like', |
| 94 | + ], |
| 95 | + [ |
| 96 | + 'field' => 'name', |
| 97 | + 'value' => 'John Smith', |
| 98 | + 'condition_type' => 'eq', |
| 99 | + ], |
| 100 | + ], |
| 101 | + 'expected_count' => 2, |
| 102 | + ], |
| 103 | + 'variation 2 (filter OR filter) AND filter' => [ |
| 104 | + 'filters' => [ |
| 105 | + [ |
| 106 | + 'field' => 'email', |
| 107 | + 'value' => '%1%', |
| 108 | + 'condition_type' => 'like', |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'field' => 'name', |
| 112 | + 'value' => 'John Smith', |
| 113 | + 'condition_type' => 'eq', |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'field' => 'email', |
| 117 | + 'value' => '%example%', |
| 118 | + 'condition_type' => 'like', |
| 119 | + ], |
| 120 | + ], |
| 121 | + 'expected_count' => 5, |
| 122 | + ], |
| 123 | + 'variation 3 (filter OR filter) AND filter' => [ |
| 124 | + 'filters' => [ |
| 125 | + [ |
| 126 | + 'field' => 'email', |
| 127 | + 'value' => 'customer%', |
| 128 | + 'condition_type' => 'like', |
| 129 | + ], |
| 130 | + [ |
| 131 | + 'field' => 'name', |
| 132 | + 'value' => 'John%', |
| 133 | + 'condition_type' => 'like', |
| 134 | + ], |
| 135 | + [ |
| 136 | + 'field' => 'email', |
| 137 | + 'value' => 'customer2@example.com', |
| 138 | + 'condition_type' => 'eq', |
| 139 | + ], |
| 140 | + ], |
| 141 | + 'expected_count' => 1, |
| 142 | + ], |
| 143 | + 'variation (filter OR filter) AND filter AND (filter OR filter)' => [ |
| 144 | + 'filters' => [ |
| 145 | + [ |
| 146 | + 'field' => 'email', |
| 147 | + 'value' => 'customer%', |
| 148 | + 'condition_type' => 'like', |
| 149 | + ], |
| 150 | + [ |
| 151 | + 'field' => 'name', |
| 152 | + 'value' => 'Test', |
| 153 | + 'condition_type' => 'eq', |
| 154 | + ], |
| 155 | + [ |
| 156 | + 'field' => 'email', |
| 157 | + 'value' => 'customer%@example.com', |
| 158 | + 'condition_type' => 'like', |
| 159 | + ], |
| 160 | + [ |
| 161 | + 'field' => 'name', |
| 162 | + 'value' => 'non existing', |
| 163 | + 'condition_type' => 'like', |
| 164 | + ], |
| 165 | + [ |
| 166 | + 'field' => 'email', |
| 167 | + 'value' => 'customer3%', |
| 168 | + 'condition_type' => 'like', |
| 169 | + ], |
| 170 | + ], |
| 171 | + 'expected_count' => 1, |
| 172 | + ], |
| 173 | + ]; |
| 174 | + } |
| 175 | +} |
0 commit comments