MassAssignGroupTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Controller\Adminhtml\Index;
  8. use Magento\Backend\Model\Session;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\Framework\App\Request\Http as HttpRequest;
  11. use Magento\Customer\Api\Data\CustomerInterface;
  12. use Magento\Framework\Message\MessageInterface;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use Magento\TestFramework\TestCase\AbstractBackendController;
  15. /**
  16. * @magentoAppArea adminhtml
  17. */
  18. class MassAssignGroupTest extends AbstractBackendController
  19. {
  20. /**
  21. * Base controller URL
  22. *
  23. * @var string
  24. */
  25. protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';
  26. /**
  27. * @var CustomerRepositoryInterface
  28. */
  29. protected $customerRepository;
  30. /**
  31. * @inheritDoc
  32. *
  33. * @throws \Magento\Framework\Exception\AuthenticationException
  34. */
  35. protected function setUp()
  36. {
  37. parent::setUp();
  38. $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. protected function tearDown()
  44. {
  45. /**
  46. * Unset customer data
  47. */
  48. Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);
  49. /**
  50. * Unset messages
  51. */
  52. Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
  53. }
  54. /**
  55. * Tests os update a single customer record.
  56. *
  57. * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
  58. * @magentoDbIsolation disabled
  59. */
  60. public function testMassAssignGroupAction()
  61. {
  62. $customerEmail = 'customer1@example.com';
  63. /** @var CustomerInterface $customer */
  64. $customer = $this->customerRepository->get($customerEmail);
  65. $this->assertEquals(1, $customer->getGroupId());
  66. $params = [
  67. 'group' => 0,
  68. 'namespace' => 'customer_listing',
  69. 'selected' => [$customer->getId()]
  70. ];
  71. $this->getRequest()->setParams($params)
  72. ->setMethod(HttpRequest::METHOD_POST);
  73. $this->dispatch('backend/customer/index/massAssignGroup');
  74. $this->assertSessionMessages(
  75. self::equalTo(['A total of 1 record(s) were updated.']),
  76. MessageInterface::TYPE_SUCCESS
  77. );
  78. $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
  79. $customer = $this->customerRepository->get($customerEmail);
  80. $this->assertEquals(0, $customer->getGroupId());
  81. }
  82. /**
  83. * Tests os update a multiple customer records.
  84. *
  85. * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
  86. * @magentoDbIsolation disabled
  87. */
  88. public function testLargeGroupMassAssignGroupAction()
  89. {
  90. $ids = [];
  91. for ($i = 1; $i <= 5; $i++) {
  92. /** @var CustomerInterface $customer */
  93. $customer = $this->customerRepository->get('customer' . $i . '@example.com');
  94. $this->assertEquals(1, $customer->getGroupId());
  95. $ids[] = $customer->getId();
  96. }
  97. $params = [
  98. 'group' => 0,
  99. 'namespace' => 'customer_listing',
  100. 'selected' => $ids,
  101. ];
  102. $this->getRequest()->setParams($params)
  103. ->setMethod(HttpRequest::METHOD_POST);
  104. $this->dispatch('backend/customer/index/massAssignGroup');
  105. $this->assertSessionMessages(
  106. self::equalTo(['A total of 5 record(s) were updated.']),
  107. MessageInterface::TYPE_SUCCESS
  108. );
  109. $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
  110. for ($i = 1; $i < 5; $i++) {
  111. /** @var CustomerInterface $customer */
  112. $customer = $this->customerRepository->get('customer' . $i . '@example.com');
  113. $this->assertEquals(0, $customer->getGroupId());
  114. }
  115. }
  116. /**
  117. * Valid group Id but no customer Ids specified
  118. *
  119. * @magentoDbIsolation enabled
  120. */
  121. public function testMassAssignGroupActionNoCustomerIds()
  122. {
  123. $params = ['group'=> 0,'namespace'=> 'customer_listing',
  124. ];
  125. $this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
  126. $this->dispatch('backend/customer/index/massAssignGroup');
  127. $this->assertSessionMessages(
  128. $this->equalTo(['An item needs to be selected. Select and try again.']),
  129. MessageInterface::TYPE_ERROR
  130. );
  131. }
  132. }