15
15
use Magento \Framework \Data \Form \Element \Image ;
16
16
use Magento \Framework \DataObject ;
17
17
use Magento \Framework \Escaper ;
18
+ use Magento \Framework \Math \Random ;
19
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
18
20
use Magento \Framework \Url ;
19
- use Magento \Framework \UrlInterface ;
21
+ use Magento \Framework \View \ Helper \ SecureHtmlRenderer ;
20
22
use PHPUnit \Framework \MockObject \MockObject ;
21
23
use PHPUnit \Framework \TestCase ;
22
- use Magento \Framework \Math \Random ;
23
- use Magento \Framework \View \Helper \SecureHtmlRenderer ;
24
+ use Magento \Framework \UrlInterface ;
24
25
25
26
/**
26
27
* Test for the widget.
@@ -44,11 +45,16 @@ class ImageTest extends TestCase
44
45
*/
45
46
protected $ _image ;
46
47
48
+ /**
49
+ * @var array
50
+ */
51
+ protected $ testData ;
52
+
47
53
protected function setUp (): void
48
54
{
55
+ $ objectManager = new ObjectManager ($ this );
49
56
$ factoryMock = $ this ->createMock (Factory::class);
50
57
$ collectionFactoryMock = $ this ->createMock (CollectionFactory::class);
51
- $ escaperMock = $ this ->createMock (Escaper::class);
52
58
$ this ->urlBuilder = $ this ->createMock (Url::class);
53
59
$ randomMock = $ this ->createMock (Random::class);
54
60
$ randomMock ->method ('getRandomString ' )->willReturn ('some-rando-string ' );
@@ -67,18 +73,28 @@ function (string $tag, array $attrs, ?string $content): string {
67
73
return "< $ tag {$ attrs ->serialize ()}> $ content</ $ tag> " ;
68
74
}
69
75
);
70
- $ this ->_image = new Image (
71
- $ factoryMock ,
72
- $ collectionFactoryMock ,
73
- $ escaperMock ,
74
- $ this ->urlBuilder ,
75
- [],
76
- $ secureRendererMock ,
77
- $ randomMock
76
+ $ this ->_image = $ objectManager ->getObject (
77
+ Image::class,
78
+ [
79
+ 'factoryMock ' =>$ factoryMock ,
80
+ 'collectionFactoryMock ' =>$ collectionFactoryMock ,
81
+ 'urlBuilder ' => $ this ->urlBuilder ,
82
+ '_escaper ' => $ objectManager ->getObject (Escaper::class),
83
+ 'random ' => $ randomMock ,
84
+ 'secureRenderer ' => $ secureRendererMock ,
85
+ ]
78
86
);
87
+ $ this ->testData = [
88
+ 'html_id_prefix ' => 'test_id_prefix_ ' ,
89
+ 'html_id ' => 'test_id ' ,
90
+ 'html_id_suffix ' => '_test_id_suffix ' ,
91
+ 'path ' => 'catalog/product/placeholder ' ,
92
+ 'value ' => 'test_value ' ,
93
+ ];
94
+
79
95
$ formMock = new DataObject ();
80
- $ formMock ->getHtmlIdPrefix (' id_prefix ' );
81
- $ formMock ->getHtmlIdPrefix (' id_suffix ' );
96
+ $ formMock ->getHtmlIdPrefix ($ this -> testData [ ' html_id_prefix ' ] );
97
+ $ formMock ->getHtmlIdPrefix ($ this -> testData [ ' html_id_suffix ' ] );
82
98
$ this ->_image ->setForm ($ formMock );
83
99
}
84
100
@@ -117,21 +133,32 @@ public function testGetElementHtmlWithoutValue()
117
133
*/
118
134
public function testGetElementHtmlWithValue ()
119
135
{
120
- $ this ->_image ->setValue ('test_value ' );
121
- $ this ->urlBuilder ->expects ($ this ->once ())
122
- ->method ('getBaseUrl ' )
123
- ->with (['_type ' => UrlInterface::URL_TYPE_MEDIA ])
124
- ->willReturn ('http://localhost/media/ ' );
136
+ $ url = 'http://test.example.com/media/ ' ;
137
+
138
+ $ this ->_image ->setValue ($ this ->testData ['value ' ]);
139
+ $ this ->_image ->setHtmlId ($ this ->testData ['html_id ' ]);
140
+
141
+ $ this ->urlBuilder ->expects ($ this ->once ())->method ('getBaseUrl ' )
142
+ ->with (['_type ' => UrlInterface::URL_TYPE_MEDIA ])->willReturn ($ url );
143
+
144
+ $ expectedHtmlId = $ this ->testData ['html_id ' ];
145
+
125
146
$ html = $ this ->_image ->getElementHtml ();
147
+
126
148
$ this ->assertStringContainsString ('class="input-file" ' , $ html );
127
149
$ this ->assertStringContainsString ('<input ' , $ html );
128
150
$ this ->assertStringContainsString ('type="file" ' , $ html );
129
151
$ this ->assertStringContainsString ('value="test_value" ' , $ html );
152
+
130
153
$ this ->assertStringContainsString (
131
- '<a previewlinkid="linkIdsome-rando-string" href="http://localhost/media/test_value" ' ,
154
+ '<a previewlinkid="linkIdsome-rando-string" href=" '
155
+ . $ url
156
+ . $ this ->testData ['value ' ]
157
+ . '" ' ,
132
158
$ html
133
159
);
134
- $ this ->assertStringContainsString ("imagePreview('_image'); \nreturn false; " , $ html );
160
+
161
+ $ this ->assertStringContainsString ("imagePreview(' {$ expectedHtmlId }_image'); \nreturn false; " , $ html );
135
162
$ this ->assertStringContainsString ('<input type="checkbox" ' , $ html );
136
163
}
137
164
}
0 commit comments