From aec1858babcab46ed30fb5bc1bca2219bc732215 Mon Sep 17 00:00:00 2001 From: serhii balko Date: Thu, 22 Feb 2018 13:23:05 +0200 Subject: [PATCH] [Forwardport] Add option to add IP address to existing list + add test --- .../Command/MaintenanceAllowIpsCommand.php | 10 ++++ .../MaintenanceAllowIpsCommandTest.php | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/setup/src/Magento/Setup/Console/Command/MaintenanceAllowIpsCommand.php b/setup/src/Magento/Setup/Console/Command/MaintenanceAllowIpsCommand.php index 5445bca8713e5..c2f4d6fce0d59 100644 --- a/setup/src/Magento/Setup/Console/Command/MaintenanceAllowIpsCommand.php +++ b/setup/src/Magento/Setup/Console/Command/MaintenanceAllowIpsCommand.php @@ -24,6 +24,7 @@ class MaintenanceAllowIpsCommand extends AbstractSetupCommand */ const INPUT_KEY_IP = 'ip'; const INPUT_KEY_NONE = 'none'; + const INPUT_KEY_ADD = 'add'; /** * @var MaintenanceMode @@ -69,6 +70,12 @@ protected function configure() InputOption::VALUE_NONE, 'Clear allowed IP addresses' ), + new InputOption( + self::INPUT_KEY_ADD, + null, + InputOption::VALUE_NONE, + 'Add the IP address to existing list' + ), ]; $this->setName('maintenance:allow-ips') ->setDescription('Sets maintenance mode exempt IPs') @@ -91,6 +98,9 @@ protected function execute(InputInterface $input, OutputInterface $output) } if (!empty($addresses)) { + if ($input->getOption(self::INPUT_KEY_ADD)) { + $addresses = array_unique(array_merge($this->maintenanceMode->getAddressInfo(), $addresses)); + } $this->maintenanceMode->setAddresses(implode(',', $addresses)); $output->writeln( 'Set exempt IP-addresses: ' . implode(' ', $this->maintenanceMode->getAddressInfo()) . diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/MaintenanceAllowIpsCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/MaintenanceAllowIpsCommandTest.php index 35af019436d71..cce7a2e39adfc 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/MaintenanceAllowIpsCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/MaintenanceAllowIpsCommandTest.php @@ -66,6 +66,33 @@ public function testExecute(array $input, array $validatorMessages, $expectedMes $this->assertEquals($expectedMessage, $tester->getDisplay()); } + /** + * @param array $addressInfo + * @param array $input + * @param array $validatorMessages + * @param string $expectedMessage + * @dataProvider executeWithAddDataProvider + */ + public function testExecuteWithAdd(array $addressInfo, array $input, array $validatorMessages, $expectedMessage) + { + $newAddressInfo = array_unique(array_merge($addressInfo, $input['ip'])); + + $this->ipValidator->expects($this->once())->method('validateIps')->willReturn($validatorMessages); + $this->maintenanceMode + ->expects($this->once()) + ->method('setAddresses') + ->with(implode(',', $newAddressInfo)); + + $this->maintenanceMode + ->expects($this->exactly(2)) + ->method('getAddressInfo') + ->willReturnOnConsecutiveCalls($addressInfo, $newAddressInfo); + + $tester = new CommandTester($this->command); + $tester->execute($input); + $this->assertEquals($expectedMessage, $tester->getDisplay()); + } + /** * return array */ @@ -99,4 +126,31 @@ public function executeDataProvider() ] ]; } + + /** + * return array + */ + public function executeWithAddDataProvider() + { + return [ + [ + [], + ['ip' => ['127.0.0.1'], '--add' => true], + [], + 'Set exempt IP-addresses: 127.0.0.1' . PHP_EOL, + ], + [ + ['127.0.0.1'], + ['ip' => ['127.0.0.1'], '--add' => true], + [], + 'Set exempt IP-addresses: 127.0.0.1' . PHP_EOL, + ], + [ + ['127.0.0.1'], + ['ip' => ['127.0.0.2'], '--add' => true], + [], + 'Set exempt IP-addresses: 127.0.0.1 127.0.0.2' . PHP_EOL, + ], + ]; + } }