File size: 2,849 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Entity;

use Mautic\CoreBundle\Helper\DateTimeHelper;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\ContactExportScheduler;
use PHPUnit\Framework\Assert;

class ContactExportSchedulerTest extends MauticMysqlTestCase
{
    private string $previousTimeZone;

    protected function setUp(): void
    {
        $this->previousTimeZone = date_default_timezone_get();
        parent::setUp();
    }

    protected function beforeTearDown(): void
    {
        date_default_timezone_set($this->previousTimeZone);
    }

    public function testScheduledDateTimeIsPersistedAndHydratedProperly(): void
    {
        $timezone = 'Asia/Taipei';
        date_default_timezone_set($timezone);

        $exportScheduler = new ContactExportScheduler();
        $exportScheduler->setScheduledDateTime(new \DateTimeImmutable());
        $this->em->persist($exportScheduler);
        $this->em->flush();

        $id        = $exportScheduler->getId();
        $localDate = $exportScheduler->getScheduledDateTime()->format(DateTimeHelper::FORMAT_DB);
        $utcDate   = $this->convertDateTimezone($localDate, $timezone, 'UTC');
        Assert::assertSame($timezone, $exportScheduler->getScheduledDateTime()->getTimezone()->getName(), sprintf('Timezone should be %s.', $timezone));
        Assert::assertSame($utcDate, $this->fetchScheduledDate($id), 'Database value should be converted to UTC.');

        $this->em->clear();

        $timezone = 'America/Cayman';
        date_default_timezone_set($timezone);

        $exportScheduler = $this->em->find(ContactExportScheduler::class, $id);
        $localDate       = $this->convertDateTimezone($this->fetchScheduledDate($id), 'UTC', $timezone);
        Assert::assertSame($timezone, $exportScheduler->getScheduledDateTime()->getTimezone()->getName(), sprintf('Timezone should be %s.', $timezone));
        Assert::assertSame($localDate, $exportScheduler->getScheduledDateTime()->format(DateTimeHelper::FORMAT_DB), sprintf('PHP value should be converted to %s.', $timezone));
    }

    private function convertDateTimezone(string $date, string $timezoneFrom, string $timezoneTo): string
    {
        return (new \DateTimeImmutable($date, new \DateTimeZone($timezoneFrom)))
            ->setTimezone(new \DateTimeZone($timezoneTo))
            ->format(DateTimeHelper::FORMAT_DB);
    }

    private function fetchScheduledDate(int $id): string
    {
        $tablePrefix   = self::getContainer()->getParameter('mautic.db_table_prefix');
        $connection    = $this->em->getConnection();
        $query         = sprintf('SELECT scheduled_datetime FROM %scontact_export_scheduler WHERE id = :id', $tablePrefix);

        return $connection->executeQuery($query, ['id' => $id])
            ->fetchOne();
    }
}