123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Adminhtml;
- use Magento\Framework\Message\MessageInterface;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\Framework\App\Request\Http as HttpRequest;
- /**
- * @magentoAppArea adminhtml
- */
- class GroupTest extends \Magento\TestFramework\TestCase\AbstractBackendController
- {
- const TAX_CLASS_ID = 3;
- const TAX_CLASS_NAME = 'Retail Customer';
- const CUSTOMER_GROUP_CODE = 'custom_group';
- const BASE_CONTROLLER_URL = 'http://localhost/index.php/backend/customer/group/';
- const CUSTOMER_GROUP_ID = 2;
- /** @var \Magento\Framework\Session\SessionManagerInterface */
- private $session;
- /** @var \Magento\Customer\Api\GroupRepositoryInterface */
- private $groupRepository;
- /**
- * @inheritDoc
- *
- * @throws \Magento\Framework\Exception\AuthenticationException
- */
- public function setUp()
- {
- parent::setUp();
- $objectManager = Bootstrap::getObjectManager();
- $this->session = $objectManager->get(\Magento\Framework\Session\SessionManagerInterface::class);
- $this->groupRepository = $objectManager->get(\Magento\Customer\Api\GroupRepositoryInterface::class);
- }
- /**
- * @inheritDoc
- */
- public function tearDown()
- {
- parent::tearDown();
- //$this->session->unsCustomerGroupData();
- }
- /**
- * Test new group form.
- */
- public function testNewActionNoCustomerGroupDataInSession()
- {
- $this->dispatch('backend/customer/group/new');
- $responseBody = $this->getResponse()->getBody();
- $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
- $expected = '<input id="customer_group_code" name="code" '
- . 'data-ui-id="group-form-fieldset-element-text-code" value=""';
- $this->assertContains($expected, $responseBody);
- }
- /**
- * Test form filling with data in session.
- */
- public function testNewActionWithCustomerGroupDataInSession()
- {
- /** @var \Magento\Customer\Api\Data\GroupInterfaceFactory $customerGroupFactory */
- $customerGroupFactory = $this->_objectManager
- ->get(\Magento\Customer\Api\Data\GroupInterfaceFactory::class);
- /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
- $customerGroup = $customerGroupFactory->create()
- ->setCode(self::CUSTOMER_GROUP_CODE)
- ->setTaxClassId(self::TAX_CLASS_ID);
- /** @var \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor */
- $dataObjectProcessor = $this->_objectManager->get(\Magento\Framework\Reflection\DataObjectProcessor::class);
- $customerGroupData = $dataObjectProcessor
- ->buildOutputDataArray($customerGroup, \Magento\Customer\Api\Data\GroupInterface::class);
- if (array_key_exists('code', $customerGroupData)) {
- $customerGroupData['customer_group_code'] = $customerGroupData['code'];
- unset($customerGroupData['code']);
- }
- $this->session->setCustomerGroupData($customerGroupData);
- $this->dispatch('backend/customer/group/new');
- $responseBody = $this->getResponse()->getBody();
- $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
- $expected = '<input id="customer_group_code" name="code" '
- . 'data-ui-id="group-form-fieldset-element-text-code" value="' . self::CUSTOMER_GROUP_CODE . '"';
- $this->assertContains($expected, $responseBody);
- }
- /**
- * Test calling delete without an ID.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testDeleteActionNoGroupId()
- {
- $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
- $this->dispatch('backend/customer/group/delete');
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL));
- }
- /**
- * Test deleting a group.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testDeleteActionExistingGroup()
- {
- $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
- $this->getRequest()->setParam('id', $groupId);
- $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
- $this->dispatch('backend/customer/group/delete');
- /**
- * Check that success message is set
- */
- $this->assertSessionMessages(
- $this->equalTo(['You deleted the customer group.']),
- MessageInterface::TYPE_SUCCESS
- );
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'index'));
- }
- /**
- * Tet deleting with wrong ID.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testDeleteActionNonExistingGroupId()
- {
- $this->getRequest()->setParam('id', 10000);
- $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
- $this->dispatch('backend/customer/group/delete');
- /**
- * Check that error message is set
- */
- $this->assertSessionMessages(
- $this->equalTo(['The customer group no longer exists.']),
- MessageInterface::TYPE_ERROR
- );
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL));
- }
- /**
- * Test saving a valid group.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testSaveActionExistingGroup()
- {
- $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
- $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
- $this->getRequest()->setParam('id', $groupId);
- $this->getRequest()->setParam('code', self::CUSTOMER_GROUP_CODE);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
- $this->assertSessionMessages($this->logicalNot($this->isEmpty()), MessageInterface::TYPE_SUCCESS);
- $this->assertSessionMessages(
- $this->equalTo(['You saved the customer group.']),
- MessageInterface::TYPE_SUCCESS
- );
- /** @var \Magento\Framework\Api\SimpleDataObjectConverter $simpleDataObjectConverter */
- $simpleDataObjectConverter = Bootstrap::getObjectManager()
- ->get(\Magento\Framework\Api\SimpleDataObjectConverter::class);
- $customerGroupData = $simpleDataObjectConverter->toFlatArray(
- $this->groupRepository->getById($groupId),
- \Magento\Customer\Api\Data\GroupInterface::class
- );
- ksort($customerGroupData);
- $this->assertEquals(
- [
- 'code' => self::CUSTOMER_GROUP_CODE,
- 'id' => $groupId,
- 'tax_class_id' => self::TAX_CLASS_ID,
- 'tax_class_name' => self::TAX_CLASS_NAME,
- ],
- $customerGroupData
- );
- }
- /**
- * Test saving an invalid group.
- */
- public function testSaveActionCreateNewGroupWithoutCode()
- {
- $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $this->assertSessionMessages(
- $this->equalTo(['"code" is required. Enter and try again.']),
- MessageInterface::TYPE_ERROR
- );
- }
- /**
- * Test saving an empty group.
- */
- public function testSaveActionForwardNewCreateNewGroup()
- {
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $responseBody = $this->getResponse()->getBody();
- $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
- }
- /**
- * Test saving an existing group.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testSaveActionForwardNewEditExistingGroup()
- {
- $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->getRequest()->setParam('id', $groupId);
- $this->dispatch('backend/customer/group/save');
- $responseBody = $this->getResponse()->getBody();
- $this->assertRegExp('/<h1 class\="page-title">\s*' . self::CUSTOMER_GROUP_CODE . '\s*<\/h1>/', $responseBody);
- }
- /**
- * Test using an invalid ID.
- */
- public function testSaveActionNonExistingGroupId()
- {
- $this->getRequest()->setParam('id', 10000);
- $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
- $this->assertSessionMessages($this->logicalNot($this->isEmpty()), MessageInterface::TYPE_ERROR);
- $this->assertSessionMessages(
- $this->equalTo(['No such entity with id = 10000']),
- MessageInterface::TYPE_ERROR
- );
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
- $this->assertEquals(null, $this->session->getCustomerGroupData());
- }
- /**
- * Test using existing code.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testSaveActionNewGroupWithExistingGroupCode()
- {
- $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
- $originalCode = $this->groupRepository->getById($groupId)->getCode();
- $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
- $this->getRequest()->setParam('code', self::CUSTOMER_GROUP_CODE);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $this->assertSessionMessages($this->equalTo(['Customer Group already exists.']), MessageInterface::TYPE_ERROR);
- $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
- $this->assertEquals($originalCode, $this->groupRepository->getById($groupId)->getCode());
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
- $this->assertEquals(self::CUSTOMER_GROUP_CODE, $this->session->getCustomerGroupData()['customer_group_code']);
- $this->assertEquals(self::TAX_CLASS_ID, $this->session->getCustomerGroupData()['tax_class_id']);
- }
- /**
- * Test saving an invalid group.
- *
- * @magentoDataFixture Magento/Customer/_files/customer_group.php
- */
- public function testSaveActionNewGroupWithoutGroupCode()
- {
- $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
- $originalCode = $this->groupRepository->getById($groupId)->getCode();
- $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
- $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
- $this->dispatch('backend/customer/group/save');
- $this->assertSessionMessages(
- $this->equalTo(['"code" is required. Enter and try again.']),
- MessageInterface::TYPE_ERROR
- );
- $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
- $this->assertEquals($originalCode, $this->groupRepository->getById($groupId)->getCode());
- $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
- $this->assertEquals('', $this->session->getCustomerGroupData()['customer_group_code']);
- $this->assertEquals(self::TAX_CLASS_ID, $this->session->getCustomerGroupData()['tax_class_id']);
- }
- /**
- * Find the group with a given code.
- *
- * @param string $code
- * @return int
- */
- protected function findGroupIdWithCode($code)
- {
- /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
- $searchBuilder = $this->_objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
- foreach ($this->groupRepository->getList($searchBuilder->create())->getItems() as $group) {
- if ($group->getCode() === $code) {
- return $group->getId();
- }
- }
- return -1;
- }
- }
|