Skip to content

Add afterGetList method in CustomerRepository plugin to retrieve is_s… #25311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Newsletter\Model\SubscriptionManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Api\SearchResults;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -229,6 +230,27 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
return $customer;
}

/**
* Add subscription status to customer list
*
* @param CustomerRepositoryInterface $subject
* @param SearchResults $searchResults
* @return SearchResults
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetList(CustomerRepositoryInterface $subject, SearchResults $searchResults): SearchResults
{
foreach ($searchResults->getItems() as $customer) {
/** @var CustomerExtensionInterface $extensionAttributes */
$extensionAttributes = $customer->getExtensionAttributes();

$isSubscribed = (int) $extensionAttributes->getIsSubscribed() === Subscriber::STATUS_SUBSCRIBED ?: false;
$extensionAttributes->setIsSubscribed($isSubscribed);
}

return $searchResults;
}

/**
* Set Is Subscribed extension attribute
*
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Newsletter/etc/extension_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
<attribute code="is_subscribed" type="boolean" />
<attribute code="is_subscribed" type="boolean" >
<join reference_table="newsletter_subscriber" reference_field="customer_id" join_on_field="entity_id">
<field>subscriber_status</field>
</join>
</attribute>
</extension_attributes>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Magento\Customer\Api\Data\CustomerInterface as Customer;
use Magento\Customer\Api\Data\AddressInterface as Address;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -482,25 +484,31 @@ public function testCreateCustomerWithoutAddressRequiresException()

/**
* Test with a single filter
*
* @param bool $subscribeStatus
* @return void
*
* @dataProvider subscriptionDataProvider
*/
public function testSearchCustomers()
public function testSearchCustomers(bool $subscribeStatus): void
{
$builder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
$customerData = $this->_createCustomer();
$builder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
$subscribeData = $this->buildSubscriptionData($subscribeStatus);
$customerData = $this->_createCustomer($subscribeData);
$filter = $builder
->setField(Customer::EMAIL)
->setValue($customerData[Customer::EMAIL])
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchData = $this->dataObjectProcessor->buildOutputDataArray(
$this->searchCriteriaBuilder->create(),
\Magento\Framework\Api\SearchCriteriaInterface::class
SearchCriteriaInterface::class
);
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
Expand All @@ -511,6 +519,35 @@ public function testSearchCustomers()
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$this->assertEquals($customerData[Customer::ID], $searchResults['items'][0][Customer::ID]);
$this->assertEquals($subscribeStatus, $searchResults['items'][0]['extension_attributes']['is_subscribed']);
}

/**
* Build subscription extension attributes data
*
* @param bool $status
* @return array
*/
private function buildSubscriptionData(bool $status): array
{
return [
'extension_attributes' => [
'is_subscribed' => $status,
],
];
}

/**
* Subscription customer data provider
*
* @return array
*/
public function subscriptionDataProvider(): array
{
return [
'subscribed user' => [true],
'not subscribed user' => [false],
];
}

/**
Expand Down Expand Up @@ -857,11 +894,12 @@ protected function _getCustomerData($customerId)
}

/**
* @param array|null $additionalData
* @return array|bool|float|int|string
*/
protected function _createCustomer()
protected function _createCustomer(?array $additionalData = [])
{
$customerData = $this->customerHelper->createSampleCustomer();
$customerData = $this->customerHelper->createSampleCustomer($additionalData);
$this->currentCustomerId[] = $customerData['id'];
return $customerData;
}
Expand Down