Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/code/Magento/Backup/Model/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
class Backup extends \Magento\Framework\DataObject implements \Magento\Framework\Backup\Db\BackupInterface
{
/**
* Compress rate
* Compression level used when the backup archive is written
*/
const COMPRESS_RATE = 9;
public const COMPRESS_RATE = 9;

/**
* Type of backup file
Expand Down Expand Up @@ -58,8 +58,6 @@ class Backup extends \Magento\Framework\DataObject implements \Magento\Framework
protected $_localeResolver;

/**
* Backend auth session
*
* @var \Magento\Backend\Model\Auth\Session
*/
protected $_backendAuthSession;
Expand Down Expand Up @@ -244,16 +242,20 @@ public function setFile(&$content)
/**
* Return content of backup file
*
* @return array
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function &getFile()
{
if (!$this->exists()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The backup file does not exist.'));
}

return $this->varDirectory->read($this->_getFilePath());
// the returned expression must be a variable, otherwise PHP raises a notice for the by-reference return
$contents = $this->varDirectory->readFile($this->_getFilePath());

return $contents;
}

/**
Expand Down
112 changes: 112 additions & 0 deletions app/code/Magento/Backup/Test/Unit/Model/BackupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Backup\Model\Backup;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -112,6 +113,117 @@ public function testOutput($isFile, $result)
$this->assertEquals($result, $this->backupModel->output());
}

/**
* @covers \Magento\Backup\Model\Backup::getFile
*/
public function testGetFileReturnsBackupContents()
{
$relativePath = '/path/to/1_db_test.sql';
$contents = 'test_result';

$this->directoryMock->expects($this->atLeastOnce())
->method('isFile')
->with($relativePath)
->willReturn(true);
$this->directoryMock->expects($this->any())
->method('getRelativePath')
->with($relativePath)
->willReturn($relativePath);
$this->directoryMock->expects($this->once())
->method('readFile')
->with($relativePath)
->willReturn($contents);
$this->directoryMock->expects($this->never())
->method('read');
$this->dataHelperMock->expects($this->any())
->method('getExtensionByType')
->with('db')
->willReturn('sql');

$this->backupModel->setPath('/path/to');
$this->backupModel->setName('test');
$this->backupModel->setTime(1);

$this->assertSame($contents, $this->backupModel->getFile());
}

/**
* The by-reference return must not raise "Only variable references should be returned by reference".
*
* @covers \Magento\Backup\Model\Backup::getFile
*/
public function testGetFileDoesNotRaiseNoticeOnReturnByReference()
{
$relativePath = '/path/to/1_db_test.sql';

$this->directoryMock->expects($this->atLeastOnce())
->method('isFile')
->with($relativePath)
->willReturn(true);
$this->directoryMock->expects($this->any())
->method('getRelativePath')
->with($relativePath)
->willReturn($relativePath);
$this->directoryMock->expects($this->any())
->method('readFile')
->with($relativePath)
->willReturn('test_result');
$this->dataHelperMock->expects($this->any())
->method('getExtensionByType')
->with('db')
->willReturn('sql');

$this->backupModel->setPath('/path/to');
$this->backupModel->setName('test');
$this->backupModel->setTime(1);

$raised = [];
set_error_handler(
static function ($errno, $errstr) use (&$raised) {
$raised[] = $errstr;
return true;
},
E_ALL
);
try {
$this->backupModel->getFile();
} finally {
restore_error_handler();
}

$this->assertSame([], $raised, 'No PHP notice is expected when returning the backup contents.');
}

/**
* @covers \Magento\Backup\Model\Backup::getFile
*/
public function testGetFileThrowsExceptionWhenBackupIsMissing()
{
$relativePath = '/path/to/1_db_test.sql';

$this->directoryMock->expects($this->atLeastOnce())
->method('isFile')
->with($relativePath)
->willReturn(false);
$this->directoryMock->expects($this->any())
->method('getRelativePath')
->with($relativePath)
->willReturn($relativePath);
$this->dataHelperMock->expects($this->any())
->method('getExtensionByType')
->with('db')
->willReturn('sql');

$this->backupModel->setPath('/path/to');
$this->backupModel->setName('test');
$this->backupModel->setTime(1);

$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('The backup file does not exist.');

$this->backupModel->getFile();
}

/**
* @return array
*/
Expand Down
100 changes: 100 additions & 0 deletions dev/tests/integration/testsuite/Magento/Backup/Model/BackupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Backup\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* @see Backup
* @magentoAppArea adminhtml
*/
class BackupTest extends TestCase
{
private const BACKUP_TIME = 1700000000;
private const BACKUP_PATH = 'backups';

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var WriteInterface
*/
private $varDirectory;

/**
* @var string
*/
private $backupFile;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->varDirectory = $this->objectManager->get(Filesystem::class)
->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->backupFile = self::BACKUP_PATH . '/' . self::BACKUP_TIME . '_db_integration.sql';
}

/**
* @inheritdoc
*/
protected function tearDown(): void
{
if ($this->varDirectory->isExist($this->backupFile)) {
$this->varDirectory->delete($this->backupFile);
}
}

/**
* The contents of the backup file are returned, not a listing of the path.
*/
public function testGetFileReturnsBackupContents(): void
{
$contents = '-- integration backup contents';
$this->varDirectory->writeFile($this->backupFile, $contents);

$this->assertSame($contents, $this->createBackup()->getFile());
}

/**
* A missing backup file is reported as a localized exception.
*/
public function testGetFileThrowsExceptionWhenBackupIsMissing(): void
{
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('The backup file does not exist.');

$this->createBackup()->getFile();
}

/**
* Create a backup model pointing at the fixture file.
*
* @return Backup
*/
private function createBackup(): Backup
{
$backup = $this->objectManager->create(Backup::class);
$backup->setPath(self::BACKUP_PATH)
->setName('integration')
->setTime(self::BACKUP_TIME);
$backup->setType('db');

return $backup;
}
}