8
8
namespace Magento \Config \Test \Unit \Block \System \Config \Form \Field ;
9
9
10
10
use Magento \Config \Block \System \Config \Form \Field \File ;
11
+ use Magento \Framework \Data \Form \Element \Factory ;
12
+ use Magento \Framework \Data \Form \Element \CollectionFactory ;
11
13
use Magento \Framework \DataObject ;
12
14
use Magento \Framework \Escaper ;
13
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
+ use PHPUnit \Framework \MockObject \MockObject ;
14
17
use PHPUnit \Framework \TestCase ;
15
18
16
19
/**
17
20
* Tests for \Magento\Framework\Data\Form\Field\File
18
21
*/
19
22
class FileTest extends TestCase
20
23
{
24
+ /**
25
+ * XSS value
26
+ */
27
+ private const XSS_FILE_NAME_TEST = '<img src=x onerror=alert(1)>.crt ' ;
28
+
29
+ /**
30
+ * Input name
31
+ */
32
+ private const INPUT_NAME_TEST = 'test_name ' ;
33
+
21
34
/**
22
35
* @var File
23
36
*/
24
37
protected $ file ;
25
38
39
+ /**
40
+ * @var Factory|MockObject
41
+ */
42
+ private $ factoryMock ;
43
+
44
+ /**
45
+ * @var CollectionFactory|MockObject
46
+ */
47
+ private $ factoryCollectionMock ;
48
+
49
+ /**
50
+ * @var Escaper|MockObject
51
+ */
52
+ private $ escaperMock ;
53
+
26
54
/**
27
55
* @var array
28
56
*/
29
- protected $ testData ;
57
+ protected array $ testData = [
58
+ 'before_element_html ' => 'test_before_element_html ' ,
59
+ 'html_id ' => 'test_id ' ,
60
+ 'name ' => 'test_name ' ,
61
+ 'value ' => 'test_value ' ,
62
+ 'title ' => 'test_title ' ,
63
+ 'disabled ' => true ,
64
+ 'after_element_js ' => 'test_after_element_js ' ,
65
+ 'after_element_html ' => 'test_after_element_html ' ,
66
+ 'html_id_prefix ' => 'test_id_prefix_ ' ,
67
+ 'html_id_suffix ' => '_test_id_suffix ' ,
68
+ ];
30
69
31
70
protected function setUp (): void
32
71
{
33
72
$ objectManager = new ObjectManager ($ this );
34
73
35
- $ this ->testData = [
36
- 'before_element_html ' => 'test_before_element_html ' ,
37
- 'html_id ' => 'test_id ' ,
38
- 'name ' => 'test_name ' ,
39
- 'value ' => 'test_value ' ,
40
- 'title ' => 'test_title ' ,
41
- 'disabled ' => true ,
42
- 'after_element_js ' => 'test_after_element_js ' ,
43
- 'after_element_html ' => 'test_after_element_html ' ,
44
- 'html_id_prefix ' => 'test_id_prefix_ ' ,
45
- 'html_id_suffix ' => '_test_id_suffix ' ,
46
- ];
47
-
74
+ $ this ->factoryMock = $ this ->getMockBuilder (Factory::class)
75
+ ->disableOriginalConstructor ()
76
+ ->getMock ();
77
+ $ this ->factoryCollectionMock = $ this ->getMockBuilder (CollectionFactory::class)
78
+ ->disableOriginalConstructor ()
79
+ ->getMock ();
80
+ $ this ->escaperMock = $ this ->getMockBuilder (Escaper::class)
81
+ ->disableOriginalConstructor ()
82
+ ->getMock ();
48
83
$ this ->file = $ objectManager ->getObject (
49
84
File::class,
50
85
[
51
- '_escaper ' => $ objectManager ->getObject (Escaper::class),
86
+ 'factoryElement ' => $ this ->factoryMock ,
87
+ 'factoryCollection ' => $ this ->factoryCollectionMock ,
88
+ '_escaper ' => $ this ->escaperMock ,
52
89
'data ' => $ this ->testData ,
53
-
54
90
]
55
91
);
56
92
@@ -60,13 +96,20 @@ protected function setUp(): void
60
96
$ this ->file ->setForm ($ formMock );
61
97
}
62
98
63
- public function testGetElementHtml ()
99
+ public function testGetElementHtml (): void
64
100
{
65
- $ html = $ this ->file ->getElementHtml ();
66
-
67
101
$ expectedHtmlId = $ this ->testData ['html_id_prefix ' ]
68
102
. $ this ->testData ['html_id ' ]
69
103
. $ this ->testData ['html_id_suffix ' ];
104
+ $ this ->escaperMock ->expects ($ this ->any ())->method ('escapeHtml ' )->willReturnMap (
105
+ [
106
+ [$ expectedHtmlId , null , $ expectedHtmlId ],
107
+ [self ::XSS_FILE_NAME_TEST , null , self ::XSS_FILE_NAME_TEST ],
108
+ [self ::INPUT_NAME_TEST , null , self ::INPUT_NAME_TEST ],
109
+ ]
110
+ );
111
+
112
+ $ html = $ this ->file ->getElementHtml ();
70
113
71
114
$ this ->assertStringContainsString ('<label class="addbefore" for=" ' . $ expectedHtmlId . '" ' , $ html );
72
115
$ this ->assertStringContainsString ($ this ->testData ['before_element_html ' ], $ html );
0 commit comments