16
16
*/
17
17
class UploaderTest extends \Magento \TestFramework \Indexer \TestCase
18
18
{
19
+ /**
20
+ * Random string appended to downloaded image name
21
+ */
22
+ const RANDOM_STRING = 'BRV8TAuR2AT88OH0 ' ;
19
23
/**
20
24
* @var \Magento\Framework\ObjectManagerInterface
21
25
*/
@@ -30,14 +34,29 @@ class UploaderTest extends \Magento\TestFramework\Indexer\TestCase
30
34
* @var \Magento\CatalogImportExport\Model\Import\Uploader
31
35
*/
32
36
private $ uploader ;
37
+ /**
38
+ * @var \Magento\Framework\Filesystem\File\ReadInterface|\PHPUnit\Framework\MockObject\MockObject
39
+ */
40
+ private $ fileReader ;
33
41
34
42
/**
35
43
* @inheritdoc
36
44
*/
37
45
protected function setUp ()
38
46
{
39
47
$ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
40
- $ this ->uploader = $ this ->objectManager ->create (\Magento \CatalogImportExport \Model \Import \Uploader::class);
48
+ $ this ->fileReader = $ this ->getMockForAbstractClass (\Magento \Framework \Filesystem \File \ReadInterface::class);
49
+ $ fileReadFactory = $ this ->createMock (\Magento \Framework \Filesystem \File \ReadFactory::class);
50
+ $ fileReadFactory ->method ('create ' )->willReturn ($ this ->fileReader );
51
+ $ random = $ this ->createMock (\Magento \Framework \Math \Random::class);
52
+ $ random ->method ('getRandomString ' )->willReturn (self ::RANDOM_STRING );
53
+ $ this ->uploader = $ this ->objectManager ->create (
54
+ \Magento \CatalogImportExport \Model \Import \Uploader::class,
55
+ [
56
+ 'random ' => $ random ,
57
+ 'readFactory ' => $ fileReadFactory
58
+ ]
59
+ );
41
60
42
61
$ filesystem = $ this ->objectManager ->create (\Magento \Framework \Filesystem::class);
43
62
@@ -60,16 +79,32 @@ protected function setUp()
60
79
parent ::setUp ();
61
80
}
62
81
82
+ /**
83
+ * Tests move with external url
84
+ *
85
+ * @magentoAppIsolation enabled
86
+ * @return void
87
+ */
88
+ public function testMoveWithExternalURL (): void
89
+ {
90
+ $ fileName = 'http://magento.com/static/images/random_image.jpg ' ;
91
+ $ this ->fileReader ->method ('readAll ' )->willReturn (file_get_contents ($ this ->getTestImagePath ()));
92
+ $ this ->uploader ->move ($ fileName );
93
+ $ destFilePath = $ this ->uploader ->getTmpDir () . '/ ' . 'random_image_ ' . self ::RANDOM_STRING . '.jpg ' ;
94
+ $ this ->assertTrue ($ this ->directory ->isExist ($ destFilePath ));
95
+ }
96
+
63
97
/**
64
98
* @magentoAppIsolation enabled
65
99
* @return void
66
100
*/
67
101
public function testMoveWithValidFile (): void
68
102
{
69
- $ fileName = 'magento_additional_image_one.jpg ' ;
103
+ $ testImagePath = $ this ->getTestImagePath ();
104
+ $ fileName = basename ($ testImagePath );
70
105
$ filePath = $ this ->directory ->getAbsolutePath ($ this ->uploader ->getTmpDir () . '/ ' . $ fileName );
71
106
//phpcs:ignore
72
- copy (__DIR__ . ' /_files/ ' . $ fileName , $ filePath );
107
+ copy ($ testImagePath , $ filePath );
73
108
$ this ->uploader ->move ($ fileName );
74
109
$ this ->assertTrue ($ this ->directory ->isExist ($ this ->uploader ->getTmpDir () . '/ ' . $ fileName ));
75
110
}
@@ -84,15 +119,17 @@ public function testMoveWithValidFile(): void
84
119
public function testMoveWithFileOutsideTemp (): void
85
120
{
86
121
$ tmpDir = $ this ->uploader ->getTmpDir ();
87
- if (!$ this ->directory ->create ($ newTmpDir = $ tmpDir .'/test1 ' )) {
122
+ $ newTmpDir = $ tmpDir . '/test1 ' ;
123
+ if (!$ this ->directory ->create ($ newTmpDir )) {
88
124
throw new \RuntimeException ('Failed to create temp dir ' );
89
125
}
90
126
$ this ->uploader ->setTmpDir ($ newTmpDir );
91
- $ fileName = 'magento_additional_image_one.jpg ' ;
127
+ $ testImagePath = $ this ->getTestImagePath ();
128
+ $ fileName = basename ($ testImagePath );
92
129
$ filePath = $ this ->directory ->getAbsolutePath ($ tmpDir . '/ ' . $ fileName );
93
130
//phpcs:ignore
94
- copy (__DIR__ . ' /_files/ ' . $ fileName , $ filePath );
95
- $ this ->uploader ->move ('../ ' .$ fileName );
131
+ copy ($ testImagePath , $ filePath );
132
+ $ this ->uploader ->move ('../ ' . $ fileName );
96
133
$ this ->assertTrue ($ this ->directory ->isExist ($ tmpDir . '/ ' . $ fileName ));
97
134
}
98
135
@@ -111,4 +148,14 @@ public function testMoveWithInvalidFile(): void
111
148
$ this ->uploader ->move ($ fileName );
112
149
$ this ->assertFalse ($ this ->directory ->isExist ($ this ->uploader ->getTmpDir () . '/ ' . $ fileName ));
113
150
}
151
+
152
+ /**
153
+ * Get the full path to the test image
154
+ *
155
+ * @return string
156
+ */
157
+ private function getTestImagePath (): string
158
+ {
159
+ return __DIR__ . '/_files/magento_additional_image_one.jpg ' ;
160
+ }
114
161
}
0 commit comments