Skip to content

Commit

Permalink
Merge pull request #39 from shopwareLabs/ntr/5.7/prepare-plugin-xml
Browse files Browse the repository at this point in the history
PT-12213 - Check cron time while saving
  • Loading branch information
Dennis Garding authored Dec 21, 2020
2 parents ec57ad9 + dc2c07f commit a33b340
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
24 changes: 23 additions & 1 deletion Controllers/Backend/SwagTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
]);
}

Expand Down Expand Up @@ -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;
}
}
49 changes: 49 additions & 0 deletions Tests/Controllers/Backend/SwagTaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<label lang="de">Steuern ändern</label>
<label lang="en">Change taxes</label>

<version>2.0.2</version>
<version>2.0.3</version>
<copyright>(c) by shopware AG</copyright>
<license>mit</license>
<link>http://store.shopware.com</link>
Expand All @@ -16,6 +16,11 @@

<compatibility minVersion="5.2.0"/>

<changelog version="2.0.3">
<changes lang="en">PT-12211 - Check cron time while saving;</changes>
<changes lang="de">PT-12211 - Überprüft die Cron-Zeit beim Speichern;</changes>
</changelog>

<changelog version="2.0.2">
<changes lang="en">PT-12211 - Fixed Shopware 5.2 compatibility;</changes>
<changes lang="de">PT-12211 - Shopware 5.2 Kompatibilität hergestellt</changes>
Expand Down

0 comments on commit a33b340

Please sign in to comment.