RollbackTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update;
  7. class RollbackTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Update\Rollback
  11. */
  12. protected $rollBack;
  13. /**
  14. * @var string
  15. */
  16. protected $backupPath;
  17. /**
  18. * @var string
  19. */
  20. protected $archivedDir;
  21. /**
  22. * @var string
  23. */
  24. protected $excludedDir;
  25. /**
  26. * @var string
  27. */
  28. protected $backupFileName;
  29. /**
  30. * @var \PharData
  31. */
  32. protected $backupFile;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Backup\BackupInfo
  35. */
  36. protected $backupInfo;
  37. protected function setup()
  38. {
  39. parent::setUp();
  40. $this->backupPath = UPDATER_BP . '/dev/tests/integration/testsuite/Magento/Update/_files/backup/';
  41. $this->archivedDir = UPDATER_BP . '/dev/tests/integration/testsuite/Magento/Update/_files/archived/';
  42. $this->excludedDir = UPDATER_BP . '/dev/tests/integration/testsuite/Magento/Update/_files/archived/excluded/';
  43. if (!is_dir($this->backupPath)) {
  44. mkdir($this->backupPath);
  45. }
  46. if (!is_dir($this->archivedDir)) {
  47. mkdir($this->archivedDir);
  48. }
  49. if (!is_dir($this->excludedDir)) {
  50. mkdir($this->excludedDir);
  51. }
  52. $this->backupFileName = $this->backupPath . '/../' . uniqid() . '_code.tar';
  53. $this->backupFile = new \PharData($this->backupFileName);
  54. $this->backupInfo = $this->getMockBuilder(\Magento\Update\Backup\BackupInfo::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->rollBack = new \Magento\Update\Rollback($this->backupPath, $this->archivedDir, null, $this->backupInfo);
  58. }
  59. protected function tearDown()
  60. {
  61. parent::tearDown();
  62. $this->autoRollbackHelper(2);
  63. if (file_exists($this->backupFileName)) {
  64. unlink($this->backupFileName);
  65. }
  66. $gtzFile = str_replace('tar', 'tgz', $this->backupFileName);
  67. if (file_exists($gtzFile)) {
  68. unlink($gtzFile);
  69. }
  70. if (is_dir($this->backupPath)) {
  71. rmdir($this->backupPath);
  72. }
  73. if (is_dir($this->excludedDir)) {
  74. rmdir($this->excludedDir);
  75. }
  76. if (is_dir($this->archivedDir)) {
  77. rmdir($this->archivedDir);
  78. }
  79. if (file_exists(MAGENTO_BP . '/var/.update_status.txt')) {
  80. unlink(MAGENTO_BP . '/var/.update_status.txt');
  81. }
  82. }
  83. /**
  84. * @expectedException \UnexpectedValueException
  85. */
  86. public function testAutoRollbackBackupFileUnavailable()
  87. {
  88. $this->rollBack->execute('someInvalidfile');
  89. }
  90. public function testAutoRollback()
  91. {
  92. // Setup
  93. $this->autoRollbackHelper();
  94. $this->backupFile->buildFromDirectory($this->archivedDir);
  95. $this->backupFile->addEmptyDir("testDirectory");
  96. $this->backupFile->compress(\Phar::GZ, '.tgz');
  97. $newFile = $this->backupPath . '/' . uniqid() . '_code.tgz';
  98. copy($this->backupFileName, $newFile);
  99. if (file_exists($this->backupFileName)) {
  100. $this->backupFile = null;
  101. unlink($this->backupFileName);
  102. }
  103. $gtzFile = str_replace('tar', 'tgz', $this->backupFileName);
  104. if (file_exists($gtzFile)) {
  105. unlink($gtzFile);
  106. }
  107. $this->backupFileName = $newFile;
  108. // Change the contents of a.txt
  109. $this->autoRollbackHelper(1);
  110. $this->assertEquals('foo changed', file_get_contents($this->archivedDir . 'a.txt'));
  111. $this->backupInfo->expects($this->once())->method('getBlacklist')->willReturn(['excluded']);
  112. // Rollback process
  113. $this->rollBack->execute($this->backupFileName);
  114. // Assert that the contents of a.txt has been restored properly
  115. $this->assertEquals('foo', file_get_contents($this->archivedDir . 'a.txt'));
  116. }
  117. /**
  118. * Helper to create simple files
  119. *
  120. * @param int $flag
  121. */
  122. protected function autoRollbackHelper($flag = 0)
  123. {
  124. $fileA = 'a.txt';
  125. $fileB = 'b.txt';
  126. $fileC = 'c.txt';
  127. if ($flag === 0) {
  128. file_put_contents($this->archivedDir . $fileA, 'foo');
  129. file_put_contents($this->archivedDir . $fileB, 'bar');
  130. file_put_contents($this->archivedDir . $fileC, 'baz');
  131. } elseif ($flag === 1) {
  132. file_put_contents($this->archivedDir . $fileA, 'foo changed');
  133. } elseif ($flag === 2) {
  134. if (file_exists($this->archivedDir . $fileA)) {
  135. unlink($this->archivedDir . $fileA);
  136. }
  137. if (file_exists($this->archivedDir . $fileB)) {
  138. unlink($this->archivedDir . $fileB);
  139. }
  140. if (file_exists($this->archivedDir . $fileC)) {
  141. unlink($this->archivedDir . $fileC);
  142. }
  143. }
  144. }
  145. }