Skip to content

Commit

Permalink
Merge pull request #5381 from magento-tsg/2.4-develop-pr13
Browse files Browse the repository at this point in the history
[TSG] Fixes for 2.4 (pr13) (2.4-develop)
  • Loading branch information
zakdma authored Feb 25, 2020
2 parents 9003079 + 2803428 commit 48423d7
Show file tree
Hide file tree
Showing 30 changed files with 887 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<argument name="subtotal" type="string"/>
<argument name="qty" type="string"/>
</arguments>

<see selector="{{CheckoutCartProductSection.productName}}" userInput="{{productName}}" stepKey="seeProductNameInCheckoutSummary"/>
<see selector="{{CheckoutCartProductSection.checkoutCartProductPrice}}" userInput="{{productPrice}}" stepKey="seeProductPriceInCart"/>
<see selector="{{CheckoutCartProductSection.checkoutCartSubtotal}}" userInput="{{subtotal}}" stepKey="seeSubtotalPrice"/>
<seeInField selector="{{CheckoutCartProductSection.qtyByContains(productSku)}}" userInput="{{qty}}" stepKey="seeProductQuantity"/>
<see selector="{{CheckoutCartProductSection.ProductPriceByName(productName)}}" userInput="{{productPrice}}" stepKey="seeProductPriceInCart"/>
<see selector="{{CheckoutCartProductSection.productSubtotalByName(productName)}}" userInput="{{subtotal}}" stepKey="seeSubtotalPrice"/>
<seeInField selector="{{CheckoutCartProductSection.ProductQuantityByName(productName)}}" userInput="{{qty}}" stepKey="seeProductQuantity"/>
</actionGroup>
</actionGroups>

This file was deleted.

83 changes: 83 additions & 0 deletions app/code/Magento/Newsletter/Controller/Ajax/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Newsletter\Controller\Ajax;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
use Magento\Newsletter\Model\GuestSubscriptionChecker;
use Psr\Log\LoggerInterface;

/**
* Newsletter subscription status verification controller.
*/
class Status extends Action implements HttpGetActionInterface
{
/**
* @var EmailAddressValidator
*/
private $emailAddressValidator;

/**
* @var GuestSubscriptionChecker
*/
private $guestSubscriptionChecker;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Context $context
* @param EmailAddressValidator $emailAddressValidator
* @param GuestSubscriptionChecker $guestSubscriptionChecker
* @param LoggerInterface $logger
*/
public function __construct(
Context $context,
EmailAddressValidator $emailAddressValidator,
GuestSubscriptionChecker $guestSubscriptionChecker,
LoggerInterface $logger
) {
parent::__construct($context);
$this->emailAddressValidator = $emailAddressValidator;
$this->guestSubscriptionChecker = $guestSubscriptionChecker;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute()
{
$email = (string)$this->getRequest()->getParam('email');

$response = [
'subscribed' => false,
'errors' => false,
];
try {
if (!empty($email) && $this->emailAddressValidator->isValid($email)) {
$response['subscribed'] = $this->guestSubscriptionChecker->isSubscribed($email);
}
} catch (LocalizedException | \DomainException $exception) {
$this->logger->error($exception->getMessage());
$response['errors'] = true;
}

/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);

return $resultJson->setData($response);
}
}
62 changes: 62 additions & 0 deletions app/code/Magento/Newsletter/Model/GuestSubscriptionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Newsletter\Model;

use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;

/**
* Checks guest subscription by email.
*/
class GuestSubscriptionChecker
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ResourceConnection $resourceConnection
* @param StoreManagerInterface $storeManager
*/
public function __construct(ResourceConnection $resourceConnection, StoreManagerInterface $storeManager)
{
$this->resourceConnection = $resourceConnection;
$this->storeManager = $storeManager;
}

