Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Controller/CustomItem/SaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function saveAction(int $objectId, ?int $itemId = null): Response
$customItem = $this->customItemModel->save($customItem->getChildCustomItem());
}

if($customItem->hasBeenUpdated()){
if ($customItem->hasBeenUpdated()) {
$message = 'custom.item.notice.merged';
}

Expand Down
11 changes: 10 additions & 1 deletion DataPersister/CustomItemDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ public function __construct(CustomItemModel $customItemModel)
$this->customItemModel = $customItemModel;
}

/**
* @param $data mixed
*/
public function supports($data): bool
{
return $data instanceof CustomItem;
}

/**
* @param $data mixed
*/
public function persist($data)
{
\assert($data instanceof CustomItem);
Expand All @@ -29,10 +35,13 @@ public function persist($data)
return $data;
}

/**
* @param $data mixed
*/
public function remove($data)
{
\assert($data instanceof CustomItem);

$this->customItemModel->delete($data);
}
}
}
3 changes: 1 addition & 2 deletions Entity/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,10 @@ class CustomField extends FormEntity implements UniqueEntityInterface, UuidInter
private $showInContactDetailList = true;

/**
* @var bool
* @Groups({"custom_field:read", "custom_field:write", "custom_object:read", "custom_object:write"})
*/
private bool $isUniqueIdentifier = false;

private bool $wasChangeIsUniqueIdentifier = false;

public function __construct()
Expand Down
3 changes: 1 addition & 2 deletions Entity/CustomObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,10 @@ function (CustomField $customField) {
public function getUniqueIdentifierFields(): ?ArrayCollection
{
return $this->customFields->filter(
static fn(CustomField $customField) => $customField->getIsUniqueIdentifier()
static fn (CustomField $customField) => $customField->getIsUniqueIdentifier()
);
}


/**
* Called when the custom fields are loaded from the database.
*/
Expand Down
4 changes: 2 additions & 2 deletions Model/CustomItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public function save(CustomItem $customItem, bool $dryRun = false): CustomItem

// We need to re-attach the entity to the entity manager so that it can be saved by the rest of the code.
$customFieldValues = $customItem->getCustomFieldValues();
$hasBeenUpdated = $customItem->hasBeenUpdated();
$hasBeenInserted = $customItem->hasBeenInserted();
$hasBeenUpdated = $customItem->hasBeenUpdated();
$hasBeenInserted = $customItem->hasBeenInserted();
$customItem = $this->fetchEntity($customItem->getId());

$customItem->setHasBeenUpdated($hasBeenUpdated);
Expand Down
1 change: 1 addition & 0 deletions Repository/CustomItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function getItemCount(int $customObjectId): int
$queryBuilder->where(CustomItem::TABLE_ALIAS.'.customObject = :customObjectId');
$queryBuilder->setParameter('customObjectId', $customObjectId);
$queryBuilder->setMaxResults(1);

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ protected function updateEntity(string $createdId, array $payload): Response
return $this->requestEntity('PUT', $createdId, $payload);
}

/**
* @param array<int, mixed> $payload
*/
protected function patchEntity(string $createdId, array $payload): Response
{
return $this->requestEntity('PATCH', $createdId, $payload);
Expand All @@ -90,7 +93,7 @@ protected function deleteEntity(string $createdId): Response

protected function requestEntity(string $method, string $path, $payload = null): Response
{
$server = ['CONTENT_TYPE' => $method == 'PATCH' ? 'application/merge-patch+json' : 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'];
$server = ['CONTENT_TYPE' => 'PATCH' == $method ? 'application/merge-patch+json' : 'application/ld+json', 'HTTP_ACCEPT' => 'application/ld+json'];
$this->client->request($method, $path, [], [], $server, $payload ? json_encode($payload) : null);

return $this->client->getResponse();
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/ApiPlatform/CustomFieldFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function getCreatePayload(string $customObject): array
'string1',
'string2',
],
'isPublished' => false,
'isPublished' => false,
'isUniqueIdentifier' => false,
];
}
Expand All @@ -102,7 +102,7 @@ private function getEditPayload(): array
'string1',
'string2',
],
'isPublished' => true,
'isPublished' => true,
'isUniqueIdentifier' => true,
];
}
Expand Down
62 changes: 42 additions & 20 deletions Tests/Functional/ApiPlatform/CustomItemFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function setUp(): void

