Refactor dynamic segments bulk actions endpoint

[MAILPOET-3177]
This commit is contained in:
Rostislav Wolny
2021-03-04 16:59:16 +01:00
committed by Veljko V
parent 2db223daf7
commit 6bc557ecb0
2 changed files with 58 additions and 90 deletions

View File

@ -9,12 +9,12 @@ use MailPoet\API\JSON\Response;
use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder; use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder;
use MailPoet\Config\AccessControl; use MailPoet\Config\AccessControl;
use MailPoet\Entities\SegmentEntity; use MailPoet\Entities\SegmentEntity;
use MailPoet\Listing\BulkActionController;
use MailPoet\Listing\Handler; use MailPoet\Listing\Handler;
use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository; use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository;
use MailPoet\Segments\DynamicSegments\Exceptions\InvalidFilterException; use MailPoet\Segments\DynamicSegments\Exceptions\InvalidFilterException;
use MailPoet\Segments\DynamicSegments\SegmentSaveController; use MailPoet\Segments\DynamicSegments\SegmentSaveController;
use MailPoet\Segments\SegmentsRepository; use MailPoet\Segments\SegmentsRepository;
use MailPoet\UnexpectedValueException;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
class DynamicSegments extends APIEndpoint { class DynamicSegments extends APIEndpoint {
@ -23,9 +23,6 @@ class DynamicSegments extends APIEndpoint {
'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS, 'global' => AccessControl::PERMISSION_MANAGE_SEGMENTS,
]; ];
/** @var BulkActionController */
private $bulkAction;
/** @var Handler */ /** @var Handler */
private $listingHandler; private $listingHandler;
@ -42,14 +39,12 @@ class DynamicSegments extends APIEndpoint {
private $saveController; private $saveController;
public function __construct( public function __construct(
BulkActionController $bulkAction,
Handler $handler, Handler $handler,
DynamicSegmentsListingRepository $dynamicSegmentsListingRepository, DynamicSegmentsListingRepository $dynamicSegmentsListingRepository,
DynamicSegmentsResponseBuilder $segmentsResponseBuilder, DynamicSegmentsResponseBuilder $segmentsResponseBuilder,
SegmentsRepository $segmentsRepository, SegmentsRepository $segmentsRepository,
SegmentSaveController $saveController SegmentSaveController $saveController
) { ) {
$this->bulkAction = $bulkAction;
$this->listingHandler = $handler; $this->listingHandler = $handler;
$this->dynamicSegmentsListingRepository = $dynamicSegmentsListingRepository; $this->dynamicSegmentsListingRepository = $dynamicSegmentsListingRepository;
$this->segmentsResponseBuilder = $segmentsResponseBuilder; $this->segmentsResponseBuilder = $segmentsResponseBuilder;
@ -188,14 +183,19 @@ class DynamicSegments extends APIEndpoint {
} }
public function bulkAction($data = []) { public function bulkAction($data = []) {
try { $definition = $this->listingHandler->getListingDefinition($data['listing']);
$meta = $this->bulkAction->apply('\MailPoet\Models\DynamicSegment', $data); $ids = $this->dynamicSegmentsListingRepository->getActionableIds($definition);
return $this->successResponse(null, $meta); if ($data['action'] === 'trash') {
} catch (\Exception $e) { $count = $this->segmentsRepository->bulkTrash($ids, SegmentEntity::TYPE_DYNAMIC);
return $this->errorResponse([ } elseif ($data['action'] === 'restore') {
$e->getCode() => $e->getMessage(), $count = $this->segmentsRepository->bulkRestore($ids, SegmentEntity::TYPE_DYNAMIC);
]); } elseif ($data['action'] === 'delete') {
$count = $this->segmentsRepository->bulkDelete($ids, SegmentEntity::TYPE_DYNAMIC);
} else {
throw UnexpectedValueException::create()
->withErrors([Error::BAD_REQUEST => "Invalid bulk action '{$data['action']}' provided."]);
} }
return $this->successResponse(null, ['count' => $count]);
} }
private function getSegment(array $data): ?SegmentEntity { private function getSegment(array $data): ?SegmentEntity {

View File

@ -2,18 +2,10 @@
namespace MailPoet\API\JSON\v1; namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder;
use MailPoet\DI\ContainerWrapper; use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\DynamicSegmentFilterData; use MailPoet\Entities\DynamicSegmentFilterData;
use MailPoet\Entities\DynamicSegmentFilterEntity; use MailPoet\Entities\DynamicSegmentFilterEntity;
use MailPoet\Entities\SegmentEntity; use MailPoet\Entities\SegmentEntity;
use MailPoet\Listing\BulkActionController;
use MailPoet\Listing\Handler;
use MailPoet\Models\DynamicSegment;
use MailPoet\Models\DynamicSegmentFilter;
use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository;
use MailPoet\Segments\DynamicSegments\SegmentSaveController;
use MailPoet\Segments\SegmentsRepository;
class DynamicSegmentsTest extends \MailPoetTest { class DynamicSegmentsTest extends \MailPoetTest {
@ -22,55 +14,29 @@ class DynamicSegmentsTest extends \MailPoetTest {
const INVALID_DATA_RESPONSE_CODE = 400; const INVALID_DATA_RESPONSE_CODE = 400;
const SERVER_ERROR_RESPONSE_CODE = 409; const SERVER_ERROR_RESPONSE_CODE = 409;
/** @var BulkActionController */ /** @var DynamicSegments */
private $bulkAction; private $endpoint;
/** @var Handler */
private $listingHandler;
/** @var DynamicSegmentsListingRepository */
private $listingRepository;
/** @var DynamicSegmentsResponseBuilder */
private $responseBuilder;
/** @var SegmentsRepository */
private $segmentsRepository;
/** @var SegmentSaveController */
private $saveController;
public function _before() { public function _before() {
$this->bulkAction = ContainerWrapper::getInstance()->get(BulkActionController::class); $this->endpoint = ContainerWrapper::getInstance()->get(DynamicSegments::class);
$this->listingHandler = ContainerWrapper::getInstance()->get(Handler::class);
$this->listingRepository = ContainerWrapper::getInstance()->get(DynamicSegmentsListingRepository::class);
$this->responseBuilder = ContainerWrapper::getInstance()->get(DynamicSegmentsResponseBuilder::class);
$this->segmentsRepository = ContainerWrapper::getInstance()->get(SegmentsRepository::class);
$this->saveController = ContainerWrapper::getInstance()->get(SegmentSaveController::class);
} }
public function testGetReturnsResponse() { public function testGetReturnsResponse() {
$segment = $this->createDynamicSegmentEntity('s1', ''); $segment = $this->createDynamicSegmentEntity('s1', '');
$endpoint = new DynamicSegments( $response = $this->endpoint->get(['id' => $segment->getId()]);
$this->bulkAction,
$this->listingHandler,
$this->listingRepository,
$this->responseBuilder,
$this->segmentsRepository,
$this->saveController
);
$response = $endpoint->get(['id' => $segment->getId()]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse'); expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data['id'])->equals($segment->getId()); expect($response->data['id'])->equals($segment->getId());
} }
public function testGetReturnsError() { public function testGetReturnsError() {
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->get(['id' => 5]);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse'); expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE); expect($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE);
} }
public function testSaverSavesData() { public function testSaverSavesData() {
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->save([
$response = $endpoint->save([
'name' => 'Test dynamic', 'name' => 'Test dynamic',
'description' => 'description dynamic', 'description' => 'description dynamic',
'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE, 'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE,
@ -82,8 +48,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
} }
public function testSaverReturnsErrorOnInvalidFilterData() { public function testSaverReturnsErrorOnInvalidFilterData() {
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->save([
$response = $endpoint->save([
'name' => 'Test dynamic', 'name' => 'Test dynamic',
]); ]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse'); expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
@ -92,15 +57,14 @@ class DynamicSegmentsTest extends \MailPoetTest {
} }
public function testSaverReturnsErrorOnDuplicateRecord() { public function testSaverReturnsErrorOnDuplicateRecord() {
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController);
$data = [ $data = [
'name' => 'Test dynamic', 'name' => 'Test dynamic',
'description' => 'description dynamic', 'description' => 'description dynamic',
'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE, 'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE,
'wordpressRole' => 'editor', 'wordpressRole' => 'editor',
]; ];
$endpoint->save($data); $this->endpoint->save($data);
$response = $endpoint->save($data); $response = $this->endpoint->save($data);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse'); expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE); expect($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
} }
@ -108,8 +72,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
public function testItCanTrashASegment() { public function testItCanTrashASegment() {
$dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description'); $dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->trash(['id' => $dynamicSegment->getId()]);
$response = $endpoint->trash(['id' => $dynamicSegment->getId()]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data['name'])->equals($dynamicSegment->getName()); expect($response->data['name'])->equals($dynamicSegment->getName());
@ -123,8 +86,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
public function testItCanRestoreASegment() { public function testItCanRestoreASegment() {
$dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description'); $dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->restore(['id' => $dynamicSegment->getId()]);
$response = $endpoint->restore(['id' => $dynamicSegment->getId()]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data['name'])->equals($dynamicSegment->getName()); expect($response->data['name'])->equals($dynamicSegment->getName());
@ -140,8 +102,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
$dynamicSegmentFilter = $dynamicSegment->getDynamicFilters()->first(); $dynamicSegmentFilter = $dynamicSegment->getDynamicFilters()->first();
assert($dynamicSegmentFilter instanceof DynamicSegmentFilterEntity); assert($dynamicSegmentFilter instanceof DynamicSegmentFilterEntity);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $this->saveController); $response = $this->endpoint->delete(['id' => $dynamicSegment->getId()]);
$response = $endpoint->delete(['id' => $dynamicSegment->getId()]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data)->equals(null); expect($response->data)->equals(null);
@ -155,51 +116,58 @@ class DynamicSegmentsTest extends \MailPoetTest {
} }
public function testItCanBulkDeleteSegments() { public function testItCanBulkDeleteSegments() {
DynamicSegment::deleteMany(); $dynamicSegment1 = $this->createDynamicSegmentEntity('Test 1', 'description');
$dynamicSegment1 = DynamicSegment::createOrUpdate([ $dynamicSegment2 = $this->createDynamicSegmentEntity('Test 2', 'description');
'name' => 'Test 1',
'description' => 'description',
]);
$dynamicSegment2 = DynamicSegment::createOrUpdate([
'name' => 'Test 2',
'description' => 'description',
]);
$filter = DynamicSegmentFilter::createOrUpdate([
'segment_id' => $dynamicSegment1->id,
]);
$endpoint = new DynamicSegments( $response = $this->endpoint->bulkAction([
$this->bulkAction,
$this->listingHandler,
$this->listingRepository,
$this->responseBuilder,
$this->segmentsRepository,
$this->saveController
);
$response = $endpoint->bulkAction([
'action' => 'trash', 'action' => 'trash',
'listing' => ['group' => 'all'], 'listing' => ['group' => 'all'],
]); ]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->meta['count'])->equals(2); expect($response->meta['count'])->equals(2);
$response = $endpoint->bulkAction([ $this->entityManager->refresh($dynamicSegment1);
'action' => 'delete', $this->entityManager->refresh($dynamicSegment2);
expect($dynamicSegment1->getDeletedAt())->notNull();
expect($dynamicSegment2->getDeletedAt())->notNull();
$response = $this->endpoint->bulkAction([
'action' => 'restore',
'listing' => ['group' => 'trash'], 'listing' => ['group' => 'trash'],
]); ]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->meta['count'])->equals(2); expect($response->meta['count'])->equals(2);
$response = $endpoint->bulkAction([ $this->entityManager->refresh($dynamicSegment1);
$this->entityManager->refresh($dynamicSegment2);
expect($dynamicSegment1->getDeletedAt())->null();
expect($dynamicSegment2->getDeletedAt())->null();
$this->endpoint->bulkAction([
'action' => 'trash',
'listing' => ['group' => 'all'],
]);
$response = $this->endpoint->bulkAction([
'action' => 'delete',
'listing' => ['group' => 'trash'],
]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->meta['count'])->equals(2);
// Second delete doesn't delete anything
$response = $this->endpoint->bulkAction([
'action' => 'delete', 'action' => 'delete',
'listing' => ['group' => 'trash'], 'listing' => ['group' => 'trash'],
]); ]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE); expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->meta['count'])->equals(0); expect($response->meta['count'])->equals(0);
expect(DynamicSegment::count())->equals(0); $this->entityManager->clear();
expect(DynamicSegmentFilter::findOne($filter->id))->equals(false);
expect($this->entityManager->find(SegmentEntity::class, $dynamicSegment1->getId()))->null();
expect($this->entityManager->find(SegmentEntity::class, $dynamicSegment2->getId()))->null();
} }
private function createDynamicSegmentEntity(string $name, string $description): SegmentEntity { private function createDynamicSegmentEntity(string $name, string $description): SegmentEntity {