@@ -43,6 +43,11 @@ class DeleteFilesTest extends \PHPUnit\Framework\TestCase
43
43
*/
44
44
private $ objectManager ;
45
45
46
+ /**
47
+ * @var string
48
+ */
49
+ private $ fullDirectoryPath ;
50
+
46
51
/**
47
52
* @inheritdoc
48
53
*/
@@ -54,6 +59,7 @@ protected function setUp()
54
59
$ this ->imagesHelper = $ this ->objectManager ->get (\Magento \Cms \Helper \Wysiwyg \Images::class);
55
60
$ this ->mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
56
61
$ this ->model = $ this ->objectManager ->get (\Magento \Cms \Controller \Adminhtml \Wysiwyg \Images \DeleteFiles::class);
62
+ $ this ->fullDirectoryPath = $ this ->imagesHelper ->getStorageRoot () . '/directory1 ' ;
57
63
}
58
64
59
65
/**
@@ -64,20 +70,15 @@ protected function setUp()
64
70
*/
65
71
public function testExecute ()
66
72
{
67
- $ directoryName = 'directory1 ' ;
68
- $ fullDirectoryPath = $ this ->imagesHelper ->getStorageRoot () . '/ ' . $ directoryName ;
69
- $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ($ fullDirectoryPath ));
70
- $ filePath = $ fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ;
73
+ $ this ->mediaDirectory ->create ($ this ->mediaDirectory ->getRelativePath ($ this ->fullDirectoryPath ));
74
+ $ filePath = $ this ->fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ;
71
75
$ fixtureDir = realpath (__DIR__ . '/../../../../../Catalog/_files ' );
72
76
copy ($ fixtureDir . '/ ' . $ this ->fileName , $ filePath );
73
77
74
- $ this ->model ->getRequest ()->setMethod ('POST ' )
75
- ->setPostValue ('files ' , [$ this ->imagesHelper ->idEncode ($ this ->fileName )]);
76
- $ this ->model ->getStorage ()->getSession ()->setCurrentPath ($ fullDirectoryPath );
77
- $ this ->model ->execute ();
78
+ $ this ->executeFileDelete ($ this ->fullDirectoryPath , $ this ->fileName );
78
79
$ this ->assertFalse (
79
80
$ this ->mediaDirectory ->isExist (
80
- $ this ->mediaDirectory ->getRelativePath ($ fullDirectoryPath . '/ ' . $ this ->fileName )
81
+ $ this ->mediaDirectory ->getRelativePath ($ this -> fullDirectoryPath . '/ ' . $ this ->fileName )
81
82
)
82
83
);
83
84
}
@@ -99,11 +100,74 @@ public function testExecuteWithLinkedMedia()
99
100
copy ($ fixtureDir . '/ ' . $ this ->fileName , $ filePath );
100
101
101
102
$ wysiwygDir = $ this ->mediaDirectory ->getAbsolutePath () . '/wysiwyg ' ;
103
+ $ this ->executeFileDelete ($ wysiwygDir , $ this ->fileName );
104
+ $ this ->assertFalse (is_file ($ fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ));
105
+ }
106
+
107
+ /**
108
+ * Check that htaccess file couldn't be removed via
109
+ * \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
110
+ *
111
+ * @return void
112
+ */
113
+ public function testDeleteHtaccess ()
114
+ {
115
+ $ this ->createFile ($ this ->fullDirectoryPath , '.htaccess ' );
116
+ $ this ->executeFileDelete ($ this ->fullDirectoryPath , '.htaccess ' );
117
+
118
+ $ this ->assertTrue (
119
+ $ this ->mediaDirectory ->isExist (
120
+ $ this ->mediaDirectory ->getRelativePath ($ this ->fullDirectoryPath . '/.htaccess ' )
121
+ )
122
+ );
123
+ }
124
+
125
+ /**
126
+ * Check that random file could be removed via
127
+ * \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
128
+ *
129
+ * @return void
130
+ */
131
+ public function testDeleteAnyFile ()
132
+ {
133
+ $ this ->createFile ($ this ->fullDirectoryPath , 'ahtaccess ' );
134
+ $ this ->executeFileDelete ($ this ->fullDirectoryPath , 'ahtaccess ' );
135
+
136
+ $ this ->assertFalse (
137
+ $ this ->mediaDirectory ->isExist (
138
+ $ this ->mediaDirectory ->getRelativePath ($ this ->fullDirectoryPath . '/ahtaccess ' )
139
+ )
140
+ );
141
+ }
142
+
143
+ /**
144
+ * Create file.
145
+ *
146
+ * @param string $path
147
+ * @param string $fileName
148
+ * @return void
149
+ */
150
+ private function createFile (string $ path , string $ fileName )
151
+ {
152
+ $ file = $ path . '/ ' . $ fileName ;
153
+ if (!$ this ->mediaDirectory ->isFile ($ file )) {
154
+ $ this ->mediaDirectory ->writeFile ($ file , 'Content ' );
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Execute file delete operation.
160
+ *
161
+ * @param string $path
162
+ * @param string $fileName
163
+ * @return void
164
+ */
165
+ private function executeFileDelete (string $ path , string $ fileName )
166
+ {
102
167
$ this ->model ->getRequest ()->setMethod ('POST ' )
103
- ->setPostValue ('files ' , [$ this ->imagesHelper ->idEncode ($ this -> fileName )]);
104
- $ this ->model ->getStorage ()->getSession ()->setCurrentPath ($ wysiwygDir );
168
+ ->setPostValue ('files ' , [$ this ->imagesHelper ->idEncode ($ fileName )]);
169
+ $ this ->model ->getStorage ()->getSession ()->setCurrentPath ($ path );
105
170
$ this ->model ->execute ();
106
- $ this ->assertFalse (is_file ($ fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ));
107
171
}
108
172
109
173
/**
0 commit comments