/**
* Check is subscribed by email
*
* @param string $subscriberEmail
* @return bool
*/
public function isSubscribed(string $subscriberEmail): bool
{
if (!empty($subscriberEmail)) {
$storeIds = $this->storeManager->getWebsite()->getStoreIds();
$connection = $this->resourceConnection->getConnection();
$select = $connection
->select()
->from($this->resourceConnection->getTableName('newsletter_subscriber'))
->where('subscriber_email = ?', $subscriberEmail)
->where('store_id IN (?)', $storeIds)
->where('customer_id = 0')
->limit(1);

return (bool)$connection->fetchOne($select);
}

return false;
}
}
22 changes: 13 additions & 9 deletions app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,20 @@ public function afterSave(
CustomerInterface $result,
CustomerInterface $customer
) {
/** @var Subscriber $subscriber */
$subscriber = $this->getSubscriber($result);
$subscribeStatus = $this->getIsSubscribedFromExtensionAttr($customer) ?? $subscriber->isSubscribed();
$subscribeStatus = $this->getIsSubscribedFromExtensionAttributes($customer) ?? $subscriber->isSubscribed();
$needToUpdate = $this->isSubscriptionChanged($result, $subscriber, $subscribeStatus);

/**
* If subscriber is waiting to confirm customer registration
* and customer is already confirmed registration
* than need to subscribe customer
*/
if ((int)$subscriber->getStatus() === Subscriber::STATUS_UNCONFIRMED && empty($result->getConfirmation())) {
if ($subscriber->getId()
&& (int)$subscriber->getStatus() === Subscriber::STATUS_UNCONFIRMED
&& empty($result->getConfirmation())
) {
$needToUpdate = true;
$subscribeStatus = true;
}
Expand All @@ -129,7 +133,7 @@ public function afterSave(
: $this->subscriptionManager->unsubscribeCustomer((int)$result->getId(), $storeId);
$this->customerSubscriber[(int)$result->getId()] = $subscriber;
}
$this->addIsSubscribedExtensionAttr($result, $subscriber->isSubscribed());
$this->addIsSubscribedExtensionAttribute($result, $subscriber->isSubscribed());

return $result;
}
Expand All @@ -140,14 +144,14 @@ public function afterSave(
* @param CustomerInterface $customer
* @return bool|null
*/
private function getIsSubscribedFromExtensionAttr(CustomerInterface $customer): ?bool
private function getIsSubscribedFromExtensionAttributes(CustomerInterface $customer): ?bool
{
$newExtensionAttributes = $customer->getExtensionAttributes();
if ($newExtensionAttributes === null || $newExtensionAttributes->getIsSubscribed() === null) {
$extensionAttributes = $customer->getExtensionAttributes();
if ($extensionAttributes === null || $extensionAttributes->getIsSubscribed() === null) {
return null;
}

return (bool)$newExtensionAttributes->getIsSubscribed();
return (bool)$extensionAttributes->getIsSubscribed();
}

/**
Expand Down Expand Up @@ -223,7 +227,7 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
$extensionAttributes = $customer->getExtensionAttributes();
if ($extensionAttributes === null || $extensionAttributes->getIsSubscribed() === null) {
$isSubscribed = $this->getSubscriber($customer)->isSubscribed();
$this->addIsSubscribedExtensionAttr($customer, $isSubscribed);
$this->addIsSubscribedExtensionAttribute($customer, $isSubscribed);
}

return $customer;
Expand All @@ -235,7 +239,7 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
* @param CustomerInterface $customer
* @param bool $isSubscribed
*/
private function addIsSubscribedExtensionAttr(CustomerInterface $customer, bool $isSubscribed): void
private function addIsSubscribedExtensionAttribute(CustomerInterface $customer, bool $isSubscribed): void
{
$extensionAttributes = $customer->getExtensionAttributes();
if ($extensionAttributes === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ public function testAfterSave(?int $originalStatus, ?bool $newValue, ?bool $expe
->method('loadByCustomer')
->with($customerId, $websiteId)
->willReturnSelf();
$subscriber->expects($this->once())
->method('loadBySubscriberEmail')
->with($customerEmail, $websiteId)
->willReturnSelf();
if ($originalStatus !== null && $originalStatus === Subscriber::STATUS_UNCONFIRMED) {
$subscriber->method('getId')->willReturn(1);
} else {
$subscriber->expects($this->once())
->method('loadBySubscriberEmail')
->with($customerEmail, $websiteId)
->willReturnSelf();
}
$this->subscriberFactory->method('create')->willReturn($subscriber);

$customerExtension = $this->createPartialMock(CustomerExtensionInterface::class, ['getIsSubscribed']);
Expand Down Expand Up @@ -162,23 +166,25 @@ public function testAfterSave(?int $originalStatus, ?bool $newValue, ?bool $expe
}

/**
* Data provider for testAfterSave()
*
* @return array
*/
public function afterSaveDataProvider()
public function afterSaveDataProvider(): array
{
return [
[null, null, null],
[null, true, true],
[null, false, null],
[Subscriber::STATUS_SUBSCRIBED, null, null],
[Subscriber::STATUS_SUBSCRIBED, true, null],
[Subscriber::STATUS_SUBSCRIBED, false, false],
[Subscriber::STATUS_UNSUBSCRIBED, null, null],
[Subscriber::STATUS_UNSUBSCRIBED, true, true],
[Subscriber::STATUS_UNSUBSCRIBED, false, null],
[Subscriber::STATUS_UNCONFIRMED, null, true],
[Subscriber::STATUS_UNCONFIRMED, true, true],
[Subscriber::STATUS_UNCONFIRMED, false, true],
'missing_previous_and_new_status' => [null, null, null],
'missing_previous_status_and_subscribe' => [null, true, true],
'new_unsubscribed_value_and_missing_previous_status' => [null, false, null],
'previous_subscribed_status_without_new_value' => [Subscriber::STATUS_SUBSCRIBED, null, null],
'same_subscribed_previous_and_new_status' => [Subscriber::STATUS_SUBSCRIBED, true, null],
'unsubscribe_previously_subscribed_customer' => [Subscriber::STATUS_SUBSCRIBED, false, false],
'previously_unsubscribed_status_without_new_value' => [Subscriber::STATUS_UNSUBSCRIBED, null, null],
'subscribe_previously_unsubscribed_customer' => [Subscriber::STATUS_UNSUBSCRIBED, true, true],
'same_unsubscribed_previous_and_new_status' => [Subscriber::STATUS_UNSUBSCRIBED, false, null],
'previous_unconfirmed_status_without_new_value' => [Subscriber::STATUS_UNCONFIRMED, null, true],
'subscribe_previously_unconfirmed_status' => [Subscriber::STATUS_UNCONFIRMED, true, true],
'unsubscribe_previously_unconfirmed_status' => [Subscriber::STATUS_UNCONFIRMED, false, true],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_form_register">
<block name="customer.form.register.newsletter" template="Magento_Newsletter::form/register/newsletter.phtml"/>
</referenceBlock>
</body>
</page>
13 changes: 13 additions & 0 deletions app/code/Magento/Newsletter/view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

var config = {
map: {
'*': {
subscriptionStatusResolver: 'Magento_Newsletter/js/subscription-status-resolver',
newsletterSignUp: 'Magento_Newsletter/js/newsletter-sign-up'
}
}
};
Loading

0 comments on commit 48423d7

Please sign in to comment.