From dc2c07f396550e25860358f877609829563dd68d Mon Sep 17 00:00:00 2001 From: Dennis Garding Date: Mon, 21 Dec 2020 09:04:31 +0100 Subject: [PATCH] PT-12213 - Check cron time while saving --- Controllers/Backend/SwagTax.php | 24 ++++++++++- Tests/Controllers/Backend/SwagTaxTest.php | 49 +++++++++++++++++++++++ plugin.xml | 7 +++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/Controllers/Backend/SwagTax.php b/Controllers/Backend/SwagTax.php index 3e80e84..b7b845a 100644 --- a/Controllers/Backend/SwagTax.php +++ b/Controllers/Backend/SwagTax.php @@ -26,7 +26,7 @@ public function saveAction() 'tax_mapping' => $params['taxMapping'], 'copy_tax_rules' => (bool) $params['copyTaxRules'], 'customer_group_mapping' => $params['customerGroupMapping'], - 'scheduled_date' => $this->Request()->getParam('scheduledDate'), + 'scheduled_date' => $this->checkCronDate($params['scheduledDate']), ]); } @@ -114,4 +114,26 @@ private function clearTable() { $this->container->get('dbal_connection')->executeQuery(sprintf('TRUNCATE TABLE %s', self::TABLE_NAME)); } + + /** + * @param string $scheduledDate + * + * @return string + */ + private function checkCronDate($scheduledDate) + { + $emptyDate = '0000-00-00 00:00:00'; + if ($scheduledDate === '' || empty($scheduledDate)) { + return $emptyDate; + } + + $scheduledDateTime = new \DateTime($scheduledDate); + $nowDateTime = new \DateTime('NOW'); + + if ($scheduledDateTime > $nowDateTime) { + return $scheduledDate; + } + + return $emptyDate; + } } diff --git a/Tests/Controllers/Backend/SwagTaxTest.php b/Tests/Controllers/Backend/SwagTaxTest.php index 1f6634e..b22287f 100644 --- a/Tests/Controllers/Backend/SwagTaxTest.php +++ b/Tests/Controllers/Backend/SwagTaxTest.php @@ -79,6 +79,55 @@ public function test_saveTaxRateAction_withNameAndTaxRateShouldBe_true() static::assertSame('33 %', $savedTaxResult['description']); } + public function test_checkCronDate_shouldReturnEmptyDate() + { + $controller = $this->getController(); + $emptyDate = '0000-00-00 00:00:00'; + + $reflectionMethod = (new \ReflectionClass(\Shopware_Controllers_Backend_SwagTax::class))->getMethod('checkCronDate'); + $reflectionMethod->setAccessible(true); + + $result = $reflectionMethod->invoke($controller, null); + static::assertSame($emptyDate, $result); + + $result = $reflectionMethod->invoke($controller, ''); + static::assertSame($emptyDate, $result); + + $result = $reflectionMethod->invoke($controller, '0'); + static::assertSame($emptyDate, $result); + + $result = $reflectionMethod->invoke($controller, '0000-00-00 00:00:00'); + static::assertSame($emptyDate, $result); + + $result = $reflectionMethod->invoke($controller, '2020-01-01 00:00:00'); + static::assertSame($emptyDate, $result); + + $result = $reflectionMethod->invoke($controller, '2020-01-01 00:00:00'); + static::assertSame($emptyDate, $result); + + $now = date('Y-m-d H:i:s'); + $past = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($now))); + $result = $reflectionMethod->invoke($controller, $past); + static::assertSame($emptyDate, $result); + } + + public function test_checkCronDate_shouldReturnGivenDate() + { + $controller = $this->getController(); + + $reflectionMethod = (new \ReflectionClass(\Shopware_Controllers_Backend_SwagTax::class))->getMethod('checkCronDate'); + $reflectionMethod->setAccessible(true); + + $future = '2222-02-02 20:20:20'; + $result = $reflectionMethod->invoke($controller, $future); + static::assertSame($future, $result); + + $now = date('Y-m-d H:i:s'); + $future = date('Y-m-d H:i:s', strtotime('+1 hour', strtotime($now))); + $result = $reflectionMethod->invoke($controller, $future); + static::assertSame($future, $result); + } + private function getController() { $controller = new \Shopware_Controllers_Backend_SwagTax(); diff --git a/plugin.xml b/plugin.xml index c7f04ea..d9ed9be 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,7 +5,7 @@ - 2.0.2 + 2.0.3 (c) by shopware AG mit http://store.shopware.com @@ -16,6 +16,11 @@ + + PT-12211 - Check cron time while saving; + PT-12211 - Überprüft die Cron-Zeit beim Speichern; + + PT-12211 - Fixed Shopware 5.2 compatibility; PT-12211 - Shopware 5.2 Kompatibilität hergestellt