diff --git a/doc/api_methods/UpdateList.md b/doc/api_methods/UpdateList.md index 119347dc66..2010933cde 100644 --- a/doc/api_methods/UpdateList.md +++ b/doc/api_methods/UpdateList.md @@ -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 couldn’t 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 couldn’t be updated in the database | +| 23 | Only lists of the type 'default' can be updated | diff --git a/mailpoet/lib/API/MP/v1/APIException.php b/mailpoet/lib/API/MP/v1/APIException.php index 7f51e2c659..90b16e4500 100644 --- a/mailpoet/lib/API/MP/v1/APIException.php +++ b/mailpoet/lib/API/MP/v1/APIException.php @@ -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; } diff --git a/mailpoet/lib/API/MP/v1/Segments.php b/mailpoet/lib/API/MP/v1/Segments.php index c5f16bc715..8c2aaafd34 100644 --- a/mailpoet/lib/API/MP/v1/Segments.php +++ b/mailpoet/lib/API/MP/v1/Segments.php @@ -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 diff --git a/mailpoet/tests/integration/API/MP/SegmentsTest.php b/mailpoet/tests/integration/API/MP/SegmentsTest.php index 0d715761a3..f551737047 100644 --- a/mailpoet/tests/integration/API/MP/SegmentsTest.php +++ b/mailpoet/tests/integration/API/MP/SegmentsTest.php @@ -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('');