Skip to content

Commit

Permalink
Merge pull request #1864 from seyfeb/fix/single-tool-import-support
Browse files Browse the repository at this point in the history
WIP: Fix/single tool import support
  • Loading branch information
christianlupus committed Nov 15, 2023
2 parents 51278c5 + 5ec75c4 commit 9bbeb0c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -29,8 +29,10 @@
[#1834](https://github.com/nextcloud/cookbook/pull/1834) @christianlupus
- Hode the button to copy ingredients unless there are some ingredients to copy
[#1844](https://github.com/nextcloud/cookbook/pull/1844) @christianlupus
- Allow single tool in JSON+LD import, fixes #1641
[#1864](https://github.com/nextcloud/cookbook/pull/1844) @seyfeb
- Allow parsing more ISO 8601 duration strings. See issue [#1749](https://github.com/nextcloud/cookbook/issues/1749)
[#XX](https://github.com/nextcloud/cookbook/pull/XX) @seyfeb
[#1871](https://github.com/nextcloud/cookbook/pull/1871) @seyfeb

### Maintenance
- Fix URL of Transifex after upstream subdomain change
Expand Down
12 changes: 6 additions & 6 deletions lib/Helper/Filter/JSON/FixDescriptionFilter.php
Expand Up @@ -16,7 +16,7 @@
* Apart from that the description is cleaned from malicious chars.
*/
class FixDescriptionFilter extends AbstractJSONFilter {
private const NUTRITION = 'description';
private const DESCRIPTION = 'description';

/** @var IL10N */
private $l;
Expand All @@ -38,16 +38,16 @@ public function __construct(
}

public function apply(array &$json): bool {
if (!isset($json[self::NUTRITION])) {
$json[self::NUTRITION] = '';
if (!isset($json[self::DESCRIPTION])) {
$json[self::DESCRIPTION] = '';
return true;
}

$description = $this->textCleaner->cleanUp($json[self::NUTRITION], false);
$description = $this->textCleaner->cleanUp($json[self::DESCRIPTION], false);
$description = trim($description);

$changed = $description !== $json[self::NUTRITION];
$json[self::NUTRITION] = $description;
$changed = $description !== $json[self::DESCRIPTION];
$json[self::DESCRIPTION] = $description;
return $changed;
}
}
30 changes: 20 additions & 10 deletions lib/Helper/Filter/JSON/FixToolsFilter.php
Expand Up @@ -41,20 +41,30 @@ public function apply(array &$json): bool {
return true;
}

if (!is_array($json[self::TOOLS])) {
throw new InvalidRecipeException($this->l->t('Could not parse recipe tools. It is no array.'));
if (!is_array($json[self::TOOLS]) && !is_string($json[self::TOOLS])) {
throw new InvalidRecipeException($this->l->t('Could not parse recipe tools. Expected array or string.'));
}

$tools = $json[self::TOOLS];
$tools = array();

$tools = array_map(function ($t) {
$t = trim($t);
if (!is_array($json[self::TOOLS])) {
$t = trim($json[self::TOOLS]);
$t = $this->textCleaner->cleanUp($t, false);
return $t;
}, $tools);
$tools = array_filter($tools, fn ($t) => ($t));
ksort($tools);
$tools = array_values($tools);

// Empty string would mean no tools (i.e., empty array)
if($t != "") {
$tools[] = $t;
}
} else {
$tools = array_map(function ($t) {
$t = trim($t);
$t = $this->textCleaner->cleanUp($t, false);
return $t;
}, $json[self::TOOLS]);
$tools = array_filter($tools, fn ($t) => ($t));
ksort($tools);
$tools = array_values($tools);
}

$changed = $tools !== $json[self::TOOLS];
$json[self::TOOLS] = $tools;
Expand Down
4 changes: 3 additions & 1 deletion tests/Integration/Helper/Filter/JSON/FixToolsFilterTest.php
Expand Up @@ -40,7 +40,9 @@ protected function setUp(): void {
public function dp() {
return [
[['a','b','c'], ['a','b','c'], false],
[[" a \tb ",' c '],['a b','c'],true],
[[" a \tb ",' c '], ['a b','c'], true],
['a', ['a'], true],
[" a \tb ", ['a b'], true],
];
}

Expand Down
15 changes: 4 additions & 11 deletions tests/Unit/Helper/Filter/JSON/FixToolsFilterTest.php
Expand Up @@ -2,7 +2,6 @@

namespace OCA\Cookbook\tests\Unit\Helper\Filter\JSON;

use OCA\Cookbook\Exception\InvalidRecipeException;
use OCA\Cookbook\Helper\Filter\JSON\FixToolsFilter;
use OCA\Cookbook\Helper\TextCleanupHelper;
use OCP\IL10N;
Expand Down Expand Up @@ -51,6 +50,10 @@ public function dp() {
[['a','b','c'], ['a','b','c'], false],
[[' a ',''], ['a'], true],
[[' a ','', 'b'], ['a', 'b'], true],
['a', ['a'], true],
['', [], true],
// The text cleaner is only stubbed, so the multiple inline spaces are not fixed here.
[" a \tb ", ["a \tb"], true],
];
}

Expand All @@ -67,14 +70,4 @@ public function testApply($startVal, $expectedVal, $changed) {
$this->assertEquals($changed, $ret);
$this->assertEquals($this->stub, $recipe);
}

public function testApplyString() {
$recipe = $this->stub;
$recipe['tool'] = 'some text';

$this->textCleanupHelper->method('cleanUp')->willReturnArgument(0);
$this->expectException(InvalidRecipeException::class);

$this->dut->apply($recipe);
}
}

0 comments on commit 9bbeb0c

Please sign in to comment.