Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\PageBundle\Tests\Entity;
|
||||
|
||||
use Mautic\CoreBundle\Entity\IpAddress;
|
||||
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\PageBundle\Entity\Hit;
|
||||
use Mautic\PageBundle\Entity\HitRepository;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class HitRepositoryTest extends MauticMysqlTestCase
|
||||
{
|
||||
private HitRepository $hitRepository;
|
||||
|
||||
private IpAddress $ipAddress;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->hitRepository = $this->em->getRepository(Hit::class);
|
||||
}
|
||||
|
||||
public function testGetLatestHitDateByLead(): void
|
||||
{
|
||||
Assert::assertNull($this->hitRepository->getLatestHitDateByLead(1, 'someId'));
|
||||
Assert::assertNull($this->hitRepository->getLatestHitDateByLead(1));
|
||||
|
||||
$leadOne = $this->createLead();
|
||||
$leadTwo = $this->createLead();
|
||||
$this->createHit($leadOne, $dateOne = new \DateTime('-10 second'), 'one-first');
|
||||
$this->createHit($leadOne, new \DateTime('-20 second'), 'one-first');
|
||||
$this->createHit($leadOne, $dateThree = new \DateTime('-5 second'), 'one-second');
|
||||
$this->createHit($leadTwo, new \DateTime('-50 second'), 'two-first');
|
||||
$this->createHit($leadTwo, $dateFive = new \DateTime('-40 second'), 'two-first');
|
||||
$this->em->flush();
|
||||
|
||||
$this->assertHitDate($dateOne, $leadOne, 'one-first');
|
||||
$this->assertHitDate($dateThree, $leadOne, 'one-second');
|
||||
$this->assertHitDate($dateFive, $leadTwo, 'two-first');
|
||||
$this->assertHitDate($dateThree, $leadOne, null);
|
||||
$this->assertHitDate($dateFive, $leadTwo, null);
|
||||
|
||||
Assert::assertNull($this->hitRepository->getLatestHitDateByLead((int) $leadOne->getId(), 'two-first'));
|
||||
Assert::assertNull($this->hitRepository->getLatestHitDateByLead((int) $leadTwo->getId(), 'one-second'));
|
||||
}
|
||||
|
||||
private function createHit(Lead $lead, \DateTime $dateHit, string $trackingId): void
|
||||
{
|
||||
$hit = new Hit();
|
||||
$hit->setLead($lead);
|
||||
$hit->setIpAddress($this->getIpAddress());
|
||||
$hit->setDateHit($dateHit);
|
||||
$hit->setTrackingId($trackingId);
|
||||
$hit->setCode(200);
|
||||
$this->em->persist($hit);
|
||||
}
|
||||
|
||||
private function createLead(): Lead
|
||||
{
|
||||
$lead = new Lead();
|
||||
$this->em->persist($lead);
|
||||
|
||||
return $lead;
|
||||
}
|
||||
|
||||
private function getIpAddress(): IpAddress
|
||||
{
|
||||
if (!isset($this->ipAddress)) {
|
||||
$this->ipAddress = new IpAddress('127.0.0.1');
|
||||
}
|
||||
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
private function assertHitDate(\DateTime $expectedHitDate, Lead $lead, ?string $trackingId): void
|
||||
{
|
||||
$hitDate = $this->hitRepository->getLatestHitDateByLead((int) $lead->getId(), $trackingId);
|
||||
|
||||
Assert::assertInstanceOf(\DateTime::class, $hitDate);
|
||||
Assert::assertSame($expectedHitDate->getTimestamp(), $hitDate->getTimestamp());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\PageBundle\Tests\Entity;
|
||||
|
||||
use Mautic\PageBundle\Entity\Hit;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class HitTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('setUrlTitle')]
|
||||
public function testSetUrlTitle(string $urlTitle, int $expected): void
|
||||
{
|
||||
$hit = new Hit();
|
||||
$hit->setUrlTitle($urlTitle);
|
||||
|
||||
Assert::assertEquals($expected, mb_strlen($hit->getUrlTitle()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array<int,int|string>>
|
||||
*/
|
||||
public static function setUrlTitle(): iterable
|
||||
{
|
||||
yield ['custom', 6];
|
||||
yield ['Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars Title longer than 191 chars ', 191];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\PageBundle\Tests\Entity;
|
||||
|
||||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
use Mautic\CoreBundle\Test\Doctrine\RepositoryConfiguratorTrait;
|
||||
use Mautic\PageBundle\Entity\Page;
|
||||
use Mautic\PageBundle\Entity\PageRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class PageRepositoryTest extends TestCase
|
||||
{
|
||||
use RepositoryConfiguratorTrait;
|
||||
|
||||
private function getRepository(): PageRepository
|
||||
{
|
||||
$repository = $this->configureRepository(Page::class);
|
||||
$this->connection->method('createQueryBuilder')->willReturnCallback(fn () => new QueryBuilder($this->connection));
|
||||
|
||||
$translator = $this->createMock(TranslatorInterface::class);
|
||||
$translator->method('trans')->willReturnCallback(fn ($id) => match ($id) {
|
||||
'mautic.page.searchcommand.isexpired' => 'is:expired',
|
||||
'mautic.page.searchcommand.ispending' => 'is:pending',
|
||||
default => $id,
|
||||
});
|
||||
$repository->setTranslator($translator);
|
||||
|
||||
return $repository;
|
||||
}
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('dataExpirationFilters')]
|
||||
public function testAddSearchCommandWhereClauseHandlesExpirationFilters(string $command, string $expected): void
|
||||
{
|
||||
$repository = $this->getRepository();
|
||||
$qb = $this->connection->createQueryBuilder();
|
||||
$filter = (object) ['command' => $command, 'string' => '', 'not' => false, 'strict' => false];
|
||||
|
||||
$method = new \ReflectionMethod(PageRepository::class, 'addSearchCommandWhereClause');
|
||||
$method->setAccessible(true);
|
||||
|
||||
[$expr, $params] = $method->invoke($repository, $qb, $filter);
|
||||
|
||||
self::assertSame($expected, (string) $expr);
|
||||
self::assertSame(['par1' => true], $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array{0: string, 1: string}>
|
||||
*/
|
||||
public static function dataExpirationFilters(): iterable
|
||||
{
|
||||
yield ['is:expired', "(p.isPublished = :par1 AND p.publishDown IS NOT NULL AND p.publishDown <> '' AND p.publishDown < CURRENT_TIMESTAMP())"];
|
||||
yield ['is:pending', "(p.isPublished = :par1 AND p.publishUp IS NOT NULL AND p.publishUp <> '' AND p.publishUp > CURRENT_TIMESTAMP())"];
|
||||
}
|
||||
|
||||
public function testGetSearchCommandsContainsExpirationFilters(): void
|
||||
{
|
||||
$repository = $this->getRepository();
|
||||
$commands = $repository->getSearchCommands();
|
||||
self::assertContains('mautic.page.searchcommand.isexpired', $commands);
|
||||
self::assertContains('mautic.page.searchcommand.ispending', $commands);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\PageBundle\Tests\Entity;
|
||||
|
||||
use Mautic\PageBundle\Entity\Page;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class PageTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('setIsPreferenceCenterDataProvider')]
|
||||
public function testSetIsPreferenceCenter($value, $expected, array $changes): void
|
||||
{
|
||||
$page = new Page();
|
||||
$page->setIsPreferenceCenter($value);
|
||||
|
||||
Assert::assertSame($expected, $page->getIsPreferenceCenter());
|
||||
Assert::assertSame($changes, $page->getChanges());
|
||||
}
|
||||
|
||||
public static function setIsPreferenceCenterDataProvider(): iterable
|
||||
{
|
||||
yield [null, null, []];
|
||||
yield [true, true, ['isPreferenceCenter' => [null, true]]];
|
||||
yield [false, false, ['isPreferenceCenter' => [null, false]]];
|
||||
yield ['', false, ['isPreferenceCenter' => [null, false]]];
|
||||
yield [0, false, ['isPreferenceCenter' => [null, false]]];
|
||||
yield ['string', true, ['isPreferenceCenter' => [null, true]]];
|
||||
}
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('setNoIndexDataProvider')]
|
||||
public function testSetNoIndex($value, $expected, array $changes): void
|
||||
{
|
||||
$page = new Page();
|
||||
$page->setNoIndex($value);
|
||||
|
||||
Assert::assertSame($expected, $page->getNoIndex());
|
||||
Assert::assertSame($changes, $page->getChanges());
|
||||
}
|
||||
|
||||
public static function setNoIndexDataProvider(): iterable
|
||||
{
|
||||
yield [null, null, []];
|
||||
yield [true, true, ['noIndex' => [null, true]]];
|
||||
yield [false, false, ['noIndex' => [null, false]]];
|
||||
yield ['', false, ['noIndex' => [null, false]]];
|
||||
yield [0, false, ['noIndex' => [null, false]]];
|
||||
yield ['string', true, ['noIndex' => [null, true]]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test setHeadScript and getHeadScript.
|
||||
*/
|
||||
public function testSetHeadScript(): void
|
||||
{
|
||||
$script = '<script>console.log("test")';
|
||||
$page = new Page();
|
||||
$page->setHeadScript($script);
|
||||
|
||||
$this->assertEquals($script, $page->getHeadScript());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test setFooterScript and getFooterScript.
|
||||
*/
|
||||
public function testSetFooterScript(): void
|
||||
{
|
||||
$script = '<script>console.log("test")';
|
||||
$page = new Page();
|
||||
$page->setFooterScript($script);
|
||||
|
||||
$this->assertEquals($script, $page->getFooterScript());
|
||||
}
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('setIsDuplicateDataProvider')]
|
||||
public function testIsDuplicate(bool $isDuplicate): void
|
||||
{
|
||||
$page = new Page();
|
||||
$page->setIsDuplicate($isDuplicate);
|
||||
Assert::assertIsBool($page->isDuplicate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<array{bool}>
|
||||
*/
|
||||
public static function setIsDuplicateDataProvider(): iterable
|
||||
{
|
||||
yield [true];
|
||||
yield [false];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\PageBundle\Tests\Entity;
|
||||
|
||||
use Mautic\PageBundle\Entity\Redirect;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RedirectTest extends TestCase
|
||||
{
|
||||
public function testGetUrlRemovesWhitespace(): void
|
||||
{
|
||||
$redirect = new Redirect();
|
||||
$reflected = new \ReflectionClass(Redirect::class);
|
||||
$property = $reflected->getProperty('url');
|
||||
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($redirect, 'https://example.com '); // trailing whitespace
|
||||
|
||||
Assert::assertSame('https://example.com', $redirect->getUrl());
|
||||
}
|
||||
|
||||
public function testSetUrlRemovesWhitespace(): void
|
||||
{
|
||||
$redirect = new Redirect();
|
||||
$reflected = new \ReflectionClass(Redirect::class);
|
||||
$property = $reflected->getProperty('url');
|
||||
|
||||
$property->setAccessible(true);
|
||||
$redirect->setUrl('https://example.com '); // trailing whitespace
|
||||
|
||||
Assert::assertSame('https://example.com', $property->getValue($redirect));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user