diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 94244ea64..6f92519b8 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -5,7 +5,7 @@ name: Run automated tests on: pull_request: - branches: [ master, 4.3.0-develop ] + branches: [ master, '*-develop', 'mainline*' ] jobs: build-linux: @@ -107,6 +107,7 @@ jobs: run: ./gradlew checkstyleCI -i --no-daemon env: MODIFIED_FILES: ${{ steps.file_changes.outputs.files}} + ACTIONS_STEP_DEBUG: true - name: Run PMD Quality Check run: ./gradlew pmdCI -i --no-daemon env: diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 3a3ca8ac9..8f9610b47 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -97,6 +97,7 @@ + diff --git a/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.ft b/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.ft new file mode 100644 index 000000000..52d51a592 --- /dev/null +++ b/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.ft @@ -0,0 +1,122 @@ +moduleDataSetup = $moduleDataSetup; + $this->eavSetupFactory = $eavSetupFactory; + $this->eavConfig = $eavConfig; + $this->attributeResource = $attributeResource; + } + + /** + * Run code inside patch + * If code fails, patch must be reverted, in case when we are speaking about schema - then under revert + * means run PatchInterface::revert() + * + * If we speak about data, under revert means: $transaction->rollback() + * + * @return $this + */ + public function apply() + { +/** @var ${EAV_SETUP} $eavSetup */ + $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); + + $eavSetup->addAttribute( + ${ENTITY_CLASS}::ENTITY, + '${ATTRIBUTE_CODE}', + [ + #set ($attributeSet = ${ATTRIBUTE_SET}) + #foreach ($attribute in $attributeSet.split("->")) + $attribute + #end +] + ); + + $eavSetup->addAttributeToSet( + ${CUSTOMER_METADATA_INTERFACE}::ENTITY_TYPE_CUSTOMER, + ${CUSTOMER_METADATA_INTERFACE}::ATTRIBUTE_SET_ID_CUSTOMER, + null, + '${ATTRIBUTE_CODE}' + ); + + #if (${CUSTOMER_FORMS}) + $attribute = $this->eavConfig->getAttribute(${ENTITY_CLASS}::ENTITY, '${ATTRIBUTE_CODE}'); + $attribute->setData( + 'used_in_forms', + [${CUSTOMER_FORMS}] + ); + $this->attributeResource->save($attribute); + #end + + return $this; + } + + /** + * Get array of patches that have to be executed prior to this. + * + * Example of implementation: + * + * [ + * \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, + * \Vendor_Name\Module_Name\Setup\Patch\Patch2::class + * ] + * + * @return string[] + */ + public static function getDependencies() + { + return []; + } + + /** + * Get aliases (previous names) for the patch. + * + * @return string[] + */ + public function getAliases() + { + return []; + } +} diff --git a/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.html b/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.html new file mode 100644 index 000000000..0a2028f8b --- /dev/null +++ b/resources/fileTemplates/internal/Magento Customer Eav Attribute Data Patch Class.php.html @@ -0,0 +1,15 @@ + + + + +

+ Data patch for the customer EAV attribute +

+
+ + \ No newline at end of file diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewCustomerEavAttributeAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewCustomerEavAttributeAction.java new file mode 100644 index 000000000..576749b7a --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/NewCustomerEavAttributeAction.java @@ -0,0 +1,31 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.MagentoIcons; +import com.magento.idea.magento2plugin.actions.generation.dialog.NewCustomerEavAttributeDialog; +import com.magento.idea.magento2plugin.actions.generation.dialog.eavattribute.EavAttributeDialog; +import com.magento.idea.magento2plugin.actions.generation.eavattribute.NewEavAttributeAction; + +public class NewCustomerEavAttributeAction extends NewEavAttributeAction { + + public static final String ACTION_NAME = "Customer Attribute"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 EAV Customer Attribute"; + + public NewCustomerEavAttributeAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); + } + + @Override + protected EavAttributeDialog getDialogWindow( + final Project project, + final PsiDirectory directory + ) { + return new NewCustomerEavAttributeDialog(project, directory, ACTION_NAME); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/CustomerEntityData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/CustomerEntityData.java new file mode 100644 index 000000000..db45bf7c7 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/CustomerEntityData.java @@ -0,0 +1,269 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity; +import java.util.Map; + +@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessivePublicCount"}) +public class CustomerEntityData implements EavEntityDataInterface { + + private String code; + private String type; + private String label; + private String input; + private String namespace; + private String moduleName; + private String directory; + private String dataPatchName; + private String source; + private int sortOrder; + private Map options; + private String model; + private Map optionsSortOrder; + private boolean required; + private boolean visible; + private boolean userDefined; + private boolean useInAdminhtmlCustomerForm; + private boolean useInAdminhtmlCheckoutForm; + private boolean useInCustomerAccountCreateForm; + private boolean useInCustomerAccountEditForm; + private boolean usedInGrid; + private boolean visibleInGrid; + private boolean filterableInGrid; + private boolean system; + + @Override + public void setCode(final String code) { + this.code = code; + } + + @Override + public void setType(final String type) { + this.type = type; + } + + @Override + public void setLabel(final String label) { + this.label = label; + } + + @Override + public void setInput(final String input) { + this.input = input; + } + + @Override + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + + @Override + public void setModuleName(final String moduleName) { + this.moduleName = moduleName; + } + + @Override + public void setDirectory(final String directory) { + this.directory = directory; + } + + @Override + public void setDataPatchName(final String dataPatchName) { + this.dataPatchName = dataPatchName; + } + + @Override + public void setSource(final String source) { + this.source = source; + } + + @Override + public void setSortOrder(final int sortOrder) { + this.sortOrder = sortOrder; + } + + @Override + public void setOptions(final Map options) { + this.options = options; + } + + @Override + public void setOptionsSortOrder(final Map optionsSortOrder) { + this.optionsSortOrder = optionsSortOrder; + } + + @Override + public void setRequired(final boolean required) { + this.required = required; + } + + @Override + public void setVisible(final boolean visible) { + this.visible = visible; + } + + @Override + public void setBackendModel(final String model) { + this.model = model; + } + + public void setUserDefined(final boolean userDefined) { + this.userDefined = userDefined; + } + + public void setUseInAdminhtmlCustomerForm(final boolean useInAdminhtmlCustomerForm) { + this.useInAdminhtmlCustomerForm = useInAdminhtmlCustomerForm; + } + + public void setUseInAdminhtmlCheckoutForm(final boolean useInAdminhtmlCheckoutForm) { + this.useInAdminhtmlCheckoutForm = useInAdminhtmlCheckoutForm; + } + + public void setUseInCustomerAccountCreateForm(final boolean useInCustomerAccountCreateForm) { + this.useInCustomerAccountCreateForm = useInCustomerAccountCreateForm; + } + + public void setUseInCustomerAccountEditForm(final boolean useInCustomerAccountEditForm) { + this.useInCustomerAccountEditForm = useInCustomerAccountEditForm; + } + + public void setUsedInGrid(final boolean usedInGrid) { + this.usedInGrid = usedInGrid; + } + + public void setVisibleInGrid(final boolean visibleInGrid) { + this.visibleInGrid = visibleInGrid; + } + + public void setFilterableInGrid(final boolean filterableInGrid) { + this.filterableInGrid = filterableInGrid; + } + + public void setSystem(final boolean system) { + this.system = system; + } + + @Override + public String getCode() { + return code; + } + + @Override + public String getType() { + return type; + } + + @Override + public String getLabel() { + return label; + } + + @Override + public String getInput() { + return input; + } + + @Override + public String getNamespace() { + return namespace; + } + + @Override + public String getModuleName() { + return moduleName; + } + + @Override + public String getDirectory() { + return directory; + } + + @Override + public String getDataPatchName() { + return dataPatchName; + } + + @Override + public String getEntityClass() { + return EavEntity.CUSTOMER.getEntityClass(); + } + + @Override + public String getSource() { + return source; + } + + @Override + public String getBackendModel() { + return null; + } + + @Override + public int getSortOrder() { + return sortOrder; + } + + @Override + public Map getOptions() { + return options; + } + + public String getModel() { + return model; + } + + @Override + public Map getOptionsSortOrder() { + return optionsSortOrder; + } + + @Override + public boolean isRequired() { + return required; + } + + @Override + public boolean isVisible() { + return visible; + } + + public boolean isUserDefined() { + return userDefined; + } + + public boolean isUseInAdminhtmlCustomerForm() { + return useInAdminhtmlCustomerForm; + } + + public boolean isUseInAdminhtmlCheckoutForm() { + return useInAdminhtmlCheckoutForm; + } + + public boolean isUseInCustomerAccountCreateForm() { + return useInCustomerAccountCreateForm; + } + + public boolean isUseInCustomerAccountEditForm() { + return useInCustomerAccountEditForm; + } + + public boolean isUsedInGrid() { + return usedInGrid; + } + + public boolean isVisibleInGrid() { + return visibleInGrid; + } + + public boolean isFilterableInGrid() { + return filterableInGrid; + } + + public boolean isSystem() { + return system; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.form new file mode 100644 index 000000000..1f26f958e --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.form @@ -0,0 +1,465 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.java new file mode 100644 index 000000000..47da9a97b --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCustomerEavAttributeDialog.java @@ -0,0 +1,338 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.dialog; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.actions.generation.data.CustomerEntityData; +import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface; +import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; +import com.magento.idea.magento2plugin.actions.generation.dialog.eavattribute.EavAttributeDialog; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.generator.CustomerEavAttributePatchGenerator; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel; +import com.magento.idea.magento2plugin.magento.packages.uicomponent.AvailableSourcesByInput; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.List; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTable; +import javax.swing.JTextField; + +@SuppressWarnings({ + "PMD.TooManyFields", + "PMD.ExcessiveImports", + "PMD.TooManyMethods", + "PMD.UnusedPrivateField" +}) +public class NewCustomerEavAttributeDialog extends EavAttributeDialog { + + private static final String ENTITY_NAME = "Customer"; + private JPanel contentPanel; + private JButton buttonOK; + private JButton buttonCancel; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Attribute Code"}) + @FieldValidation(rule = RuleRegistry.LOWERCASE, + message = {Lowercase.MESSAGE, "Attribute Code"}) + private JTextField codeTextField; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Attribute Label"}) + private JTextField labelTextField; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Data Patch Name"}) + private JTextField dataPatchNameTextField; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Attribute Sort Order"}) + @FieldValidation(rule = RuleRegistry.ALPHANUMERIC, + message = {NotEmptyRule.MESSAGE, "Attribute Sort Order"}) + private JTextField sortOrderTextField; + private JComboBox inputComboBox; + private JComboBox typeComboBox; + private JComboBox sourceComboBox; + private JCheckBox requiredCheckBox; + private JCheckBox visibleCheckBox; + private JPanel sourcePanel; + private JPanel customSourceModelPanel; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Source Model Directory"}) + private JTextField sourceModelDirectoryTextField; + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, "Source Model Name"}) + private JTextField sourceModelNameTextField; + private JTable optionsTable; + private JButton addNewOptionButton; + private JPanel optionsPanel; + private JLabel codeTextFieldErrorMessage; + private JLabel labelTextFieldErrorMessage; + private JLabel dataPatchNameTextFieldErrorMessage; + private JLabel sourceModelDirectoryTextFieldErrorMessage; + private JLabel sourceModelNameTextFieldErrorMessage; + private JLabel sortOrderTextFieldErrorMessage; + private JCheckBox userDefineCheckBox; + private JCheckBox useInAdminhtmlCustomerCheckBox; + private JCheckBox useInCustomerAccountCreateCheckBox; + private JCheckBox useInCustomerAccountEditCheckBox; + private JCheckBox useInGridCheckBox; + private JCheckBox filterableInGridCheckBox; + private JCheckBox visibleInGridCheckBox; + private JCheckBox systemAttributeCheckBox; + private JCheckBox useInAdminhtmlCheckoutCheckBox; + + /** + * Constructor. + * + * @param project Project + * @param directory PsiDirectory + * @param actionName String + */ + public NewCustomerEavAttributeDialog( + final Project project, + final PsiDirectory directory, + final String actionName + ) { + super(project, directory, actionName); + } + + @Override + protected JPanel getContentPanel() { + return contentPanel; + } + + @Override + protected JButton getButtonOk() { + return buttonOK; + } + + @Override + protected JButton getButtonCancel() { + return buttonCancel; + } + + @Override + protected JComboBox getAttributeTypeCompoBox() { + return typeComboBox; + } + + @Override + protected JComboBox getAttributeInputComboBox() { + return inputComboBox; + } + + @Override + protected JTable getOptionsTable() { + return optionsTable; + } + + @Override + protected JButton getNewOptionButton() { + return addNewOptionButton; + } + + @Override + protected JComboBox getAttributeSourceComboBox() { + return sourceComboBox; + } + + @Override + protected JTextField getAttributeSourceModelNameTexField() { + return sourceModelNameTextField; + } + + @Override + protected JTextField getSourceModelDirectoryTextField() { + return sourceModelDirectoryTextField; + } + + @Override + protected JPanel getAttributeCustomSourceModelPanel() { + return customSourceModelPanel; + } + + @Override + protected JPanel getAttributeOptionsPanel() { + return optionsPanel; + } + + @Override + protected JTextField getAttributeCodeTextField() { + return codeTextField; + } + + @Override + protected JTextField getDataPatchNameTextField() { + return dataPatchNameTextField; + } + + @Override + protected JTextField getSourceModelNameTextField() { + return sourceModelNameTextField; + } + + @Override + protected JTextField getAttributeLabelTexField() { + return labelTextField; + } + + @Override + protected JTextField getAttributeSortOrderTextField() { + return sortOrderTextField; + } + + @Override + protected JCheckBox getAttributeRequiredCheckBox() { + return requiredCheckBox; + } + + @Override + protected JCheckBox getAttributeVisibleBox() { + return visibleCheckBox; + } + + @Override + protected String getEntityName() { + return ENTITY_NAME; + } + + @Override + protected void generateDataPatchFile(final EavEntityDataInterface eavEntityDataInterface) { + new CustomerEavAttributePatchGenerator( + eavEntityDataInterface, + project, + true + ).generate(actionName, true); + } + + @Override + protected EavEntityDataInterface getEavEntityData() { + return populateCategoryEntityData(new CustomerEntityData()); + } + + private CustomerEntityData populateCategoryEntityData( + final CustomerEntityData customerEntityData + ) { + customerEntityData.setModuleName(moduleName); + customerEntityData.setType(getAttributeBackendType()); + customerEntityData.setDataPatchName(getDataPatchName()); + customerEntityData.setCode(getAttributeCode()); + customerEntityData.setLabel(getAttributeLabel()); + customerEntityData.setSortOrder(getAttributeSortOrder()); + customerEntityData.setRequired(isRequiredAttribute()); + customerEntityData.setVisible(isVisibleAttribute()); + customerEntityData.setInput(getAttributeInput()); + customerEntityData.setSource(getAttributeSource(sourceModelData)); + customerEntityData.setOptions( + getAttributeOptions(entityPropertiesTableGroupWrapper) + ); + customerEntityData.setOptionsSortOrder( + getAttributeOptionsSortOrders(entityPropertiesTableGroupWrapper) + ); + customerEntityData.setUserDefined(userDefineCheckBox.isSelected()); + customerEntityData.setUseInAdminhtmlCustomerForm( + useInAdminhtmlCustomerCheckBox.isSelected() + ); + customerEntityData.setUseInAdminhtmlCheckoutForm( + useInAdminhtmlCheckoutCheckBox.isSelected() + ); + customerEntityData.setUseInCustomerAccountCreateForm( + useInCustomerAccountCreateCheckBox.isSelected() + ); + customerEntityData.setUseInCustomerAccountEditForm( + useInCustomerAccountEditCheckBox.isSelected() + ); + customerEntityData.setVisibleInGrid(visibleInGridCheckBox.isSelected()); + customerEntityData.setUsedInGrid(useInGridCheckBox.isSelected()); + customerEntityData.setFilterableInGrid(filterableInGridCheckBox.isSelected()); + customerEntityData.setSystem(systemAttributeCheckBox.isSelected()); + + return customerEntityData; + } + + @Override + protected void addOptionPanelListener( + final JComboBox attributeSourceComboBox, + final JComboBox attributeInputComboBox, + final JPanel attributeOptionsPanel + ) { + if (attributeSourceComboBox == null + || attributeInputComboBox == null + || attributeOptionsPanel == null + ) { + return; + } + + attributeSourceComboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(final ItemEvent itemEvent) { + final ComboBoxItemData selectedInputItem = + (ComboBoxItemData) attributeInputComboBox.getSelectedItem(); + final String selectedInput = selectedInputItem == null + ? "" : selectedInputItem.toString(); + final boolean isAllowedInput = + AttributeInput.SELECT.getInput().equals(selectedInput) + || AttributeInput.MULTISELECT.getInput().equals(selectedInput); + + attributeOptionsPanel.setVisible(isAllowedInput); + } + }); + } + + @Override + protected void setAttributeInputComboBoxAction( + final JComboBox sourceComboBox, + final JComboBox inputComboBox + ) { + if (sourceComboBox == null || inputComboBox == null) { + return; + } + + inputComboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(final ItemEvent itemEvent) { + final String selectedInput = itemEvent.getItem().toString(); + + final List availableSources = + new AvailableSourcesByInput(selectedInput).getItems(); + sourceComboBox.removeAllItems(); + + if (!selectedInput.equals(AttributeInput.SELECT.getInput()) + || !selectedInput.equals(AttributeInput.MULTISELECT.getInput())) { + final ComboBoxItemData defaultSourceItem = new ComboBoxItemData( + AttributeSourceModel.NULLABLE_SOURCE.name(), + AttributeSourceModel.NULLABLE_SOURCE.getSource() + ); + + sourceComboBox.addItem(defaultSourceItem); + sourceComboBox.setSelectedItem(defaultSourceItem); + } + + if (availableSources.isEmpty()) { + return; + } + + for (final ComboBoxItemData comboBoxItemData : availableSources) { + sourceComboBox.addItem(comboBoxItemData); + + if (comboBoxItemData.getText().equals(AttributeSourceModel.TABLE.getSource())) { + sourceComboBox.setSelectedItem(comboBoxItemData); + } + } + } + }); + } + + private void createUIComponents() { //NOPMD - suppressed UnusedPrivateMethod + // TODO: place custom component creation code here + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/eavattribute/EavAttributeDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/eavattribute/EavAttributeDialog.java index cdd4daaad..885baff52 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/eavattribute/EavAttributeDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/eavattribute/EavAttributeDialog.java @@ -45,7 +45,12 @@ import javax.swing.KeyStroke; import org.jetbrains.annotations.NotNull; -@SuppressWarnings({"PMD.GodClass", "PMD.TooManyMethods"}) +@SuppressWarnings({ + "PMD.GodClass", + "PMD.TooManyMethods", + "PMD.ExcessiveImports", + "PMD.AccessorMethodGeneration" +}) public abstract class EavAttributeDialog extends AbstractDialog { protected String moduleName; @@ -149,7 +154,10 @@ protected void initBaseDialogState() { this.addCancelActionForWindow(); this.addCancelActionForEsc(); - this.setAttributeInputComboBoxAction(getAttributeSourceComboBox()); + this.setAttributeInputComboBoxAction( + getAttributeSourceComboBox(), + getAttributeInputComboBox() + ); this.setSourceComboBoxAction(getAttributeSourceComboBox()); this.setSourceModelPanelAction( @@ -294,6 +302,10 @@ protected void generateExtraFilesBeforeDataPatchGeneration() { generateSourceModelFile(); } + @SuppressWarnings({ + "PMD.EmptyMethodInAbstractClassShouldBeAbstract", + "PMD.UncommentedEmptyMethodBody" + }) protected void generateExtraFilesAfterDataPatchGeneration( final EavEntityDataInterface eavEntityDataInterface ) {} @@ -316,20 +328,19 @@ protected void addCancelActionForEsc() { ); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void setAttributeInputComboBoxAction( - final JComboBox sourceComboBox + final JComboBox sourceComboBox, + final JComboBox inputComboBox ) { - if (sourceComboBox == null) { + if (sourceComboBox == null || inputComboBox == null) { return; } - getAttributeInputComboBox().addItemListener( + inputComboBox.addItemListener( new EavAttributeInputItemListener(sourceComboBox) ); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void setSourceComboBoxAction(final JComboBox sourceComboBox) { if (sourceComboBox == null) { return; @@ -340,7 +351,6 @@ protected void setSourceComboBoxAction(final JComboBox sourceC ); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void setSourceModelPanelAction( final JPanel attributeCustomSourceModelPanel, final JTextField sourceModelDirectoryTexField @@ -354,7 +364,6 @@ protected void setSourceModelPanelAction( ); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void addOptionPanelListener( final JComboBox attributeSourceComboBox, final JComboBox attributeInputComboBox, @@ -403,7 +412,6 @@ protected void setAutocompleteListenerForAttributeCodeField( .addDocumentListener(new AttributeCodeAdapter(attributeCodeTextField)); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void setAutocompleteListenerForDataPathNameField( final JTextField mainTextField, final JTextField dependentTextField @@ -417,7 +425,6 @@ protected void setAutocompleteListenerForDataPathNameField( .addDocumentListener(new DataPatchNameAdapter(dependentTextField, getEntityName())); } - @SuppressWarnings("PMD.AccessorMethodGeneration") protected void setAutocompleteListenerForSourceModelNameField( final JTextField mainTextField, final JTextField dependentTextField diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/CustomerEavAttributePatchGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/CustomerEavAttributePatchGenerator.java new file mode 100644 index 000000000..890be334d --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/CustomerEavAttributePatchGenerator.java @@ -0,0 +1,114 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.magento.idea.magento2plugin.actions.generation.data.CustomerEntityData; +import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; +import com.magento.idea.magento2plugin.magento.files.CustomerEavAttributeDataPatchFile; +import com.magento.idea.magento2plugin.magento.packages.eav.CustomerForm; +import com.magento.idea.magento2plugin.magento.packages.eav.DataPatchDependency; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import org.jetbrains.annotations.NotNull; + +public class CustomerEavAttributePatchGenerator extends EavAttributeSetupPatchGenerator { + + private final EavEntityDataInterface data; + + public CustomerEavAttributePatchGenerator( + final @NotNull EavEntityDataInterface data, + final Project project + ) { + this(data, project, true); + } + + /** + * Php file generator constructor. + * + * @param project Project + * @param checkFileAlreadyExists boolean + */ + public CustomerEavAttributePatchGenerator( + final @NotNull EavEntityDataInterface data, + final @NotNull Project project, + final boolean checkFileAlreadyExists + ) { + super(data, project, checkFileAlreadyExists); + this.data = data; + } + + @Override + protected AbstractPhpFile initFile() { + return new CustomerEavAttributeDataPatchFile(data.getModuleName(), data.getDataPatchName()); + } + + @Override + protected void fillAttributes(final Properties attributes) { + super.fillAttributes(attributes); + phpClassTypesBuilder + .append( + "EAV_CONFIG_CLASS", + DataPatchDependency.EAV_CONFIG.getClassPatch() + ).append( + "ATTRIBUTE_RESOURCE", + DataPatchDependency.ATTRIBUTE_RESOURCE.getClassPatch() + ).append( + "CUSTOMER_METADATA_INTERFACE", + DataPatchDependency.CUSTOMER_METADATA_INTERFACE.getClassPatch() + ); + + final String selectedCustomerForms = getFormsForAttribute((CustomerEntityData) data); + + if (!selectedCustomerForms.isEmpty()) { + phpClassTypesBuilder.appendProperty("CUSTOMER_FORMS", selectedCustomerForms); + } + + phpClassTypesBuilder.mergeProperties(attributes); + + attributes.setProperty( + "USES", + PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) + ); + } + + private String getFormsForAttribute(final CustomerEntityData customerEntityData) { + final List usedInForms = new ArrayList<>(); + + if (customerEntityData.isUseInAdminhtmlCustomerForm()) { + usedInForms.add( + "'" + CustomerForm.ADMINHTML_CUSTOMER.getFormCode() + "'" + ); + } + + if (customerEntityData.isUseInAdminhtmlCheckoutForm()) { + usedInForms.add( + "'" + CustomerForm.ADMINHTML_CHECKOUT.getFormCode() + "'" + ); + } + + if (customerEntityData.isUseInCustomerAccountCreateForm()) { + usedInForms.add( + "'" + CustomerForm.CUSTOMER_ACCOUNT_CREATE.getFormCode() + "'" + ); + } + + if (customerEntityData.isUseInCustomerAccountEditForm()) { + usedInForms.add( + "'" + CustomerForm.CUSTOMER_ACCOUNT_EDIT.getFormCode() + "'" + ); + } + + if (usedInForms.isEmpty()) { + return ""; + } + + return String.join(",", usedInForms); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/EavAttributeSetupPatchGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/EavAttributeSetupPatchGenerator.java index 55b6dc99c..3339abf99 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/EavAttributeSetupPatchGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/EavAttributeSetupPatchGenerator.java @@ -19,7 +19,9 @@ import org.jetbrains.annotations.NotNull; public class EavAttributeSetupPatchGenerator extends PhpFileGenerator { + private final EavEntityDataInterface data; + protected final PhpClassTypesBuilder phpClassTypesBuilder; /** * Constructor. @@ -47,6 +49,7 @@ public EavAttributeSetupPatchGenerator( ) { super(project, checkFileAlreadyExists); this.data = data; + this.phpClassTypesBuilder = new PhpClassTypesBuilder(); } @Override @@ -56,8 +59,6 @@ protected AbstractPhpFile initFile() { @Override protected void fillAttributes(final Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - phpClassTypesBuilder .appendProperty("CLASS_NAME", data.getDataPatchName()) .appendProperty("NAMESPACE", this.getFile().getNamespace()) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/AttributeMapperFactory.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/AttributeMapperFactory.java index b5d8f102b..1d9bf01d5 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/AttributeMapperFactory.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/AttributeMapperFactory.java @@ -19,6 +19,8 @@ public AttributeMapperInterface createByEntityClass(@NotNull final String entity return new ProductAttributeMapper(); } else if (entityClass.equals(EavEntity.CATEGORY.getEntityClass())) { return new CategoryAttributeMapper(); + } else if (entityClass.equals(EavEntity.CUSTOMER.getEntityClass())) { + return new CustomerAttributeMapper(); } return null; diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CategoryAttributeMapper.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CategoryAttributeMapper.java index e51d38782..b291e3e06 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CategoryAttributeMapper.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CategoryAttributeMapper.java @@ -17,6 +17,10 @@ protected Map getMappedAttributes(final EavEntityDataInterface e final Map mappedAttributes = super.getMappedAttributes(eavEntityData); final CategoryEntityData categoryEavEntityData = (CategoryEntityData) eavEntityData; + mappedAttributes.put( + AttributeProperty.SORT_ORDER.getProperty(), + Integer.toString(categoryEavEntityData.getSortOrder()) + ); mappedAttributes.put( AttributeProperty.GLOBAL.getProperty(), categoryEavEntityData.getScope() diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CustomerAttributeMapper.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CustomerAttributeMapper.java new file mode 100644 index 000000000..fa74a6052 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/CustomerAttributeMapper.java @@ -0,0 +1,47 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util.eav; + +import com.magento.idea.magento2plugin.actions.generation.data.CustomerEntityData; +import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeProperty; +import java.util.Map; + +public class CustomerAttributeMapper extends DefaultAttributeMapper { + + @Override + protected Map getMappedAttributes(final EavEntityDataInterface eavEntityData) { + final Map mappedAttributes = super.getMappedAttributes(eavEntityData); + final CustomerEntityData customerEntityData = (CustomerEntityData) eavEntityData; + + mappedAttributes.put( + AttributeProperty.POSITION.getProperty(), + Integer.toString(customerEntityData.getSortOrder()) + ); + mappedAttributes.put( + AttributeProperty.USER_DEFINED.getProperty(), + Boolean.toString(customerEntityData.isUserDefined()) + ); + mappedAttributes.put( + AttributeProperty.IS_USED_IN_GRID.getProperty(), + Boolean.toString(customerEntityData.isUsedInGrid()) + ); + mappedAttributes.put( + AttributeProperty.IS_VISIBLE_IN_GRID.getProperty(), + Boolean.toString(customerEntityData.isVisibleInGrid()) + ); + mappedAttributes.put( + AttributeProperty.IS_FILTERABLE_IN_GRID.getProperty(), + Boolean.toString(customerEntityData.isFilterableInGrid()) + ); + mappedAttributes.put( + AttributeProperty.SYSTEM.getProperty(), + Boolean.toString(customerEntityData.isSystem()) + ); + + return mappedAttributes; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/DefaultAttributeMapper.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/DefaultAttributeMapper.java index 1b683a0ec..f209305c0 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/DefaultAttributeMapper.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/DefaultAttributeMapper.java @@ -1,7 +1,6 @@ /* * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. - * */ package com.magento.idea.magento2plugin.actions.generation.generator.util.eav; @@ -9,14 +8,16 @@ import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface; import com.magento.idea.magento2plugin.magento.packages.eav.AttributeBackendModel; import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput; -import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel; import com.magento.idea.magento2plugin.magento.packages.eav.AttributeProperty; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class DefaultAttributeMapper implements AttributeMapperInterface { + private static final String PHP_DOUBLE_ARROW_OPERATOR = " => "; + @Override public List mapAttributesByEntityData(final EavEntityDataInterface entityData) { final List attributesWithValues = new ArrayList<>(); @@ -58,10 +59,6 @@ protected Map getMappedAttributes(final EavEntityDataInterface e AttributeProperty.REQUIRED.getProperty(), Boolean.toString(eavEntityData.isRequired()) ); - mappedAttributes.put( - AttributeProperty.SORT_ORDER.getProperty(), - Integer.toString(eavEntityData.getSortOrder()) - ); mappedAttributes.put( AttributeProperty.VISIBLE.getProperty(), Boolean.toString(eavEntityData.isVisible()) @@ -75,7 +72,10 @@ protected Map getMappedAttributes(final EavEntityDataInterface e if (!attributeOptions.isEmpty()) { mappedAttributes.put( AttributeProperty.OPTION.getProperty(), - getMappedOptions(eavEntityData.getOptions(), eavEntityData.getOptionsSortOrder()) + getMappedOptions( + eavEntityData.getOptions(), + eavEntityData.getOptionsSortOrder() + ) ); } @@ -119,7 +119,8 @@ protected String getMappedOptions( } protected String getParsedOptions(final Map optionValues) { - final String valueNode = "->" + wrapStringValueForTemplate("value") + " => "; + final String valueNode = "->" + wrapStringValueForTemplate("value") + + PHP_DOUBLE_ARROW_OPERATOR; final StringBuilder optionsContent = new StringBuilder(); for (final Integer optionKey : optionValues.keySet()) { @@ -132,8 +133,8 @@ protected String getParsedOptions(final Map optionValues) { optionsContent .append("->") .append(wrapStringValueForTemplate("option_" + optionKey)) - .append(" => ") - .append("[") + .append(PHP_DOUBLE_ARROW_OPERATOR) + .append('[') .append(wrapStringValueForTemplate(optionValue)) .append("], "); } @@ -142,7 +143,8 @@ protected String getParsedOptions(final Map optionValues) { } protected String getParsedOptionSortOrders(final Map optionSortOrders) { - final String orderNode = "->" + wrapStringValueForTemplate("order") + " => "; + final String orderNode = "->" + wrapStringValueForTemplate("order") + + PHP_DOUBLE_ARROW_OPERATOR; final StringBuilder ordersContent = new StringBuilder(); for (final Integer optionKey : optionSortOrders.keySet()) { @@ -155,9 +157,9 @@ protected String getParsedOptionSortOrders(final Map optionSort ordersContent .append("->") .append(wrapStringValueForTemplate("option_" + optionKey)) - .append(" => ") + .append(PHP_DOUBLE_ARROW_OPERATOR) .append(orderValue) - .append(","); + .append(','); } return orderNode + "[" + ordersContent + "->]->"; diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/ProductAttributeMapper.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/ProductAttributeMapper.java index 943c29580..4dd4c7265 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/ProductAttributeMapper.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/ProductAttributeMapper.java @@ -18,6 +18,10 @@ protected Map getMappedAttributes(final EavEntityDataInterface e final Map mappedAttributes = super.getMappedAttributes(eavEntityData); final ProductEntityData productEavEntityData = (ProductEntityData) eavEntityData; + mappedAttributes.put( + AttributeProperty.SORT_ORDER.getProperty(), + Integer.toString(productEavEntityData.getSortOrder()) + ); mappedAttributes.put( AttributeProperty.GROUP.getProperty(), wrapStringValueForTemplate(productEavEntityData.getGroup()) @@ -47,7 +51,8 @@ protected Map getMappedAttributes(final EavEntityDataInterface e Boolean.toString(productEavEntityData.isVisibleOnFront()) ); - if (productEavEntityData.getApplyTo() != null && !productEavEntityData.getApplyTo().isEmpty()) { + if (productEavEntityData.getApplyTo() != null + && !productEavEntityData.getApplyTo().isEmpty()) { mappedAttributes.put( AttributeProperty.APPLY_TO.getProperty(), wrapStringValueForTemplate(productEavEntityData.getApplyTo()) diff --git a/src/com/magento/idea/magento2plugin/magento/files/CustomerEavAttributeDataPatchFile.java b/src/com/magento/idea/magento2plugin/magento/files/CustomerEavAttributeDataPatchFile.java new file mode 100644 index 000000000..02a90c03b --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/CustomerEavAttributeDataPatchFile.java @@ -0,0 +1,42 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +import org.jetbrains.annotations.NotNull; + +public class CustomerEavAttributeDataPatchFile extends AbstractPhpFile { + public static final String HUMAN_READABLE_NAME = "Customer Eav Attribute Data Patch Class"; + public static final String TEMPLATE = "Magento Customer Eav Attribute Data Patch Class"; + public static final String DEFAULT_DIR = "Setup/Patch/Data"; + + /** + * Abstract php file constructor. + * + * @param moduleName String + * @param className String + */ + public CustomerEavAttributeDataPatchFile( + final @NotNull String moduleName, + final @NotNull String className + ) { + super(moduleName, className); + } + + @Override + public String getDirectory() { + return DEFAULT_DIR; + } + + @Override + public String getHumanReadableName() { + return HUMAN_READABLE_NAME; + } + + @Override + public String getTemplate() { + return TEMPLATE; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/eav/AttributeProperty.java b/src/com/magento/idea/magento2plugin/magento/packages/eav/AttributeProperty.java index 5804022c5..3a2b47200 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/eav/AttributeProperty.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/eav/AttributeProperty.java @@ -22,7 +22,10 @@ public enum AttributeProperty { VISIBLE_ON_FRONT("visible_on_front"), APPLY_TO("apply_to"), OPTION("option"), - BACKEND_MODEL("backend"); + BACKEND_MODEL("backend"), + USER_DEFINED("user_defined"), + POSITION("position"), + SYSTEM("system"); private String attribute; diff --git a/src/com/magento/idea/magento2plugin/magento/packages/eav/CustomerForm.java b/src/com/magento/idea/magento2plugin/magento/packages/eav/CustomerForm.java new file mode 100644 index 000000000..40c7733ff --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/eav/CustomerForm.java @@ -0,0 +1,27 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.eav; + +public enum CustomerForm { + + ADMINHTML_CHECKOUT("adminhtml_checkout"), + ADMINHTML_CUSTOMER("adminhtml_customer"), + ADMINHTML_CUSTOMER_ADDRESS("adminhtml_customer_address"), + CUSTOMER_ACCOUNT_CREATE("customer_account_create"), + CUSTOMER_ACCOUNT_EDIT("customer_account_edit"), + CUSTOMER_ADDRESS_EDIT("customer_address_edit"), + CUSTOMER_REGISTER_ADDRESS("customer_register_address"); + + private final String formCode; + + CustomerForm(final String formCode) { + this.formCode = formCode; + } + + public String getFormCode() { + return this.formCode; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/eav/DataPatchDependency.java b/src/com/magento/idea/magento2plugin/magento/packages/eav/DataPatchDependency.java index 73f2bf0fa..aee63c3d9 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/eav/DataPatchDependency.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/eav/DataPatchDependency.java @@ -6,10 +6,13 @@ package com.magento.idea.magento2plugin.magento.packages.eav; public enum DataPatchDependency { - ENV_SETUP("Magento\\Eav\\Setup\\EavSetup"), + CUSTOMER_METADATA_INTERFACE("Magento\\Customer\\Api\\CustomerMetadataInterface"), + DATA_PATCH_INTERFACE("Magento\\Framework\\Setup\\Patch\\DataPatchInterface"), + EAV_CONFIG("Magento\\Eav\\Model\\Config"), EAV_SETUP_FACTORY("Magento\\Eav\\Setup\\EavSetupFactory"), + ENV_SETUP("Magento\\Eav\\Setup\\EavSetup"), MODULE_DATA_SETUP_INTERFACE("Magento\\Framework\\Setup\\ModuleDataSetupInterface"), - DATA_PATCH_INTERFACE("Magento\\Framework\\Setup\\Patch\\DataPatchInterface"); + ATTRIBUTE_RESOURCE("Magento\\Customer\\Model\\ResourceModel\\Attribute"); private String classPatch; diff --git a/src/com/magento/idea/magento2plugin/magento/packages/eav/EavEntity.java b/src/com/magento/idea/magento2plugin/magento/packages/eav/EavEntity.java index 36f2487cb..ee3e770ea 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/eav/EavEntity.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/eav/EavEntity.java @@ -7,7 +7,8 @@ public enum EavEntity { PRODUCT("Magento\\Catalog\\Model\\Product"), - CATEGORY("Magento\\Catalog\\Model\\Category"); + CATEGORY("Magento\\Catalog\\Model\\Category"), + CUSTOMER("Magento\\Customer\\Model\\Customer"); private String entityClass; diff --git a/testData/actions/generation/generator/CustomerAttributeSetupPatchGenerator/generateMultiselectAttributeDataPatch/AddMultiselectTestCustomerAttribute.php b/testData/actions/generation/generator/CustomerAttributeSetupPatchGenerator/generateMultiselectAttributeDataPatch/AddMultiselectTestCustomerAttribute.php new file mode 100644 index 000000000..4757629dd --- /dev/null +++ b/testData/actions/generation/generator/CustomerAttributeSetupPatchGenerator/generateMultiselectAttributeDataPatch/AddMultiselectTestCustomerAttribute.php @@ -0,0 +1,139 @@ +moduleDataSetup = $moduleDataSetup; + $this->eavSetupFactory = $eavSetupFactory; + $this->eavConfig = $eavConfig; + $this->attributeResource = $attributeResource; + } + + /** + * Run code inside patch + * If code fails, patch must be reverted, in case when we are speaking about schema - then under revert + * means run PatchInterface::revert() + * + * If we speak about data, under revert means: $transaction->rollback() + * + * @return $this + */ + public function apply() + { + /** @var EavSetup $eavSetup */ + $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); + + $eavSetup->addAttribute( + Customer::ENTITY, + 'multiselect_test', + [ + 'is_visible_in_grid' => false, + 'visible' => true, + 'label' => 'Multiselect Test', + 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class, + 'type' => 'varchar', + 'is_used_in_grid' => false, + 'required' => false, + 'input' => 'multiselect', + 'user_defined' => true, + 'is_filterable_in_grid' => false, + 'system' => false, + 'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class, + 'position' => 10, + 'option' => [ + 'value' => [ + 'option_0' => ['option1'], + 'option_1' => ['option2'], + 'option_2' => ['option3'], + ] + ], + ] + ); + + $eavSetup->addAttributeToSet( + CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, + CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, + null, + 'multiselect_test' + ); + + $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'multiselect_test'); + $attribute->setData( + 'used_in_forms', + ['adminhtml_customer'] + ); + $this->attributeResource->save($attribute); + + return $this; + } + + /** + * Get array of patches that have to be executed prior to this. + * + * Example of implementation: + * + * [ + * \Vendor_Name\Module_Name\Setup\Patch\Patch1::class, + * \Vendor_Name\Module_Name\Setup\Patch\Patch2::class + * ] + * + * @return string[] + */ + public static function getDependencies() + { + return []; + } + + /** + * Get aliases (previous names) for the patch. + * + * @return string[] + */ + public function getAliases() + { + return []; + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/CustomerAttributeSetupPatchGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/CustomerAttributeSetupPatchGeneratorTest.java new file mode 100644 index 000000000..bcd76c58c --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/CustomerAttributeSetupPatchGeneratorTest.java @@ -0,0 +1,63 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.CustomerEntityData; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel; +import com.magento.idea.magento2plugin.magento.packages.eav.AttributeType; +import java.util.HashMap; +import java.util.Map; + +public class CustomerAttributeSetupPatchGeneratorTest extends BaseGeneratorTestCase { + + private static final String MODULE_NAME = "Foo_Bar"; + + /** + * Test generating the customer attribute data patch. + */ + public void testGenerateMultiselectAttributeDataPatch() { + final Project project = myFixture.getProject(); + + final CustomerEntityData customerEntityData = new CustomerEntityData(); + customerEntityData.setCode("multiselect_test"); + customerEntityData.setLabel("Multiselect Test"); + customerEntityData.setVisible(true); + customerEntityData.setSource(AttributeSourceModel.TABLE.getSource()); + customerEntityData.setType(AttributeType.VARCHAR.getType()); + customerEntityData.setInput(AttributeInput.MULTISELECT.getInput()); + customerEntityData.setUserDefined(true); + customerEntityData.setSortOrder(10); + customerEntityData.setUseInAdminhtmlCustomerForm(true); + + final Map options = new HashMap<>(); + options.put(0, "option1"); + options.put(1, "option2"); + options.put(2, "option3"); + customerEntityData.setOptions(options); + + customerEntityData.setDataPatchName("AddMultiselectTestCustomerAttribute"); + customerEntityData.setModuleName(MODULE_NAME); + + + final CustomerEavAttributePatchGenerator setupPatchGenerator = + new CustomerEavAttributePatchGenerator(customerEntityData, project); + final PsiFile dataPatchFile = setupPatchGenerator.generate( + "testGenerateMultiselectAttributeDataPatch" + ); + + final String filePatch = this.getFixturePath("AddMultiselectTestCustomerAttribute.php"); + final PsiFile expectedFile = myFixture.configureByFile(filePatch); + + assertGeneratedFileIsCorrect( + expectedFile, + "src/app/code/Foo/Bar/Setup/Patch/Data", + dataPatchFile + ); + } +}