Skip to content

Commit db49b23

Browse files
author
Vitaliy
authored
Merge pull request #127 from vasilii-b/test-coverage-php-class-completion-for-backend_model
[bugfix] [Tests] Test coverage for PHP class completion for backend model; bugfix for #126
2 parents bf76b18 + 482871d commit db49b23

File tree

19 files changed

+425
-27
lines changed

19 files changed

+425
-27
lines changed

src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,19 @@ public XmlCompletionContributor() {
118118
new PhpClassCompletionProvider()
119119
);
120120

121-
// <backend_model>completion</backend_model>
122-
extend(CompletionType.BASIC,
123-
psiElement(XmlTokenType.XML_DATA_CHARACTERS)
124-
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_BACKEND_MODEL)),
125-
new PhpClassCompletionProvider()
121+
// <backend_model>completion</backend_model> in system.xml
122+
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_DATA_CHARACTERS)
123+
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_BACKEND_MODEL)
124+
.withParent(XmlPatterns.xmlTag().withName(ModuleSystemXml.FIELD_ELEMENT_NAME))
125+
).inFile(xmlFile().withName(string().matches(ModuleSystemXml.FILE_NAME))),
126+
new PhpClassCompletionProvider()
126127
);
127128

128-
// <randomTag backend_model="completion">
129+
// <randomTag backend_model="completion"> in config.xml
129130
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
130-
.inside(XmlPatterns.xmlAttribute().withName(ModuleConfigXml.XML_ATTRIBUTE_BACKEND_MODEL)),
131-
new PhpClassCompletionProvider()
131+
.inside(XmlPatterns.xmlAttribute().withName(ModuleConfigXml.XML_ATTRIBUTE_BACKEND_MODEL))
132+
.inFile(xmlFile().withName(string().matches(ModuleConfigXml.FILE_NAME))),
133+
new PhpClassCompletionProvider()
132134
);
133135

134136
// <parameter source_model="completion">...</parameter> in widget.xml files.

src/com/magento/idea/magento2plugin/magento/files/ModuleConfigXml.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
package com.magento.idea.magento2plugin.magento.files;
66

