diff --git a/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php new file mode 100644 index 000000000000..68efc81ed542 --- /dev/null +++ b/AdobeStockImageAdminUi/Test/Unit/Ui/Component/Listing/Filter/ColorTest.php @@ -0,0 +1,233 @@ + [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => true, + 'showSelectionPalette' => true, + ], + 'simple' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => false, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'noalpha' => [ + 'showInput' => true, + 'showInitial' => false, + 'showPalette' => true, + 'showAlpha' => false, + 'showSelectionPalette' => true, + ], + 'palette' => [ + 'showInput' => false, + 'showInitial' => false, + 'showPalette' => true, + 'showPaletteOnly' => true, + 'showAlpha' => false, + 'showSelectionPalette' => false, + ] + ]; + + /** + * @var MockObject|UiComponentFactory $uiComponentFactory + */ + private $uiComponentFactory; + + /** + * @var MockObject|FilterBuilder $filterBuilder + */ + private $filterBuilder; + + /** + * @var MockObject|FilterModifier $filterModifier + */ + private $filterModifier; + + /** + * @var MockObject|ColorModesProvider $colorModesProvider + */ + private $colorModesProvider; + + /** + * Create Color filter object + */ + private function createObject(array $data, ContextInterface $context): Color + { + + $this->uiComponentFactory = $this->createMock(UiComponentFactory::class); + $this->filterBuilder = $this->createMock(FilterBuilder::class); + $this->filterModifier = $this->createMock(FilterModifier::class); + $this->colorModesProvider = $this->createMock(ColorModesProvider::class); + return new Color( + $context, + $this->uiComponentFactory, + $this->filterBuilder, + $this->filterModifier, + $this->colorModesProvider, + [], + $data + ); + } + + /** + * Get context + * + * @param array $filterParams + * @return ContextInterface + */ + private function getContext(array $filterParams): ContextInterface + { + $context = $this->createMock(ContextInterface::class); + $context->expects($this->once()) + ->method('getFiltersParams') + ->willReturn($filterParams); + $context->expects($this->any()) + ->method('getNamespace'); + + $processorMock = $this->createMock(Processor::class); + $context->expects($this->exactly(2)) + ->method('getProcessor') + ->willReturn($processorMock); + $processorMock->expects($this->exactly(1)) + ->method('register') + ->willReturn(null); + + return $context; + } + + /** + * Prepare test + * + * @param array $testData + * @dataProvider colorPickerModeProvider + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function testPrepare(?string $colorPickerMode, string $appliedValue): void + { + $filter = $this->createMock(Filter::class); + $context = $this->getContext( + [ + self::FILTER_NAME => $appliedValue + ] + ); + + $color = $this->createObject( + [ + 'config' => [ + 'colorPickerMode' => $colorPickerMode + ], + 'name' => self::FILTER_NAME + ], + $context + ); + + $this->uiComponentFactory->expects($this->once()) + ->method('create') + ->with( + self::FILTER_NAME, + Input::COMPONENT, + ['context' => $context] + ) + ->willReturn($this->getWrappedComponent($context)); + + $this->verifyApplyFilter($appliedValue, $filter, $context); + + $this->colorModesProvider->expects($this->once()) + ->method('getModes') + ->willReturn(self::COLOR_PICKER_MODES); + + $color->prepare(); + } + + private function verifyApplyFilter(string $appliedValue, Filter $filter, ContextInterface $context): void + { + $this->filterBuilder->expects($this->once()) + ->method('setConditionType') + ->willReturnSelf(); + $this->filterBuilder->expects($this->once()) + ->method('setField') + ->willReturnSelf(); + $this->filterBuilder->expects($this->once()) + ->method('setValue') + ->with(str_replace('#', '', $appliedValue)) + ->willReturnSelf(); + + $this->filterBuilder->expects($this->once()) + ->method('create') + ->willReturn($filter); + + $dataProvider = $this->createMock(DataProviderInterface::class); + $context->expects($this->any()) + ->method('getDataProvider') + ->willReturn($dataProvider); + $dataProvider->expects($this->once()) + ->method('addFilter') + ->with($filter); + } + + /** + * Get wrapped component + * + * @return MockObject|UiComponentInterface + */ + private function getWrappedComponent(ContextInterface $context): UiComponentInterface + { + $wrappedComponent = $this->createMock(UiComponentInterface::class); + $wrappedComponent->expects($this->once()) + ->method('prepare'); + $wrappedComponent->expects($this->once()) + ->method('getContext') + ->willReturn($context); + + return $wrappedComponent; + } + + /** + * Data provider for testPrepare + * + * @return array + */ + public function colorPickerModeProvider(): array + { + return [ + [ + 'full', '#21ffff' + ], + [ + null, '#ffffff' + ] + ]; + } +}