Files
piratepoet/mailpoet/tests/integration/API/JSON/v1/DynamicSegmentsTest.php
John Oleksowicz 6675a616b5 Display proper notices for segment actions
This updates some of the segment listing messages to include the name of
 the segment to avoid confusion, and allows for the possibility that
 bulk actions will work for some segments but not all, in which case we
 need to show both success and error messages.

MAILPOET-5395
2024-04-24 12:59:37 +02:00

229 lines
8.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php declare(strict_types = 1);
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\DynamicSegmentFilterData;
use MailPoet\Entities\DynamicSegmentFilterEntity;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\NewsletterSegmentEntity;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Segments\DynamicSegments\Filters\UserRole;
class DynamicSegmentsTest extends \MailPoetTest {
const SUCCESS_RESPONSE_CODE = 200;
const SEGMENT_NOT_FOUND_RESPONSE_CODE = 404;
const INVALID_DATA_RESPONSE_CODE = 400;
const SERVER_ERROR_RESPONSE_CODE = 409;
/** @var DynamicSegments */
private $endpoint;
public function _before() {
$this->endpoint = ContainerWrapper::getInstance()->get(DynamicSegments::class);
}
public function testGetReturnsResponse() {
$segment = $this->createDynamicSegmentEntity('s1', '');
$response = $this->endpoint->get(['id' => $segment->getId()]);
verify($response)->instanceOf('\MailPoet\API\JSON\SuccessResponse');
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->data['id'])->equals($segment->getId());
}
public function testGetReturnsError() {
$response = $this->endpoint->get(['id' => 5]);
verify($response)->instanceOf('\MailPoet\API\JSON\ErrorResponse');
verify($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE);
}
public function testSaverSavesData() {
$response = $this->endpoint->save([
'name' => 'Test dynamic',
'description' => 'description dynamic',
'filters' => [[
'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE,
'wordpressRole' => 'editor',
'action' => UserRole::TYPE,
]],
]);
verify($response)->instanceOf('\MailPoet\API\JSON\SuccessResponse');
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->data['name'])->equals('Test dynamic');
}
public function testSaverReturnsErrorOnInvalidFilterData() {
$response = $this->endpoint->save([
'name' => 'Test dynamic',
]);
verify($response)->instanceOf('\MailPoet\API\JSON\ErrorResponse');
verify($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
verify($response->errors[0]['message'])->equals('Please add at least one condition for filtering.');
}
public function testSaverReturnsErrorOnDuplicateRecord() {
$data = [
'name' => 'Test dynamic',
'description' => 'description dynamic',
'filters' => [[
'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE,
'wordpressRole' => 'editor',
]],
];
$this->endpoint->save($data);
$response = $this->endpoint->save($data);
verify($response)->instanceOf('\MailPoet\API\JSON\ErrorResponse');
verify($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
verify($response->errors[0]['message'])->equals('Another record already exists. Please specify a different "name".');
}
public function testSaverReturnsErrorOnEmptyName() {
$data = [
'description' => 'description dynamic',
'filters' => [[
'segmentType' => DynamicSegmentFilterData::TYPE_USER_ROLE,
'wordpressRole' => 'editor',
]],
];
$this->endpoint->save($data);
$response = $this->endpoint->save($data);
verify($response)->instanceOf('\MailPoet\API\JSON\ErrorResponse');
verify($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
verify($response->errors[0]['message'])->equals('Please specify a name.');
}
public function testItCanTrashASegment() {
$dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$response = $this->endpoint->trash(['id' => $dynamicSegment->getId()]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->data['name'])->equals($dynamicSegment->getName());
verify($response->meta['count'])->equals(1);
$this->entityManager->refresh($dynamicSegment);
$this->assertInstanceOf(SegmentEntity::class, $dynamicSegment);
verify($dynamicSegment->getDeletedAt())->notNull();
}
public function testItReturnsErrorWhenTrashingSegmentWithActiveNewsletter() {
$dynamicSegment = $this->createDynamicSegmentEntity('Trash test 2', 'description');
$newsletter = new NewsletterEntity();
$newsletter->setSubject('Subject');
$newsletter->setType(NewsletterEntity::TYPE_NOTIFICATION);
$newsletterSegment = new NewsletterSegmentEntity($newsletter, $dynamicSegment);
$this->entityManager->persist($newsletter);
$this->entityManager->persist($newsletterSegment);
$this->entityManager->flush();
$response = $this->endpoint->trash(['id' => $dynamicSegment->getId()]);
$this->entityManager->refresh($dynamicSegment);
verify($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
verify($response->errors[0]['message'])->equals("Segment '{$dynamicSegment->getName()}' cannot be deleted because its used for 'Subject' email");
}
public function testItCanRestoreASegment() {
$dynamicSegment = $this->createDynamicSegmentEntity('Trash test', 'description');
$response = $this->endpoint->restore(['id' => $dynamicSegment->getId()]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->data['name'])->equals($dynamicSegment->getName());
verify($response->meta['count'])->equals(1);
$this->entityManager->refresh($dynamicSegment);
$this->assertInstanceOf(SegmentEntity::class, $dynamicSegment);
verify($dynamicSegment->getDeletedAt())->null();
}
public function testItCanDeleteASegment() {
$dynamicSegment = $this->createDynamicSegmentEntity('Delete test', 'description');
$dynamicSegmentFilter = $dynamicSegment->getDynamicFilters()->first();
$this->assertInstanceOf(DynamicSegmentFilterEntity::class, $dynamicSegmentFilter);
$response = $this->endpoint->delete(['id' => $dynamicSegment->getId()]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->data)->equals(null);
verify($response->meta['count'])->equals(1);
// Clear entity manager to forget all entities
$this->entityManager->clear();
verify($this->entityManager->find(SegmentEntity::class, $dynamicSegment->getId()))->null();
verify($this->entityManager->find(DynamicSegmentFilterEntity::class, $dynamicSegmentFilter->getId()))->null();
}
public function testItCanBulkDeleteSegments() {
$dynamicSegment1 = $this->createDynamicSegmentEntity('Test 1', 'description');
$dynamicSegment2 = $this->createDynamicSegmentEntity('Test 2', 'description');
$response = $this->endpoint->bulkAction([
'action' => 'trash',
'listing' => ['group' => 'all'],
]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->meta['count'])->equals(2);
$this->entityManager->refresh($dynamicSegment1);
$this->entityManager->refresh($dynamicSegment2);
verify($dynamicSegment1->getDeletedAt())->notNull();
verify($dynamicSegment2->getDeletedAt())->notNull();
$response = $this->endpoint->bulkAction([
'action' => 'restore',
'listing' => ['group' => 'trash'],
]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->meta['count'])->equals(2);
$this->entityManager->refresh($dynamicSegment1);
$this->entityManager->refresh($dynamicSegment2);
verify($dynamicSegment1->getDeletedAt())->null();
verify($dynamicSegment2->getDeletedAt())->null();
$this->endpoint->bulkAction([
'action' => 'trash',
'listing' => ['group' => 'all'],
]);
$response = $this->endpoint->bulkAction([
'action' => 'delete',
'listing' => ['group' => 'trash'],
]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->meta['count'])->equals(2);
// Second delete doesn't delete anything
$response = $this->endpoint->bulkAction([
'action' => 'delete',
'listing' => ['group' => 'trash'],
]);
verify($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
verify($response->meta['count'])->equals(0);
$this->entityManager->clear();
verify($this->entityManager->find(SegmentEntity::class, $dynamicSegment1->getId()))->null();
verify($this->entityManager->find(SegmentEntity::class, $dynamicSegment2->getId()))->null();
}
private function createDynamicSegmentEntity(string $name, string $description): SegmentEntity {
$segment = new SegmentEntity($name, SegmentEntity::TYPE_DYNAMIC, $description);
$filterData = new DynamicSegmentFilterData(
DynamicSegmentFilterData::TYPE_USER_ROLE,
UserRole::TYPE,
['wordpressRole' => 'editor']
);
$dynamicFilter = new DynamicSegmentFilterEntity($segment, $filterData);
$segment->getDynamicFilters()->add($dynamicFilter);
$this->entityManager->persist($segment);
$this->entityManager->persist($dynamicFilter);
$this->entityManager->flush();
return $segment;
}
}