StatusTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 StatusTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Update\Status
  11. */
  12. protected $status;
  13. /**
  14. * @var string
  15. */
  16. protected $statusFilePath;
  17. /**
  18. * @var string
  19. */
  20. private $statusScrubbedFilePath;
  21. /**
  22. * @var string
  23. */
  24. protected $tmpStatusFilePath;
  25. /**
  26. * @var string
  27. */
  28. protected $tmpStatusLogFilePath;
  29. /**
  30. * @var string
  31. */
  32. protected $updateInProgressFlagFilePath;
  33. /**
  34. * @var string
  35. */
  36. protected $updateErrorFlagFilePath;
  37. /**
  38. * @var string
  39. */
  40. private $somePath = '/someDir/someFile.ext';
  41. protected function setUp()
  42. {
  43. parent::setUp();
  44. $this->statusFilePath = __DIR__ . '/_files/update_status.txt';
  45. $this->statusScrubbedFilePath = __DIR__ . '/_files/update_status_scrubbed.txt';
  46. $this->tmpStatusFilePath = TESTS_TEMP_DIR . '/update_status.txt';
  47. $this->tmpStatusLogFilePath = TESTS_TEMP_DIR . '/update.log';
  48. $this->updateInProgressFlagFilePath = TESTS_TEMP_DIR . '/update_in_progress.flag';
  49. $this->updateErrorFlagFilePath = TESTS_TEMP_DIR . '/update_error.flag';
  50. $statusFileContent = file_get_contents($this->statusFilePath);
  51. /** Prepare temporary status file which can be modified */
  52. file_put_contents($this->tmpStatusFilePath, $statusFileContent);
  53. /** Make sure it was created */
  54. $this->assertEquals($statusFileContent, file_get_contents($this->tmpStatusFilePath), "Precondition failed.");
  55. file_put_contents($this->tmpStatusLogFilePath, $statusFileContent);
  56. $this->assertEquals($statusFileContent, file_get_contents($this->tmpStatusLogFilePath), "Precondition failed.");
  57. }
  58. protected function tearDown()
  59. {
  60. parent::tearDown();
  61. if (file_exists($this->tmpStatusFilePath)) {
  62. unlink($this->tmpStatusFilePath);
  63. }
  64. if (file_exists($this->tmpStatusLogFilePath)) {
  65. unlink($this->tmpStatusLogFilePath);
  66. }
  67. if (file_exists($this->updateInProgressFlagFilePath)) {
  68. unlink($this->updateInProgressFlagFilePath);
  69. }
  70. if (file_exists($this->updateErrorFlagFilePath)) {
  71. unlink($this->updateErrorFlagFilePath);
  72. }
  73. }
  74. public function testGet()
  75. {
  76. $tmpFileStatus = TESTS_TEMP_DIR . 'tmpStatus.txt';
  77. $statusContent = file_get_contents($this->statusFilePath);
  78. $statusContent .= PHP_EOL . MAGENTO_BP . $this->somePath;
  79. file_put_contents($tmpFileStatus, $statusContent);
  80. $status = new \Magento\Update\Status($tmpFileStatus);
  81. $actualStatusText = $status->get();
  82. /** Ensure that actual status text is correct */
  83. $statusContent = file_get_contents($this->statusFilePath);
  84. $statusScrubbedContent = file_get_contents($this->statusScrubbedFilePath);
  85. $statusScrubbedContent .= PHP_EOL . '{magento_root}' . $this->somePath;
  86. $this->assertNotEquals($statusContent, $actualStatusText);
  87. $this->assertEquals($statusScrubbedContent, $actualStatusText);
  88. if (file_exists($tmpFileStatus)) {
  89. unlink($tmpFileStatus);
  90. }
  91. }
  92. public function testGetFileDoesNotExixt()
  93. {
  94. $status = new \Magento\Update\Status('invalid_file_path');
  95. $this->assertEquals('', $status->get());
  96. }
  97. public function testGetEmptyFile()
  98. {
  99. file_put_contents($this->tmpStatusFilePath, '');
  100. $status = new \Magento\Update\Status($this->tmpStatusFilePath);
  101. $this->assertEquals('', $status->get());
  102. }
  103. public function testAdd()
  104. {
  105. $originalStatus = file_get_contents($this->tmpStatusFilePath);
  106. $status = new \Magento\Update\Status($this->tmpStatusFilePath, $this->tmpStatusLogFilePath);
  107. $firstUpdate = <<<FIRST_UPDATE
  108. Praesent blandit dolor.
  109. Sed non quam.
  110. FIRST_UPDATE;
  111. $status->add($firstUpdate);
  112. $textAfterFirstUpdate = "$originalStatus\n{$firstUpdate}";
  113. $this->verifyAddedStatus($textAfterFirstUpdate, $this->tmpStatusFilePath, 1);
  114. $textAfterFirstUpdate = $originalStatus . $this->getLogFilePattern($firstUpdate);
  115. $this->verifyAddedStatus($textAfterFirstUpdate, $this->tmpStatusLogFilePath, 3*1);
  116. $secondUpdate = <<<SECOND_UPDATE
  117. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue.
  118. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim.
  119. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper.
  120. SECOND_UPDATE;
  121. $this->assertInstanceOf('Magento\Update\Status', $status->add($secondUpdate));
  122. $textAfterSecondUpdate = "{$originalStatus}\n{$firstUpdate}\n{$secondUpdate}";
  123. $this->verifyAddedStatus($textAfterSecondUpdate, $this->tmpStatusFilePath, 2);
  124. $textAfterSecondUpdate = $originalStatus .
  125. $this->getLogFilePattern($firstUpdate) . ' ' . $this->getLogFilePattern($secondUpdate);
  126. $this->verifyAddedStatus($textAfterSecondUpdate, $this->tmpStatusLogFilePath, 3*2);
  127. }
  128. public function testAddToNotExistingFile()
  129. {
  130. unlink($this->tmpStatusFilePath);
  131. $this->assertFalse(file_exists($this->tmpStatusFilePath), "Precondition failed.");
  132. $status = new \Magento\Update\Status($this->tmpStatusFilePath);
  133. $statusUpdate = <<<STATUS_UPDATE
  134. Praesent blandit dolor.
  135. Sed non quam.
  136. STATUS_UPDATE;
  137. $status->add($statusUpdate);
  138. $this->verifyAddedStatus($statusUpdate, $this->tmpStatusFilePath, 1);
  139. }
  140. public function testClear()
  141. {
  142. $originalLogFileContent = file_get_contents($this->tmpStatusLogFilePath);
  143. $status = new \Magento\Update\Status($this->tmpStatusFilePath, $this->tmpStatusLogFilePath);
  144. $this->assertInstanceOf('Magento\Update\Status', $status->clear());
  145. $this->assertEquals('', file_get_contents($this->tmpStatusFilePath));
  146. $this->assertEquals($originalLogFileContent, file_get_contents($this->tmpStatusLogFilePath));
  147. }
  148. public function testClearNotExistingFile()
  149. {
  150. unlink($this->tmpStatusFilePath);
  151. $this->assertFalse(file_exists($this->tmpStatusFilePath), "Precondition failed.");
  152. $status = new \Magento\Update\Status($this->tmpStatusFilePath);
  153. $this->assertInstanceOf('Magento\Update\Status', $status->clear());
  154. $this->assertFalse(file_exists($this->tmpStatusFilePath));
  155. }
  156. public function testIsUpdateInProgress()
  157. {
  158. $status = new \Magento\Update\Status(
  159. $this->tmpStatusFilePath,
  160. $this->tmpStatusLogFilePath,
  161. $this->updateInProgressFlagFilePath
  162. );
  163. $this->assertFalse($status->isUpdateInProgress());
  164. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateInProgress());
  165. $this->assertTrue($status->isUpdateInProgress());
  166. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateInProgress(false));
  167. $this->assertFalse($status->isUpdateInProgress());
  168. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateInProgress(true));
  169. $this->assertTrue($status->isUpdateInProgress());
  170. }
  171. public function testIsUpdateError()
  172. {
  173. $status = new \Magento\Update\Status(
  174. $this->tmpStatusFilePath,
  175. $this->tmpStatusLogFilePath,
  176. $this->updateInProgressFlagFilePath,
  177. $this->updateErrorFlagFilePath
  178. );
  179. $this->assertFalse($status->isUpdateError());
  180. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateError());
  181. $this->assertTrue($status->isUpdateError());
  182. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateError(false));
  183. $this->assertFalse($status->isUpdateError());
  184. $this->assertInstanceOf('Magento\Update\Status', $status->setUpdateError(true));
  185. $this->assertTrue($status->isUpdateError());
  186. }
  187. /**
  188. * @param string $expectedTextAfterUpdate
  189. * @param string $filePath
  190. * @param int $expectedNumberOfTimeEntries
  191. */
  192. protected function verifyAddedStatus($expectedTextAfterUpdate, $filePath, $expectedNumberOfTimeEntries)
  193. {
  194. $actualStatusText = file_get_contents($filePath);
  195. /** Make sure that number of current date/time entries matches expected value */
  196. preg_match_all('/\[.*?\]\s/', $actualStatusText, $matches);
  197. $this->assertCount(1, $matches);
  198. $this->assertCount($expectedNumberOfTimeEntries, $matches[0]);
  199. /** Eliminate current date/time entries from the actual status content before text comparison */
  200. $actualStatusText = trim(preg_replace('/\[.*?\]\s/', '', $actualStatusText));
  201. $this->assertEquals($expectedTextAfterUpdate, $actualStatusText);
  202. }
  203. /**
  204. * Get log pattern
  205. *
  206. * @param string $msg
  207. * @return string
  208. */
  209. private function getLogFilePattern($msg)
  210. {
  211. return 'update-cron.INFO: ' . preg_replace('/' . PHP_EOL . '/', ' ', $msg);
  212. }
  213. }