Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filters to allow for multi-line text in some fields #1162

Merged
merged 3 commits into from
Aug 22, 2022
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
4 changes: 2 additions & 2 deletions .github/actions/run-tests/test_runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def runTests(self, args, subArgs, db):

sp = p.pr.run(cmd, env=env)

l.logger.printSetOutput('silent-fail', 'false')
l.logger.printSetOutput('silentFail', 'false')
if sp.returncode != 0:
if args.install_untested:
l.logger.printWarning('Failed testing with setting --install-untested')
l.logger.printSetOutput('silent-fail', 'true')
l.logger.printSetOutput('silentFail', 'true')
return 0
else:
return sp.returncode
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
[#1119](https://github.com/nextcloud/cookbook/pull/1119) @MarcelRobitaille
- Reactivate step debugging in PHP
[#1160](https://github.com/nextcloud/cookbook/pull/1160) @christianlupus
- Fix multi-line code entry in some fields during editing
[#1162](https://github.com/nextcloud/cookbook/pull/1162) @christianlupus

### Maintenance
- Add composer.json to version control to have unique installed dependency versions
Expand Down
3 changes: 1 addition & 2 deletions lib/Helper/Filter/JSON/FixIngredientsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function apply(array &$json): bool {

$ingredients = array_map(function ($t) {
$t = trim($t);
$t = preg_replace('/\s+/', ' ', $t);
$t = $this->textCleaner->cleanUp($t);
$t = $this->textCleaner->cleanUp($t, false);
return $t;
}, $ingredients);
$ingredients = array_values($ingredients);
Expand Down
1 change: 0 additions & 1 deletion lib/Helper/Filter/JSON/FixInstructionsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function apply(array &$json): bool {

$instructions = array_map(function ($x) {
$x = trim($x);
$x = preg_replace('/\s+/', ' ', $x);
$x = $this->textCleaner->cleanUp($x, false);

return $x;
Expand Down
3 changes: 1 addition & 2 deletions lib/Helper/Filter/JSON/FixToolsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function apply(array &$json): bool {

$tools = array_map(function ($t) {
$t = trim($t);
$t = preg_replace('/\s+/', ' ', $t);
$t = $this->textCleaner->cleanUp($t);
$t = $this->textCleaner->cleanUp($t, false);
return $t;
}, $tools);
$tools = array_filter($tools, fn ($t) => ($t));
Expand Down
70 changes: 70 additions & 0 deletions tests/Integration/Helper/Filter/JSON/FixIngredientsFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

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

use OCP\IL10N;
use OCP\ILogger;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use OCA\Cookbook\Helper\TextCleanupHelper;
use OCA\Cookbook\Exception\InvalidRecipeException;
use OCA\Cookbook\Helper\Filter\JSON\FixIngredientsFilter;

class FixIngredientsFilterTest extends TestCase {
/** @var FixIngredientsFilter */
private $dut;

/** @var TextCleanupHelper */
private $textCleanupHelper;

/** @var array */
private $stub;

protected function setUp(): void {
/** @var Stub|IL10N $l */
$l = $this->createStub(IL10N::class);
$l->method('t')->willReturnArgument(0);

/** @var ILogger */
$logger = $this->createStub(ILogger::class);

$this->textCleanupHelper = new TextCleanupHelper();

$this->dut = new FixIngredientsFilter($l, $logger, $this->textCleanupHelper);

$this->stub = [
'name' => 'The name of the recipe',
'id' => 1234,
];
}

public function dp() {
return [
[['a','b','c'], ['a','b','c'], false],
[[' a ',''], ['a'], true],
[[" a \tb ",' c '],['a b','c'],true],
[["a\nb"],["a\nb"],false],
];
}

/** @dataProvider dp */
public function testApply($startVal, $expectedVal, $changed) {
$recipe = $this->stub;
$recipe['recipeIngredient'] = $startVal;

$ret = $this->dut->apply($recipe);

$this->stub['recipeIngredient'] = $expectedVal;
$this->assertEquals($changed, $ret);
$this->assertEquals($this->stub, $recipe);
}

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

$this->expectException(InvalidRecipeException::class);

$this->dut->apply($recipe);
}
}
81 changes: 81 additions & 0 deletions tests/Integration/Helper/Filter/JSON/FixInstructionsFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

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

use OCP\IL10N;
use OCP\ILogger;
use PHPUnit\Framework\TestCase;
use OCA\Cookbook\Service\JsonService;
use PHPUnit\Framework\MockObject\Stub;
use OCA\Cookbook\Helper\TextCleanupHelper;
use OCA\Cookbook\Helper\Filter\JSON\FixInstructionsFilter;

/**
* @covers OCA\Cookbook\Helper\Filter\JSON\FixInstructionsFilter
* @covers OCA\Cookbook\Exception\InvalidRecipeException
*/
class FixInstructionsFilterTest extends TestCase {
/** @var FixInstructionsFilter */
private $dut;

/** @var TextCleanupHelper */
private $textCleanupHelper;

/** @var array */
private $stub;

protected function setUp(): void {
/** @var Stub|IL10N $l */
$l = $this->createStub(IL10N::class);
$l->method('t')->willReturnArgument(0);

/** @var ILogger */
$logger = $this->createStub(ILogger::class);

$this->textCleanupHelper = new TextCleanupHelper();
$jsonService = new JsonService();

$this->dut = new FixInstructionsFilter($l, $logger, $this->textCleanupHelper, $jsonService);

$this->stub = [
'name' => 'The name of the recipe',
'id' => 1234,
];
}

public function dpParseInstructions() {
yield 'Instructions in multiple lines' => [
"a\nb\rc\r\nd",
['a','b','c','d'], true
];
yield 'Instructions with HTML Entities' => [
"<p>a</p>\n<ul><li>b</li><li>c</li></ul><p>d</p>",
['a','b','c','d'], true
];
yield 'Instructions with empty HTML Entities' => [
"a\n<p></p><ul><li></li><li></li></ul><p></p>\nb\nc\nd",
['a','b','c','d'], true
];

yield 'Instructions with Markdown' => [
["a\nb\nc"],
["a\nb\nc"], false
];
yield 'Instructions with untrimmed Markdown' => [
["a\n b \td\nc"],
["a\n b d\nc"], true
];
}

/** @dataProvider dpParseInstructions */
public function testParseInstructions($originalInstructions, $expected, $changeExpected) {
$recipe = $this->stub;
$recipe['recipeInstructions'] = $originalInstructions;

$changed = $this->dut->apply($recipe);

$this->stub['recipeInstructions'] = $expected;
$this->assertEquals($this->stub, $recipe);
$this->assertEquals($changeExpected, $changed);
}
}
58 changes: 58 additions & 0 deletions tests/Integration/Helper/Filter/JSON/FixToolsFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

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

use OCP\IL10N;
use OCP\ILogger;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use OCA\Cookbook\Helper\TextCleanupHelper;
use OCA\Cookbook\Helper\Filter\JSON\FixToolsFilter;

class FixToolsFilterTest extends TestCase {
/** @var FixToolsFilter */
private $dut;

/** @var TextCleanupHelper */
private $textCleanupHelper;

/** @var array */
private $stub;

protected function setUp(): void {
/** @var Stub|IL10N $l */
$l = $this->createStub(IL10N::class);
$l->method('t')->willReturnArgument(0);

/** @var ILogger */
$logger = $this->createStub(ILogger::class);

$this->textCleanupHelper = new TextCleanupHelper();

$this->dut = new FixToolsFilter($l, $logger, $this->textCleanupHelper);

$this->stub = [
'name' => 'The name of the recipe',
'id' => 1234,
];
}

public function dp() {
return [
[['a','b','c'], ['a','b','c'], false],
[[" a \tb ",' c '],['a b','c'],true],
];
}

/** @dataProvider dp */
public function testApply($startVal, $expectedVal, $changed) {
$recipe = $this->stub;
$recipe['tools'] = $startVal;

$ret = $this->dut->apply($recipe);

$this->stub['tools'] = $expectedVal;
$this->assertEquals($changed, $ret);
$this->assertEquals($this->stub, $recipe);
}
}
1 change: 0 additions & 1 deletion tests/Unit/Helper/Filter/JSON/FixIngredientsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function dp() {
return [
[['a','b','c'], ['a','b','c'], false],
[[' a ',''], ['a'], true],
[[" a \tb ",' c '],['a b','c'],true],
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Helper/Filter/JSON/FixInstructionsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public function dpParseInstructions() {
"a\n<p></p><ul><li></li><li></li></ul><p></p>\nb\nc\nd",
['a','b','c','d'], true
];

yield 'Instructions with Markdown' => [
["a\nb\nc"],
["a\nb\nc"], false
];
}

/** @dataProvider dpParseInstructions */
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Helper/Filter/JSON/FixToolsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function dp() {
return [
[['a','b','c'], ['a','b','c'], false],
[[' a ',''], ['a'], true],
[[" a \tb ",' c '],['a b','c'],true],
];
}

Expand Down