Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2022-07-06 12:43:34 +02:00
Родитель 44e85bf4c8
Коммит 2303776c50
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 9FC3120E932F73F1
9 изменённых файлов: 67 добавлений и 52 удалений

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

@ -21,23 +21,31 @@ class CookbookConfig extends Config {
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true
];
return array_merge(['@PSR12' => true], $parentRules, $additionalRules);
$ret = array_merge(['@PSR12' => true], $parentRules, $additionalRules);
// print_r($ret);
return $ret;
}
}
$config = new CookbookConfig();
$config
$finder = $config
->getFinder()
->ignoreVCSIgnored(true)
// ->ignoreVCSIgnored(true)
->exclude('build')
->exclude('l10n')
// ->notPath('lib/Vendor')
->exclude('src')
->exclude('node_modules')
->exclude('vendor')
->exclude('.github')
->in(__DIR__);
->in(__DIR__ );
$config->setFinder($finder);
// foreach($finder as $f) {
// print_r($f);
// }
// print_r($config);
return $config;

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

@ -465,13 +465,13 @@ class RecipeDb {
foreach ($recipes as $recipe) {
$qb->setParameter('id', $recipe['id'], $this->types->INT());
$qb->setParameter('name', $recipe['name'], $this->types->STRING());
$dateCreated = $this->parseDate($recipe['dateCreated']);
if($dateCreated === null) {
if ($dateCreated === null) {
$dateCreated = $this->parseDate('now');
}
$qb->setParameter('dateCreated', $dateCreated, $this->types->DATE());
$dateModified = $this->parseDate($recipe['dateModified']);
$qb->setParameter('dateModified', $dateModified, $this->types->DATE());
@ -642,7 +642,7 @@ class RecipeDb {
return null;
}
try{
try {
return new DateTimeImmutable($date);
} catch (\Exception $ex) {
return new DateTimeImmutable();

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

@ -7,19 +7,19 @@ use OCP\Files\File;
/**
* An abstract filter on a recipe.
*
*
* A filter should have a single purpose that is serves and implement this interface
*/
interface AbstractRecipeFilter {
/**
* Filter the given recipe according to the filter class specification.
*
*
* This function can make changes to the recipe array to carry out the needed changes.
* In order to signal if the JSON file needs updating, the return value must be true if and only if the recipe was changed.
*
* @param array $json The recipe data as array
* @param File $recipe The file containing the recipe for further processing
* @return boolean true, if and only if the recipe was changed
* @return bool true, if and only if the recipe was changed
* @throws InvalidRecipeException if the recipe was not correctly filtered
*/
public function apply(array &$json, File $recipe): bool;

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

@ -3,22 +3,20 @@
namespace OCA\Cookbook\Helper\Filter\DB;
use DateTime;
use DateTimeImmutable;
use OCA\Cookbook\Helper\Filter\AbstractRecipeFilter;
use OCP\Files\File;
/**
* Ensure the dates of a recipe are valid
*
*
* This filter will update the recipe to have both valid dateCreated and dateModified.
* If the dates are given in correct format, nothing is changed.
*
*
* If only the dateModified is given, the dateCreated is set to the same value.
*
*
* If neither is given, the file modification time of the JSON file is taken into account.
*/
class RecipeDatesFilter implements AbstractRecipeFilter {
private const DATE_CREATED = 'dateCreated';
private const DATE_MODIFIED = 'dateModified';
@ -28,15 +26,14 @@ class RecipeDatesFilter implements AbstractRecipeFilter {
/** @var string */
private $patternDate;
public function __construct()
{
public function __construct() {
$this->patternDate = join('|', [
'^' . self::PATTERN_DATE . '$',
'^' . self::PATTERN_DATE . 'T' . self::PATTERN_TIME . '$'
]);
}
public function apply(array &$json, File $recipe): bool {
public function apply(array &$json, File $recipe): bool {
$ret = false;
// First ensure the entries are present in general
@ -47,7 +44,7 @@ class RecipeDatesFilter implements AbstractRecipeFilter {
$this->checkDateFormat($json, self::DATE_CREATED, $ret);
$this->checkDateFormat($json, self::DATE_MODIFIED, $ret);
if(is_null($json['dateCreated'])) {
if (is_null($json['dateCreated'])) {
if (is_null($json['dateModified'])) {
// No dates have been set. Fall back to time of file
$json['dateCreated'] = $this->getTimeFromFile($recipe);
@ -67,45 +64,49 @@ class RecipeDatesFilter implements AbstractRecipeFilter {
return $ret;
}
/**
* Undocumented function
*
*/
private function getTimeFromFile(File $file): string {
$timestamp = $file->getCreationTime();
if($timestamp === 0) {
if ($timestamp === 0) {
$timestamp = $file->getUploadTime();
}
if($timestamp === 0) {
if ($timestamp === 0) {
$timestamp = $file->getMTime();
}
return $this->getDateFromTimestamp($timestamp);
}
private function getDateFromTimestamp(int $timestamp): string {
$date = new DateTime();
$date->setTimestamp($timestamp);
return $date->format(DateTime::ISO8601);
}
private function ensureEntryExists(array &$json, string $name, bool &$ret) {
if(!array_key_exists($name, $json)) {
if (!array_key_exists($name, $json)) {
$json[$name] = null;
$ret = true;
}
}
private function checkDateFormat(array &$json, string $name, bool &$ret) {
if($json[$name] === null) {
if ($json[$name] === null) {
return;
}
// Check for valid date format
if(preg_match('/' . $this->patternDate . '/', $json[$name]) === 1) {
if (preg_match('/' . $this->patternDate . '/', $json[$name]) === 1) {
return;
}
// Last desperate approach: Is it a timestamp?
if(preg_match('/^\d+$/', $json[$name])) {
if($json[$name] > 0) {
if (preg_match('/^\d+$/', $json[$name])) {
if ($json[$name] > 0) {
$json[$name] = $this->getDateFromTimestamp($json[$name]);
$ret = true;
return;

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

@ -13,7 +13,6 @@ use OCP\Migration\SimpleMigrationStep;
* Auto-generated migration step: Please modify to your needs!
*/
class Version000000Date20220703174647 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
@ -33,14 +32,14 @@ class Version000000Date20220703174647 extends SimpleMigrationStep {
$schema = $schemaClosure();
$table = $schema->getTable('cookbook_names');
if(! $table->hasColumn('dateCreated')) {
if (! $table->hasColumn('dateCreated')) {
$table->addColumn('dateCreated', 'datetime_immutable', [
'notnull' => false,
]);
}
if(!$table->hasColumn('dateModified')) {
if (!$table->hasColumn('dateModified')) {
$table->addColumn('dateModified', 'datetime_immutable', [
'notnull' => false,
]);

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

@ -142,7 +142,7 @@ class DbCacheService {
$id = (int) $jsonFile->getParent()->getId();
$json['id'] = $id;
if(!isset($json['dateModified'])) {
if (!isset($json['dateModified'])) {
$json['dateModified'] = null;
}
@ -226,11 +226,11 @@ class DbCacheService {
if ($dbEntry['name'] !== $fileEntry['name']) {
return false;
}
if($dbEntry['dateCreated'] !== $fileEntry['dateCreated']) {
if ($dbEntry['dateCreated'] !== $fileEntry['dateCreated']) {
return false;
}
if($dbEntry['dateModified'] !== $fileEntry['dateModified']) {
if ($dbEntry['dateModified'] !== $fileEntry['dateModified']) {
return false;
}

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

@ -942,7 +942,7 @@ class RecipeService {
*/
private function addDatesToRecipes(array &$recipes) {
foreach ($recipes as $i => $recipe) {
if(! array_key_exists('dateCreated', $recipe) || ! array_key_exists('dateModified', $recipe)) {
if (! array_key_exists('dateCreated', $recipe) || ! array_key_exists('dateModified', $recipe)) {
$r = $this->getRecipeById($recipe['recipe_id']);
$recipes[$i]['dateCreated'] = $r['dateCreated'];
$recipes[$i]['dateModified'] = $r['dateModified'];

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

@ -2,8 +2,6 @@
namespace OCA\Cookbook\tests\Migration\Setup\Migrations;
use OCP\DB\QueryBuilder\IQueryBuilder;
include_once __DIR__ . '/AbstractMigrationTestCase.php';
class Version000000Date20220703174647Test extends AbstractMigrationTestCase {
@ -51,7 +49,7 @@ class Version000000Date20220703174647Test extends AbstractMigrationTestCase {
$this->assertEquals(2, count($row));
$this->assertNull($row['dateCreated']);
$this->assertNull($row['dateModified']);
//$this->assertEquals([null, null], $data[0]);
}

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

@ -14,8 +14,7 @@ class RecipeDatesFilterTest extends TestCase {
/** @var RecipeDatesFilter */
private $dut;
protected function setUp(): void
{
protected function setUp(): void {
$this->dut = new RecipeDatesFilter();
}
@ -29,6 +28,11 @@ class RecipeDatesFilterTest extends TestCase {
/**
* @dataProvider dpFromJson
* @param mixed $created
* @param mixed $modified
* @param mixed $expectedCreation
* @param mixed $expectedModification
* @param mixed $updated
*/
public function testFilterFromJson($created, $modified, $expectedCreation, $expectedModification, $updated) {
$recipe = [
@ -53,7 +57,7 @@ class RecipeDatesFilterTest extends TestCase {
$this->assertEquals($copy, $recipe, 'Other entries must not change.');
}
public function dpFromFile() {
yield ['2022-07-06T09:08:54+0000', false, false, 1657098534, 1657098535, 1657098536];
yield ['2022-07-06T09:08:55+0000', false, false, 0, 1657098535, 1657098536];
@ -63,39 +67,44 @@ class RecipeDatesFilterTest extends TestCase {
/**
* @dataProvider dpFromFile
* @param mixed $creation
* @param mixed $creationPresent
* @param mixed $modificationPresent
* @param mixed $creationTime
* @param mixed $uploadTime
* @param mixed $mTime
*/
public function testFilterFromFile($creation, $creationPresent, $modificationPresent, $creationTime, $uploadTime, $mTime) {
$recipe = [
'name' => 'my Recipe',
];
if($creationPresent) {
if ($creationPresent) {
$recipe['dateCreated'] = null;
}
if($modificationPresent) {
if ($modificationPresent) {
$recipe['dateModified'] = null;
}
$copy = $recipe;
/** @var Stub|File */
$file = $this->createStub(File::class);
$file->method('getCreationTime')->willReturn($creationTime);
$file->method('getUploadTime')->willReturn($uploadTime);
$file->method('getMTime')->willReturn($mTime);
$ret = $this->dut->apply($recipe, $file);
$this->assertTrue($ret, 'Reporting of modification status');
$this->assertEquals($creation, $recipe['dateCreated'], 'Wrong creation date');
$this->assertNull($recipe['dateModified'], 'Wrong modification date');
unset($recipe['dateCreated']);
unset($recipe['dateModified']);
unset($copy['dateCreated']);
unset($copy['dateModified']);
$this->assertEquals($copy, $recipe, 'Other entries must not change.');
$this->assertEquals($copy, $recipe, 'Other entries must not change.');
}
}