|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Ui\DataProvider\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\App\ObjectManager; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Limit total number of products in grid collection test. |
| 19 | + */ |
| 20 | +class LimitTotalNumberOfProductsTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ObjectManagerInterface |
| 24 | + */ |
| 25 | + private $objectManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ScopeConfigInterface |
| 29 | + */ |
| 30 | + private $scopeConfig; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 35 | + $this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test limit total number of products is enabled and limit is reached. |
| 40 | + * |
| 41 | + * @magentoDataFixture Magento/Catalog/_files/products_list.php |
| 42 | + */ |
| 43 | + public function testLimitNumberOfProductsEnabled() |
| 44 | + { |
| 45 | + $productCollection = $this->objectManager->create( |
| 46 | + CollectionFactory::class, |
| 47 | + [ |
| 48 | + ObjectManager::class, |
| 49 | + 'instanceName' => ProductCollection::class |
| 50 | + ] |
| 51 | + ); |
| 52 | + $dataProvider = $this->objectManager->create( |
| 53 | + ProductDataProvider::class, |
| 54 | + [ |
| 55 | + 'name' => 'product_listing_data_source', |
| 56 | + 'primaryFieldName' => 'entity_id', |
| 57 | + 'requestFieldName' => 'id', |
| 58 | + 'collectionFactory' => $productCollection |
| 59 | + ] |
| 60 | + ); |
| 61 | + |
| 62 | + $this->scopeConfig->setValue( |
| 63 | + 'admin/grid/limit_total_number_of_products', |
| 64 | + 1 |
| 65 | + ); |
| 66 | + $this->scopeConfig->setValue( |
| 67 | + 'admin/grid/records_limit', |
| 68 | + 2 |
| 69 | + ); |
| 70 | + |
| 71 | + $data = $dataProvider->getData(); |
| 72 | + $this->assertEquals(2, $data['totalRecords']); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test limit total number of products is enabled and limit is not reached. |
| 77 | + * |
| 78 | + * @magentoDataFixture Magento/Catalog/_files/products_list.php |
| 79 | + */ |
| 80 | + public function testLimitNumberOfProductsEnabledAndLimitIsNotReached() |
| 81 | + { |
| 82 | + $productCollection = $this->objectManager->create( |
| 83 | + CollectionFactory::class, |
| 84 | + [ |
| 85 | + ObjectManager::class, |
| 86 | + 'instanceName' => ProductCollection::class |
| 87 | + ] |
| 88 | + ); |
| 89 | + $dataProvider = $this->objectManager->create( |
| 90 | + ProductDataProvider::class, |
| 91 | + [ |
| 92 | + 'name' => 'product_listing_data_source', |
| 93 | + 'primaryFieldName' => 'entity_id', |
| 94 | + 'requestFieldName' => 'id', |
| 95 | + 'collectionFactory' => $productCollection |
| 96 | + ] |
| 97 | + ); |
| 98 | + |
| 99 | + $this->scopeConfig->setValue( |
| 100 | + 'admin/grid/limit_total_number_of_products', |
| 101 | + 1 |
| 102 | + ); |
| 103 | + $this->scopeConfig->setValue( |
| 104 | + 'admin/grid/records_limit', |
| 105 | + 3 |
| 106 | + ); |
| 107 | + |
| 108 | + $data = $dataProvider->getData(); |
| 109 | + $this->assertEquals(3, $data['totalRecords']); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Test limit total number of products is disabled. |
| 114 | + * |
| 115 | + * @magentoDataFixture Magento/Catalog/_files/products_list.php |
| 116 | + */ |
| 117 | + public function testLimitNumberOfProductsDisabled() |
| 118 | + { |
| 119 | + $productCollection = $this->objectManager->create( |
| 120 | + CollectionFactory::class, |
| 121 | + [ |
| 122 | + ObjectManager::class, |
| 123 | + 'instanceName' => ProductCollection::class |
| 124 | + ] |
| 125 | + ); |
| 126 | + $dataProvider = $this->objectManager->create( |
| 127 | + ProductDataProvider::class, |
| 128 | + [ |
| 129 | + 'name' => 'product_listing_data_source', |
| 130 | + 'primaryFieldName' => 'entity_id', |
| 131 | + 'requestFieldName' => 'id', |
| 132 | + 'collectionFactory' => $productCollection |
| 133 | + ] |
| 134 | + ); |
| 135 | + |
| 136 | + $this->scopeConfig->setValue( |
| 137 | + 'admin/grid/limit_total_number_of_products', |
| 138 | + 0 |
| 139 | + ); |
| 140 | + $this->scopeConfig->setValue( |
| 141 | + 'admin/grid/records_limit', |
| 142 | + 20000 |
| 143 | + ); |
| 144 | + |
| 145 | + $data = $dataProvider->getData(); |
| 146 | + $this->assertEquals(3, $data['totalRecords']); |
| 147 | + } |
| 148 | + |
| 149 | + protected function tearDown(): void |
| 150 | + { |
| 151 | + $this->scopeConfig->setValue( |
| 152 | + 'admin/grid/limit_total_number_of_products', |
| 153 | + 0 |
| 154 | + ); |
| 155 | + $this->scopeConfig->setValue( |
| 156 | + 'admin/grid/records_limit', |
| 157 | + 20000 |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments