CronTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. use Magento\Update\Backup\BackupInfo;
  8. class CronTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $cronScript;
  14. /**
  15. * @var string
  16. */
  17. protected $backupToRollback;
  18. /**
  19. * @var string
  20. */
  21. protected $backupToRemoveA;
  22. /**
  23. * @var string
  24. */
  25. protected $backupToRemoveB;
  26. /**
  27. * @var \Magento\Update\Status
  28. */
  29. protected $status;
  30. /** @var string */
  31. protected $backupPath;
  32. protected function setUp()
  33. {
  34. $this->backupPath = TESTS_TEMP_DIR . '/var/backup';
  35. if (!is_dir($this->backupPath)) {
  36. mkdir($this->backupPath, 0777, true);
  37. }
  38. $this->cronScript = UPDATER_BP . '/dev/tests/integration/framework/cron.php';
  39. $this->backupToRollback = $this->backupPath . '/BackupToRollback.zip';
  40. $this->backupToRemoveA = $this->backupPath . '/BackupToRemoveA.zip';
  41. $this->backupToRemoveB = $this->backupPath . '/BackupToRemoveB.zip';
  42. $this->status = new \Magento\Update\Status();
  43. file_put_contents($this->backupToRollback, 'w');
  44. file_put_contents($this->backupToRemoveA, 'w');
  45. file_put_contents($this->backupToRemoveB, 'w');
  46. }
  47. protected function tearDown()
  48. {
  49. parent::tearDown();
  50. $this->status->setUpdateInProgress(false);
  51. $this->status->setUpdateError(false);
  52. if (file_exists($this->backupToRollback)) {
  53. unlink($this->backupToRollback);
  54. }
  55. if (file_exists($this->backupToRemoveA)) {
  56. unlink($this->backupToRemoveA);
  57. }
  58. if (file_exists($this->backupToRemoveB)) {
  59. unlink($this->backupToRemoveB);
  60. }
  61. array_map('unlink', glob($this->backupPath . '/*.zip'));
  62. if (is_dir($this->backupPath)) {
  63. rmdir($this->backupPath);
  64. rmdir(TESTS_TEMP_DIR . '/var');
  65. }
  66. if (file_exists(MAGENTO_BP . '/var/.update_queue.json')) {
  67. unlink(MAGENTO_BP . '/var/.update_queue.json');
  68. }
  69. if (file_exists(MAGENTO_BP . '/var/.update_status.txt')) {
  70. unlink(MAGENTO_BP . '/var/.update_status.txt');
  71. }
  72. if (file_exists(MAGENTO_BP . '/var/.update_cronjob_status')) {
  73. unlink(MAGENTO_BP . '/var/.update_cronjob_status');
  74. }
  75. }
  76. public function testUpdateInProgress()
  77. {
  78. $this->status->setUpdateInProgress();
  79. shell_exec('php -f ' . $this->cronScript);
  80. $jobStatus = $this->status->get();
  81. $this->assertContains('Update is already in progress.', $jobStatus);
  82. }
  83. public function testValidQueue()
  84. {
  85. $this->assertTrue(file_exists($this->backupToRollback));
  86. $this->assertTrue(file_exists($this->backupToRemoveA));
  87. $this->assertTrue(file_exists($this->backupToRemoveB));
  88. file_put_contents(MAGENTO_BP . '/var/.update_queue.json',
  89. '{
  90. "jobs": [
  91. {
  92. "name": "remove_backups",
  93. "params": {
  94. "backups_file_names": [
  95. "' . $this->backupToRemoveA . '",
  96. "' . $this->backupToRemoveB . '"
  97. ]
  98. }
  99. }
  100. ],
  101. "ignored_field": ""
  102. }');
  103. shell_exec('php -f ' . $this->cronScript);
  104. $jobStatus = $this->status->get();
  105. // verify removals
  106. $this->assertNotContains('An error occurred while executing job "<remove_backups>"', $jobStatus);
  107. $this->assertContains('Job "remove_backups {"backups_file_names":["' .
  108. $this->backupToRemoveA . '","' . $this->backupToRemoveB .
  109. '"]}" has successfully completed', $jobStatus);
  110. $this->assertFalse(file_exists($this->backupToRemoveA));
  111. $this->assertFalse(file_exists($this->backupToRemoveB));
  112. }
  113. /**
  114. * Test invalid queue file
  115. *
  116. * @expectedException /RuntimeException
  117. * @expectedExceptionMessage RuntimeException: Missing job params "params" field is missing for one or more jobs
  118. */
  119. public function testInvalidQueue()
  120. {
  121. file_put_contents(MAGENTO_BP . '/var/.update_queue.json',
  122. '{
  123. "jobs": [
  124. {
  125. "name": "backup"
  126. }
  127. ],
  128. "ignored_field": ""
  129. }');
  130. shell_exec('php -f ' . $this->cronScript);
  131. $jobStatus = $this->status->get();
  132. $this->assertContains('"params" field is missing for one or more jobs', $jobStatus);
  133. }
  134. }