Fix the issue of wrong times in backend answers

Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2024-05-20 16:41:20 +02:00
Родитель 824f416489
Коммит 23f6a08d47
3 изменённых файлов: 78 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,32 @@
<?php
namespace OCA\Cookbook\Helper\Filter\JSON;
/**
* Fix the timestamp of the dates created and modified for recipe stubs
*
* This mainly handles the missing `T` in the date between date and time to be ISO confirm.
*/
class TimestampFixFilter extends AbstractJSONFilter {
public function apply(array &$json): bool {
$changed = false;
foreach(['dateCreated', 'dateModified'] as $key) {
if($json[$key]) {
$json[$key] = $this->handleTimestamp($json[$key], $changed);
}
}
return $changed;
}
private function handleTimestamp(string $value, bool &$changed): string {
$pattern = '/^([0-9]{4}-[0-9]{2}-[0-9]{2}) +(.*)/';
$replacement = '${1}T${2}';
$nv = preg_replace($pattern, $replacement, $value);
if($nv !== $value) {
$changed = true;
}
return $nv;
}
}

Просмотреть файл

@ -5,6 +5,7 @@ namespace OCA\Cookbook\Helper\Filter\Output;
use OCA\Cookbook\Helper\Filter\JSON\AbstractJSONFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdCopyFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdTypeFilter;
use OCA\Cookbook\Helper\Filter\JSON\TimestampFixFilter;
class RecipeStubFilter {
/** @var AbstractJSONFilter[] */
@ -12,11 +13,13 @@ class RecipeStubFilter {
public function __construct(
RecipeIdTypeFilter $recipeIdTypeFilter,
RecipeIdCopyFilter $recipeIdCopyFilter
RecipeIdCopyFilter $recipeIdCopyFilter,
TimestampFixFilter $timestampFixFilter
) {
$this->filters = [
$recipeIdCopyFilter,
$recipeIdTypeFilter,
$timestampFixFilter,
];
}

Просмотреть файл

@ -0,0 +1,42 @@
<?php
namespace OCA\Cookbook\tests\Unit\Helper\Filter\JSON;
use OCA\Cookbook\Helper\Filter\JSON\TimestampFixFilter;
use PHPUnit\Framework\TestCase;
class TimestampFixFilterTest extends TestCase {
/** @var TimestampFixFilter */
private $dut;
protected function setUp(): void {
$this->dut = new TimestampFixFilter();
}
public function dp() {
yield ['2024-01-02T13:15:10', '2024-01-02T13:15:10', false];
yield ['2024-01-02 13:15:10', '2024-01-02T13:15:10', true];
yield ['2024-01-02T13:15:10+00:00', '2024-01-02T13:15:10+00:00', false];
yield ['2024-01-02 13:15:10+00:00', '2024-01-02T13:15:10+00:00', true];
}
private function getStub($time) {
return [
'dateCreated' => $time,
'dateModified' => $time,
];
}
/** @dataProvider dp */
public function testFilter($inputTime, $expectedOutputTime, $changed) {
$input = $this->getStub($inputTime);
$expectedOutput = $this->getStub($expectedOutputTime);
$ret = $this->dut->apply($input);
$this->assertEquals($changed, $ret);
$this->assertSame($expectedOutput, $input);
}
// }
}