Skip to content

Commit 6982520

Browse files
Added test for category attribute generator
1 parent 2c06892 commit 6982520

File tree

5 files changed

+190
-8
lines changed

5 files changed

+190
-8
lines changed

src/com/magento/idea/magento2plugin/actions/generation/data/CategoryFormXmlData.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ public CategoryFormXmlData(
2020
@NotNull final String attributeInput,
2121
@NotNull final int sortOrder
2222
) {
23-
this.fieldSetName = fieldSetName;
23+
this.fieldSetName = convertGroupNameToFieldSet(fieldSetName);
2424
this.fieldName = fieldName;
2525
this.attributeInput = attributeInput;
2626
this.sortOrder = sortOrder;
2727
}
2828

29+
private String convertGroupNameToFieldSet(final String groupName) {
30+
final String[] nameParts = groupName.toLowerCase().split(" ");//NOPMD
31+
32+
return String.join("_", nameParts);
33+
}
34+
2935
public String getFieldSetName() {
3036
return fieldSetName;
3137
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCategoryEavAttributeDialog.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ protected void generateExtraFilesAfterDataPatchGeneration(
247247

248248
private void generateCategoryAdminForm(final CategoryEntityData categoryEntityData) {
249249
final CategoryFormXmlData categoryFormXmlData = new CategoryFormXmlData(
250-
convertGroupNameToFieldSet(categoryEntityData.getGroup()),
250+
categoryEntityData.getGroup(),
251251
categoryEntityData.getCode(),
252252
categoryEntityData.getInput(),
253253
categoryEntityData.getSortOrder()
@@ -259,10 +259,4 @@ private void generateCategoryAdminForm(final CategoryEntityData categoryEntityDa
259259
moduleName
260260
).generate(actionName, false);
261261
}
262-
263-
private String convertGroupNameToFieldSet(final String groupName) {
264-
final String[] nameParts = groupName.toLowerCase().split(" ");//NOPMD
265-
266-
return String.join("_", nameParts);
267-
}
268262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Foo\Bar\Setup\Patch\Data;
4+
5+
use Magento\Eav\Setup\EavSetup;
6+
use Magento\Eav\Setup\EavSetupFactory;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
10+
class AddTestAttributeCategoryAttribute implements DataPatchInterface
11+
{
12+
13+
/**
14+
* @var ModuleDataSetupInterface
15+
*/
16+
private $moduleDataSetup;
17+
18+
/**
19+
* @var EavSetupFactory
20+
*/
21+
private $eavSetupFactory;
22+
23+
/**
24+
* AddRecommendedAttribute constructor.
25+
*
26+
* @param ModuleDataSetupInterface $moduleDataSetup
27+
* @param EavSetupFactory $eavSetupFactory
28+
*/
29+
public function __construct(
30+
ModuleDataSetupInterface $moduleDataSetup,
31+
EavSetupFactory $eavSetupFactory
32+
)
33+
{
34+
$this->moduleDataSetup = $moduleDataSetup;
35+
$this->eavSetupFactory = $eavSetupFactory;
36+
}
37+
38+
/**
39+
* Get array of patches that have to be executed prior to this.
40+
*
41+
* Example of implementation:
42+
*
43+
* [
44+
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
45+
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
46+
* ]
47+
*
48+
* @return string[]
49+
*/
50+
public static function getDependencies()
51+
{
52+
return [];
53+
}
54+
55+
/**
56+
* Get aliases (previous names) for the patch.
57+
*
58+
* @return string[]
59+
*/
60+
public function getAliases()
61+
{
62+
return [];
63+
}
64+
65+
/**
66+
* Run code inside patch
67+
* If code fails, patch must be reverted, in case when we are speaking about schema - then under revert
68+
* means run PatchInterface::revert()
69+
*
70+
* If we speak about data, under revert means: $transaction->rollback()
71+
*
72+
* @return $this
73+
*/
74+
public function apply()
75+
{
76+
/** @var EavSetup $eavSetup */
77+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
78+
79+
$eavSetup->addAttribute(
80+
\Magento\Catalog\Model\Category::ENTITY,
81+
'test_attribute',
82+
[
83+
'input' => 'text',
84+
'visible' => true,
85+
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
86+
'label' => 'Test Attribute',
87+
'source' => null,
88+
'type' => 'static',
89+
'sort_order' => 10,
90+
'required' => false,
91+
'group' => 'Content',
92+
]
93+
);
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
4+
<fieldset name="content">
5+
<field name="test_attribute" sortOrder="10" formElement="input"/>
6+
</fieldset>
7+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*
5+
*/
6+
7+
package com.magento.idea.magento2plugin.actions.generation.generator;
8+
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.generation.data.CategoryEntityData;
12+
import com.magento.idea.magento2plugin.actions.generation.data.CategoryFormXmlData;
13+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScope;
14+
15+
public class CategoryAttributePropertySetupPatchGeneratorTest extends BaseGeneratorTestCase {
16+
17+
private final static String MODULE_NAME = "Foo_Bar";
18+
19+
public void testGenerateFile() {
20+
final Project project = myFixture.getProject();
21+
22+
final CategoryEntityData categoryEntityData = new CategoryEntityData();
23+
categoryEntityData.setCode("test_attribute");
24+
categoryEntityData.setInput("text");
25+
categoryEntityData.setVisible(true);
26+
categoryEntityData.setLabel("Test Attribute");
27+
categoryEntityData.setType("static");
28+
categoryEntityData.setRequired(false);
29+
categoryEntityData.setGroup("Content");
30+
categoryEntityData.setSortOrder(10);
31+
categoryEntityData.setScope(AttributeScope.GLOBAL.getScope());
32+
33+
categoryEntityData.setDataPatchName("AddTestAttributeCategoryAttribute");
34+
categoryEntityData.setModuleName(MODULE_NAME);
35+
36+
37+
final EavAttributeSetupPatchGenerator setupPatchGenerator =
38+
new EavAttributeSetupPatchGenerator(categoryEntityData, project);
39+
final PsiFile dataPatchFile = setupPatchGenerator.generate("testGenerateFile");
40+
41+
final String filePatch = this.getFixturePath("AddTestAttributeCategoryAttribute.php");
42+
final PsiFile expectedFile = myFixture.configureByFile(filePatch);
43+
44+
assertGeneratedFileIsCorrect(expectedFile, "src/app/code/Foo/Bar/Setup/Patch/Data", dataPatchFile);
45+
}
46+
47+
public void testGenerateFormFile() {
48+
final Project project = myFixture.getProject();
49+
50+
final CategoryEntityData categoryEntityData = new CategoryEntityData();
51+
categoryEntityData.setCode("test_attribute");
52+
categoryEntityData.setInput("text");
53+
categoryEntityData.setVisible(true);
54+
categoryEntityData.setLabel("Test Attribute");
55+
categoryEntityData.setType("static");
56+
categoryEntityData.setRequired(false);
57+
categoryEntityData.setGroup("Content");
58+
categoryEntityData.setSortOrder(10);
59+
categoryEntityData.setScope(AttributeScope.GLOBAL.getScope());
60+
61+
categoryEntityData.setDataPatchName("AddTestAttributeCategoryAttribute");
62+
categoryEntityData.setModuleName(MODULE_NAME);
63+
64+
final CategoryFormXmlData categoryFormXmlData = new CategoryFormXmlData(
65+
categoryEntityData.getGroup(),
66+
categoryEntityData.getCode(),
67+
categoryEntityData.getInput(),
68+
categoryEntityData.getSortOrder()
69+
);
70+
71+
final CategoryFormXmlGenerator categoryFormXmlGenerator =
72+
new CategoryFormXmlGenerator(categoryFormXmlData, project, MODULE_NAME);
73+
final PsiFile categoryForm = categoryFormXmlGenerator.generate("category_form");
74+
75+
final String fileCategoryForm = this.getFixturePath("category_form.xml");
76+
final PsiFile expectedCategoryFile = myFixture.configureByFile(fileCategoryForm);
77+
78+
assertGeneratedFileIsCorrect(expectedCategoryFile, "src/app/code/Foo/Bar/view/adminhtml/ui_component", categoryForm);
79+
}
80+
}

0 commit comments

Comments
 (0)