Skip to content

Commit

Permalink
Merge "FormatSnakValue: Validate "showcalendar" formatter option"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Nov 4, 2024
2 parents b3fc3a2 + 7073ed4 commit cd212d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions repo/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@
"wikibase-api-invalid-formatter-options-lang": "Illegal \"options\" given: \"lang\" needs to be a string (MediaWiki language code).",
"wikibase-api-invalid-formatter-options-apply-rounding": "Illegal \"options\" given: \"applyRounding\" needs to be a bool or a string.",
"wikibase-api-invalid-formatter-options-apply-unit": "Illegal \"options\" given: \"applyUnit\" needs to be a bool.",
"wikibase-api-invalid-formatter-options-showcalendar": "Illegal \"options\" given: \"showcalendar\" needs to be a bool or \"auto\".",
"wikibase-self-conflict-patched": "Your edit was patched into the latest version, overriding some of your own intermediate changes.",
"wikibase-conflict-patched": "Your edit was patched into the latest version.",
"wikibase-save-unresolved-redirect": "Encountered an unresolved redirect from $1 to $2 while attempting to save a change.",
Expand Down
1 change: 1 addition & 0 deletions repo/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
"wikibase-api-invalid-formatter-options-lang": "API error message indicating that the passed formatter option \"lang\" is invalid.",
"wikibase-api-invalid-formatter-options-apply-rounding": "API error message indicating that the passed formatter option \"applyRounding\" is invalid.",
"wikibase-api-invalid-formatter-options-apply-unit": "API error message indicating that the passed formatter option \"applyUnit\" is invalid.",
"wikibase-api-invalid-formatter-options-showcalendar": "API error message indicating that the passed formatter option \"showcalendar\" is invalid.",
"wikibase-self-conflict-patched": "Warning issued when an edit is made based on an old revision, clean patching was not possible, but all intermediate edits where made by the same user, so aggressive patching was applied, overriding earlier changes by the same user. This may happen e.g. when editing the same item in multiple browser windows. This warning is usually not displayed.",
"wikibase-conflict-patched": "Warning issued when an edit is made based on an old revision, but the edit conflict could be resolved by patching. This warning is usually not displayed.",
"wikibase-save-unresolved-redirect": "Error shown when an unresolved redirect was encountered while trying to save a change to an entity. Parameters:\n* $1 - the source id\n* $2 - the target id",
Expand Down
8 changes: 8 additions & 0 deletions repo/includes/Api/FormatSnakValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ private function setValidOption( FormatterOptions $options, string $option, $val
);
}
break;
case ShowCalendarModelDecider::OPT_SHOW_CALENDAR:
if ( !is_bool( $value ) && $value !== 'auto' ) {
$this->errorReporter->dieWithError(
'wikibase-api-invalid-formatter-options-showcalendar',
'param-illegal'
);
}
break;
}
$options->setOption( $option, $value );
}
Expand Down
19 changes: 19 additions & 0 deletions repo/tests/phpunit/includes/Api/FormatSnakValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\Property;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Lib\Formatters\ShowCalendarModelDecider;
use Wikibase\Lib\Formatters\SnakFormatter;
use Wikibase\Repo\WikibaseRepo;

Expand Down Expand Up @@ -453,4 +454,22 @@ public function testApiRequest_invalidApplyUnitInOptions(): void {
'options' => json_encode( [ QuantityFormatter::OPT_APPLY_UNIT => 'invalid' ] ),
] );
}

/** @dataProvider provideInvalidShowcalendar */
public function testApiRequest_invalidShowcalendarInOptions( $showcalendar ): void {
$this->expectException( ApiUsageException::class, 'wikibase-api-invalid-formatter-options-showcalendar' );

$this->doApiRequest( [
'action' => 'wbformatvalue',
'datatype' => 'time',
'datavalue' => json_encode( ( new StringValue( 'string' ) )->toArray() ),
'options' => json_encode( [ ShowCalendarModelDecider::OPT_SHOW_CALENDAR => $showcalendar ] ),
] );
}

public static function provideInvalidShowcalendar(): iterable {
yield [ 'invalid' ];
yield [ 12 ];
}

}

0 comments on commit cd212d1

Please sign in to comment.