Skip to content

Commit 64184b3

Browse files
committed
Addedtests to cover store/default scenarios with getAttributeRawValue
1 parent 01f1f15 commit 64184b3

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/ProductTest.php

100644100755
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Magento\Framework\ObjectManagerInterface;
1010
use Magento\TestFramework\Helper\Bootstrap;
1111
use PHPUnit\Framework\TestCase;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Exception\CouldNotSaveException;
14+
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\StateException;
1216

1317
class ProductTest extends TestCase
1418
{
@@ -53,6 +57,87 @@ public function testGetAttributeRawValue()
5357
self::assertEquals($product->getName(), $actual);
5458
}
5559

60+
/**
61+
* @magentoAppArea adminhtml
62+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_store_scope_attribute.php
63+
* @throws NoSuchEntityException
64+
* @throws CouldNotSaveException
65+
* @throws InputException
66+
* @throws StateException
67+
*/
68+
public function testGetAttributeRawValueGetDefault()
69+
{
70+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 0, true);
71+
$product->setCustomAttribute('store_scoped_attribute_code', 'default_value');
72+
$this->productRepository->save($product);
73+
74+
$actual = $this->model->getAttributeRawValue($product->getId(), 'store_scoped_attribute_code', 1);
75+
$this->assertEquals('default_value', $actual);
76+
}
77+
78+
/**
79+
* @magentoAppArea adminhtml
80+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_store_scope_attribute.php
81+
* @throws NoSuchEntityException
82+
* @throws CouldNotSaveException
83+
* @throws InputException
84+
* @throws StateException
85+
*/
86+
public function testGetAttributeRawValueGetStoreSpecificValueNoDefault()
87+
{
88+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 0, true);
89+
$product->setCustomAttribute('store_scoped_attribute_code', null);
90+
$this->productRepository->save($product);
91+
92+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 1, true);
93+
$product->setCustomAttribute('store_scoped_attribute_code', 'store_value');
94+
$this->productRepository->save($product);
95+
96+
$actual = $this->model->getAttributeRawValue($product->getId(), 'store_scoped_attribute_code', 1);
97+
$this->assertEquals('store_value', $actual);
98+
}
99+
100+
/**
101+
* @magentoAppArea adminhtml
102+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_store_scope_attribute.php
103+
* @throws NoSuchEntityException
104+
* @throws CouldNotSaveException
105+
* @throws InputException
106+
* @throws StateException
107+
*/
108+
public function testGetAttributeRawValueGetStoreSpecificValueWithDefault()
109+
{
110+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 0, true);
111+
$product->setCustomAttribute('store_scoped_attribute_code', 'default_value');
112+
$this->productRepository->save($product);
113+
114+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 1, true);
115+
$product->setCustomAttribute('store_scoped_attribute_code', 'store_value');
116+
$this->productRepository->save($product);
117+
118+
$actual = $this->model->getAttributeRawValue($product->getId(), 'store_scoped_attribute_code', 1);
119+
$this->assertEquals('store_value', $actual);
120+
}
121+
122+
/**
123+
* @magentoAppArea adminhtml
124+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_store_scope_attribute.php
125+
* @throws NoSuchEntityException
126+
* @throws CouldNotSaveException
127+
* @throws InputException
128+
* @throws StateException
129+
* @throws NoSuchEntityException
130+
*/
131+
public function testGetAttributeRawValueGetStoreValueFallbackToDefault()
132+
{
133+
$product = $this->productRepository->get('simple_with_store_scoped_custom_attribute', true, 0, true);
134+
$product->setCustomAttribute('store_scoped_attribute_code', 'default_value');
135+
$this->productRepository->save($product);
136+
137+
$actual = $this->model->getAttributeRawValue($product->getId(), 'store_scoped_attribute_code', 1);
138+
$this->assertEquals('default_value', $actual);
139+
}
140+
56141
/**
57142
* @magentoAppArea adminhtml
58143
* @magentoDataFixture Magento/Catalog/_files/product_special_price.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\TestFramework\Helper\Bootstrap;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\ProductFactory;
11+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Catalog\Setup\CategorySetup;
14+
use Magento\Eav\Model\Entity;
15+
use Magento\Catalog\Model\Product;
16+
use Magento\Catalog\Model\Product\Type;
17+
18+
/** @var \Magento\TestFramework\ObjectManager $objectManager */
19+
$objectManager = Bootstrap::getObjectManager();
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
22+
/** @var ProductFactory $productFactory */
23+
$productFactory = $objectManager->get(ProductFactory::class);
24+
/** @var ProductAttributeRepositoryInterface $attributeRepository */
25+
$attributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
26+
27+
28+
/** @var $installer CategorySetup */
29+
$installer = $objectManager->create(CategorySetup::class);
30+
$entityModel = $objectManager->create(Entity::class);
31+
$attributeSetId = $installer->getAttributeSetId(Product::ENTITY, 'Default');
32+
$entityTypeId = $entityModel->setType(Product::ENTITY)
33+
->getTypeId();
34+
$groupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
35+
36+
/** @var ProductAttributeInterface $attribute */
37+
$attribute = $objectManager->create(ProductAttributeInterface::class);
38+
39+
$attribute->setAttributeCode('store_scoped_attribute_code')
40+
->setEntityTypeId($entityTypeId)
41+
->setIsVisible(true)
42+
->setFrontendInput('text')
43+
->setIsFilterable(1)
44+
->setIsUserDefined(1)
45+
->setUsedInProductListing(1)
46+
->setBackendType('varchar')
47+
->setIsUsedInGrid(1)
48+
->setIsVisibleInGrid(1)
49+
->setIsFilterableInGrid(1)
50+
->setFrontendLabel('nobody cares')
51+
->setAttributeGroupId($groupId)
52+
->setAttributeSetId(4);
53+
54+
$attributeRepository->save($attribute);
55+
56+
$product = $productFactory->create()
57+
->setTypeId(Type::TYPE_SIMPLE)
58+
->setAttributeSetId(4)
59+
->setName('Simple With Store Scoped Custom Attribute')
60+
->setSku('simple_with_store_scoped_custom_attribute')
61+
->setPrice(100)
62+
->setVisibility(1)
63+
->setStockData(
64+
[
65+
'use_config_manage_stock' => 1,
66+
'qty' => 100,
67+
'is_in_stock' => 1,
68+
]
69+
)
70+
->setStatus(1);
71+
$product->setCustomAttribute('store_scoped_attribute_code', 'default_value');
72+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Framework\Registry;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
14+
/** @var Magento\Framework\ObjectManagerInterface $objectManager */
15+
$objectManager = Bootstrap::getObjectManager();
16+
/** @var ProductRepositoryInterface $productRepository */
17+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
18+
/** @var ProductAttributeRepositoryInterface $attributeRepository */
19+
$attributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
20+
/** @var Registry $registry */
21+
$registry = $objectManager->get(Registry::class);
22+
23+
$registry->unregister('isSecureArea');
24+
$registry->register('isSecureArea', true);
25+
26+
try {
27+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
28+
$product = $productRepository->get('simple_with_store_scoped_custom_attribute');
29+
$productRepository->delete($product);
30+
} catch (NoSuchEntityException $e) {
31+
}
32+
33+
try {
34+
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
35+
$attribute = $attributeRepository->get('store_scoped_attribute_code');
36+
$attributeRepository->delete($attribute);
37+
} catch (NoSuchEntityException $e) {
38+
}
39+
40+
$registry->unregister('isSecureArea');
41+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)