Skip to content

Commit 1d4a31a

Browse files
safwkhaneddielau
authored andcommitted
MAGETWO-38151: Data Rollback
- Changed rollback file validation logic.
1 parent 5d4fdb1 commit 1d4a31a

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

lib/internal/Magento/Framework/Setup/BackupRollback.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public function codeBackup($time, $type = Factory::TYPE_FILESYSTEM)
127127
*/
128128
public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM)
129129
{
130+
if (preg_match('/[0-9]_(filesystem)_(code|media)\.(tgz)$/', $rollbackFile) !== 1) {
131+
throw new LocalizedException(new Phrase('Invalid rollback file.'));
132+
}
130133
$backupsDir = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/' . self::DEFAULT_BACKUP_DIRECTORY;
131134
if (!$this->file->isExists($backupsDir . '/' . $rollbackFile)) {
132135
throw new LocalizedException(new Phrase('The rollback file does not exist.'));
@@ -159,7 +162,6 @@ public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM)
159162
$fsRollback->setBackupsDir($backupsDir);
160163
$fsRollback->setBackupExtension('tgz');
161164
$time = explode('_', $rollbackFile);
162-
$this->checkRollbackFileValidity($time, 'tgz');
163165
$fsRollback->setTime($time[0]);
164166
$this->log->log($granularType . ' rollback is starting ...');
165167
$fsRollback->rollback();
@@ -206,6 +208,9 @@ public function dbBackup($time)
206208
*/
207209
public function dbRollback($rollbackFile)
208210
{
211+
if (preg_match('/[0-9]_(db).(gz)$/', $rollbackFile) !== 1) {
212+
throw new LocalizedException(new Phrase('Invalid rollback file.'));
213+
}
209214
$backupsDir = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/' . self::DEFAULT_BACKUP_DIRECTORY;
210215
if (!$this->file->isExists($backupsDir . '/' . $rollbackFile)) {
211216
throw new LocalizedException(new Phrase('The rollback file does not exist.'));
@@ -217,7 +222,6 @@ public function dbRollback($rollbackFile)
217222
$dbRollback->setBackupsDir($backupsDir);
218223
$dbRollback->setBackupExtension('gz');
219224
$time = explode('_', $rollbackFile);
220-
$this->checkRollbackFileValidity($time, 'gz');
221225
if (count($time) === 3) {
222226
$thirdPart = explode('.', $time[2]);
223227
$dbRollback->setName($thirdPart[0]);
@@ -284,24 +288,4 @@ private function getMediaBackupIgnorePaths()
284288
}
285289
return $ignorePaths;
286290
}
287-
288-
/**
289-
* Check if the provided backup file for rollback is valid
290-
*
291-
* @param array $time
292-
* @param string $type
293-
* @return void
294-
* @throws LocalizedException
295-
*/
296-
private function checkRollbackFileValidity($time, $type)
297-
{
298-
$invalidFileNameMsg = 'Invalid rollback file.';
299-
if (!in_array(count($time), [2, 3])) {
300-
throw new LocalizedException(new Phrase($invalidFileNameMsg));
301-
}
302-
$extension = explode('.', $time[count($time) - 1]);
303-
if ($extension[count($extension) - 1] !== $type) {
304-
throw new LocalizedException(new Phrase($invalidFileNameMsg));
305-
}
306-
}
307291
}

lib/internal/Magento/Framework/Setup/Test/Unit/BackupRollbackTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ public function testCodeRollback()
120120
$this->setupCodeBackupRollback();
121121
$this->file->expects($this->once())
122122
->method('isExists')
123-
->with($this->path . '/backups/RollbackFile_A.tgz')
123+
->with($this->path . '/backups/12345_filesystem_code.tgz')
124124
->willReturn(true);
125-
$this->model->codeRollback('RollbackFile_A.tgz');
125+
$this->model->codeRollback('12345_filesystem_code.tgz');
126126
}
127127

128128
/**
@@ -134,7 +134,7 @@ public function testCodeRollbackWithInvalidFilePath()
134134
$this->file->expects($this->once())
135135
->method('isExists')
136136
->willReturn(false);
137-
$this->model->codeRollback('RollbackFile_A.tgz');
137+
$this->model->codeRollback('12345_filesystem_code.tgz');
138138
}
139139

140140
/**
@@ -143,24 +143,9 @@ public function testCodeRollbackWithInvalidFilePath()
143143
*/
144144
public function testCodeRollbackWithInvalidFileType()
145145
{
146-
$this->file->expects($this->once())
147-
->method('isExists')
148-
->willReturn(true);
149146
$this->model->codeRollback('RollbackFile_A.txt');
150147
}
151148

152-
/**
153-
* @expectedException \Magento\Framework\Exception\LocalizedException
154-
* @expectedExceptionMessage Invalid rollback file.
155-
*/
156-
public function testCodeRollbackWithInvalidFileFormat()
157-
{
158-
$this->file->expects($this->once())
159-
->method('isExists')
160-
->willReturn(true);
161-
$this->model->codeRollback('RollbackFile.gz');
162-
}
163-
164149
public function testMediaBackup()
165150
{
166151
$this->setupCodeBackupRollback();
@@ -177,9 +162,9 @@ public function testMediaRollback()
177162
$this->setupCodeBackupRollback();
178163
$this->file->expects($this->once())
179164
->method('isExists')
180-
->with($this->path . '/backups/RollbackFile_A.tgz')
165+
->with($this->path . '/backups/12345_filesystem_media.tgz')
181166
->willReturn(true);
182-
$this->model->codeRollback('RollbackFile_A.tgz', Factory::TYPE_MEDIA);
167+
$this->model->codeRollback('12345_filesystem_media.tgz', Factory::TYPE_MEDIA);
183168
}
184169

185170
public function testDbBackup()
@@ -197,9 +182,9 @@ public function testDbRollback()
197182
$this->database->expects($this->once())->method('rollback');
198183
$this->file->expects($this->once())
199184
->method('isExists')
200-
->with($this->path . '/backups/RollbackFile_A.gz')
185+
->with($this->path . '/backups/12345_db.gz')
201186
->willReturn(true);
202-
$this->model->dbRollback('RollbackFile_A.gz');
187+
$this->model->dbRollback('12345_db.gz');
203188
}
204189

205190
private function setupCodeBackupRollback()
@@ -217,7 +202,7 @@ private function setupCodeBackupRollback()
217202
->willReturn('RollbackFile_A.tgz');
218203
$this->filesystem->expects($this->once())
219204
->method('getBackupPath')
220-
->willReturn('pathToFile/RollbackFile_A.tgz');
205+
->willReturn('pathToFile/12345_filesystem_code.tgz');
221206
$this->log->expects($this->once())
222207
->method('logSuccess');
223208
}
@@ -235,7 +220,7 @@ private function setupDbBackupRollback()
235220
->willReturn('RollbackFile_A.gz');
236221
$this->database->expects($this->once())
237222
->method('getBackupPath')
238-
->willReturn('pathToFile/RollbackFile_A.gz');
223+
->willReturn('pathToFile/12345_db.tgz');
239224
$this->log->expects($this->once())
240225
->method('logSuccess');
241226
}

0 commit comments

Comments
 (0)