Refactor mailpoet_get_subscribers_listings_in_segment_handlers to direct calls
[MAILPOET-3077]
This commit is contained in:
committed by
Veljko V
parent
f9f8428b42
commit
1a56b2534e
@ -4,7 +4,6 @@ namespace MailPoet\DynamicSegments;
|
|||||||
|
|
||||||
use MailPoet\DynamicSegments\FreePluginConnectors\AddToSubscribersFilters;
|
use MailPoet\DynamicSegments\FreePluginConnectors\AddToSubscribersFilters;
|
||||||
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersBulkActionHandler;
|
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersBulkActionHandler;
|
||||||
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersListingsHandlerFactory;
|
|
||||||
use MailPoet\DynamicSegments\Mappers\DBMapper;
|
use MailPoet\DynamicSegments\Mappers\DBMapper;
|
||||||
use MailPoet\DynamicSegments\Persistence\Loading\Loader;
|
use MailPoet\DynamicSegments\Persistence\Loading\Loader;
|
||||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
||||||
@ -20,11 +19,6 @@ class DynamicSegmentHooks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
$this->wp->addAction(
|
|
||||||
'mailpoet_get_subscribers_listings_in_segment_handlers',
|
|
||||||
[$this, 'getSubscribersListingsInSegmentsHandlers']
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->wp->addAction(
|
$this->wp->addAction(
|
||||||
'mailpoet_subscribers_listings_filters_segments',
|
'mailpoet_subscribers_listings_filters_segments',
|
||||||
[$this, 'addDynamicFiltersToSubscribersListingsFilters']
|
[$this, 'addDynamicFiltersToSubscribersListingsFilters']
|
||||||
@ -41,11 +35,6 @@ class DynamicSegmentHooks {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubscribersListingsInSegmentsHandlers(array $handlers) {
|
|
||||||
$handlers[] = new SubscribersListingsHandlerFactory();
|
|
||||||
return $handlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addDynamicFiltersToSubscribersListingsFilters($segmentFilters) {
|
public function addDynamicFiltersToSubscribersListingsFilters($segmentFilters) {
|
||||||
$newslettersAddSegments = new AddToSubscribersFilters(new Loader(new DBMapper()), new SubscribersCount());
|
$newslettersAddSegments = new AddToSubscribersFilters(new Loader(new DBMapper()), new SubscribersCount());
|
||||||
return $newslettersAddSegments->add($segmentFilters);
|
return $newslettersAddSegments->add($segmentFilters);
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\DynamicSegments\FreePluginConnectors;
|
|
||||||
|
|
||||||
use MailPoet\Listing\Handler;
|
|
||||||
use MailPoet\Models\DynamicSegment;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
|
|
||||||
class SubscribersListingsHandlerFactory {
|
|
||||||
public function get(Segment $segment, $data) {
|
|
||||||
if ($segment->type === DynamicSegment::TYPE_DYNAMIC) {
|
|
||||||
$listing = new Handler();
|
|
||||||
return $listingData = $listing->get('\MailPoet\Models\SubscribersInDynamicSegment', $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,20 +2,17 @@
|
|||||||
|
|
||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
use MailPoet\Entities\SegmentEntity;
|
||||||
use MailPoet\Listing\Handler;
|
use MailPoet\Listing\Handler;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
|
||||||
|
|
||||||
class SubscribersListings {
|
class SubscribersListings {
|
||||||
|
|
||||||
/** @var Handler */
|
/** @var Handler */
|
||||||
private $handler;
|
private $handler;
|
||||||
|
|
||||||
private $wp;
|
public function __construct(Handler $handler) {
|
||||||
|
|
||||||
public function __construct(Handler $handler, WPFunctions $wp) {
|
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
$this->wp = $wp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getListingsInSegment($data) {
|
public function getListingsInSegment($data) {
|
||||||
@ -29,16 +26,12 @@ class SubscribersListings {
|
|||||||
|
|
||||||
private function getListings($data, Segment $segment = null) {
|
private function getListings($data, Segment $segment = null) {
|
||||||
if (!$segment
|
if (!$segment
|
||||||
|| in_array($segment->type, [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true)
|
|| in_array($segment->type, [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS, SegmentEntity::TYPE_WC_USERS], true)
|
||||||
) {
|
) {
|
||||||
return $listingData = $this->handler->get('\MailPoet\Models\Subscriber', $data);
|
return $listingData = $this->handler->get('\MailPoet\Models\Subscriber', $data);
|
||||||
}
|
}
|
||||||
$handlers = $this->wp->applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', []);
|
if ($segment->type === SegmentEntity::TYPE_DYNAMIC) {
|
||||||
foreach ($handlers as $handler) {
|
return $this->handler->get('\MailPoet\Models\SubscribersInDynamicSegment', $data);
|
||||||
$listings = $handler->get($segment, $data);
|
|
||||||
if ($listings) {
|
|
||||||
return $listings;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new \InvalidArgumentException('No handler found for segment');
|
throw new \InvalidArgumentException('No handler found for segment');
|
||||||
}
|
}
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\DynamicSegments\FreePluginConnectors;
|
|
||||||
|
|
||||||
use MailPoet\Models\DynamicSegment;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
class SubscribersListingsHandlerFactoryTest extends \MailPoetTest {
|
|
||||||
public function testItReturnsNullWithUnknownSegment() {
|
|
||||||
$segment = DynamicSegment::create();
|
|
||||||
$segment->id = 1;
|
|
||||||
$segment->name = 'name';
|
|
||||||
$segment->type = 'unknown';
|
|
||||||
$listings = new SubscribersListingsHandlerFactory();
|
|
||||||
$result = $listings->get($segment, ['filter' => ['segment' => null]]);
|
|
||||||
expect($result)->null();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItReturnsDataForDynamicSegment() {
|
|
||||||
$segment = DynamicSegment::createOrUpdate([
|
|
||||||
'name' => 'name',
|
|
||||||
'description' => 'desc',
|
|
||||||
'type' => DynamicSegment::TYPE_DYNAMIC,
|
|
||||||
]);
|
|
||||||
$listings = new SubscribersListingsHandlerFactory();
|
|
||||||
$result = $listings->get($segment, ['filter' => ['segment' => null]]);
|
|
||||||
expect($result)->notNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _before() {
|
|
||||||
$this->cleanData();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _after() {
|
|
||||||
$this->cleanData();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function cleanData() {
|
|
||||||
ORM::raw_execute('TRUNCATE ' . DynamicSegment::$_table);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Test\Segments;
|
|
||||||
|
|
||||||
class DynamicListingsHandlerMock {
|
|
||||||
public function get() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,22 +2,22 @@
|
|||||||
|
|
||||||
namespace MailPoet\Segments;
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
require_once('DynamicListingsHandlerMock.php');
|
require_once(ABSPATH . 'wp-admin/includes/user.php');
|
||||||
|
|
||||||
use Codeception\Util\Stub;
|
|
||||||
use MailPoet\DI\ContainerWrapper;
|
use MailPoet\DI\ContainerWrapper;
|
||||||
|
use MailPoet\Models\DynamicSegmentFilter;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\Test\DataFactories\DynamicSegment;
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
use MailPoetVendor\Idiorm\ORM;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
|
||||||
|
|
||||||
class SubscribersListingsTest extends \MailPoetTest {
|
class SubscribersListingsTest extends \MailPoetTest {
|
||||||
public $subscriber2;
|
public $subscriber2;
|
||||||
public $subscriber1;
|
public $subscriber1;
|
||||||
public $segment2;
|
public $segment2;
|
||||||
public $segment1;
|
public $segment1;
|
||||||
|
public $dynamicSegment;
|
||||||
|
|
||||||
/** @var SubscribersListings */
|
/** @var SubscribersListings */
|
||||||
private $finder;
|
private $finder;
|
||||||
@ -26,8 +26,19 @@ class SubscribersListingsTest extends \MailPoetTest {
|
|||||||
parent::_before();
|
parent::_before();
|
||||||
$this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class);
|
$this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class);
|
||||||
$this->cleanData();
|
$this->cleanData();
|
||||||
|
wp_insert_user([
|
||||||
|
'user_login' => 'user-role-test1',
|
||||||
|
'user_email' => 'user-role-test1@example.com',
|
||||||
|
'role' => 'editor',
|
||||||
|
'user_pass' => '12123154',
|
||||||
|
]);
|
||||||
$this->segment1 = Segment::createOrUpdate(['name' => 'Segment 1', 'type' => 'default']);
|
$this->segment1 = Segment::createOrUpdate(['name' => 'Segment 1', 'type' => 'default']);
|
||||||
$this->segment2 = Segment::createOrUpdate(['name' => 'Segment 3', 'type' => 'not default']);
|
$this->segment2 = Segment::createOrUpdate(['name' => 'Segment 3', 'type' => 'not default']);
|
||||||
|
$dynamicSegmentFactory = new DynamicSegment();
|
||||||
|
$this->dynamicSegment = $dynamicSegmentFactory
|
||||||
|
->withName('Dynamic')
|
||||||
|
->withUserRoleFilter('editor')
|
||||||
|
->create();
|
||||||
$this->subscriber1 = Subscriber::createOrUpdate([
|
$this->subscriber1 = Subscriber::createOrUpdate([
|
||||||
'email' => 'john@mailpoet.com',
|
'email' => 'john@mailpoet.com',
|
||||||
'first_name' => 'John',
|
'first_name' => 'John',
|
||||||
@ -57,7 +68,12 @@ class SubscribersListingsTest extends \MailPoetTest {
|
|||||||
private function cleanData() {
|
private function cleanData() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
||||||
|
ORM::raw_execute('TRUNCATE ' . DynamicSegmentFilter::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
|
$user = get_user_by('email', 'user-role-test1@example.com');
|
||||||
|
if ($user) {
|
||||||
|
\wp_delete_user($user->ID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTryToGetListingsWithoutPassingSegment() {
|
public function testTryToGetListingsWithoutPassingSegment() {
|
||||||
@ -75,26 +91,14 @@ class SubscribersListingsTest extends \MailPoetTest {
|
|||||||
expect($listings['items'])->notEmpty();
|
expect($listings['items'])->notEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetListingsUsingFilter() {
|
public function testGetListingsForDynamicSegment() {
|
||||||
/** @var MockObject $mock */
|
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->dynamicSegment->id]]);
|
||||||
$mock = Stub::makeEmpty('MailPoet\Test\Segments\DynamicListingsHandlerMock', ['get']);
|
expect($listings['items'])->count(1);
|
||||||
$mock
|
expect($listings['items'][0]->email)->equals('user-role-test1@example.com');
|
||||||
->expects($this->once())
|
|
||||||
->method('get')
|
|
||||||
->will($this->returnValue('dynamic listings'));
|
|
||||||
|
|
||||||
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
|
||||||
(new WPFunctions)->addFilter('mailpoet_get_subscribers_listings_in_segment_handlers', function () use ($mock) {
|
|
||||||
return [$mock];
|
|
||||||
});
|
|
||||||
|
|
||||||
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
|
|
||||||
expect($listings)->equals('dynamic listings');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTryToGetListingsForSegmentWithoutHandler() {
|
public function testTryToGetListingsForSegmentWithoutHandler() {
|
||||||
$this->expectException('InvalidArgumentException');
|
$this->expectException('InvalidArgumentException');
|
||||||
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
|
||||||
$this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
|
$this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user