Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Controller\Api;
|
||||
|
||||
use Mautic\ChannelBundle\Entity\Channel;
|
||||
use Mautic\ChannelBundle\Entity\Message;
|
||||
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
final class MessageApiControllerTest extends MauticMysqlTestCase
|
||||
{
|
||||
public function testCreateMessage(): void
|
||||
{
|
||||
$payloadJson = <<<'JSON'
|
||||
{
|
||||
"name": "API message",
|
||||
"description": "Marketing message created via API functional test",
|
||||
"channels": {
|
||||
"email": {
|
||||
"channel": "email",
|
||||
"channelId": 12,
|
||||
"isEnabled": true
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON;
|
||||
|
||||
$payloadArray = json_decode($payloadJson, true);
|
||||
|
||||
$this->client->request('POST', '/api/messages/new', $payloadArray);
|
||||
$responseJson = $this->client->getResponse()->getContent();
|
||||
self::assertResponseStatusCodeSame(201, $responseJson);
|
||||
$this->assertMessagePayload($payloadArray, json_decode($responseJson, true)['message'], $responseJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $payload
|
||||
* @param mixed[] $expectedResponsePayload
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('patchProvider')]
|
||||
public function testEditMessageWithPatch(array $payload, array $expectedResponsePayload): void
|
||||
{
|
||||
$channel = new Channel();
|
||||
$channel->setChannel('email');
|
||||
$channel->setChannelId(12);
|
||||
$channel->setIsEnabled(true);
|
||||
|
||||
$message = new Message();
|
||||
$message->setName('API message');
|
||||
$message->addChannel($channel);
|
||||
|
||||
$this->em->persist($channel);
|
||||
$this->em->persist($message);
|
||||
$this->em->flush();
|
||||
$this->em->detach($channel);
|
||||
$this->em->detach($message);
|
||||
|
||||
$patchPayload = ['id' => $message->getId()] + $payload;
|
||||
$this->client->request('PATCH', "/api/messages/{$message->getId()}/edit", $patchPayload);
|
||||
$responseJson = $this->client->getResponse()->getContent();
|
||||
self::assertResponseIsSuccessful($responseJson);
|
||||
$this->assertMessagePayload(
|
||||
['id' => $message->getId()] + $expectedResponsePayload,
|
||||
json_decode($responseJson, true)['message'],
|
||||
$responseJson
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: the ID is added to the payload automatically in the test.
|
||||
*
|
||||
* @return iterable<mixed[]>
|
||||
*/
|
||||
public static function patchProvider(): iterable
|
||||
{
|
||||
yield [
|
||||
[
|
||||
'name' => 'API message (updated)',
|
||||
],
|
||||
[
|
||||
'name' => 'API message (updated)',
|
||||
'description' => null,
|
||||
'channels' => [
|
||||
'email' => [
|
||||
'channel' => 'email',
|
||||
'channelId' => 12,
|
||||
'isEnabled' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
[
|
||||
'description' => 'Description (updated)',
|
||||
'channels' => [
|
||||
'email' => [
|
||||
'channel' => 'email',
|
||||
'channelId' => 13,
|
||||
'isEnabled' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'API message',
|
||||
'description' => 'Description (updated)',
|
||||
'channels' => [
|
||||
'email' => [
|
||||
'channel' => 'email',
|
||||
'channelId' => 13,
|
||||
'isEnabled' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testEditMessagesWithPatch(): void
|
||||
{
|
||||
$channel1 = new Channel();
|
||||
$channel1->setChannel('email');
|
||||
$channel1->setChannelId(12);
|
||||
$channel1->setIsEnabled(true);
|
||||
|
||||
$message1 = new Message();
|
||||
$message1->setName('API message 1');
|
||||
$message1->addChannel($channel1);
|
||||
|
||||
$channel2 = new Channel();
|
||||
$channel2->setChannel('email');
|
||||
$channel2->setChannelId(13);
|
||||
$channel2->setIsEnabled(true);
|
||||
|
||||
$message2 = new Message();
|
||||
$message2->setName('API message 2');
|
||||
$message2->addChannel($channel2);
|
||||
|
||||
$this->em->persist($channel1);
|
||||
$this->em->persist($channel2);
|
||||
$this->em->persist($message1);
|
||||
$this->em->persist($message2);
|
||||
$this->em->flush();
|
||||
$this->em->detach($channel1);
|
||||
$this->em->detach($channel2);
|
||||
$this->em->detach($message1);
|
||||
$this->em->detach($message2);
|
||||
|
||||
$patchPayload = [
|
||||
['id' => $message1->getId(), 'name' => 'API message 1 (updated)'],
|
||||
['id' => $message2->getId(), 'channels' => ['email' => ['channelId' => 14, 'isEnabled' => false]]],
|
||||
];
|
||||
$this->client->request('PATCH', '/api/messages/batch/edit', $patchPayload);
|
||||
$responseJson = $this->client->getResponse()->getContent();
|
||||
self::assertResponseIsSuccessful($responseJson);
|
||||
$responseArray = json_decode($responseJson, true);
|
||||
$this->assertMessagePayload(
|
||||
[
|
||||
'id' => $message1->getId(),
|
||||
'name' => 'API message 1 (updated)',
|
||||
'description' => null,
|
||||
'channels' => [
|
||||
'email' => [
|
||||
'channel' => 'email',
|
||||
'channelId' => 12,
|
||||
'isEnabled' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
$responseArray['messages'][0],
|
||||
$responseJson
|
||||
);
|
||||
$this->assertMessagePayload(
|
||||
[
|
||||
'id' => $message2->getId(),
|
||||
'name' => 'API message 2',
|
||||
'description' => null,
|
||||
'channels' => [
|
||||
'email' => [
|
||||
'channel' => 'email',
|
||||
'channelId' => 14,
|
||||
'isEnabled' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
$responseArray['messages'][1],
|
||||
$responseJson
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $expectedPayload
|
||||
* @param mixed[] $actualPayload
|
||||
*/
|
||||
private function assertMessagePayload(array $expectedPayload, array $actualPayload, string $deliveredPayloadJson): void
|
||||
{
|
||||
Assert::assertSame($expectedPayload['name'], $actualPayload['name'], $deliveredPayloadJson);
|
||||
Assert::assertSame($expectedPayload['description'], $actualPayload['description'], $deliveredPayloadJson);
|
||||
Assert::assertCount(count($expectedPayload['channels']), $actualPayload['channels'], $deliveredPayloadJson);
|
||||
Assert::assertGreaterThan(0, $actualPayload['id'], $deliveredPayloadJson);
|
||||
|
||||
Assert::assertSame($expectedPayload['channels']['email']['channel'], $actualPayload['channels'][0]['channel'], $deliveredPayloadJson);
|
||||
Assert::assertSame($expectedPayload['channels']['email']['channelId'], $actualPayload['channels'][0]['channelId'], $deliveredPayloadJson);
|
||||
Assert::assertSame($expectedPayload['channels']['email']['isEnabled'], $actualPayload['channels'][0]['isEnabled'], $deliveredPayloadJson);
|
||||
Assert::assertGreaterThan(0, $actualPayload['channels'][0]['id'], $deliveredPayloadJson);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Controller;
|
||||
|
||||
use Mautic\ChannelBundle\Entity\Message;
|
||||
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
|
||||
use Mautic\ProjectBundle\Entity\Project;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class MessageControllerFunctionalTest extends MauticMysqlTestCase
|
||||
{
|
||||
public function testFormWithProject(): void
|
||||
{
|
||||
$message = new Message();
|
||||
$message->setName('Test message');
|
||||
$this->em->persist($message);
|
||||
|
||||
$project = new Project();
|
||||
$project->setName('Test Project');
|
||||
$this->em->persist($project);
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
|
||||
$crawler = $this->client->request('GET', '/s/messages/edit/'.$message->getId());
|
||||
$form = $crawler->selectButton('Save')->form();
|
||||
$form['message[projects]']->setValue((string) $project->getId());
|
||||
|
||||
$this->client->submit($form);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$savedMessage = $this->em->find(Message::class, $message->getId());
|
||||
Assert::assertSame($project->getId(), $savedMessage->getProjects()->first()->getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Controller;
|
||||
|
||||
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
final class MessageControllerTest extends MauticMysqlTestCase
|
||||
{
|
||||
public function testMMUiWorkflow(): void
|
||||
{
|
||||
$crawler = $this->client->request(Request::METHOD_GET, '/s/messages/new');
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$form = $crawler->selectButton('Save & Close')->form([
|
||||
'message[name]' => 'Test message',
|
||||
'message[description]' => 'Test message description',
|
||||
]);
|
||||
|
||||
$this->client->submit($form);
|
||||
$this->assertResponseIsSuccessful();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Controller;
|
||||
|
||||
use Mautic\ChannelBundle\Entity\Channel;
|
||||
use Mautic\ChannelBundle\Entity\Message;
|
||||
use Mautic\ProjectBundle\Tests\Functional\AbstractProjectSearchTestCase;
|
||||
|
||||
final class MessageProjectSearchFunctionalTest extends AbstractProjectSearchTestCase
|
||||
{
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('searchDataProvider')]
|
||||
public function testProjectSearch(string $searchTerm, array $expectedEntities, array $unexpectedEntities): void
|
||||
{
|
||||
$projectOne = $this->createProject('Project One');
|
||||
$projectTwo = $this->createProject('Project Two');
|
||||
$projectThree = $this->createProject('Project Three');
|
||||
|
||||
$messageAlpha = $this->createMessage('Message Alpha');
|
||||
$messageBeta = $this->createMessage('Message Beta');
|
||||
$this->createMessage('Message Gamma');
|
||||
$this->createMessage('Message Delta');
|
||||
|
||||
$messageAlpha->addProject($projectOne);
|
||||
$messageAlpha->addProject($projectTwo);
|
||||
$messageBeta->addProject($projectTwo);
|
||||
$messageBeta->addProject($projectThree);
|
||||
|
||||
$this->em->flush();
|
||||
$this->em->clear();
|
||||
|
||||
$this->searchAndAssert($searchTerm, $expectedEntities, $unexpectedEntities, ['/api/messages', '/s/messages']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Generator<string, array{searchTerm: string, expectedEntities: array<string>, unexpectedEntities: array<string>}>
|
||||
*/
|
||||
public static function searchDataProvider(): \Generator
|
||||
{
|
||||
yield 'search by one project' => [
|
||||
'searchTerm' => 'project:"Project Two"',
|
||||
'expectedEntities' => ['Message Alpha', 'Message Beta'],
|
||||
'unexpectedEntities' => ['Message Gamma', 'Message Delta'],
|
||||
];
|
||||
|
||||
yield 'search by one project AND message name' => [
|
||||
'searchTerm' => 'project:"Project Two" AND Beta',
|
||||
'expectedEntities' => ['Message Beta'],
|
||||
'unexpectedEntities' => ['Message Alpha', 'Message Gamma', 'Message Delta'],
|
||||
];
|
||||
|
||||
yield 'search by one project OR message name' => [
|
||||
'searchTerm' => 'project:"Project Two" OR Gamma',
|
||||
'expectedEntities' => ['Message Alpha', 'Message Beta', 'Message Gamma'],
|
||||
'unexpectedEntities' => ['Message Delta'],
|
||||
];
|
||||
|
||||
yield 'search by NOT one project' => [
|
||||
'searchTerm' => '!project:"Project Two"',
|
||||
'expectedEntities' => ['Message Gamma', 'Message Delta'],
|
||||
'unexpectedEntities' => ['Message Alpha', 'Message Beta'],
|
||||
];
|
||||
|
||||
yield 'search by two projects with AND' => [
|
||||
'searchTerm' => 'project:"Project Two" AND project:"Project Three"',
|
||||
'expectedEntities' => ['Message Beta'],
|
||||
'unexpectedEntities' => ['Message Alpha', 'Message Gamma', 'Message Delta'],
|
||||
];
|
||||
|
||||
yield 'search by two projects with NOT AND' => [
|
||||
'searchTerm' => '!project:"Project Two" AND !project:"Project Three"',
|
||||
'expectedEntities' => ['Message Gamma', 'Message Delta'],
|
||||
'unexpectedEntities' => ['Message Alpha', 'Message Beta'],
|
||||
];
|
||||
|
||||
yield 'search by two projects with OR' => [
|
||||
'searchTerm' => 'project:"Project Two" OR project:"Project Three"',
|
||||
'expectedEntities' => ['Message Alpha', 'Message Beta'],
|
||||
'unexpectedEntities' => ['Message Gamma', 'Message Delta'],
|
||||
];
|
||||
|
||||
yield 'search by two projects with NOT OR' => [
|
||||
'searchTerm' => '!project:"Project Two" OR !project:"Project Three"',
|
||||
'expectedEntities' => ['Message Alpha', 'Message Gamma', 'Message Delta'],
|
||||
'unexpectedEntities' => ['Message Beta'],
|
||||
];
|
||||
}
|
||||
|
||||
private function createMessage(string $name): Message
|
||||
{
|
||||
$message = new Message();
|
||||
$message->setName($name);
|
||||
$message->addChannel((new Channel())
|
||||
->setChannel('email')
|
||||
->setMessage($message));
|
||||
$this->em->persist($message);
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user