GroupTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml;
  7. use Magento\Framework\Message\MessageInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\Framework\App\Request\Http as HttpRequest;
  10. /**
  11. * @magentoAppArea adminhtml
  12. */
  13. class GroupTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  14. {
  15. const TAX_CLASS_ID = 3;
  16. const TAX_CLASS_NAME = 'Retail Customer';
  17. const CUSTOMER_GROUP_CODE = 'custom_group';
  18. const BASE_CONTROLLER_URL = 'http://localhost/index.php/backend/customer/group/';
  19. const CUSTOMER_GROUP_ID = 2;
  20. /** @var \Magento\Framework\Session\SessionManagerInterface */
  21. private $session;
  22. /** @var \Magento\Customer\Api\GroupRepositoryInterface */
  23. private $groupRepository;
  24. /**
  25. * @inheritDoc
  26. *
  27. * @throws \Magento\Framework\Exception\AuthenticationException
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $objectManager = Bootstrap::getObjectManager();
  33. $this->session = $objectManager->get(\Magento\Framework\Session\SessionManagerInterface::class);
  34. $this->groupRepository = $objectManager->get(\Magento\Customer\Api\GroupRepositoryInterface::class);
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. //$this->session->unsCustomerGroupData();
  43. }
  44. /**
  45. * Test new group form.
  46. */
  47. public function testNewActionNoCustomerGroupDataInSession()
  48. {
  49. $this->dispatch('backend/customer/group/new');
  50. $responseBody = $this->getResponse()->getBody();
  51. $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
  52. $expected = '<input id="customer_group_code" name="code" '
  53. . 'data-ui-id="group-form-fieldset-element-text-code" value=""';
  54. $this->assertContains($expected, $responseBody);
  55. }
  56. /**
  57. * Test form filling with data in session.
  58. */
  59. public function testNewActionWithCustomerGroupDataInSession()
  60. {
  61. /** @var \Magento\Customer\Api\Data\GroupInterfaceFactory $customerGroupFactory */
  62. $customerGroupFactory = $this->_objectManager
  63. ->get(\Magento\Customer\Api\Data\GroupInterfaceFactory::class);
  64. /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
  65. $customerGroup = $customerGroupFactory->create()
  66. ->setCode(self::CUSTOMER_GROUP_CODE)
  67. ->setTaxClassId(self::TAX_CLASS_ID);
  68. /** @var \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor */
  69. $dataObjectProcessor = $this->_objectManager->get(\Magento\Framework\Reflection\DataObjectProcessor::class);
  70. $customerGroupData = $dataObjectProcessor
  71. ->buildOutputDataArray($customerGroup, \Magento\Customer\Api\Data\GroupInterface::class);
  72. if (array_key_exists('code', $customerGroupData)) {
  73. $customerGroupData['customer_group_code'] = $customerGroupData['code'];
  74. unset($customerGroupData['code']);
  75. }
  76. $this->session->setCustomerGroupData($customerGroupData);
  77. $this->dispatch('backend/customer/group/new');
  78. $responseBody = $this->getResponse()->getBody();
  79. $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
  80. $expected = '<input id="customer_group_code" name="code" '
  81. . 'data-ui-id="group-form-fieldset-element-text-code" value="' . self::CUSTOMER_GROUP_CODE . '"';
  82. $this->assertContains($expected, $responseBody);
  83. }
  84. /**
  85. * Test calling delete without an ID.
  86. *
  87. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  88. */
  89. public function testDeleteActionNoGroupId()
  90. {
  91. $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
  92. $this->dispatch('backend/customer/group/delete');
  93. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL));
  94. }
  95. /**
  96. * Test deleting a group.
  97. *
  98. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  99. */
  100. public function testDeleteActionExistingGroup()
  101. {
  102. $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
  103. $this->getRequest()->setParam('id', $groupId);
  104. $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
  105. $this->dispatch('backend/customer/group/delete');
  106. /**
  107. * Check that success message is set
  108. */
  109. $this->assertSessionMessages(
  110. $this->equalTo(['You deleted the customer group.']),
  111. MessageInterface::TYPE_SUCCESS
  112. );
  113. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'index'));
  114. }
  115. /**
  116. * Tet deleting with wrong ID.
  117. *
  118. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  119. */
  120. public function testDeleteActionNonExistingGroupId()
  121. {
  122. $this->getRequest()->setParam('id', 10000);
  123. $this->getRequest()->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST);
  124. $this->dispatch('backend/customer/group/delete');
  125. /**
  126. * Check that error message is set
  127. */
  128. $this->assertSessionMessages(
  129. $this->equalTo(['The customer group no longer exists.']),
  130. MessageInterface::TYPE_ERROR
  131. );
  132. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL));
  133. }
  134. /**
  135. * Test saving a valid group.
  136. *
  137. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  138. */
  139. public function testSaveActionExistingGroup()
  140. {
  141. $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
  142. $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
  143. $this->getRequest()->setParam('id', $groupId);
  144. $this->getRequest()->setParam('code', self::CUSTOMER_GROUP_CODE);
  145. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  146. $this->dispatch('backend/customer/group/save');
  147. $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
  148. $this->assertSessionMessages($this->logicalNot($this->isEmpty()), MessageInterface::TYPE_SUCCESS);
  149. $this->assertSessionMessages(
  150. $this->equalTo(['You saved the customer group.']),
  151. MessageInterface::TYPE_SUCCESS
  152. );
  153. /** @var \Magento\Framework\Api\SimpleDataObjectConverter $simpleDataObjectConverter */
  154. $simpleDataObjectConverter = Bootstrap::getObjectManager()
  155. ->get(\Magento\Framework\Api\SimpleDataObjectConverter::class);
  156. $customerGroupData = $simpleDataObjectConverter->toFlatArray(
  157. $this->groupRepository->getById($groupId),
  158. \Magento\Customer\Api\Data\GroupInterface::class
  159. );
  160. ksort($customerGroupData);
  161. $this->assertEquals(
  162. [
  163. 'code' => self::CUSTOMER_GROUP_CODE,
  164. 'id' => $groupId,
  165. 'tax_class_id' => self::TAX_CLASS_ID,
  166. 'tax_class_name' => self::TAX_CLASS_NAME,
  167. ],
  168. $customerGroupData
  169. );
  170. }
  171. /**
  172. * Test saving an invalid group.
  173. */
  174. public function testSaveActionCreateNewGroupWithoutCode()
  175. {
  176. $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
  177. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  178. $this->dispatch('backend/customer/group/save');
  179. $this->assertSessionMessages(
  180. $this->equalTo(['"code" is required. Enter and try again.']),
  181. MessageInterface::TYPE_ERROR
  182. );
  183. }
  184. /**
  185. * Test saving an empty group.
  186. */
  187. public function testSaveActionForwardNewCreateNewGroup()
  188. {
  189. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  190. $this->dispatch('backend/customer/group/save');
  191. $responseBody = $this->getResponse()->getBody();
  192. $this->assertRegExp('/<h1 class\="page-title">\s*New Customer Group\s*<\/h1>/', $responseBody);
  193. }
  194. /**
  195. * Test saving an existing group.
  196. *
  197. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  198. */
  199. public function testSaveActionForwardNewEditExistingGroup()
  200. {
  201. $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
  202. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  203. $this->getRequest()->setParam('id', $groupId);
  204. $this->dispatch('backend/customer/group/save');
  205. $responseBody = $this->getResponse()->getBody();
  206. $this->assertRegExp('/<h1 class\="page-title">\s*' . self::CUSTOMER_GROUP_CODE . '\s*<\/h1>/', $responseBody);
  207. }
  208. /**
  209. * Test using an invalid ID.
  210. */
  211. public function testSaveActionNonExistingGroupId()
  212. {
  213. $this->getRequest()->setParam('id', 10000);
  214. $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
  215. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  216. $this->dispatch('backend/customer/group/save');
  217. $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
  218. $this->assertSessionMessages($this->logicalNot($this->isEmpty()), MessageInterface::TYPE_ERROR);
  219. $this->assertSessionMessages(
  220. $this->equalTo(['No such entity with id = 10000']),
  221. MessageInterface::TYPE_ERROR
  222. );
  223. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
  224. $this->assertEquals(null, $this->session->getCustomerGroupData());
  225. }
  226. /**
  227. * Test using existing code.
  228. *
  229. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  230. */
  231. public function testSaveActionNewGroupWithExistingGroupCode()
  232. {
  233. $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
  234. $originalCode = $this->groupRepository->getById($groupId)->getCode();
  235. $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
  236. $this->getRequest()->setParam('code', self::CUSTOMER_GROUP_CODE);
  237. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  238. $this->dispatch('backend/customer/group/save');
  239. $this->assertSessionMessages($this->equalTo(['Customer Group already exists.']), MessageInterface::TYPE_ERROR);
  240. $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
  241. $this->assertEquals($originalCode, $this->groupRepository->getById($groupId)->getCode());
  242. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
  243. $this->assertEquals(self::CUSTOMER_GROUP_CODE, $this->session->getCustomerGroupData()['customer_group_code']);
  244. $this->assertEquals(self::TAX_CLASS_ID, $this->session->getCustomerGroupData()['tax_class_id']);
  245. }
  246. /**
  247. * Test saving an invalid group.
  248. *
  249. * @magentoDataFixture Magento/Customer/_files/customer_group.php
  250. */
  251. public function testSaveActionNewGroupWithoutGroupCode()
  252. {
  253. $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
  254. $originalCode = $this->groupRepository->getById($groupId)->getCode();
  255. $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
  256. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  257. $this->dispatch('backend/customer/group/save');
  258. $this->assertSessionMessages(
  259. $this->equalTo(['"code" is required. Enter and try again.']),
  260. MessageInterface::TYPE_ERROR
  261. );
  262. $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_SUCCESS);
  263. $this->assertEquals($originalCode, $this->groupRepository->getById($groupId)->getCode());
  264. $this->assertRedirect($this->stringStartsWith(self::BASE_CONTROLLER_URL . 'edit/'));
  265. $this->assertEquals('', $this->session->getCustomerGroupData()['customer_group_code']);
  266. $this->assertEquals(self::TAX_CLASS_ID, $this->session->getCustomerGroupData()['tax_class_id']);
  267. }
  268. /**
  269. * Find the group with a given code.
  270. *
  271. * @param string $code
  272. * @return int
  273. */
  274. protected function findGroupIdWithCode($code)
  275. {
  276. /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
  277. $searchBuilder = $this->_objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  278. foreach ($this->groupRepository->getList($searchBuilder->create())->getItems() as $group) {
  279. if ($group->getCode() === $code) {
  280. return $group->getId();
  281. }
  282. }
  283. return -1;
  284. }
  285. }