|
| 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; |
| 9 | + |
| 10 | +use Magento\Customer\Controller\Plugin\Account as AccountPlugin; |
| 11 | +use Magento\TestFramework\TestCase\AbstractController; |
| 12 | + |
| 13 | +/** |
| 14 | + * Set of Tests to verify that Authentication methods work properly |
| 15 | + */ |
| 16 | +class AuthenticationTest extends AbstractController |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Make sure that customized AccountPlugin was reverted. |
| 20 | + */ |
| 21 | + protected function tearDown() |
| 22 | + { |
| 23 | + $this->resetAllowedActions(); |
| 24 | + parent::tearDown(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * After changes to `di.xml` and overriding list of allowed actions, unallowed ones should cause redirect. |
| 29 | + */ |
| 30 | + public function testExpectRedirectResponseWhenDispatchNotAllowedAction() |
| 31 | + { |
| 32 | + $this->overrideAllowedActions(['notExistingRoute']); |
| 33 | + |
| 34 | + $this->dispatch('customer/account/create'); |
| 35 | + $this->assertRedirect($this->stringContains('customer/account/login')); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Allowed actions should be displayed |
| 40 | + */ |
| 41 | + public function testExpectPageDispatchWhenAllowedAction() |
| 42 | + { |
| 43 | + $this->overrideAllowedActions(['create']); |
| 44 | + |
| 45 | + $this->dispatch('customer/account/create'); |
| 46 | + $this->assertEquals(200, $this->getResponse()->getStatusCode()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Overrides list of `allowedActions` for Authorization Plugin |
| 51 | + * |
| 52 | + * @param string[] $allowedActions |
| 53 | + * @see \Magento\Customer\Controller\Plugin\Account |
| 54 | + */ |
| 55 | + private function overrideAllowedActions(array $allowedActions): void |
| 56 | + { |
| 57 | + $allowedActions = array_combine($allowedActions, $allowedActions); |
| 58 | + $pluginMock = $this->_objectManager->create(AccountPlugin::class, ['allowedActions' => $allowedActions]); |
| 59 | + $this->_objectManager->addSharedInstance($pluginMock, AccountPlugin::class); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Removes all the customizations applied to `allowedActions` |
| 64 | + * @see \Magento\Customer\Controller\Plugin\Account |
| 65 | + */ |
| 66 | + private function resetAllowedActions() |
| 67 | + { |
| 68 | + $this->_objectManager->removeSharedInstance(AccountPlugin::class); |
| 69 | + } |
| 70 | +} |
0 commit comments