CronReadinessCheckTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 CronReadinessCheckTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var bool
  11. */
  12. static public $writable = true;
  13. /**
  14. * @var string
  15. */
  16. private $setupCronJobStatusFilePath;
  17. /**
  18. * @var string
  19. */
  20. private $cronJobStatusFilePath;
  21. protected function setUp()
  22. {
  23. if (!is_dir(MAGENTO_BP . '/var/')) {
  24. mkdir(MAGENTO_BP . '/var/', 0777, true);
  25. }
  26. $this->setupCronJobStatusFilePath = MAGENTO_BP . '/var/' . CronReadinessCheck::SETUP_CRON_JOB_STATUS_FILE;
  27. file_put_contents(
  28. $this->setupCronJobStatusFilePath,
  29. json_encode([CronReadinessCheck::KEY_FILE_PATHS => [CronReadinessCheck::KEY_LIST => [__FILE__]]])
  30. );
  31. $this->cronJobStatusFilePath = MAGENTO_BP . '/var/' . CronReadinessCheck::CRON_JOB_STATUS_FILE;
  32. file_put_contents(
  33. $this->cronJobStatusFilePath,
  34. json_encode([CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 150])
  35. );
  36. }
  37. public function tearDown()
  38. {
  39. if (file_exists($this->setupCronJobStatusFilePath)) {
  40. unlink($this->setupCronJobStatusFilePath);
  41. }
  42. if (file_exists($this->cronJobStatusFilePath)) {
  43. unlink($this->cronJobStatusFilePath);
  44. }
  45. }
  46. public function testRunReadinessCheckNotWritable()
  47. {
  48. $cronReadinessCheck = new CronReadinessCheck();
  49. self::$writable = false;
  50. $this->assertFalse($cronReadinessCheck->runReadinessCheck());
  51. $file = fopen($this->cronJobStatusFilePath, 'r');
  52. $data = fread($file, filesize($this->cronJobStatusFilePath));
  53. $json = json_decode($data, true);
  54. $expected = [
  55. CronReadinessCheck::KEY_READINESS_CHECKS => [
  56. CronReadinessCheck::KEY_FILE_PERMISSIONS_VERIFIED => false,
  57. ],
  58. CronReadinessCheck::KEY_LAST_TIMESTAMP => 150,
  59. CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 200,
  60. ];
  61. $errorMessage = $json[CronReadinessCheck::KEY_READINESS_CHECKS]['error'];
  62. unset($json[CronReadinessCheck::KEY_READINESS_CHECKS]['error']);
  63. $this->assertEquals($expected, $json);
  64. $this->assertContains('Found non-writable path(s):<br/>', $errorMessage);
  65. }
  66. public function testRunReadinessCheck()
  67. {
  68. $cronReadinessCheck = new CronReadinessCheck();
  69. self::$writable = true;
  70. $this->assertTrue($cronReadinessCheck->runReadinessCheck());
  71. $file = fopen($this->cronJobStatusFilePath, 'r');
  72. $data = fread($file, filesize($this->cronJobStatusFilePath));
  73. $json = json_decode($data, true);
  74. $expected = [
  75. CronReadinessCheck::KEY_READINESS_CHECKS => [CronReadinessCheck::KEY_FILE_PERMISSIONS_VERIFIED => true],
  76. CronReadinessCheck::KEY_LAST_TIMESTAMP => 150,
  77. CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 200,
  78. ];
  79. sort($expected);
  80. sort($json);
  81. $this->assertEquals($expected, $json);
  82. }
  83. }
  84. function time()
  85. {
  86. return 200;
  87. }
  88. function is_writable()
  89. {
  90. return CronReadinessCheckTest::$writable;
  91. }