|
| 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\MediaGalleryUi\Model\Listing; |
| 9 | + |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 13 | +use Magento\Framework\View\Element\UiComponentInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks standalone media gallery listing data provider behavior |
| 19 | + * |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + */ |
| 22 | +class DataProviderTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var ObjectManagerInterface */ |
| 25 | + private $objectManager; |
| 26 | + |
| 27 | + /** @var UiComponentFactory */ |
| 28 | + private $componentFactory; |
| 29 | + |
| 30 | + /** @var RequestInterface */ |
| 31 | + private $request; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 41 | + $this->request = $this->objectManager->get(RequestInterface::class); |
| 42 | + $this->componentFactory = $this->objectManager->get(UiComponentFactory::class); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @magentoDataFixture Magento/MediaGallery/_files/media_asset.php |
| 47 | + * @magentoDataFixture Magento/MediaGallery/_files/media_asset_loaded_year_ago.php |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function testFilterByDate(): void |
| 52 | + { |
| 53 | + $filter = [ |
| 54 | + 'created_at' => [ |
| 55 | + 'from' => (new \DateTime('-1 day'))->format('m/d/Y'), |
| 56 | + 'to' => (new \DateTime())->format('m/d/Y'), |
| 57 | + ], |
| 58 | + ]; |
| 59 | + $this->request->setParams(['filters' => $filter]); |
| 60 | + $data = $this->getComponentProvidedData('standalone_media_gallery_listing'); |
| 61 | + $items = $data['items']; |
| 62 | + $this->assertCount(1, $items); |
| 63 | + $item = reset($items); |
| 64 | + $this->assertEquals('testDirectory/path.jpg', $item['path']); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Call prepare method in the child components |
| 69 | + * |
| 70 | + * @param UiComponentInterface $component |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + private function prepareChildComponents(UiComponentInterface $component): void |
| 74 | + { |
| 75 | + foreach ($component->getChildComponents() as $child) { |
| 76 | + $this->prepareChildComponents($child); |
| 77 | + } |
| 78 | + |
| 79 | + $component->prepare(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get component provided data |
| 84 | + * |
| 85 | + * @param string $namespace |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + private function getComponentProvidedData(string $namespace): array |
| 89 | + { |
| 90 | + $component = $this->componentFactory->create($namespace); |
| 91 | + $this->prepareChildComponents($component); |
| 92 | + |
| 93 | + return $component->getContext()->getDataProvider()->getData(); |
| 94 | + } |
| 95 | +} |
0 commit comments