MassAssignGroupTest.php 4.54 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Customer\Controller\Adminhtml\Index;

use Magento\Backend\Model\Session;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
 * @magentoAppArea adminhtml
 */
class MassAssignGroupTest extends AbstractBackendController
{
    /**
     * Base controller URL
     *
     * @var string
     */
    protected $baseControllerUrl = 'http://localhost/index.php/backend/customer/index/index';

    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerRepository;

    /**
     * @inheritDoc
     *
     * @throws \Magento\Framework\Exception\AuthenticationException
     */
    protected function setUp()
    {
        parent::setUp();
        $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
    }

    /**
     * @inheritDoc
     */
    protected function tearDown()
    {
        /**
         * Unset customer data
         */
        Bootstrap::getObjectManager()->get(Session::class)->setCustomerData(null);

        /**
         * Unset messages
         */
        Bootstrap::getObjectManager()->get(Session::class)->getMessages(true);
    }

    /**
     * Tests os update a single customer record.
     *
     * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
     * @magentoDbIsolation disabled
     */
    public function testMassAssignGroupAction()
    {
        $customerEmail = 'customer1@example.com';
        /** @var CustomerInterface $customer */
        $customer = $this->customerRepository->get($customerEmail);
        $this->assertEquals(1, $customer->getGroupId());

        $params = [
            'group' => 0,
            'namespace' => 'customer_listing',
            'selected' => [$customer->getId()]
        ];

        $this->getRequest()->setParams($params)
            ->setMethod(HttpRequest::METHOD_POST);
        $this->dispatch('backend/customer/index/massAssignGroup');
        $this->assertSessionMessages(
            self::equalTo(['A total of 1 record(s) were updated.']),
            MessageInterface::TYPE_SUCCESS
        );
        $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));

        $customer = $this->customerRepository->get($customerEmail);
        $this->assertEquals(0, $customer->getGroupId());
    }

    /**
     * Tests os update a multiple customer records.
     *
     * @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
     * @magentoDbIsolation disabled
     */
    public function testLargeGroupMassAssignGroupAction()
    {
        $ids = [];
        for ($i = 1; $i <= 5; $i++) {
            /** @var CustomerInterface $customer */
            $customer = $this->customerRepository->get('customer' . $i . '@example.com');
            $this->assertEquals(1, $customer->getGroupId());
            $ids[] = $customer->getId();
        }

        $params = [
            'group' => 0,
            'namespace' => 'customer_listing',
            'selected' => $ids,
        ];

        $this->getRequest()->setParams($params)
            ->setMethod(HttpRequest::METHOD_POST);
        $this->dispatch('backend/customer/index/massAssignGroup');
        $this->assertSessionMessages(
            self::equalTo(['A total of 5 record(s) were updated.']),
            MessageInterface::TYPE_SUCCESS
        );
        $this->assertRedirect($this->stringStartsWith($this->baseControllerUrl));
        for ($i = 1; $i < 5; $i++) {
            /** @var CustomerInterface $customer */
            $customer = $this->customerRepository->get('customer' . $i . '@example.com');
            $this->assertEquals(0, $customer->getGroupId());
        }
    }

    /**
     * Valid group Id but no customer Ids specified
     *
     * @magentoDbIsolation enabled
     */
    public function testMassAssignGroupActionNoCustomerIds()
    {
        $params = ['group'=> 0,'namespace'=> 'customer_listing',
        ];
        $this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
        $this->dispatch('backend/customer/index/massAssignGroup');
        $this->assertSessionMessages(
            $this->equalTo(['An item needs to be selected. Select and try again.']),
            MessageInterface::TYPE_ERROR
        );
    }
}