CronReadinessCheckTest.php 3.21 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Update;

class CronReadinessCheckTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var bool
     */
    static public $writable = true;

    /**
     * @var string
     */
    private $setupCronJobStatusFilePath;

    /**
     * @var string
     */
    private $cronJobStatusFilePath;

    protected function setUp()
    {
        if (!is_dir(MAGENTO_BP . '/var/')) {
            mkdir(MAGENTO_BP . '/var/', 0777, true);
        }
        $this->setupCronJobStatusFilePath = MAGENTO_BP . '/var/' . CronReadinessCheck::SETUP_CRON_JOB_STATUS_FILE;
        file_put_contents(
            $this->setupCronJobStatusFilePath,
            json_encode([CronReadinessCheck::KEY_FILE_PATHS => [CronReadinessCheck::KEY_LIST => [__FILE__]]])
        );

        $this->cronJobStatusFilePath = MAGENTO_BP . '/var/' . CronReadinessCheck::CRON_JOB_STATUS_FILE;
        file_put_contents(
            $this->cronJobStatusFilePath,
            json_encode([CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 150])
        );
    }

    public function tearDown()
    {
        if (file_exists($this->setupCronJobStatusFilePath)) {
            unlink($this->setupCronJobStatusFilePath);
        }
        if (file_exists($this->cronJobStatusFilePath)) {
            unlink($this->cronJobStatusFilePath);
        }
    }

    public function testRunReadinessCheckNotWritable()
    {
        $cronReadinessCheck = new CronReadinessCheck();
        self::$writable = false;
        $this->assertFalse($cronReadinessCheck->runReadinessCheck());

        $file = fopen($this->cronJobStatusFilePath, 'r');
        $data = fread($file, filesize($this->cronJobStatusFilePath));
        $json = json_decode($data, true);
        $expected = [
            CronReadinessCheck::KEY_READINESS_CHECKS => [
                CronReadinessCheck::KEY_FILE_PERMISSIONS_VERIFIED => false,
            ],
            CronReadinessCheck::KEY_LAST_TIMESTAMP => 150,
            CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 200,
        ];
        $errorMessage = $json[CronReadinessCheck::KEY_READINESS_CHECKS]['error'];
        unset($json[CronReadinessCheck::KEY_READINESS_CHECKS]['error']);
        $this->assertEquals($expected, $json);
        $this->assertContains('Found non-writable path(s):<br/>', $errorMessage);
    }

    public function testRunReadinessCheck()
    {
        $cronReadinessCheck = new CronReadinessCheck();
        self::$writable = true;
        $this->assertTrue($cronReadinessCheck->runReadinessCheck());
        $file = fopen($this->cronJobStatusFilePath, 'r');
        $data = fread($file, filesize($this->cronJobStatusFilePath));
        $json = json_decode($data, true);
        $expected = [
            CronReadinessCheck::KEY_READINESS_CHECKS => [CronReadinessCheck::KEY_FILE_PERMISSIONS_VERIFIED => true],
            CronReadinessCheck::KEY_LAST_TIMESTAMP => 150,
            CronReadinessCheck::KEY_CURRENT_TIMESTAMP => 200,
        ];
        sort($expected);
        sort($json);
        $this->assertEquals($expected, $json);
    }
}

function time()
{
    return 200;
}

function is_writable()
{
    return CronReadinessCheckTest::$writable;
}