diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml
index 52958ffdc..b568b4315 100644
--- a/resources/META-INF/plugin.xml
+++ b/resources/META-INF/plugin.xml
@@ -65,6 +65,7 @@
+
diff --git a/resources/fileTemplates/code/Magento Module Email Template Xml.xml.ft b/resources/fileTemplates/code/Magento Module Email Template Xml.xml.ft
new file mode 100644
index 000000000..359f7d77a
--- /dev/null
+++ b/resources/fileTemplates/code/Magento Module Email Template Xml.xml.ft
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/resources/fileTemplates/code/Magento Module Email Template Xml.xml.html b/resources/fileTemplates/code/Magento Module Email Template Xml.xml.html
new file mode 100644
index 000000000..66462e777
--- /dev/null
+++ b/resources/fileTemplates/code/Magento Module Email Template Xml.xml.html
@@ -0,0 +1,52 @@
+
+
+
+
+
+
+ Email templates configuration.
+ |
+
+
+
+
+ Predefined variables explanation: |
+
+
+ ${ID} |
+ |
+ Email template ID. |
+
+
+ ${LABEL} |
+ |
+ Email Template Label. |
+
+
+ ${FILE_NAME} |
+ |
+ Email template file name. |
+
+
+ ${TYPE} |
+ |
+ Email template type. (HTML or TEXT) |
+
+
+ ${MODULE} |
+ |
+ Module's name. |
+
+
+ ${AREA} |
+ |
+ Email template area. (Adminhtml or Frontend) |
+
+
+
+
\ No newline at end of file
diff --git a/resources/fileTemplates/internal/Magento Module Email Template Html.html.ft b/resources/fileTemplates/internal/Magento Module Email Template Html.html.ft
new file mode 100644
index 000000000..60ee91ef3
--- /dev/null
+++ b/resources/fileTemplates/internal/Magento Module Email Template Html.html.ft
@@ -0,0 +1,7 @@
+
+
+
+#if (${HTML_TYPE}){{template config_path="design/email/header_template"}}#end
+
+
+#if (${HTML_TYPE}){{template config_path="design/email/footer_template"}}#end
diff --git a/resources/fileTemplates/internal/Magento Module Email Template Html.html.html b/resources/fileTemplates/internal/Magento Module Email Template Html.html.html
new file mode 100644
index 000000000..88051f483
--- /dev/null
+++ b/resources/fileTemplates/internal/Magento Module Email Template Html.html.html
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+ Email template.
+ |
+
+
+
+
+ Predefined variables explanation: |
+
+
+ ${SUBJECT} |
+ |
+ Email subject. |
+
+
+ ${HTML_TYPE} |
+ |
+ Defines if it's HTML type template. |
+
+
+ ${TYPE} |
+ |
+ Email template type. (HTML or TEXT) |
+
+
+
+
\ No newline at end of file
diff --git a/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.ft b/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.ft
new file mode 100644
index 000000000..ed3c17ff9
--- /dev/null
+++ b/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.ft
@@ -0,0 +1,4 @@
+
+
+
+
diff --git a/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.html b/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.html
new file mode 100644
index 000000000..464c24c2f
--- /dev/null
+++ b/resources/fileTemplates/internal/Magento Module Email Templates Xml.xml.html
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+ Email templates configurations.
+ |
+
+
+
+
\ No newline at end of file
diff --git a/resources/magento2/common.properties b/resources/magento2/common.properties
index b72dad68b..e6b406f14 100644
--- a/resources/magento2/common.properties
+++ b/resources/magento2/common.properties
@@ -64,3 +64,8 @@ common.addToolbarFullTextSearch=Add Toolbar Full Text Search
common.addToolbarColumnsControl=Add Toolbar Columns Control
common.addToolbarBookmarks=Add Toolbar Bookmarks
common.argument=Argument name
+common.template.id=Template ID
+common.template.label=Template Label
+common.template.filename=Template File Name
+common.template.subject=Email Subject
+common.template.type=Email Type
\ No newline at end of file
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewEmailTemplateAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewEmailTemplateAction.java
new file mode 100644
index 000000000..78a99904b
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/NewEmailTemplateAction.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation;
+
+import com.intellij.ide.IdeView;
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.actionSystem.DataContext;
+import com.intellij.openapi.actionSystem.LangDataKeys;
+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.NewEmailTemplateDialog;
+
+@SuppressWarnings({"PMD.OnlyOneReturn", "PMD.FieldNamingConventions"})
+public class NewEmailTemplateAction extends AnAction {
+ public static final String ACTION_NAME = "Magento 2 Email Template";
+ public static final String ACTION_DESCRIPTION = "Create a new Magento 2 email template";
+
+ /**
+ * New email template action constructor.
+ */
+ public NewEmailTemplateAction() {
+ super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
+ }
+
+ @Override
+ public void actionPerformed(final AnActionEvent event) {
+ final DataContext dataContext = event.getDataContext();
+ final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
+
+ if (view == null) {
+ return;
+ }
+
+ final Project project = CommonDataKeys.PROJECT.getData(dataContext);
+ if (project == null) {
+ return;
+ }
+
+ final PsiDirectory directory = view.getOrChooseDirectory();
+ if (directory == null) {
+ return;
+ }
+
+ NewEmailTemplateDialog.open(project, directory);
+ }
+
+ @Override
+ public boolean isDumbAware() {
+ return false;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplateHtmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplateHtmlData.java
new file mode 100644
index 000000000..1385503f4
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplateHtmlData.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation.data;
+
+@SuppressWarnings({"PMD.DataClass"})
+public class EmailTemplateHtmlData {
+ private final String module;
+ private final String fileName;
+ private final String area;
+ private final String subject;
+ private final String type;
+
+ /**
+ * Email template HTML data constructor.
+ *
+ * @param module String
+ * @param area String
+ * @param subject String
+ * @param type String
+ */
+ public EmailTemplateHtmlData(
+ final String module,
+ final String fileName,
+ final String area,
+ final String subject,
+ final String type
+ ) {
+ this.module = module;
+ this.fileName = fileName;
+ this.area = area;
+ this.subject = subject;
+ this.type = type;
+ }
+
+ /**
+ * Get module name.
+ *
+ * @return String
+ */
+ public String getModule() {
+ return module;
+ }
+
+ /**
+ * Get file name.
+ *
+ * @return String
+ */
+ public String getFileName() {
+ return fileName;
+ }
+
+ /**
+ * Get area.
+ *
+ * @return String
+ */
+ public String getArea() {
+ return area;
+ }
+
+ /**
+ * Get subject.
+ *
+ * @return String
+ */
+ public String getSubject() {
+ return subject;
+ }
+
+ /**
+ * Get type.
+ *
+ * @return String
+ */
+ public String getType() {
+ return type;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplatesXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplatesXmlData.java
new file mode 100644
index 000000000..c2ae8c1d2
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/data/EmailTemplatesXmlData.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation.data;
+
+@SuppressWarnings({"PMD.DataClass"})
+public class EmailTemplatesXmlData {
+ private final String module;
+ private final String emailTemplateId;
+ private final String label;
+ private final String templateFileName;
+ private final String type;
+ private final String area;
+
+ /**
+ * Email templates data constructor.
+ *
+ * @param module String
+ * @param emailTemplateId String
+ * @param label String
+ * @param templateFileName String
+ * @param type String
+ * @param area String
+ */
+ public EmailTemplatesXmlData(
+ final String module,
+ final String emailTemplateId,
+ final String label,
+ final String templateFileName,
+ final String type,
+ final String area
+ ) {
+ this.module = module;
+ this.emailTemplateId = emailTemplateId;
+ this.label = label;
+ this.templateFileName = templateFileName;
+ this.type = type;
+ this.area = area;
+ }
+
+ /**
+ * Get template file name.
+ *
+ * @return String
+ */
+ public String getTemplateFileName() {
+ return templateFileName;
+ }
+
+ /**
+ * Get ID.
+ *
+ * @return String
+ */
+ public String getId() {
+ return emailTemplateId;
+ }
+
+ /**
+ * Get label.
+ *
+ * @return String
+ */
+ public String getLabel() {
+ return label;
+ }
+
+ /**
+ * Get module.
+ *
+ * @return String
+ */
+ public String getModule() {
+ return module;
+ }
+
+ /**
+ * Get type.
+ *
+ * @return String
+ */
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * Get area.
+ *
+ * @return String
+ */
+ public String getArea() {
+ return area;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.form
new file mode 100644
index 000000000..1c074587b
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.form
@@ -0,0 +1,166 @@
+
+
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.java
new file mode 100644
index 000000000..748f283b8
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEmailTemplateDialog.java
@@ -0,0 +1,257 @@
+/*
+ * 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.NewEmailTemplateAction;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplateHtmlData;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplatesXmlData;
+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.dialog.NewEmailTemplateDialogValidator;
+import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
+import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
+import com.magento.idea.magento2plugin.actions.generation.generator.ModuleEmailTemplateHtmlGenerator;
+import com.magento.idea.magento2plugin.actions.generation.generator.ModuleEmailTemplatesXmlGenerator;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplateHtml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+import com.magento.idea.magento2plugin.ui.FilteredComboBox;
+import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import javax.swing.JButton;
+import javax.swing.JComponent;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.KeyStroke;
+
+public class NewEmailTemplateDialog extends AbstractDialog {
+ private final String moduleName;
+ private final Project project;
+ private final NewEmailTemplateDialogValidator validator;
+ private static final String EMAIL_TEMPLATE_ID = "id";
+ private static final String LABEL = "label";
+ private static final String FILENAME = "file name";
+ private JPanel contentPane;
+ private JButton buttonOK;
+ private JButton buttonCancel;
+ @FieldValidation(rule = RuleRegistry.NOT_EMPTY,
+ message = {NotEmptyRule.MESSAGE, EMAIL_TEMPLATE_ID})
+ @FieldValidation(rule = RuleRegistry.IDENTIFIER,
+ message = {IdentifierRule.MESSAGE, EMAIL_TEMPLATE_ID})
+ private JTextField identifier;
+ @FieldValidation(rule = RuleRegistry.NOT_EMPTY,
+ message = {NotEmptyRule.MESSAGE, LABEL})
+ private JTextField label;
+ @FieldValidation(rule = RuleRegistry.NOT_EMPTY,
+ message = {NotEmptyRule.MESSAGE, FILENAME})
+ @FieldValidation(rule = RuleRegistry.IDENTIFIER,
+ message = {IdentifierRule.MESSAGE, FILENAME})
+ private JTextField fileName;
+ private FilteredComboBox area;
+ private FilteredComboBox templateType;
+ private JTextField subject;
+
+ /**
+ * New email template dialog.
+ *
+ * @param project Project
+ * @param directory Directory
+ */
+ public NewEmailTemplateDialog(final Project project, final PsiDirectory directory) {
+ super();
+ setContentPane(contentPane);
+ setModal(true);
+ getRootPane().setDefaultButton(buttonOK);
+ this.project = project;
+ this.validator = new NewEmailTemplateDialogValidator(project);
+ this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
+
+ buttonOK.addActionListener(e -> onOK());
+ buttonCancel.addActionListener(e -> onCancel());
+
+ // call onCancel() when cross is clicked
+ setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
+ addWindowListener(new WindowAdapter() {
+ public void windowClosing(final WindowEvent windowEvent) {
+ onCancel();
+ }
+ });
+
+ // call onCancel() on ESCAPE
+ contentPane.registerKeyboardAction(
+ new ActionListener() {
+ public void actionPerformed(final ActionEvent actionEvent) {
+ onCancel();
+ }
+ },
+ KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
+ JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+ );
+ }
+
+ /**
+ * Get email template data.
+ *
+ * @return EmailTemplatesData
+ */
+ public EmailTemplatesXmlData getEmailTemplateData() {
+ return new EmailTemplatesXmlData(
+ getModuleName(),
+ getIdentifier(),
+ getLabel(),
+ getFileName(),
+ getTemplateType(),
+ getArea()
+ );
+ }
+
+ /**
+ * Get email template HTML data.
+ *
+ * @return EmailTemplateHtmlData
+ */
+ public EmailTemplateHtmlData getEmailTemplateHtmlData() {
+ return new EmailTemplateHtmlData(
+ getModuleName(),
+ getFileName(),
+ getArea(),
+ getSubject(),
+ getTemplateType()
+ );
+ }
+
+ /**
+ * Get identifier.
+ *
+ * @return String
+ */
+ public String getIdentifier() {
+ return identifier.getText().trim();
+ }
+
+ /**
+ * Get label.
+ *
+ * @return String
+ */
+ public String getLabel() {
+ return label.getText().trim();
+ }
+
+ /**
+ * Get subject.
+ *
+ * @return String
+ */
+ public String getSubject() {
+ return subject.getText().trim();
+ }
+
+ /**
+ * Get file name.
+ *
+ * @return String
+ */
+ public String getFileName() {
+ return fileName.getText().trim();
+ }
+
+ /**
+ * Get area.
+ *
+ * @return String
+ */
+ public String getArea() {
+ return area.getSelectedItem().toString();
+ }
+
+ /**
+ * Get template type.
+ *
+ * @return String
+ */
+ public String getTemplateType() {
+ return templateType.getSelectedItem().toString();
+ }
+
+ /**
+ * Open new controller dialog.
+ *
+ * @param project Project
+ * @param directory PsiDirectory
+ */
+ public static void open(final Project project, final PsiDirectory directory) {
+ final NewEmailTemplateDialog dialog = new NewEmailTemplateDialog(project, directory);
+ dialog.pack();
+ dialog.centerDialog(dialog);
+ dialog.setVisible(true);
+ }
+
+ private String getModuleName() {
+ return this.moduleName;
+ }
+
+ private void onOK() {
+ final boolean emailTemplateCanBeDeclared = !this.validator.validate(this);
+ if (!validateFormFields() || emailTemplateCanBeDeclared) {
+ return;
+ }
+
+ generateFile();
+ this.setVisible(false);
+ }
+
+ private void generateFile() {
+ final ModuleEmailTemplateHtmlGenerator moduleEmailTemplateHtmlGenerator;
+ moduleEmailTemplateHtmlGenerator = new ModuleEmailTemplateHtmlGenerator(
+ this.getEmailTemplateHtmlData(),
+ project
+ );
+ moduleEmailTemplateHtmlGenerator.generate(NewEmailTemplateAction.ACTION_NAME);
+ final ModuleEmailTemplatesXmlGenerator xmlGenerator = new ModuleEmailTemplatesXmlGenerator(
+ this.getEmailTemplateData(),
+ project
+ );
+ xmlGenerator.generate(NewEmailTemplateAction.ACTION_NAME, true);
+ }
+
+ protected void onCancel() {
+ // add your code here if necessary
+ dispose();
+ }
+
+ @SuppressWarnings({"PMD.UnusedPrivateMethod"})
+ private void createUIComponents() {
+ this.area = new FilteredComboBox(getAreaList());
+ this.templateType = new FilteredComboBox(getTypeList());
+ }
+
+ private List getAreaList() {
+ return new ArrayList<>(
+ Arrays.asList(
+ Areas.frontend.toString(),
+ Areas.adminhtml.toString()
+ )
+ );
+ }
+
+ private List getTypeList() {
+ return new ArrayList<>(
+ Arrays.asList(
+ EmailTemplateHtml.HTML_TYPE,
+ EmailTemplateHtml.TEXT_TYPE
+ )
+ );
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/dialog/NewEmailTemplateDialogValidator.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/dialog/NewEmailTemplateDialogValidator.java
new file mode 100644
index 000000000..3aa893aff
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/dialog/NewEmailTemplateDialogValidator.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation.dialog.validator.dialog;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.util.PsiTreeUtil;
+import com.intellij.psi.xml.XmlAttributeValue;
+import com.intellij.psi.xml.XmlFile;
+import com.intellij.psi.xml.XmlTag;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplateHtmlData;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplatesXmlData;
+import com.magento.idea.magento2plugin.actions.generation.dialog.NewEmailTemplateDialog;
+import com.magento.idea.magento2plugin.bundles.CommonBundle;
+import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplatesXml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+import com.magento.idea.magento2plugin.magento.packages.Package;
+import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
+import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
+import java.util.Collection;
+import javax.swing.JOptionPane;
+
+public class NewEmailTemplateDialogValidator {
+ private final ValidatorBundle validatorBundle;
+ private final CommonBundle commonBundle;
+ private final Project project;
+
+ /**
+ * Constructor.
+ */
+ public NewEmailTemplateDialogValidator(final Project project) {
+ this.validatorBundle = new ValidatorBundle();
+ this.commonBundle = new CommonBundle();
+ this.project = project;
+ }
+
+ /**
+ * Validate dialog.
+ *
+ * @param dialog NewEmailTemplateDialog
+ * @return boolean
+ */
+ public boolean validate(final NewEmailTemplateDialog dialog) {
+ final EmailTemplatesXmlData emailTemplatesXmlData = dialog.getEmailTemplateData();
+ final String errorTitle = commonBundle.message("common.error");
+ final EmailTemplateHtmlData emailTemplateHtmlData = dialog.getEmailTemplateHtmlData();
+
+ if (isTemplateFileAlreadyExists(emailTemplateHtmlData)) {
+ final String templateFileName = emailTemplatesXmlData.getTemplateFileName();
+ final String errorMessage = this.validatorBundle.message(
+ "validator.alreadyDeclared",
+ String.format("%s Email Template File", templateFileName),
+ emailTemplatesXmlData.getModule()
+ );
+ JOptionPane.showMessageDialog(
+ null,
+ errorMessage,
+ errorTitle,
+ JOptionPane.ERROR_MESSAGE
+ );
+
+ return false;
+ }
+
+ if (isTemplateTagAlreadyExists(emailTemplatesXmlData)) {
+ final String errorMessage = this.validatorBundle.message(
+ "validator.alreadyDeclared",
+ String.format("%s Email Template", emailTemplatesXmlData.getId()),
+ emailTemplatesXmlData.getModule()
+ );
+ JOptionPane.showMessageDialog(
+ null,
+ errorMessage,
+ errorTitle,
+ JOptionPane.ERROR_MESSAGE
+ );
+
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Is template tag already exists.
+ *
+ * @param emailTemplatesXmlData email templates XML data
+ *
+ * @return bool
+ */
+ private boolean isTemplateTagAlreadyExists(final EmailTemplatesXmlData emailTemplatesXmlData) {
+ final EmailTemplatesXml emailTemplatesXml = new EmailTemplatesXml();
+ final XmlFile emailTemplatesFile = (XmlFile) FileBasedIndexUtil.findModuleConfigFile(
+ emailTemplatesXml.getFileName(),
+ Areas.base,
+ emailTemplatesXmlData.getModule(),
+ project
+ );
+
+ if (emailTemplatesFile == null) {
+ return false;
+ }
+
+ final Collection attributes = XmlPsiTreeUtil.findAttributeValueElements(
+ emailTemplatesFile,
+ EmailTemplatesXml.TEMPLATE_TAG,
+ EmailTemplatesXml.TEMPLATE_ID_ATTRIBUTE,
+ emailTemplatesXmlData.getId()
+ );
+
+ for (final XmlAttributeValue emailTemplateAttribute: attributes) {
+ final XmlTag templateXmlTag = PsiTreeUtil.getParentOfType(
+ emailTemplateAttribute,
+ XmlTag.class
+ );
+ final String existingTemplateArea = templateXmlTag.getAttribute("area").getValue();
+
+ if (existingTemplateArea.equals(emailTemplatesXmlData.getArea())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Is template file already exists.
+ *
+ * @param emailTemplateData EmailTemplateHtmlData
+ * @return boolean
+ */
+ private boolean isTemplateFileAlreadyExists(
+ final EmailTemplateHtmlData emailTemplateData
+ ) {
+ final PsiFile templateFile = FileBasedIndexUtil.findModuleViewFile(
+ emailTemplateData.getFileName(),
+ getArea(emailTemplateData.getArea()),
+ emailTemplateData.getModule(),
+ project,
+ Package.moduleViewEmailDir
+ );
+
+ return templateFile != null;
+ }
+
+ private Areas getArea(final String area) {
+ return Areas.getAreaByString(area);
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGenerator.java
new file mode 100644
index 000000000..08de0666c
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGenerator.java
@@ -0,0 +1,107 @@
+/*
+ * 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.PsiDirectory;
+import com.intellij.psi.PsiFile;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplateHtmlData;
+import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
+import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
+import com.magento.idea.magento2plugin.indexes.ModuleIndex;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplateHtml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+import com.magento.idea.magento2plugin.magento.packages.Package;
+import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
+import java.util.ArrayList;
+import java.util.Properties;
+
+public class ModuleEmailTemplateHtmlGenerator extends FileGenerator {
+ private final EmailTemplateHtmlData emailTemplateData;
+ private final FileFromTemplateGenerator fileFromTemplateGenerator;
+ private final ModuleIndex moduleIndex;
+ private final DirectoryGenerator directoryGenerator;
+ private final Project project;
+
+ /**
+ * Constructor.
+ *
+ * @param emailTemplateData EmailTemplateData
+ * @param project Project
+ */
+ public ModuleEmailTemplateHtmlGenerator(
+ final EmailTemplateHtmlData emailTemplateData,
+ final Project project
+ ) {
+ super(project);
+
+ this.emailTemplateData = emailTemplateData;
+ this.project = project;
+ this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
+ this.directoryGenerator = DirectoryGenerator.getInstance();
+ this.moduleIndex = ModuleIndex.getInstance(project);
+ }
+
+ /**
+ * Generate email template HTML file.
+ *
+ * @param actionName Action name
+ * @return PsiFile
+ */
+ public PsiFile generate(final String actionName) {
+ final PsiFile templateFile = FileBasedIndexUtil.findModuleViewFile(
+ this.emailTemplateData.getFileName(),
+ getArea(this.emailTemplateData.getArea()),
+ this.emailTemplateData.getModule(),
+ this.project,
+ Package.moduleViewEmailDir
+ );
+
+ if (templateFile != null) {
+ return null;
+ }
+
+ PsiDirectory parentDirectory = this.moduleIndex.getModuleDirectoryByModuleName(
+ this.emailTemplateData.getModule()
+ );
+ final ArrayList fileDirectories = new ArrayList<>();
+
+ fileDirectories.add(Package.moduleViewDir);
+ fileDirectories.add(this.emailTemplateData.getArea());
+ fileDirectories.add(Package.moduleViewEmailDir);
+
+ for (final String fileDirectory: fileDirectories) {
+ parentDirectory = this.directoryGenerator.findOrCreateSubdirectory(
+ parentDirectory,
+ fileDirectory
+ );
+ }
+ final EmailTemplateHtml emailTemplateHtml = EmailTemplateHtml.getInstance(
+ this.emailTemplateData.getFileName()
+ );
+
+ return fileFromTemplateGenerator.generate(
+ emailTemplateHtml,
+ getAttributes(),
+ parentDirectory,
+ actionName
+ );
+ }
+
+ @Override
+ protected void fillAttributes(final Properties attributes) {
+ attributes.setProperty("SUBJECT", emailTemplateData.getSubject());
+ attributes.setProperty("TYPE", emailTemplateData.getType());
+
+ if (emailTemplateData.getType().equals(EmailTemplateHtml.HTML_TYPE)) {
+ attributes.setProperty("HTML_TYPE", "true");
+ }
+ }
+
+ private Areas getArea(final String area) {
+ return Areas.getAreaByString(area);
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGenerator.java
new file mode 100644
index 000000000..5fce6a964
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGenerator.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation.generator;
+
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiDocumentManager;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.codeStyle.CodeStyleManager;
+import com.intellij.psi.xml.XmlFile;
+import com.magento.idea.magento2plugin.actions.generation.data.EmailTemplatesXmlData;
+import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateEmailTemplatesXml;
+import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplateUtil;
+import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
+import com.magento.idea.magento2plugin.bundles.CommonBundle;
+import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplatesXml;
+import java.io.IOException;
+import java.util.Properties;
+import javax.swing.JOptionPane;
+
+@SuppressWarnings({"PMD.AvoidPrintStackTrace"})
+public class ModuleEmailTemplatesXmlGenerator extends FileGenerator {
+ private final EmailTemplatesXmlData emailTemplatesData;
+ private final Project project;
+ private final FindOrCreateEmailTemplatesXml findOrCreateEmailTemplatesXml;
+ private final GetCodeTemplateUtil getCodeTemplateUtil;
+ private final XmlFilePositionUtil positionUtil;
+ private final PsiDocumentManager psiDocumentManager;
+ private final CodeStyleManager codeStyleManager;
+ private final ValidatorBundle validatorBundle;
+ private final CommonBundle commonBundle;
+
+ /**
+ * Module email templates XML generator constructor.
+ *
+ * @param emailTemplatesData EmailTemplatesData
+ * @param project Project
+ */
+ public ModuleEmailTemplatesXmlGenerator(
+ final EmailTemplatesXmlData emailTemplatesData,
+ final Project project
+ ) {
+ super(project);
+ this.project = project;
+ this.emailTemplatesData = emailTemplatesData;
+ this.getCodeTemplateUtil = new GetCodeTemplateUtil(project);
+ this.findOrCreateEmailTemplatesXml = new FindOrCreateEmailTemplatesXml(project);
+ this.positionUtil = XmlFilePositionUtil.getInstance();
+ this.psiDocumentManager = PsiDocumentManager.getInstance(project);
+ this.codeStyleManager = CodeStyleManager.getInstance(project);
+ this.validatorBundle = new ValidatorBundle();
+ this.commonBundle = new CommonBundle();
+ }
+
+
+ /**
+ * Generate email template XML file.
+ *
+ * @param actionName Action name
+ * @return PsiFile
+ */
+ public PsiFile generate(final String actionName) {
+ final String moduleName = this.emailTemplatesData.getModule();
+ final XmlFile emailTemplateXmlFile = (XmlFile) this.findOrCreateEmailTemplatesXml.execute(
+ actionName,
+ moduleName
+ );
+
+ WriteCommandAction.runWriteCommandAction(project, () -> {
+ final StringBuffer textBuf = new StringBuffer();
+
+ try {
+ final String emailTemplateRegistrationTemplate = this.getCodeTemplateUtil.execute(
+ EmailTemplatesXml.EMAIL_TEMPLATE_REGISTRATION,
+ getAttributes()
+ );
+
+ textBuf.append(emailTemplateRegistrationTemplate);
+ } catch (final IOException exception) {
+ exception.printStackTrace();
+ return;
+ }
+
+ final int insertPos = this.positionUtil.getRootInsertPosition(emailTemplateXmlFile);
+
+ if (textBuf.length() > 0 && insertPos >= 0) {
+ final Document document = this.psiDocumentManager.getDocument(emailTemplateXmlFile);
+ final String filePath = emailTemplateXmlFile.getVirtualFile().getPath();
+
+ if (document == null) {
+ // practically this should not be possible as we tell to edit XML file
+ final String errorMessage = this.validatorBundle.message(
+ "validator.file.noDocumentAssociations",
+ filePath
+ );
+ JOptionPane.showMessageDialog(
+ null,
+ errorMessage,
+ commonBundle.message("common.error"),
+ JOptionPane.ERROR_MESSAGE
+ );
+
+ return;
+ }
+
+ if (!document.isWritable()) {
+ final String errorMessage = this.validatorBundle.message(
+ "validator.file.isNotWritable",
+ filePath
+ );
+ JOptionPane.showMessageDialog(
+ null,
+ errorMessage,
+ commonBundle.message("common.error"),
+ JOptionPane.ERROR_MESSAGE
+ );
+
+ return;
+ }
+
+ document.insertString(insertPos, textBuf);
+ final int endPos = insertPos + textBuf.length() + 1;
+
+ this.codeStyleManager.reformatText(emailTemplateXmlFile, insertPos, endPos);
+ this.psiDocumentManager.commitDocument(document);
+ }
+ });
+
+ return emailTemplateXmlFile;
+ }
+
+ /**
+ * Fill attributes.
+ *
+ * @param attributes attributes
+ */
+ protected void fillAttributes(final Properties attributes) {
+ attributes.setProperty("ID", emailTemplatesData.getId());
+ attributes.setProperty("LABEL", emailTemplatesData.getLabel());
+ attributes.setProperty("FILE_NAME", emailTemplatesData.getTemplateFileName());
+ attributes.setProperty("TYPE", emailTemplatesData.getType());
+ attributes.setProperty("MODULE", emailTemplatesData.getModule());
+ attributes.setProperty("AREA", emailTemplatesData.getArea());
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateEmailTemplatesXml.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateEmailTemplatesXml.java
new file mode 100644
index 000000000..d924c4a8c
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateEmailTemplatesXml.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.actions.generation.generator.util;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiDirectory;
+import com.intellij.psi.PsiFile;
+import com.magento.idea.magento2plugin.indexes.ModuleIndex;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplatesXml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+import com.magento.idea.magento2plugin.magento.packages.Package;
+import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
+import java.util.ArrayList;
+import java.util.Properties;
+
+@SuppressWarnings({"PMD.OnlyOneReturn"})
+public class FindOrCreateEmailTemplatesXml {
+ private final Project project;
+ private final DirectoryGenerator directoryGenerator;
+ private final FileFromTemplateGenerator fileFromTemplateGenerator;
+ private final ModuleIndex moduleIndex;
+
+ /**
+ * Find or create email templates XML controller.
+ *
+ * @param project Project
+ */
+ public FindOrCreateEmailTemplatesXml(final Project project) {
+ this.project = project;
+ this.directoryGenerator = DirectoryGenerator.getInstance();
+ this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
+ this.moduleIndex = ModuleIndex.getInstance(project);
+ }
+
+ /**
+ * Find or create email_templates.xml file in the module.
+ *
+ * @param actionName action name
+ * @param moduleName module name
+ *
+ * @return PsiFile
+ */
+ public PsiFile execute(final String actionName, final String moduleName) {
+ PsiDirectory parentDirectory = this.moduleIndex.getModuleDirectoryByModuleName(moduleName);
+ final ArrayList fileDirectories = new ArrayList<>();
+
+ fileDirectories.add(Package.moduleBaseAreaDir);
+
+ for (final String fileDirectory: fileDirectories) {
+ parentDirectory = this.directoryGenerator.findOrCreateSubdirectory(
+ parentDirectory,
+ fileDirectory
+ );
+ }
+
+ final EmailTemplatesXml emailTemplatesXml = new EmailTemplatesXml();
+ PsiFile emailTemplatesFile = FileBasedIndexUtil.findModuleConfigFile(
+ emailTemplatesXml.getFileName(),
+ Areas.base,
+ moduleName,
+ project
+ );
+
+ // email_templates.xml is already declared
+ if (emailTemplatesFile != null) {
+ return emailTemplatesFile;
+ }
+
+ // create a new empty email_templates.xml file
+ emailTemplatesFile = fileFromTemplateGenerator.generate(
+ emailTemplatesXml,
+ new Properties(),
+ parentDirectory,
+ actionName
+ );
+
+ return emailTemplatesFile;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/magento/files/EmailTemplateHtml.java b/src/com/magento/idea/magento2plugin/magento/files/EmailTemplateHtml.java
new file mode 100644
index 000000000..deb31f0b6
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/magento/files/EmailTemplateHtml.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.magento.files;
+
+import com.intellij.lang.Language;
+import com.intellij.lang.html.HTMLLanguage;
+
+public class EmailTemplateHtml implements ModuleFileInterface {
+ public static final String TEMPLATE = "Magento Module Email Template Html";
+ public static final String HTML_TYPE = "html";
+ public static final String TEXT_TYPE = "text";
+ public static final String HTML_FILE_EXTENSION = "html";
+ private static EmailTemplateHtml instance;
+ private String fileName;
+
+ /**
+ * Returns a new instance of the class.
+ *
+ * @return EmailTemplatesXml
+ */
+ public static EmailTemplateHtml getInstance(final String fileName) {
+ if (null == instance) { //NOPMD
+ instance = new EmailTemplateHtml();
+ }
+
+ instance.setFileName(fileName);
+
+ return instance;
+ }
+
+ @Override
+ public String getFileName() {
+ return String.format("%s.%s", this.fileName, HTML_FILE_EXTENSION);
+ }
+
+ @Override
+ public String getTemplate() {
+ return TEMPLATE;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return HTMLLanguage.INSTANCE;
+ }
+
+ private void setFileName(final String fileName) {
+ this.fileName = fileName;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/magento/files/EmailTemplatesXml.java b/src/com/magento/idea/magento2plugin/magento/files/EmailTemplatesXml.java
new file mode 100644
index 000000000..03844f4cd
--- /dev/null
+++ b/src/com/magento/idea/magento2plugin/magento/files/EmailTemplatesXml.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+package com.magento.idea.magento2plugin.magento.files;
+
+import com.intellij.lang.Language;
+import com.intellij.lang.xml.XMLLanguage;
+
+public class EmailTemplatesXml implements ModuleFileInterface {
+ public static final String TEMPLATE = "Magento Module Email Templates Xml";
+ public static final String FILE_NAME = "email_templates.xml";
+
+ // code templates
+ public static final String EMAIL_TEMPLATE_REGISTRATION = "Magento Module Email Template Xml";
+
+ // XML definitions
+ public static final String TEMPLATE_TAG = "template";
+ public static final String TEMPLATE_ID_ATTRIBUTE = "id";
+
+ private static EmailTemplatesXml instance;
+
+ /**
+ * Returns a new instance of the class.
+ *
+ * @return EmailTemplatesXml
+ */
+ public static EmailTemplatesXml getInstance() {
+ if (null == instance) { //NOPMD
+ instance = new EmailTemplatesXml();
+ }
+
+ return instance;
+ }
+
+ @Override
+ public String getFileName() {
+ return FILE_NAME;
+ }
+
+ @Override
+ public String getTemplate() {
+ return TEMPLATE;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return XMLLanguage.INSTANCE;
+ }
+}
diff --git a/src/com/magento/idea/magento2plugin/magento/packages/Package.java b/src/com/magento/idea/magento2plugin/magento/packages/Package.java
index 205a456d3..dcd91eeec 100644
--- a/src/com/magento/idea/magento2plugin/magento/packages/Package.java
+++ b/src/com/magento/idea/magento2plugin/magento/packages/Package.java
@@ -12,6 +12,7 @@ public class Package { //NOPMD
public static String frameworkRootGit = "lib/internal/Magento/Framework";
public static String vendor = "vendor";
public static String moduleBaseAreaDir = "etc";
+ public static String moduleViewEmailDir = "email";
public static String moduleViewDir = "view";
public static String moduleViewUiComponentDir = "ui_component";
public static String vendorModuleNameSeparator = "_";
diff --git a/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateHtml/custom_email_template_html.html b/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateHtml/custom_email_template_html.html
new file mode 100644
index 000000000..bee3e775e
--- /dev/null
+++ b/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateHtml/custom_email_template_html.html
@@ -0,0 +1,6 @@
+
+
+
+{{template config_path="design/email/header_template"}}
+
+{{template config_path="design/email/footer_template"}}
\ No newline at end of file
diff --git a/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateText/custom_email_template_text.html b/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateText/custom_email_template_text.html
new file mode 100644
index 000000000..7dbf3a87c
--- /dev/null
+++ b/testData/actions/generation/generator/ModuleEmailTemplateHtmlGenerator/generateEmailTemplateText/custom_email_template_text.html
@@ -0,0 +1,5 @@
+
+
+
+
+
diff --git a/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/addTwoEmailTemplatesToEmailTemplatesXmlFile/email_templates.xml b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/addTwoEmailTemplatesToEmailTemplatesXmlFile/email_templates.xml
new file mode 100644
index 000000000..4bcb42774
--- /dev/null
+++ b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/addTwoEmailTemplatesToEmailTemplatesXmlFile/email_templates.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
diff --git a/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateHtmlXml/email_templates.xml b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateHtmlXml/email_templates.xml
new file mode 100644
index 000000000..690a51a01
--- /dev/null
+++ b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateHtmlXml/email_templates.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
diff --git a/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateTextXml/email_templates.xml b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateTextXml/email_templates.xml
new file mode 100644
index 000000000..9682ba2f0
--- /dev/null
+++ b/testData/actions/generation/generator/ModuleEmailTemplatesXmlGenerator/generateEmailTemplateTextXml/email_templates.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGeneratorTest.java
new file mode 100644
index 000000000..5e5bfa193
--- /dev/null
+++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplateHtmlGeneratorTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.EmailTemplateHtmlData;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplateHtml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+
+public class ModuleEmailTemplateHtmlGeneratorTest extends BaseGeneratorTestCase {
+ private static final String EMAIL_TEMPLATE_DIR = "src/app/code/Foo/Bar/view/%s/email";
+ private static final String MODULE_NAME = "Foo_Bar";
+ private static final String EMAIL_TEMPLATE_FILE_NAME_HTML = "custom_email_template_html";
+ private static final String EMAIL_TEMPLATE_FILE_NAME_TEXT = "custom_email_template_text";
+ private static final String EMAIL_TEMPLATE_SUBJECT_HTML = "HTML Email Template Subject";
+ private static final String EMAIL_TEMPLATE_SUBJECT_TEXT = "TEXT Email Template Subject";
+
+ /**
+ * Test generating email template with HTML type.
+ */
+ public void testGenerateEmailTemplateHtml() {
+ final String filePath = this.getFixturePath(
+ String.format(
+ "%s.%s",
+ EMAIL_TEMPLATE_FILE_NAME_HTML,
+ EmailTemplateHtml.HTML_FILE_EXTENSION
+ )
+ );
+ final String area = Areas.adminhtml.toString();
+ final PsiFile expectedFile = myFixture.configureByFile(filePath);
+ final PsiFile emailTemplateXmlFile = generateEmailTemplateHtml(
+ EMAIL_TEMPLATE_FILE_NAME_HTML,
+ EMAIL_TEMPLATE_SUBJECT_HTML,
+ EmailTemplateHtml.HTML_TYPE,
+ area
+ );
+
+ assertGeneratedFileIsCorrect(
+ expectedFile,
+ getExpectedDirectory(area),
+ emailTemplateXmlFile
+ );
+ }
+
+ /**
+ * Test generating email template with TEXT type.
+ */
+ public void testGenerateEmailTemplateText() {
+ final String filePath = this.getFixturePath(
+ String.format(
+ "%s.%s",
+ EMAIL_TEMPLATE_FILE_NAME_TEXT,
+ EmailTemplateHtml.HTML_FILE_EXTENSION
+ )
+ );
+ final String area = Areas.frontend.toString();
+ final PsiFile expectedFile = myFixture.configureByFile(filePath);
+ final PsiFile emailTemplateXmlFile = generateEmailTemplateHtml(
+ EMAIL_TEMPLATE_FILE_NAME_TEXT,
+ EMAIL_TEMPLATE_SUBJECT_TEXT,
+ EmailTemplateHtml.TEXT_TYPE,
+ area
+ );
+
+ assertGeneratedFileIsCorrect(
+ expectedFile,
+ getExpectedDirectory(area),
+ emailTemplateXmlFile
+ );
+ }
+
+ /**
+ * Generate email template Html file.
+ *
+ * @param filename Email Template Filename
+ * @param subject Email Template Filename
+ * @param type Email Type
+ * @param area Area
+ * @return PsiFile
+ */
+ private PsiFile generateEmailTemplateHtml(
+ final String filename,
+ final String subject,
+ final String type,
+ final String area
+ ) {
+ final Project project = myFixture.getProject();
+ final EmailTemplateHtmlData emailTemplatesData = new EmailTemplateHtmlData(
+ MODULE_NAME,
+ filename,
+ area,
+ subject,
+ type
+ );
+ final ModuleEmailTemplateHtmlGenerator generator = new ModuleEmailTemplateHtmlGenerator(
+ emailTemplatesData,
+ project
+ );
+
+ return generator.generate("test");
+ }
+
+ private String getExpectedDirectory(final String area) {
+ return String.format(EMAIL_TEMPLATE_DIR, area);
+ }
+}
diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGeneratorTest.java
new file mode 100644
index 000000000..17d82a941
--- /dev/null
+++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleEmailTemplatesXmlGeneratorTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.EmailTemplatesXmlData;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplateHtml;
+import com.magento.idea.magento2plugin.magento.files.EmailTemplatesXml;
+import com.magento.idea.magento2plugin.magento.packages.Areas;
+
+public class ModuleEmailTemplatesXmlGeneratorTest extends BaseGeneratorTestCase {
+ private static final String EXPECTED_DIRECTORY = "src/app/code/Foo/Bar/etc";
+ private static final String MODULE_NAME = "Foo_Bar";
+ private static final String EMAIL_TEMPLATE_ID_HTML = "custom_email_template_html";
+ private static final String EMAIL_TEMPLATE_LABEL_HTML = "Custom Email Template HTML";
+ private static final String EMAIL_TEMPLATE_FILE_NAME_HTML = "custom_email_template_html";
+ private static final String EMAIL_TEMPLATE_ID_TEXT = "custom_email_template_text";
+ private static final String EMAIL_TEMPLATE_LABEL_TEXT = "Custom Email Template TEXT";
+ private static final String EMAIL_TEMPLATE_FILE_NAME_TEXT = "custom_email_template_text";
+
+ /**
+ * Test generating email template configuration with HTML type.
+ */
+ public void testGenerateEmailTemplateHtmlXml() {
+ final String filePath = this.getFixturePath(EmailTemplatesXml.FILE_NAME);
+ final PsiFile expectedFile = myFixture.configureByFile(filePath);
+ final PsiFile emailTemplateXmlFile = addEmailTemplateXml(
+ EMAIL_TEMPLATE_ID_HTML,
+ EMAIL_TEMPLATE_LABEL_HTML,
+ EMAIL_TEMPLATE_FILE_NAME_HTML,
+ EmailTemplateHtml.HTML_TYPE,
+ Areas.adminhtml.toString()
+ );
+
+ assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, emailTemplateXmlFile);
+ }
+
+ /**
+ * Test generating email template configuration with TEXT type.
+ */
+ public void testGenerateEmailTemplateTextXml() {
+ final String filePath = this.getFixturePath(EmailTemplatesXml.FILE_NAME);
+ final PsiFile expectedFile = myFixture.configureByFile(filePath);
+ final PsiFile emailTemplateXmlFile = addEmailTemplateXml(
+ EMAIL_TEMPLATE_ID_TEXT,
+ EMAIL_TEMPLATE_LABEL_TEXT,
+ EMAIL_TEMPLATE_FILE_NAME_TEXT,
+ EmailTemplateHtml.TEXT_TYPE,
+ Areas.frontend.toString()
+ );
+
+ assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, emailTemplateXmlFile);
+ }
+
+ /**
+ * Test adding two email templates to email_templates.xml.
+ */
+ public void testAddTwoEmailTemplatesToEmailTemplatesXmlFile() {
+ final String filePath = this.getFixturePath(EmailTemplatesXml.FILE_NAME);
+ final PsiFile expectedFile = myFixture.configureByFile(filePath);
+ addEmailTemplateXml(
+ EMAIL_TEMPLATE_ID_HTML,
+ EMAIL_TEMPLATE_LABEL_HTML,
+ EMAIL_TEMPLATE_FILE_NAME_HTML,
+ EmailTemplateHtml.HTML_TYPE,
+ Areas.adminhtml.toString()
+ );
+ final PsiFile emailTemplateXmlFile = addEmailTemplateXml(
+ EMAIL_TEMPLATE_ID_TEXT,
+ EMAIL_TEMPLATE_LABEL_TEXT,
+ EMAIL_TEMPLATE_FILE_NAME_TEXT,
+ EmailTemplateHtml.TEXT_TYPE,
+ Areas.frontend.toString()
+ );
+
+ assertGeneratedFileIsCorrect(expectedFile, EXPECTED_DIRECTORY, emailTemplateXmlFile);
+ }
+
+ /**
+ * Add email template XML.
+ *
+ * @param emailTemplateId Email Template ID
+ * @param label Email Template Label
+ * @param filename Email Template Filename
+ * @param type Email Type
+ * @param area Area
+ * @return PsiFile
+ */
+ private PsiFile addEmailTemplateXml(
+ final String emailTemplateId,
+ final String label,
+ final String filename,
+ final String type,
+ final String area
+ ) {
+ final Project project = myFixture.getProject();
+ final EmailTemplatesXmlData emailTemplatesData = new EmailTemplatesXmlData(
+ MODULE_NAME,
+ emailTemplateId,
+ label,
+ filename,
+ type,
+ area
+ );
+ final ModuleEmailTemplatesXmlGenerator generator = new ModuleEmailTemplatesXmlGenerator(
+ emailTemplatesData,
+ project
+ );
+
+ return generator.generate("test");
+ }
+}