Skip to content

Commit 934338c

Browse files
committed
MC-29691: Storefront: Simple product with custom attribute per multiple websites/storeviews, countries/states
1 parent fa00ff7 commit 934338c

File tree

6 files changed

+434
-24
lines changed

6 files changed

+434
-24
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/View/Attribute/AbstractAttributeTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\ObjectManagerInterface;
1717
use Magento\Framework\Registry;
1818
use Magento\Framework\View\LayoutInterface;
19+
use Magento\Store\Model\StoreManagerInterface;
1920
use Magento\TestFramework\Helper\Bootstrap;
2021
use PHPUnit\Framework\TestCase;
2122

@@ -48,6 +49,9 @@ abstract class AbstractAttributeTest extends TestCase
4849
/** @var Output */
4950
private $outputHelper;
5051

52+
/** @var StoreManagerInterface */
53+
private $storeManager;
54+
5155
/**
5256
* @inheritdoc
5357
*/
@@ -62,6 +66,7 @@ protected function setUp()
6266
$this->registry = $this->objectManager->get(Registry::class);
6367
$this->block = $this->layout->createBlock(Attributes::class);
6468
$this->outputHelper = $this->objectManager->create(Output::class);
69+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
6570
}
6671

6772
/**
@@ -145,6 +150,35 @@ protected function processAttributeHtmlOutput(
145150
$this->assertEquals($expectedAttributeValue, $output);
146151
}
147152

153+
/**
154+
* Process attribute view per store views
155+
*
156+
* @param string $sku
157+
* @param int $attributeScopeValue
158+
* @param string $attributeValue
159+
* @param string $expectedAttributeValue
160+
* @param string $storeCode
161+
* @return void
162+
*/
163+
protected function processMultiStoreView(
164+
string $sku,
165+
int $attributeScopeValue,
166+
string $attributeValue,
167+
string $storeCode
168+
): void {
169+
$currentStore = $this->storeManager->getStore();
170+
$this->updateAttribute(['is_global' => $attributeScopeValue, 'is_visible_on_front' => true]);
171+
$this->storeManager->setCurrentStore($storeCode);
172+
173+
try {
174+
$product = $this->updateProduct($sku, $attributeValue);
175+
$this->registerProduct($product);
176+
$this->assertEquals($this->prepareExpectedData($attributeValue), $this->block->getAdditionalData());
177+
} finally {
178+
$this->storeManager->setCurrentStore($currentStore);
179+
}
180+
}
181+
148182
/**
149183
* Get attribute
150184
*
@@ -185,8 +219,11 @@ private function prepareExpectedData(string $expectedValue): array
185219
*/
186220
private function updateProduct(string $productSku, string $attributeValue): ProductInterface
187221
{
222+
$value = $this->getAttribute()->usesSource()
223+
? $this->attribute->getSource()->getOptionId($attributeValue)
224+
: $attributeValue;
188225
$product = $this->productRepository->get($productSku);
189-
$product->addData([$this->getAttributeCode() => $attributeValue]);
226+
$product->addData([$this->getAttributeCode() => $value]);
190227

191228
return $this->productRepository->save($product);
192229
}

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/View/Attribute/DropdownAttributeTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Catalog\Block\Product\View\Attribute;
99

10+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
11+
1012
/**
1113
* Class checks dropdown attribute displaying on frontend
1214
*
@@ -77,6 +79,43 @@ public function attributeWithTagsProvider(): array
7779
];
7880
}
7981

82+
/**
83+
* @magentoDbIsolation disabled
84+
*
85+
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
86+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
87+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
88+
*
89+
* @return void
90+
*/
91+
public function testAttributePerStoreView(): void
92+
{
93+
$this->processMultiStoreView(
94+
'simple2',
95+
ScopedAttributeInterface::SCOPE_STORE,
96+
'Option 3',
97+
'fixturestore'
98+
);
99+
}
100+
101+
/**
102+
* @magentoDbIsolation disabled
103+
*
104+
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
105+
* @magentoDataFixture Magento/Catalog/_files/product_two_websites.php
106+
*
107+
* @return void
108+
*/
109+
public function testAttributePerWebsites(): void
110+
{
111+
$this->processMultiStoreView(
112+
'simple-on-two-websites',
113+
ScopedAttributeInterface::SCOPE_WEBSITE,
114+
'Option 3',
115+
'fixture_second_store'
116+
);
117+
}
118+
80119
/**
81120
* @inheritdoc
82121
*/

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/View/Attribute/TextAttributeTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Catalog\Block\Product\View\Attribute;
99

