1515use Magento \Framework \Data \Form \Element \Image ;
1616use Magento \Framework \DataObject ;
1717use Magento \Framework \Escaper ;
18+ use Magento \Framework \Math \Random ;
19+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1820use Magento \Framework \Url ;
19- use Magento \Framework \UrlInterface ;
21+ use Magento \Framework \View \ Helper \ SecureHtmlRenderer ;
2022use PHPUnit \Framework \MockObject \MockObject ;
2123use PHPUnit \Framework \TestCase ;
22- use Magento \Framework \Math \Random ;
23- use Magento \Framework \View \Helper \SecureHtmlRenderer ;
24+ use Magento \Framework \UrlInterface ;
2425
2526/**
2627 * Test for the widget.
@@ -44,11 +45,16 @@ class ImageTest extends TestCase
4445 */
4546 protected $ _image ;
4647
48+ /**
49+ * @var array
50+ */
51+ protected $ testData ;
52+
4753 protected function setUp (): void
4854 {
55+ $ objectManager = new ObjectManager ($ this );
4956 $ factoryMock = $ this ->createMock (Factory::class);
5057 $ collectionFactoryMock = $ this ->createMock (CollectionFactory::class);
51- $ escaperMock = $ this ->createMock (Escaper::class);
5258 $ this ->urlBuilder = $ this ->createMock (Url::class);
5359 $ randomMock = $ this ->createMock (Random::class);
5460 $ randomMock ->method ('getRandomString ' )->willReturn ('some-rando-string ' );
@@ -67,18 +73,28 @@ function (string $tag, array $attrs, ?string $content): string {
6773 return "< $ tag {$ attrs ->serialize ()}> $ content</ $ tag> " ;
6874 }
6975 );
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+ ]
7886 );
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+
7995 $ 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 ' ] );
8298 $ this ->_image ->setForm ($ formMock );
8399 }
84100
@@ -117,21 +133,32 @@ public function testGetElementHtmlWithoutValue()
117133 */
118134 public function testGetElementHtmlWithValue ()
119135 {
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+
125146 $ html = $ this ->_image ->getElementHtml ();
147+
126148 $ this ->assertStringContainsString ('class="input-file" ' , $ html );
127149 $ this ->assertStringContainsString ('<input ' , $ html );
128150 $ this ->assertStringContainsString ('type="file" ' , $ html );
129151 $ this ->assertStringContainsString ('value="test_value" ' , $ html );
152+
130153 $ 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+ . '" ' ,
132158 $ html
133159 );
134- $ this ->assertStringContainsString ("imagePreview('_image'); \nreturn false; " , $ html );
160+
161+ $ this ->assertStringContainsString ("imagePreview(' {$ expectedHtmlId }_image'); \nreturn false; " , $ html );
135162 $ this ->assertStringContainsString ('<input type="checkbox" ' , $ html );
136163 }
137164}
0 commit comments