Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/Helper/Filter/JSON/RecipeIdTypeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace OCA\Cookbook\Helper\Filter\JSON;

use OCA\Cookbook\Helper\Filter\AbstractJSONFilter;

/**
* 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;
}
}
3 changes: 3 additions & 0 deletions lib/Helper/Filter/JSONFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;

Expand All @@ -24,6 +25,7 @@ class JSONFilter {
public function __construct(
SchemaConformityFilter $schemaConformityFilter,
RecipeNameFilter $recipeNameFilter,
RecipeIdTypeFilter $recipeIdTypeFilter,
ExtractImageUrlFilter $extractImageUrlFilter,
FixImageSchemeFilter $fixImageSchemeFilter,
CleanCategoryFilter $cleanCategoryFilter,
Expand All @@ -40,6 +42,7 @@ public function __construct(
$this->filters = [
$schemaConformityFilter,
$recipeNameFilter,
$recipeIdTypeFilter,
$extractImageUrlFilter,
$fixImageSchemeFilter,
$cleanCategoryFilter,
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Helper/Filter/JSON/RecipeIdTypeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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']));
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Helper/Filter/JSONFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -25,6 +26,7 @@ class JSONFilterTest extends TestCase {

private $schemaConformityFilter;
private $recipeNameFilter;
private $recipeIdTypeFilter;
private $extractImageUrlFilter;
private $fixImageSchemeFilter;
private $cleanCategoryFilter;
Expand All @@ -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);
Expand All @@ -58,6 +61,7 @@ protected function setUp(): void {
$this->dut = new JSONFilter(
$this->schemaConformityFilter,
$this->recipeNameFilter,
$this->recipeIdTypeFilter,
$this->extractImageUrlFilter,
$this->fixImageSchemeFilter,
$this->cleanCategoryFilter,
Expand Down Expand Up @@ -88,6 +92,7 @@ public function testSequence() {

$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());
Expand Down