Add check on list type

[MAILPOET-4752]
This commit is contained in:
Jan Lysý
2022-10-31 15:06:58 +01:00
committed by Aschepikov
parent f231f3dc6d
commit ad46d05c6b
4 changed files with 40 additions and 8 deletions

View File

@ -4,7 +4,7 @@
## `array updateList(array $list)`
This method provides functionality for updating a list name or description.
This method provides functionality for updating a list name or description. Only lists of type 'default' are supported.
It returns the updated list. See [Get Lists](GetLists.md) for a list data structure description.
@ -29,10 +29,11 @@ An exception of base class `\Exception` can be thrown when something unexpected
Codes description:
| Code | Description |
| ---- | -------------------------------------------- |
| 5 | The list was not found by id |
| 14 | Missing list name |
| 15 | Trying to use a list that is already used |
| 18 | Missing list id |
| 19 | The list couldnt be updated in the database |
| Code | Description |
| ---- | ----------------------------------------------- |
| 5 | The list was not found by id |
| 14 | Missing list name |
| 15 | Trying to use a list name that is already used |
| 18 | Missing list id |
| 19 | The list couldnt be updated in the database |
| 23 | Only lists of the type 'default' can be updated |

View File

@ -23,4 +23,5 @@ class APIException extends \Exception {
const LIST_USED_IN_EMAIL = 20;
const LIST_USED_IN_FORM = 21;
const FAILED_TO_DELETE_LIST = 22;
const LIST_TYPE_IS_NOT_SUPPORTED = 23;
}

View File

@ -60,6 +60,9 @@ class Segments {
// secondly validation on list name
$this->validateSegmentName($data);
// update is supported only for default segment type
$this->validateSegmentType((string)$data['id']);
try {
$segment = $this->segmentsRepository->createOrUpdate(
$data['name'],
@ -160,6 +163,21 @@ class Segments {
}
}
private function validateSegmentType(string $segmentId): void {
$segment = $this->segmentsRepository->findOneById($segmentId);
if ($segment && $segment->getType() !== SegmentEntity::TYPE_DEFAULT) {
throw new APIException(
str_replace(
'%1$s',
"'" . $segment->getType() . "'",
// translators: %1$s is an invalid segment type.
__('List of the type %1$s is not supported for this action.', 'mailpoet')
),
APIException::LIST_TYPE_IS_NOT_SUPPORTED
);
}
}
/**
* @param SegmentEntity $segment
* @return array

View File

@ -168,6 +168,18 @@ class SegmentsTest extends \MailPoetTest {
expect($result['description'])->equals($data['description']);
}
public function testItDoesNotAllowUpdateWPSegment(): void {
$wpSegment = $this->segmentsRepository->getWPUsersSegment();
$this->assertInstanceOf(SegmentEntity::class, $wpSegment);
try {
$this->getApi()->updateList(['id' => $wpSegment->getId(), 'name' => 'Test']);
$this->fail('WP list cannot be updated.');
} catch (\Exception $e) {
expect($e->getMessage())->equals('List of the type \'' . $wpSegment->getType() . '\' is not supported for this action.');
expect($e->getCode())->equals(APIException::LIST_TYPE_IS_NOT_SUPPORTED);
}
}
public function testItRequiresIdToDeleteList(): void {
try {
$this->getApi()->deleteList('');