Skip to content

Commit 9308b07

Browse files
committed
MC-24930: Admin: Edit product Attribute
1 parent 333e8af commit 9308b07

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
namespace Magento\TestFramework\Eav\Model\Attribute\DataProvider;
9+
10+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
11+
use Magento\Store\Model\Store;
12+
13+
/**
14+
* Product attribute data for attribute with input type datetime.
15+
*/
16+
class DateTime extends AbstractBaseAttributeData
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function __construct()
22+
{
23+
parent::__construct();
24+
$this->defaultAttributePostData['used_for_sort_by'] = '0';
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function getAttributeData(): array
31+
{
32+
return array_replace_recursive(
33+
parent::getAttributeData(),
34+
[
35+
"{$this->getFrontendInput()}_with_default_value" => [
36+
[
37+
'default_value_text' => '',
38+
'default_value_datetime' => '02/4/2020 6:30 AM',
39+
]
40+
]
41+
]
42+
);
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getAttributeDataWithCheckArray(): array
49+
{
50+
return array_replace_recursive(
51+
parent::getAttributeDataWithCheckArray(),
52+
[
53+
"{$this->getFrontendInput()}_with_default_value" => [
54+
1 => [
55+
'default_value' => '2020-02-04 06:30:00',
56+
],
57+
],
58+
]
59+
);
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
public function getUpdateProvider(): array
66+
{
67+
$frontendInput = $this->getFrontendInput();
68+
return array_replace_recursive(
69+
parent::getUpdateProvider(),
70+
[
71+
"{$frontendInput}_other_attribute_code" => [
72+
'post_data' => [
73+
'attribute_code' => 'text_attribute_update',
74+
],
75+
'expected_data' => [
76+
'attribute_code' => 'datetime_attribute',
77+
],
78+
],
79+
]
80+
);
81+
}
82+
83+
/**
84+
* @inheritdoc
85+
*/
86+
public function getUpdateProviderWithErrorMessage(): array
87+
{
88+
$frontendInput = $this->getFrontendInput();
89+
return array_replace_recursive(
90+
parent::getUpdateProviderWithErrorMessage(),
91+
[
92+
"{$frontendInput}_wrong_default_value" => [
93+
'post_data' => [
94+
'default_value_datetime' => '//02/4/2020 6:30 AM',
95+
],
96+
'error_message' => (string)__('The default date is invalid. Verify the date and try again.'),
97+
],
98+
]
99+
);
100+
}
101+
102+
/**
103+
* @inheritdoc
104+
*/
105+
protected function getFrontendInput(): string
106+
{
107+
return 'datetime';
108+
}
109+
110+
/**
111+
* @inheritdoc
112+
*/
113+
protected function getUpdatePostData(): array
114+
{
115+
return [
116+
'frontend_label' => [
117+
Store::DEFAULT_STORE_ID => 'Date Time Attribute Update',
118+
],
119+
'frontend_input' => 'datetime',
120+
'is_required' => '1',
121+
'is_global' => ScopedAttributeInterface::SCOPE_WEBSITE,
122+
'default_value_datetime' => '02/4/2020 6:30 AM',
123+
'is_unique' => '1',
124+
'is_used_in_grid' => '1',
125+
'is_visible_in_grid' => '1',
126+
'is_filterable_in_grid' => '1',
127+
'is_searchable' => '1',
128+
'search_weight' => '2',
129+
'is_visible_in_advanced_search' => '1',
130+
'is_comparable' => '1',
131+
'is_used_for_promo_rules' => '1',
132+
'is_html_allowed_on_front' => '1',
133+
'is_visible_on_front' => '1',
134+
'used_in_product_listing' => '0',
135+
'used_for_sort_by' => '1',
136+
];
137+
}
138+
139+
/**
140+
* @inheritdoc
141+
*/
142+
protected function getUpdateExpectedData(): array
143+
{
144+
$updatePostData = $this->getUpdatePostData();
145+
unset($updatePostData['default_value_datetime']);
146+
return array_merge(
147+
$updatePostData,
148+
[
149+
'frontend_label' => 'Date Time Attribute Update',
150+
'attribute_code' => 'datetime_attribute',
151+
'default_value' => '2020-02-04 06:30:00',
152+
'frontend_class' => null,
153+
'is_filterable' => '0',
154+
'is_filterable_in_search' => '0',
155+
'position' => '0',
156+
'is_user_defined' => '1',
157+
'backend_type' => 'datetime',
158+
]
159+
);
160+
}
161+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
11+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
12+
use Magento\Catalog\Setup\CategorySetup;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
/** @var CategorySetup $installer */
17+
$installer = $objectManager->create(CategorySetup::class);
18+
$entityType = $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE);
19+
/** @var ProductAttributeRepositoryInterface $attributeRepository */
20+
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
21+
/** @var Attribute $attribute */
22+
$attribute = $objectManager->get(AttributeFactory::class)->create();
23+
if (!$attribute->loadByCode($entityType, 'datetime_attribute')->getAttributeId()) {
24+
$attribute->setData(
25+
[
26+
'attribute_code' => 'datetime_attribute',
27+
'entity_type_id' => $entityType,
28+
'is_global' => 1,
29+
'is_user_defined' => 1,
30+
'frontend_input' => 'datetime',
31+
'is_unique' => 0,
32+
'is_required' => 0,
33+
'is_searchable' => 0,
34+
'is_visible_in_advanced_search' => 0,
35+
'is_comparable' => 0,
36+
'is_filterable' => 0,
37+
'is_filterable_in_search' => 0,
38+
'is_used_for_promo_rules' => 0,
39+
'is_html_allowed_on_front' => 1,
40+
'is_visible_on_front' => 0,
41+
'used_in_product_listing' => 1,
42+
'used_for_sort_by' => 0,
43+
'frontend_label' => ['Date Time Attribute'],
44+
'backend_type' => 'datetime',
45+
]
46+
);
47+
$attributeRepository->save($attribute);
48+
$installer->addAttributeToGroup(
49+
ProductAttributeInterface::ENTITY_TYPE_CODE,
50+
'Default',
51+
'General',
52+
$attribute->getId()
53+
);
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Exception\NoSuchEntityException;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Framework\Registry;
11+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
$registry = $objectManager->get(Registry::class);
15+
$registry->unregister('isSecureArea');
16+
$registry->register('isSecureArea', true);
17+
/** @var ProductAttributeRepositoryInterface $attributeRepository */
18+
$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class);
19+
20+
try {
21+
$attributeRepository->deleteById('datetime_attribute');
22+
} catch (NoSuchEntityException $e) {
23+
}
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
9+
10+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
11+
12+
/**
13+
* Test cases related to create attribute with input type datetime.
14+
*
15+
* @magentoDbIsolation enabled
16+
* @magentoAppArea adminhtml
17+
*/
18+
class DateTimeTest extends AbstractSaveAttributeTest
19+
{
20+
/**
21+
* Test create attribute and compare attribute data and input data.
22+
*
23+
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getAttributeDataWithCheckArray
24+
* @magentoConfigFixture default/general/locale/timezone UTC
25+
*
26+
* @param array $attributePostData
27+
* @param array $checkArray
28+
* @return void
29+
*/
30+
public function testCreateAttribute(array $attributePostData, array $checkArray): void
31+
{
32+
$this->createAttributeUsingDataAndAssert($attributePostData, $checkArray);
33+
}
34+
35+
/**
36+
* Test create attribute with error.
37+
*
38+
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getAttributeDataWithErrorMessage
39+
*
40+
* @param array $attributePostData
41+
* @param string $errorMessage
42+
* @return void
43+
*/
44+
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
45+
{
46+
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
9+
10+
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
11+
12+
/**
13+
* Test cases related to update attribute with input type date.
14+
*
15+
* @magentoDbIsolation enabled
16+
* @magentoAppArea adminhtml
17+
*/
18+
class DateTimeTest extends AbstractUpdateAttributeTest
19+
{
20+
/**
21+
* Test update attribute.
22+
*
23+
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateProvider
24+
* @magentoConfigFixture default/general/locale/timezone UTC
25+
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
26+
*
27+
* @param array $postData
28+
* @param array $expectedData
29+
* @return void
30+
*/
31+
public function testUpdateAttribute(array $postData, array $expectedData): void
32+
{
33+
$this->updateAttributeUsingData('datetime_attribute', $postData);
34+
$this->assertUpdateAttributeProcess('datetime_attribute', $postData, $expectedData);
35+
}
36+
37+
/**
38+
* Test update attribute with error.
39+
*
40+
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateProviderWithErrorMessage
41+
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
42+
*
43+
* @param array $postData
44+
* @param string $errorMessage
45+
* @return void
46+
*/
47+
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
48+
{
49+
$this->updateAttributeUsingData('datetime_attribute', $postData);
50+
$this->assertErrorSessionMessages($errorMessage);
51+
}
52+
53+
/**
54+
* Test update attribute frontend labels on stores.
55+
*
56+
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateFrontendLabelsProvider
57+
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
58+
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
59+
*
60+
* @param array $postData
61+
* @param array $expectedData
62+
* @return void
63+
*/
64+
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
65+
{
66+
$this->processUpdateFrontendLabelOnStores('datetime_attribute', $postData, $expectedData);
67+
}
68+
}

0 commit comments

Comments
 (0)