|
| 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\Customer\Controller\Adminhtml\Index; |
| 9 | + |
| 10 | +use Magento\Customer\Api\CustomerMetadataInterface; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 13 | +use Magento\Eav\Model\AttributeRepository; |
| 14 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\Framework\Serialize\SerializerInterface; |
| 17 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test inline edit action on customers grid. |
| 23 | + * |
| 24 | + * @magentoAppArea adminhtml |
| 25 | + * @magentoDbIsolation enabled |
| 26 | + */ |
| 27 | +class InlineEditTest extends AbstractBackendController |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var CustomerRepositoryInterface */ |
| 33 | + private $customerRepository; |
| 34 | + |
| 35 | + /** @var SerializerInterface */ |
| 36 | + private $json; |
| 37 | + |
| 38 | + /** @var WebsiteRepositoryInterface */ |
| 39 | + private $websiteRepository; |
| 40 | + |
| 41 | + /** @var AttributeRepository */ |
| 42 | + private $attributeRepository; |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritdoc |
| 46 | + */ |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + parent::setUp(); |
| 50 | + |
| 51 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 52 | + $this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); |
| 53 | + $this->json = $this->objectManager->get(SerializerInterface::class); |
| 54 | + $this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class); |
| 55 | + $this->attributeRepository = $this->objectManager->get(AttributeRepository::class); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @magentoDataFixture Magento/Customer/_files/two_customers.php |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function testInlineEditAction(): void |
| 64 | + { |
| 65 | + $firstCustomer = $this->customerRepository->get('customer@example.com'); |
| 66 | + $secondCustomer = $this->customerRepository->get('customer_two@example.com'); |
| 67 | + $defaultWebsiteId = $this->websiteRepository->get('base')->getId(); |
| 68 | + $genderId = $this->attributeRepository->get(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'gender') |
| 69 | + ->getSource()->getOptionId('Male'); |
| 70 | + $params = [ |
| 71 | + 'items' => [ |
| 72 | + $firstCustomer->getId() => [ |
| 73 | + CustomerInterface::EMAIL => 'updated_customer@example.com', |
| 74 | + CustomerInterface::GROUP_ID => 2, |
| 75 | + CustomerInterface::WEBSITE_ID => $defaultWebsiteId, |
| 76 | + CustomerInterface::TAXVAT => 123123, |
| 77 | + CustomerInterface::GENDER => $genderId, |
| 78 | + ], |
| 79 | + $secondCustomer->getId() => [ |
| 80 | + CustomerInterface::EMAIL => 'updated_customer_two@example.com', |
| 81 | + CustomerInterface::GROUP_ID => 3, |
| 82 | + CustomerInterface::WEBSITE_ID => $defaultWebsiteId, |
| 83 | + CustomerInterface::TAXVAT => 456456, |
| 84 | + CustomerInterface::GENDER => $genderId, |
| 85 | + ], |
| 86 | + ], |
| 87 | + 'isAjax' => true, |
| 88 | + ]; |
| 89 | + $actual = $this->performInlineEditRequest($params); |
| 90 | + $this->assertEmpty($actual['messages']); |
| 91 | + $this->assertFalse($actual['error']); |
| 92 | + $this->assertCustomersData($params); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @dataProvider inlineEditParametersDataProvider |
| 97 | + * |
| 98 | + * @param array $params |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function testInlineEditWithWrongParams(array $params): void |
| 102 | + { |
| 103 | + $actual = $this->performInlineEditRequest($params); |
| 104 | + $this->assertEquals([(string)__('Please correct the data sent.')], $actual['messages']); |
| 105 | + $this->assertTrue($actual['error']); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + public function inlineEditParametersDataProvider(): array |
| 112 | + { |
| 113 | + return [ |
| 114 | + [ |
| 115 | + 'items' => [], |
| 116 | + 'isAjax' => true, |
| 117 | + ], |
| 118 | + [ |
| 119 | + 'items' => [], |
| 120 | + ], |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Perform inline edit request. |
| 126 | + * |
| 127 | + * @param array $params |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + private function performInlineEditRequest(array $params): array |
| 131 | + { |
| 132 | + $this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST); |
| 133 | + $this->dispatch('backend/customer/index/inlineEdit'); |
| 134 | + |
| 135 | + return $this->json->unserialize($this->getResponse()->getBody()); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Assert customers data. |
| 140 | + * |
| 141 | + * @param array $data |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + private function assertCustomersData(array $data): void |
| 145 | + { |
| 146 | + foreach ($data['items'] as $customerId => $expectedData) { |
| 147 | + $customerData = $this->customerRepository->getById($customerId)->__toArray(); |
| 148 | + foreach ($expectedData as $key => $value) { |
| 149 | + $this->assertEquals($value, $customerData[$key]); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments