Skip to content

Commit ae045a2

Browse files
ENGCOM-6945: Add afterGetList method in CustomerRepository plugin to retrieve is_s… #25311
- Merge Pull Request #25311 from enriquei4/magento2:feature/getlist-plugin-customerRepository - Merged commits: 1. b2d553a 2. 8f5bfad 3. 8b33f69 4. d867655 5. c9f9d71 6. 5020752
2 parents 2d46d98 + 5020752 commit ae045a2

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Newsletter\Model\SubscriptionManagerInterface;
1818
use Magento\Store\Model\Store;
1919
use Magento\Store\Model\StoreManagerInterface;
20+
use Magento\Framework\Api\SearchResults;
2021
use Psr\Log\LoggerInterface;
2122

2223
/**
@@ -233,6 +234,27 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
233234
return $customer;
234235
}
235236

237+
/**
238+
* Add subscription status to customer list
239+
*
240+
* @param CustomerRepositoryInterface $subject
241+
* @param SearchResults $searchResults
242+
* @return SearchResults
243+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
244+
*/
245+
public function afterGetList(CustomerRepositoryInterface $subject, SearchResults $searchResults): SearchResults
246+
{
247+
foreach ($searchResults->getItems() as $customer) {
248+
/** @var CustomerExtensionInterface $extensionAttributes */
249+
$extensionAttributes = $customer->getExtensionAttributes();
250+
251+
$isSubscribed = (int) $extensionAttributes->getIsSubscribed() === Subscriber::STATUS_SUBSCRIBED ?: false;
252+
$extensionAttributes->setIsSubscribed($isSubscribed);
253+
}
254+
255+
return $searchResults;
256+
}
257+
236258
/**
237259
* Set Is Subscribed extension attribute
238260
*

app/code/Magento/Newsletter/etc/extension_attributes.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
1010
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
11-
<attribute code="is_subscribed" type="boolean" />
11+
<attribute code="is_subscribed" type="boolean" >
12+
<join reference_table="newsletter_subscriber" reference_field="customer_id" join_on_field="entity_id">
13+
<field>subscriber_status</field>
14+
</join>
15+
</attribute>
1216
</extension_attributes>
1317
</config>

dev/tests/api-functional/testsuite/Magento/Customer/Api/CustomerRepositoryTest.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
use Magento\Customer\Api\Data\CustomerInterface as Customer;
1010
use Magento\Customer\Api\Data\AddressInterface as Address;
11+
use Magento\Framework\Api\FilterBuilder;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
1113
use Magento\Framework\Api\SortOrder;
1214
use Magento\Framework\Exception\InputException;
1315
use Magento\Framework\Exception\LocalizedException;
@@ -482,25 +484,31 @@ public function testCreateCustomerWithoutAddressRequiresException()
482484

483485
/**
484486
* Test with a single filter
487+
*
488+
* @param bool $subscribeStatus
489+
* @return void
490+
*
491+
* @dataProvider subscriptionDataProvider
485492
*/
486-
public function testSearchCustomers()
493+
public function testSearchCustomers(bool $subscribeStatus): void
487494
{
488-
$builder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
489-
$customerData = $this->_createCustomer();
495+
$builder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
496+
$subscribeData = $this->buildSubscriptionData($subscribeStatus);
497+
$customerData = $this->_createCustomer($subscribeData);
490498
$filter = $builder
491499
->setField(Customer::EMAIL)
492500
->setValue($customerData[Customer::EMAIL])
493501
->create();
494502
$this->searchCriteriaBuilder->addFilters([$filter]);
495503
$searchData = $this->dataObjectProcessor->buildOutputDataArray(
496504
$this->searchCriteriaBuilder->create(),
497-
\Magento\Framework\Api\SearchCriteriaInterface::class
505+
SearchCriteriaInterface::class
498506
);
499507
$requestData = ['searchCriteria' => $searchData];
500508
$serviceInfo = [
501509
'rest' => [
502510
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
503-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
511+
'httpMethod' => Request::HTTP_METHOD_GET,
504512
],
505513
'soap' => [
506514
'service' => self::SERVICE_NAME,
@@ -511,6 +519,35 @@ public function testSearchCustomers()
511519
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
512520
$this->assertEquals(1, $searchResults['total_count']);
513521
$this->assertEquals($customerData[Customer::ID], $searchResults['items'][0][Customer::ID]);
522+
$this->assertEquals($subscribeStatus, $searchResults['items'][0]['extension_attributes']['is_subscribed']);
523+
}
524+
525+
/**
526+
* Build subscription extension attributes data
527+
*
528+
* @param bool $status
529+
* @return array
530+
*/
531+
private function buildSubscriptionData(bool $status): array
532+
{
533+
return [
534+
'extension_attributes' => [
535+
'is_subscribed' => $status,
536+
],
537+
];
538+
}
539+
540+
/**
541+
* Subscription customer data provider
542+
*
543+
* @return array
544+
*/
545+
public function subscriptionDataProvider(): array
546+
{
547+
return [
548+
'subscribed user' => [true],
549+
'not subscribed user' => [false],
550+
];
514551
}
515552

516553
/**
@@ -857,11 +894,12 @@ protected function _getCustomerData($customerId)
857894
}
858895

859896
/**
897+
* @param array|null $additionalData
860898
* @return array|bool|float|int|string
861899
*/
862-
protected function _createCustomer()
900+
protected function _createCustomer(?array $additionalData = [])
863901
{
864-
$customerData = $this->customerHelper->createSampleCustomer();
902+
$customerData = $this->customerHelper->createSampleCustomer($additionalData);
865903
$this->currentCustomerId[] = $customerData['id'];
866904
return $customerData;
867905
}

0 commit comments

Comments
 (0)