10+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
11+
1012
/**
1113
* Class checks text attribute displaying on frontend
1214
*
@@ -77,6 +79,43 @@ public function attributeWithTagsProvider(): array
7779
];
7880
}
7981

82+
/**
83+
* @magentoDbIsolation disabled
84+
*
85+
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
86+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
87+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
88+
*
89+
* @return void
90+
*/
91+
public function testAttributePerStoreView(): void
92+
{
93+
$this->processMultiStoreView(
94+
'simple2',
95+
ScopedAttributeInterface::SCOPE_STORE,
96+
'second store view value',
97+
'fixturestore'
98+
);
99+
}
100+
101+
/**
102+
* @magentoDbIsolation disabled
103+
*
104+
* @magentoDataFixture Magento/Catalog/_files/product_two_websites.php
105+
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
106+
*
107+
* @return void
108+
*/
109+
public function testAttributePerWebsites(): void
110+
{
111+
$this->processMultiStoreView(
112+
'simple-on-two-websites',
113+
ScopedAttributeInterface::SCOPE_WEBSITE,
114+
'second website value',
115+
'fixture_second_store'
116+
);
117+
}
118+
80119
/**
81120
* @inheritdoc
82121
*/
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Customer\Api\AddressMetadataInterface;
9+
use Magento\Customer\Api\AddressRepositoryInterface;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Model\Address;
13+
use Magento\Customer\Model\AddressFactory;
14+
use Magento\Customer\Model\CustomerFactory;
15+
use Magento\Customer\Model\CustomerRegistry;
16+
use Magento\Customer\Model\AddressRegistry;
17+
use Magento\Store\Api\WebsiteRepositoryInterface;
18+
use Magento\Store\Model\Website;
19+
use Magento\Store\Model\WebsiteRepository;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
22+
$objectManager = Bootstrap::getObjectManager();
23+
/** @var CustomerRepositoryInterface $customerRepository */
24+
$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
25+
/** @var CustomerFactory $customerFactory */
26+
$customerFactory = $objectManager->get(CustomerFactory::class);
27+
$customer = $customerFactory->create();
28+
/** @var CustomerRegistry $customerRegistry */
29+
$customerRegistry = $objectManager->get(CustomerRegistry::class);
30+
/** @var WebsiteRepository $websiteRepository */
31+
$websiteRepository = $objectManager->create(WebsiteRepositoryInterface::class);
32+
/** @var Website $mainWebsite */
33+
$mainWebsite = $websiteRepository->get('base');
34+
$customer->setWebsiteId($mainWebsite->getId())
35+
->setEmail('customer_uk_address@test.com')
36+
->setPassword('password')
37+
->setGroupId(1)
38+
->setStoreId($mainWebsite->getDefaultStore()->getId())
39+
->setIsActive(1)
40+
->setPrefix('Mr.')
41+
->setFirstname('John')
42+
->setMiddlename('A')
43+
->setLastname('Smith')
44+
->setSuffix('Esq.')
45+
->setTaxvat('12')
46+
->setGender(0);
47+
/** @var AddressFactory $customerAddressFactory */
48+
$customerAddressFactory = $objectManager->get(AddressFactory::class);
49+
/** @var AddressRepositoryInterface $customerAddressRepository */
50+
$customerAddressRepository = $objectManager->create(AddressRepositoryInterface::class);
51+
/** @var Address $customerAddress */
52+
$customerAddress = $customerAddressFactory->create();
53+
$customerAddress->isObjectNew(true);
54+
$customerAddress->setData(
55+
[
56+
'attribute_set_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
57+
AddressInterface::TELEPHONE => 3468676,
58+
AddressInterface::POSTCODE => 'EC1A 1AA',
59+
AddressInterface::COUNTRY_ID => 'GB',
60+
AddressInterface::CITY => 'London',
61+
AddressInterface::COMPANY => 'CompanyName',
62+
AddressInterface::STREET => 'test street address',
63+
AddressInterface::LASTNAME => 'Smith',
64+
AddressInterface::FIRSTNAME => 'John',
65+
AddressInterface::REGION_ID => 1,
66+
]
67+
);
68+
$customer->addAddress($customerAddress);
69+
$customer->isObjectNew(true);
70+
$customerDataModel = $customerRepository->save($customer->getDataModel());
71+
$addressId = $customerDataModel->getAddresses()[0]->getId();
72+
$customerDataModel->setDefaultShipping($addressId);
73+
$customerDataModel->setDefaultBilling($addressId);
74+
$customerRepository->save($customerDataModel);
75+
$customerRegistry->remove($customerDataModel->getId());
76+
/** @var AddressRegistry $addressRegistry */
77+
$addressRegistry = $objectManager->get(AddressRegistry::class);
78+
$addressRegistry->remove($customerAddress->getId());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Customer\Api\AddressRepositoryInterface;
9+
use Magento\Customer\Api\CustomerRepositoryInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Registry;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
$registry->unregister('isSecureArea');
18+
$registry->register('isSecureArea', true);
19+
/** @var CustomerRepositoryInterface $customerRepository */
20+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
21+
22+
try {
23+
$customer = $customerRepository->get('customer_uk_address@test.com');
24+
$customerRepository->delete($customer);
25+
} catch (NoSuchEntityException $exception) {
26+
//Already deleted
27+
}
28+
29+
$registry->unregister('isSecureArea');
30+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)