1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Update\Queue;
use Magento\Update\Status;
class JobRemoveBackupsTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Update\Queue\JobRemoveBackups */
protected $jobRemoveBackup;
/** @var string */
protected $backupFilename;
/** @var string */
protected $backupFilenameA;
/** @var string */
protected $backupFilenameB;
/** @var string */
protected $backupFilenameC;
/** @var string */
protected $backupPath;
/** @var string */
protected $maintenanceFlagFilePath;
/** @var string */
protected $maintenanceAddressFlag;
/** @var string */
protected $updateErrorFlagFilePath;
protected function setUp()
{
parent::setUp();
$this->backupFilenameA = uniqid('test_backupA') . '.zip';
$this->backupFilenameB = uniqid('test_backupB') . '.zip';
$this->backupFilenameC = uniqid('test_backupC') . '.zip';
$this->backupPath = TESTS_TEMP_DIR . '/backup/';
if (!is_dir($this->backupPath)) {
mkdir($this->backupPath);
}
$this->maintenanceFlagFilePath = TESTS_TEMP_DIR . '/.maintenance.flag';
$this->maintenanceAddressFlag = $this->maintenanceAddressFlag;
$this->updateErrorFlagFilePath = TESTS_TEMP_DIR . '/.update_error.flag';
}
protected function tearDown()
{
parent::tearDown();
if (file_exists($this->backupPath . $this->backupFilenameA)) {
unlink($this->backupPath . $this->backupFilenameA);
}
if (file_exists($this->backupPath . $this->backupFilenameB)) {
unlink($this->backupPath . $this->backupFilenameB);
}
if (file_exists($this->backupPath . $this->backupFilenameC)) {
unlink($this->backupPath . $this->backupFilenameC);
}
if (is_dir($this->backupPath)) {
rmdir($this->backupPath);
}
if (file_exists($this->maintenanceFlagFilePath)) {
unlink($this->maintenanceFlagFilePath);
}
if (file_exists($this->updateErrorFlagFilePath)) {
unlink($this->updateErrorFlagFilePath);
}
}
/**
* @param bool $isMaintenanceModeOn
* @param bool $isUpdaterError
* @dataProvider flagFileDataProvider
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot remove backup archives while setup is in progress.
*/
public function testExecuteFlag($isMaintenanceModeOn, $isUpdaterError)
{
/** @var \Magento\Update\MaintenanceMode $maintenanceModeMock */
$maintenanceModeMock = $this->getMockBuilder('Magento\Update\MaintenanceMode')
->disableOriginalConstructor()
->getMock();
$maintenanceModeMock->expects($this->any())->method('isOn')->willReturn($isMaintenanceModeOn);
/** @var \Magento\Update\Status $statusMock */
$statusMock = $this->getMockBuilder('Magento\Update\Status')
->disableOriginalConstructor()
->getMock();
$statusMock->expects($this->any())->method('isUpdateError')->willReturn($isUpdaterError);
$this->jobRemoveBackup = new \Magento\Update\Queue\JobRemoveBackups(
'remove_backups',
[$this->backupPath . $this->backupFilenameA],
$statusMock,
$maintenanceModeMock
);
$this->jobRemoveBackup->execute();
}
public function flagFileDataProvider()
{
return [
"Updater error" => [false, true],
"Maintenance mode on" => [true, true]
];
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Could not delete backup archive
*/
public function testExecuteInvalidBackupFile()
{
$maintenanceMode = new \Magento\Update\MaintenanceMode(
$this->maintenanceFlagFilePath,
$this->maintenanceAddressFlag
);
$this->jobRemoveBackup = new \Magento\Update\Queue\JobRemoveBackups(
'remove_backups',
['backups_file_names' => [$this->backupPath . 'no-such-file.zip']],
new Status(),
$maintenanceMode
);
$this->jobRemoveBackup->execute();
}
public function testExecuteSingle()
{
if (!file_exists($this->backupPath . $this->backupFilenameA)) {
file_put_contents($this->backupPath . $this->backupFilenameA, '');
}
$maintenanceMode = new \Magento\Update\MaintenanceMode(
$this->maintenanceFlagFilePath,
$this->maintenanceAddressFlag
);
$this->jobRemoveBackup = new \Magento\Update\Queue\JobRemoveBackups(
'remove_backups',
['backups_file_names' => [$this->backupPath . $this->backupFilenameA]],
new \Magento\Update\Status(),
$maintenanceMode
);
$this->jobRemoveBackup->execute();
$this->assertFalse(file_exists($this->backupPath . $this->backupFilenameA));
}
public function testExecuteMultiple()
{
if (!file_exists($this->backupPath . $this->backupFilenameA)) {
file_put_contents($this->backupPath . $this->backupFilenameA, '');
}
if (!file_exists($this->backupPath . $this->backupFilenameB)) {
file_put_contents($this->backupPath . $this->backupFilenameB, '');
}
if (!file_exists($this->backupPath . $this->backupFilenameC)) {
file_put_contents($this->backupPath . $this->backupFilenameC, '');
}
$maintenanceMode = new \Magento\Update\MaintenanceMode(
$this->maintenanceFlagFilePath,
$this->maintenanceAddressFlag
);
$this->jobRemoveBackup = new \Magento\Update\Queue\JobRemoveBackups(
'remove_backups',
[
'backups_file_names' => [
$this->backupPath . $this->backupFilenameA,
$this->backupPath . $this->backupFilenameB
]
],
new \Magento\Update\Status(),
$maintenanceMode
);
$this->jobRemoveBackup->execute();
$this->assertFalse(file_exists($this->backupPath . $this->backupFilenameA));
$this->assertFalse(file_exists($this->backupPath . $this->backupFilenameB));
$this->assertTrue(file_exists($this->backupPath . $this->backupFilenameC));
}
}