/**
* @dataProvider getCustomItemsDataProvider
* @param array<int, string> $permissions
*/
public function testGetCustomItem(array $permissions, int $expectedResponse): void
{
Expand All @@ -50,23 +51,27 @@ public function testGetCustomItem(array $permissions, int $expectedResponse): vo
$this->assertSuccessContent($json, $customItem);
}

/**
* @return iterable<int, mixed>
*/
public function getCustomItemsDataProvider(): iterable
{
yield [['viewother'], Response::HTTP_OK,];
yield [['viewother'], Response::HTTP_OK];

yield [['editother'], Response::HTTP_OK,];
yield [['editother'], Response::HTTP_OK];

yield [['deleteother'], Response::HTTP_OK,];
yield [['deleteother'], Response::HTTP_OK];

yield [['publishother'], Response::HTTP_OK,];
yield [['publishother'], Response::HTTP_OK];

yield [[], Response::HTTP_FORBIDDEN,];
yield [[], Response::HTTP_FORBIDDEN];

yield [['viewown', 'editown', 'create', 'deleteown', 'publishown'], Response::HTTP_FORBIDDEN,];
yield [['viewown', 'editown', 'create', 'deleteown', 'publishown'], Response::HTTP_FORBIDDEN];
}

/**
* @dataProvider postCustomItemsDataProvider
* @param array<int, string>
*/
public function testPostCustomItem(array $permissions, int $expectedResponse): void
{
Expand Down Expand Up @@ -105,17 +110,21 @@ public function testPostCustomItem(array $permissions, int $expectedResponse): v
$this->assertSuccessContent($json, $customItem);
}

