Implement filter to ensure string representation of id in returned values

Signed-off-by: Christian Wolf <git@christianwolf.email>
This commit is contained in:
Nikolas Rimikis 2023-02-04 10:42:43 +01:00 коммит произвёл Christian Wolf
Родитель 66e27202b4
Коммит e8d2926738
5 изменённых файлов: 71 добавлений и 0 удалений

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

@ -13,6 +13,8 @@
[#1446](https://github.com/nextcloud/cookbook/pull/1446) @christianlupus
- Add network logging for responses, not only requests
[1405](https://github.com/nextcloud/cookbook/pull/1405) @MarcelRobitaille
- Make the server api compliant
[#1464](https://github.com/nextcloud/cookbook/pull/1464) @leptopoda
### Maintenance
- Update dependency for GitHub pages builder

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

@ -0,0 +1,20 @@
<?php
namespace OCA\Cookbook\Helper\Filter\JSON;
use OCA\Cookbook\Exception\InvalidRecipeException;
use OCA\Cookbook\Helper\Filter\AbstractJSONFilter;
use OCP\IL10N;
/**
* Fix the data type of the id of a recipe.
*
* The id should be a string and no integer.
*/
class RecipeIdTypeFilter extends AbstractJSONFilter {
public function apply(array &$json): bool {
$copy = $json;
$json['id'] = strval($json['id']);
return $json !== $copy;
}
}

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

@ -14,6 +14,7 @@ use OCA\Cookbook\Helper\Filter\JSON\FixNutritionFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixRecipeYieldFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixToolsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixUrlFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdTypeFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeNameFilter;
use OCA\Cookbook\Helper\Filter\JSON\SchemaConformityFilter;
@ -24,6 +25,7 @@ class JSONFilter {
public function __construct(
SchemaConformityFilter $schemaConformityFilter,
RecipeNameFilter $recipeNameFilter,
RecipeIdTypeFilter $recipeIdTypeFilter,
ExtractImageUrlFilter $extractImageUrlFilter,
FixImageSchemeFilter $fixImageSchemeFilter,
CleanCategoryFilter $cleanCategoryFilter,
@ -40,6 +42,7 @@ class JSONFilter {
$this->filters = [
$schemaConformityFilter,
$recipeNameFilter,
$recipeIdTypeFilter,
$extractImageUrlFilter,
$fixImageSchemeFilter,
$cleanCategoryFilter,

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

@ -0,0 +1,41 @@
<?php
namespace OCA\Cookbook\tests\Unit\Helper\Filter\JSON;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdTypeFilter;
use Test\TestCase;
class RecipeIdTypeFilterTest extends TestCase {
/** @var RecipeIdTypeFilter */
private $dut;
protected function setUp(): void
{
$this->dut = new RecipeIdTypeFilter();
}
public function dp() {
$stub = [
'name' => 'The name of the recipe',
'servings' => 5,
'ingredients' => ['Spaghetti', 'Tomatoes', 'Salt'],
];
$stub['id'] = 123;
$expected = $stub;
yield [$stub, $expected, true];
$stub['id'] = '123';
yield [$stub, $expected, false];
}
/** @dataProvider dp */
public function testFilter($input, $expected, $changed) {
$ret = $this->dut->apply($input);
$this->assertEquals($expected, $input);
$this->assertEquals($changed, $ret);
$this->assertTrue(is_string($input['id']));
}
}

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

@ -14,6 +14,7 @@ use OCA\Cookbook\Helper\Filter\JSON\FixNutritionFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixRecipeYieldFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixToolsFilter;
use OCA\Cookbook\Helper\Filter\JSON\FixUrlFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeIdTypeFilter;
use OCA\Cookbook\Helper\Filter\JSON\RecipeNameFilter;
use OCA\Cookbook\Helper\Filter\JSON\SchemaConformityFilter;
use OCA\Cookbook\Helper\Filter\JSONFilter;
@ -25,6 +26,7 @@ class JSONFilterTest extends TestCase {
private $schemaConformityFilter;
private $recipeNameFilter;
private $recipeIdTypeFilter;
private $extractImageUrlFilter;
private $fixImageSchemeFilter;
private $cleanCategoryFilter;
@ -41,6 +43,7 @@ class JSONFilterTest extends TestCase {
protected function setUp(): void {
$this->schemaConformityFilter = $this->createStub(SchemaConformityFilter::class);
$this->recipeNameFilter = $this->createStub(RecipeNameFilter::class);
$this->recipeIdTypeFilter = $this->createStub(RecipeIdTypeFilter::class);
$this->extractImageUrlFilter = $this->createStub(ExtractImageUrlFilter::class);
$this->fixImageSchemeFilter = $this->createStub(FixImageSchemeFilter::class);
$this->cleanCategoryFilter = $this->createStub(CleanCategoryFilter::class);
@ -58,6 +61,7 @@ class JSONFilterTest extends TestCase {
$this->dut = new JSONFilter(
$this->schemaConformityFilter,
$this->recipeNameFilter,
$this->recipeIdTypeFilter,
$this->extractImageUrlFilter,
$this->fixImageSchemeFilter,
$this->cleanCategoryFilter,
@ -88,6 +92,7 @@ class JSONFilterTest extends TestCase {
$this->schemaConformityFilter->method('apply')->willReturnCallback($closure());
$this->recipeNameFilter->method('apply')->willReturnCallback($closure());
$this->recipeIdTypeFilter->method('apply')->willReturnCallback($closure());
$this->extractImageUrlFilter->method('apply')->willReturnCallback($closure());
$this->fixImageSchemeFilter->method('apply')->willReturnCallback($closure());
$this->cleanCategoryFilter->method('apply')->willReturnCallback($closure());