JobRemoveBackupsTest.php 6.4 KB

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