/**
* @return iterable<int, mixed>
*/
public function postCustomItemsDataProvider(): iterable
{
yield [['create'], Response::HTTP_CREATED,];
yield [['create'], Response::HTTP_CREATED];

yield [[], Response::HTTP_FORBIDDEN,];
yield [[], Response::HTTP_FORBIDDEN];

yield [['viewown', 'viewother', 'editown', 'editother', 'deleteown', 'deleteother', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN,];
yield [['viewown', 'viewother', 'editown', 'editother', 'deleteown', 'deleteother', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN];
}

/**
* @dataProvider putCustomItemsDataProvider
* @param array<int, string> $permissions
*/
public function testPutCustomItem(array $permissions, int $expectedResponse): void
{
Expand Down Expand Up @@ -145,19 +154,23 @@ public function testPutCustomItem(array $permissions, int $expectedResponse): vo
$this->assertSuccessContent($json, $customItem);
}

public function putCustomItemsDataProvider()
/**
* @return iterable<int, mixed>
*/
public function putCustomItemsDataProvider(): iterable
{
yield [['editother'], Response::HTTP_OK,];
yield [['editother'], Response::HTTP_OK];

yield [['deleteother'], Response::HTTP_OK,];
yield [['deleteother'], Response::HTTP_OK];

yield [[], Response::HTTP_FORBIDDEN,];
yield [[], Response::HTTP_FORBIDDEN];

yield [['viewown', 'viewother', 'editown', 'create', 'deleteown', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN,];
yield [['viewown', 'viewother', 'editown', 'create', 'deleteown', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN];
}

/**
* @dataProvider putCustomItemsDataProvider
* @param array<int, string> $permissions
*/
public function testPatchCustomItem(array $permissions, int $expectedResponse): void
{
Expand Down Expand Up @@ -189,6 +202,7 @@ public function testPatchCustomItem(array $permissions, int $expectedResponse):

/**
* @dataProvider deleteCustomItemsDataProvider
* @param array<int, string> $permissions
*/
public function testDeleteCustomItem(array $permissions, int $expectedResponse): void
{
Expand All @@ -211,14 +225,16 @@ public function testDeleteCustomItem(array $permissions, int $expectedResponse):
self::assertNull($customItem);
}

public function deleteCustomItemsDataProvider()
/**
* @return iterable<int, mixed>
*/
public function deleteCustomItemsDataProvider(): iterable
{
yield [['deleteother'], Response::HTTP_NO_CONTENT];

yield [['deleteother'], Response::HTTP_NO_CONTENT,];

yield [[], Response::HTTP_FORBIDDEN,];
yield [[], Response::HTTP_FORBIDDEN];

yield [['viewown', 'viewother', 'editown', 'create', 'deleteown', 'editother', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN,];
yield [['viewown', 'viewother', 'editown', 'create', 'deleteown', 'editother', 'publishown', 'publishother'], Response::HTTP_FORBIDDEN];
}

private function createCustomObject(): CustomObject
Expand All @@ -238,7 +254,7 @@ private function createCustomItem(array $permissions): CustomItem
$customObject = $this->createCustomObject();
$category = $this->createCategory();
$customField = $this->createCustomField($customObject);
$customItem = new CustomItem($customObject);
$customItem = new CustomItem($customObject);
$customItem->setName('Custom Item');
$customItem->setLanguage('en');
$customItem->setCategory($category);
Expand Down Expand Up @@ -286,6 +302,9 @@ private function createCustomField(CustomObject $customObject): CustomField
return $customField;
}

/**
* @param array<string, mixed> $json
*/
private function assertAccessForbiddenContent(array $json): void
{
self::assertEquals($json['@context'], '/api/v2/contexts/Error');
Expand All @@ -295,6 +314,9 @@ private function assertAccessForbiddenContent(array $json): void
self::assertCount(4, $json);
}

/**
* @param array<string, mixed> $json
*/
private function assertSuccessContent(array $json, CustomItem $customItem): void
{
self::assertEquals($json['@context'], '/api/v2/contexts/custom_items');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function createCustomItem(ContainerInterface $container, CustomObject $cu

/** @var CustomItemModel $customItemModel */
$customItemModel = $container->get('mautic.custom.model.item');

return $customItemModel->save($customItem);
}

Expand Down
5 changes: 2 additions & 3 deletions Tests/Unit/Form/Type/CustomFieldTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MauticPlugin\CustomObjectsBundle\Tests\Unit\Form\Type;

use Mautic\CoreBundle\Form\Type\FormButtonsType;
use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
use MauticPlugin\CustomObjectsBundle\Entity\CustomField;
use MauticPlugin\CustomObjectsBundle\Entity\CustomFieldFactory;
Expand Down Expand Up @@ -160,9 +159,9 @@ public function testBuildModalFormFields(): void
'label' => 'custom.field.is_unique_identifier.label',
'attr' => [
'data-toggle-button' => true,
'tooltip' => 'custom.field.help.is_unique_identifier'
'tooltip' => 'custom.field.help.is_unique_identifier',
],
]
],
]
);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Form/Validator/AllowUniqueIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public function testGetTargets(): void
{
$this->assertSame(AllowUniqueIdentifier::CLASS_CONSTRAINT, $this->constraint->getTargets());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use MauticPlugin\CustomObjectsBundle\Entity\CustomField;
use MauticPlugin\CustomObjectsBundle\Entity\CustomObject;
use MauticPlugin\CustomObjectsBundle\Form\Validator\Constraints\AllowUniqueIdentifier;
use MauticPlugin\CustomObjectsBundle\Form\Validator\Constraints\AllowUniqueIdentifierValidator;
use MauticPlugin\CustomObjectsBundle\Model\CustomFieldModel;
use MauticPlugin\CustomObjectsBundle\Model\CustomItemModel;
use MauticPlugin\CustomObjectsBundle\Repository\CustomItemRepository;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down