77
public class ModuleConfigXml {
8+
public static final String FILE_NAME = "config.xml";
89
public static final String XML_ATTRIBUTE_BACKEND_MODEL = "backend_model";
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
9+
<default>
10+
<payment>
11+
<test>
12+
<test backend_model="Yesno<caret>" />
13+
</test>
14+
</payment>
15+
</default>
16+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
9+
<default>
10+
<payment>
11+
<test>
12+
<test backend_model="Yesno<caret>" />
13+
</test>
14+
</payment>
15+
</default>
16+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
9+
<default>
10+
<payment>
11+
<test>
12+
<test backend_model="NotExistentClass<caret>" />
13+
</test>
14+
</payment>
15+
</default>
16+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
9+
<default>
10+
<payment>
11+
<test>
12+
<test backend_model="Yesno<caret>" />
13+
</test>
14+
</payment>
15+
</default>
16+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check completion WON'T work for backend_model tag</label>
15+
<backend_model>Roles<caret></backend_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check completion for backend_model tag exactly in system.xml file</label>
15+
<backend_model>Roles<caret></backend_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check missing completion for backend_model tag</label>
15+
<backend_model>NotExistingSource<caret></backend_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check completion for backend_model tag</label>
15+
<backend_model>Roles<caret></backend_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
10+
<widget id="test">
11+
<label translate="true">Test Widget</label>
12+
<description translate="true">Test Widget</description>
13+
<parameters>
14+
<parameter name="test_yesno" xsi:type="select" source_model="Yesno<caret>">
15+
<label translate="true">Test Boolean field</label>
16+
</parameter>
17+
</parameters>
18+
</widget>
19+
</widgets>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
10+
<widget id="test">
11+
<label translate="true">Test Widget</label>
12+
<description translate="true">Test Widget</description>
13+
<parameters>
14+
<parameter name="test_yesno" xsi:type="select" source_model="Yesno<caret>">
15+
<label translate="true">Test Boolean field</label>
16+
</parameter>
17+
</parameters>
18+
</widget>
19+
</widgets>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check completion for source_model tag</label>
15+
<source_model>Yesno<caret></source_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
10+
<system>
11+
<section id="catalog">
12+
<group id="test">
13+
<field id="test">
14+
<label>Test suit to check completion for source_model tag</label>
15+
<source_model>Yesno<caret></source_model>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.completion.xml;
6+
7+
import com.intellij.openapi.vfs.VirtualFile;
8+
import com.magento.idea.magento2plugin.magento.files.ModuleConfigXml;
9+
import com.magento.idea.magento2plugin.magento.files.ModuleSystemXml;
10+
import java.io.File;
11+
12+
public class BackendModelXmlCompletionRegistrarTest extends CompletionLightJavaCodeInsightFixtureTestCase {
13+
14+
private static VirtualFile systemXmlVirtualFile;
15+
private static VirtualFile configXmlVirtualFile;
16+
private static final String testDataFolderPath;
17+
private static final String fixturesFolderPath;
18+
private static final String classesSourceFile;
19+
private static final String[] systemXmlBackendModelLookupStringCheck;
20+
private static final String[] configXmlBackendModelLookupStringCheck;
21+
22+
static {
23+
testDataFolderPath = "testData/completion/common/";
24+
fixturesFolderPath = "xml/BackendModelXmlCompletionRegistrar";
25+
classesSourceFile = "classes.php";
26+
systemXmlBackendModelLookupStringCheck = new String[]{
27+
"Magento\\Backend\\Model\\Source\\Roles"
28+
};
29+
configXmlBackendModelLookupStringCheck = new String[]{
30+
"Magento\\Config\\Model\\Config\\Source\\Yesno"
31+
};
32+
}
33+
34+
@Override
35+
protected void setUp() throws Exception {
36+
super.setUp();
37+
myFixture.copyFileToProject(classesSourceFile);
38+
}
39+
40+
@Override
41+
protected String getTestDataPath() {
42+
return testDataFolderPath;
43+
}
44+
45+
protected String getFixturePath(String folderName, String fileName) {
46+
return fixturesFolderPath + File.separator + folderName + File.separator + fileName;
47+
}
48+
49+
public void testSystemXmlBackendModelElementProvideCompletion() {
50+
String filePath = this.getFixturePath("systemXmlElementProvideCompletion", ModuleSystemXml.FILE_NAME);
51+
systemXmlVirtualFile = myFixture.copyFileToProject(filePath);
52+
53+
assertCompletionContains(filePath, systemXmlBackendModelLookupStringCheck);
54+
}
55+
56+
public void testSystemXmlBackendModelElementCompletionWontShow() {
57+
String filePath = this.getFixturePath(
58+
"systemXmlElementCompletionWontShow",
59+
ModuleSystemXml.FILE_NAME
60+
);
61+
systemXmlVirtualFile = myFixture.copyFileToProject(filePath);
62+
63+
assertCompletionNotShowing(filePath);
64+
}
65+
66+
public void testSystemXmlBackendModelAttributeMatchWithFile() {
67+
String filePath = this.getFixturePath(
68+
"systemXmlBackendModelAttributeMatchWithFile",
69+
ModuleSystemXml.FILE_NAME
70+
);
71+
72+
assertCompletionMatchWithFilePositiveCase(filePath, systemXmlBackendModelLookupStringCheck);
73+
}
74+
75+
public void testSystemXmlBackendModelAttributeDontMatchWithFile() {
76+
String filePath = this.getFixturePath(
77+
"systemXmlBackendModelAttributeDontMatchWithFile",
78+
"other-file-than-system.xml"
79+
);
80+
81+
assertCompletionMatchWithFileNegativeCase(
82+
filePath,
83+
systemXmlBackendModelLookupStringCheck
84+
);
85+
}
86+
87+
public void testConfigXmlBackendModelElementProvideCompletion() {
88+
String filePath = this.getFixturePath(
89+
"configXmlElementProvideCompletion",
90+
ModuleConfigXml.FILE_NAME
91+
);
92+
configXmlVirtualFile = myFixture.copyFileToProject(filePath);
93+
94+
assertCompletionContains(filePath, configXmlBackendModelLookupStringCheck);
95+
}
96+
97+
public void testConfigXmlBackendModelElementCompletionWontShow() {
98+
String filePath = this.getFixturePath(
99+
"configXmlElementCompletionWontShow",
100+
ModuleConfigXml.FILE_NAME
101+
);
102+
configXmlVirtualFile = myFixture.copyFileToProject(filePath);
103+
104+
assertCompletionNotShowing(filePath);
105+
}
106+
107+
public void testConfigXmlBackendModelAttributeMatchWithFile() {
108+
String filePath = this.getFixturePath(
109+
"configXmlBackendModelAttributeMatchWithFile",
110+
ModuleConfigXml.FILE_NAME
111+
);
112+
113+
assertCompletionMatchWithFilePositiveCase(filePath, configXmlBackendModelLookupStringCheck);
114+
}
115+
116+
public void testConfigXmlBackendModelAttributeDontMatchWithFile() {
117+
String filePath = this.getFixturePath(
118+
"configXmlBackendModelAttributeDontMatchWithFile",
119+
"other-file-than-config.xml"
120+
);
121+
122+
assertCompletionMatchWithFileNegativeCase(
123+
filePath,
124+
configXmlBackendModelLookupStringCheck
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)