Skip to content

Commit

Permalink
REST: GetPropertyAliases happy path
Browse files Browse the repository at this point in the history
* This includes adding the route handler, and the e2e test.

Bug: T346752
Change-Id: Ib6cd40d0faba3a9788a77511ff3e96a579ca8a79
  • Loading branch information
MuhammadJaziraly committed Sep 20, 2023
1 parent 90d0870 commit d42cddb
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 1 deletion.
8 changes: 7 additions & 1 deletion repo/rest-api/routes.dev.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
[ ]
[
{
"path": "/wikibase/v0/entities/properties/{property_id}/aliases",
"method": "GET",
"factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\GetPropertyAliasesRouteHandler::factory"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases;

use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\Repo\RestApi\Domain\Services\PropertyAliasesRetriever;

/**
* @license GPL-2.0-or-later
*/
class GetPropertyAliases {

private PropertyAliasesRetriever $propertyAliasesRetriever;

public function __construct( PropertyAliasesRetriever $propertyAliasesRetriever ) {
$this->propertyAliasesRetriever = $propertyAliasesRetriever;
}

public function execute( GetPropertyAliasesRequest $request ): GetPropertyAliasesResponse {
$propertyId = new NumericPropertyId( $request->getPropertyId() );

return new GetPropertyAliasesResponse(
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
$this->propertyAliasesRetriever->getAliases( $propertyId )
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases;

/**
* @license GPL-2.0-or-later
*/
class GetPropertyAliasesRequest {
private string $propertyId;

public function __construct( string $propertyId ) {
$this->propertyId = $propertyId;
}

public function getPropertyId(): string {
return $this->propertyId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases;

use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases;

/**
* @license GPL-2.0-or-later
*/
class GetPropertyAliasesResponse {

private Aliases $aliases;

public function __construct( Aliases $aliases ) {
$this->aliases = $aliases;
}

public function getAliases(): Aliases {
return $this->aliases;
}

}
60 changes: 60 additions & 0 deletions repo/rest-api/src/RouteHandlers/GetPropertyAliasesRouteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\RouteHandlers;

use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\StringStream;
use Wikibase\Repo\RestApi\Application\Serialization\AliasesSerializer;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliasesRequest;
use Wikibase\Repo\RestApi\WbRestApi;
use Wikimedia\ParamValidator\ParamValidator;

/**
* @license GPL-2.0-or-later
*/
class GetPropertyAliasesRouteHandler extends SimpleHandler {

private const PROPERTY_ID_PATH_PARAM = 'property_id';

private GetPropertyAliases $useCase;
private AliasesSerializer $aliasesSerializer;

public function __construct(
GetPropertyAliases $useCase,
AliasesSerializer $aliasesSerializer
) {
$this->useCase = $useCase;
$this->aliasesSerializer = $aliasesSerializer;
}

public static function factory(): self {
return new self(
WbRestApi::getGetPropertyAliases(),
new AliasesSerializer(),
);
}

public function run( string $propertyId ): Response {
$useCaseResponse = $this->useCase->execute( new GetPropertyAliasesRequest( $propertyId ) );
$httpResponse = $this->getResponseFactory()->create();
$httpResponse->setHeader( 'Content-Type', 'application/json' );
$httpResponse->setBody(
new StringStream( json_encode( $this->aliasesSerializer->serialize( $useCaseResponse->getAliases() ) ) )
);

return $httpResponse;
}

public function getParamSettings(): array {
return [
self::PROPERTY_ID_PATH_PARAM => [
self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true,
],
];
}

}
10 changes: 10 additions & 0 deletions repo/rest-api/src/WbRestApi.ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestStatementSubjectRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\GetProperty\GetProperty;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabels\GetPropertyLabels;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\GetPropertyStatement;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\GetPropertyStatements;
Expand Down Expand Up @@ -399,6 +400,15 @@ function ( MediaWikiServices $services ): ItemDescriptionEditRequestValidatingDe
);
},

'WbRestApi.GetPropertyAliases' => function( MediaWikiServices $services ): GetPropertyAliases {
return new GetPropertyAliases(
new PrefetchingTermLookupAliasesRetriever(
WikibaseRepo::getPrefetchingTermLookup( $services ),
WikibaseRepo::getTermsLanguages( $services )
)
);
},

'WbRestApi.GetPropertyLabels' => function( MediaWikiServices $services ): GetPropertyLabels {
return new GetPropertyLabels(
WbRestApi::getGetLatestPropertyRevisionMetadata( $services ),
Expand Down
6 changes: 6 additions & 0 deletions repo/rest-api/src/WbRestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestStatementSubjectRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\GetProperty\GetProperty;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabels\GetPropertyLabels;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\GetPropertyStatement;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\GetPropertyStatements;
Expand Down Expand Up @@ -284,6 +285,11 @@ public static function getGetPropertyLabels( ContainerInterface $services = null
->get( 'WbRestApi.GetPropertyLabels' );
}

public static function getGetPropertyAliases( ContainerInterface $services = null ): GetPropertyAliases {
return ( $services ?: MediaWikiServices::getInstance() )
->get( 'WbRestApi.GetPropertyAliases' );
}

public static function getValidatingRequestDeserializer( ContainerInterface $services = null ): ValidatingRequestDeserializer {
return ( $services ?: MediaWikiServices::getInstance() )->get( 'WbRestApi.ValidatingRequestDeserializer' );
}
Expand Down
33 changes: 33 additions & 0 deletions repo/rest-api/tests/mocha/api-testing/GetPropertyAliasesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const { assert } = require( 'api-testing' );
const { expect } = require( '../helpers/chaiHelper' );
const { createEntity } = require( '../helpers/entityHelper' );
const { newGetPropertyAliasesRequestBuilder } = require( '../helpers/RequestBuilderFactory' );

describe( newGetPropertyAliasesRequestBuilder().getRouteDescription(), () => {

let propertyId;

before( async () => {
const testProperty = await createEntity( 'property', {
aliases: {
en: [
{ language: 'en', value: 'en-alias1' },
{ language: 'en', value: 'en-alias2' }
]
},
datatype: 'string'
} );
propertyId = testProperty.entity.id;
} );

it( 'can get the aliases of a property', async () => {
const response = await newGetPropertyAliasesRequestBuilder( propertyId ).assertValidRequest()
.makeRequest();

expect( response ).to.have.status( 200 );
assert.deepEqual( response.body, { en: [ 'en-alias1', 'en-alias2' ] } );
} );

} );
6 changes: 6 additions & 0 deletions repo/rest-api/tests/mocha/helpers/RequestBuilderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module.exports = {
.withPathParam( 'item_id', itemId );
},

newGetPropertyAliasesRequestBuilder( propertyId ) {
return new RequestBuilder()
.withRoute( 'GET', '/entities/properties/{property_id}/aliases' )
.withPathParam( 'property_id', propertyId );
},

newGetItemAliasesInLanguageRequestBuilder( itemId, languageCode ) {
return new RequestBuilder()
.withRoute( 'GET', '/entities/items/{item_id}/aliases/{language_code}' )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\Tests\RestApi\Application\UseCases\GetPropertyAliases;

use PHPUnit\Framework\TestCase;
use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases;
use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliasesRequest;
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases;
use Wikibase\Repo\RestApi\Domain\ReadModel\AliasesInLanguage;
use Wikibase\Repo\RestApi\Domain\Services\PropertyAliasesRetriever;

/**
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases
*
* @group Wikibase
*
* @license GPL-2.0-or-later
*/
class GetPropertyAliasesTest extends TestCase {

private PropertyAliasesRetriever $propertyAliasesRetriever;

protected function setUp(): void {
parent::setUp();

$this->propertyAliasesRetriever = $this->createStub( PropertyAliasesRetriever::class );
}

public function testSuccess(): void {
$propertyId = 'P123';

$aliases = new Aliases(
new AliasesInLanguage( 'en', [ 'en-alias1', 'en-alias2' ] ),
new AliasesInLanguage( 'de', [ 'de-alias1', 'de-alias2' ] )
);

$this->propertyAliasesRetriever = $this->createMock( PropertyAliasesRetriever::class );
$this->propertyAliasesRetriever->expects( $this->once() )
->method( 'getAliases' )
->with( new NumericPropertyId( $propertyId ) )
->willReturn( $aliases );

$response = $this->newUseCase()->execute( new GetPropertyAliasesRequest( $propertyId ) );

$this->assertSame( $aliases, $response->getAliases() );
}

public function newUseCase(): GetPropertyAliases {
return new GetPropertyAliases( $this->propertyAliasesRetriever );
}

}

0 comments on commit d42cddb

Please sign in to comment.