Add check on list type
[MAILPOET-4752]
This commit is contained in:
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
## `array updateList(array $list)`
|
## `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.
|
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:
|
Codes description:
|
||||||
|
|
||||||
| Code | Description |
|
| Code | Description |
|
||||||
| ---- | -------------------------------------------- |
|
| ---- | ----------------------------------------------- |
|
||||||
| 5 | The list was not found by id |
|
| 5 | The list was not found by id |
|
||||||
| 14 | Missing list name |
|
| 14 | Missing list name |
|
||||||
| 15 | Trying to use a list that is already used |
|
| 15 | Trying to use a list name that is already used |
|
||||||
| 18 | Missing list id |
|
| 18 | Missing list id |
|
||||||
| 19 | The list couldn’t be updated in the database |
|
| 19 | The list couldn’t be updated in the database |
|
||||||
|
| 23 | Only lists of the type 'default' can be updated |
|
||||||
|
@ -23,4 +23,5 @@ class APIException extends \Exception {
|
|||||||
const LIST_USED_IN_EMAIL = 20;
|
const LIST_USED_IN_EMAIL = 20;
|
||||||
const LIST_USED_IN_FORM = 21;
|
const LIST_USED_IN_FORM = 21;
|
||||||
const FAILED_TO_DELETE_LIST = 22;
|
const FAILED_TO_DELETE_LIST = 22;
|
||||||
|
const LIST_TYPE_IS_NOT_SUPPORTED = 23;
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,9 @@ class Segments {
|
|||||||
// secondly validation on list name
|
// secondly validation on list name
|
||||||
$this->validateSegmentName($data);
|
$this->validateSegmentName($data);
|
||||||
|
|
||||||
|
// update is supported only for default segment type
|
||||||
|
$this->validateSegmentType((string)$data['id']);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$segment = $this->segmentsRepository->createOrUpdate(
|
$segment = $this->segmentsRepository->createOrUpdate(
|
||||||
$data['name'],
|
$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
|
* @param SegmentEntity $segment
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -168,6 +168,18 @@ class SegmentsTest extends \MailPoetTest {
|
|||||||
expect($result['description'])->equals($data['description']);
|
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 {
|
public function testItRequiresIdToDeleteList(): void {
|
||||||
try {
|
try {
|
||||||
$this->getApi()->deleteList('');
|
$this->getApi()->deleteList('');
|
||||||
|
Reference in New Issue
Block a user