-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename `MediaWikiEditEntityFactoryItemUpdater` to `EntityUpdater` * Rename its related classes * Make the new generic `EntityUpdater` entity type agnostic * Create `EntityUpdaterItemUpdater` implementation which uses the new generic `EntityUpdater` Bug: T342886 Change-Id: Id69cbdb77638b35afa5bf5c9ccea89dfe4004499
- Loading branch information
1 parent
33b354d
commit e2d315b
Showing
14 changed files
with
547 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
repo/rest-api/src/Domain/Services/Exceptions/EntityUpdatePrevented.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Domain\Services\Exceptions; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class EntityUpdatePrevented extends EntityUpdateFailed { | ||
// Don't consider prevented edits unexpected | ||
// | ||
// This patch should only be considered a temporary solution to stop the | ||
// error log spam. | ||
// | ||
// TODO: think of a better way to handle it. | ||
} |
9 changes: 0 additions & 9 deletions
9
repo/rest-api/src/Domain/Services/Exceptions/ItemUpdatePrevented.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
repo/rest-api/src/Infrastructure/DataAccess/EntityUpdaterItemUpdater.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Infrastructure\DataAccess; | ||
|
||
use Wikibase\DataModel\Entity\Item as DataModelItem; | ||
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\Descriptions; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\Item; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\ItemRevision; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\Labels; | ||
use Wikibase\Repo\RestApi\Domain\ReadModel\StatementList; | ||
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater; | ||
use Wikibase\Repo\RestApi\Domain\Services\StatementReadModelConverter; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class EntityUpdaterItemUpdater implements ItemUpdater { | ||
|
||
private EntityUpdater $entityUpdater; | ||
private StatementReadModelConverter $statementReadModelConverter; | ||
|
||
public function __construct( EntityUpdater $entityUpdater, StatementReadModelConverter $statementReadModelConverter ) { | ||
$this->entityUpdater = $entityUpdater; | ||
$this->statementReadModelConverter = $statementReadModelConverter; | ||
} | ||
|
||
public function update( DataModelItem $item, EditMetadata $editMetadata ): ItemRevision { | ||
$entityRevision = $this->entityUpdater->update( $item, $editMetadata ); | ||
|
||
/** @var DataModelItem $savedItem */ | ||
$savedItem = $entityRevision->getEntity(); | ||
'@phan-var DataModelItem $savedItem'; | ||
|
||
return new ItemRevision( | ||
$this->convertDataModelItemToReadModel( $savedItem ), | ||
$entityRevision->getTimestamp(), | ||
$entityRevision->getRevisionId() | ||
); | ||
} | ||
|
||
private function convertDataModelItemToReadModel( DataModelItem $item ): Item { | ||
return new Item( | ||
Labels::fromTermList( $item->getLabels() ), | ||
Descriptions::fromTermList( $item->getDescriptions() ), | ||
new StatementList( | ||
...array_map( | ||
[ $this->statementReadModelConverter, 'convert' ], | ||
iterator_to_array( $item->getStatements() ) | ||
) | ||
) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
repo/rest-api/tests/phpunit/Infrastructure/DataAccess/EntityUpdaterIntegrationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\Tests\RestApi\Infrastructure\DataAccess; | ||
|
||
use Generator; | ||
use MediaWiki\Permissions\PermissionManager; | ||
use MediaWikiIntegrationTestCase; | ||
use Psr\Log\NullLogger; | ||
use RequestContext; | ||
use Wikibase\DataModel\Entity\EntityDocument; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
use Wikibase\DataModel\Entity\Property; | ||
use Wikibase\DataModel\Entity\StatementListProvidingEntity; | ||
use Wikibase\DataModel\Statement\StatementGuid; | ||
use Wikibase\DataModel\Statement\StatementList; | ||
use Wikibase\DataModel\Tests\NewItem; | ||
use Wikibase\DataModel\Tests\NewStatement; | ||
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata; | ||
use Wikibase\Repo\RestApi\Infrastructure\DataAccess\EntityUpdater; | ||
use Wikibase\Repo\RestApi\Infrastructure\EditSummaryFormatter; | ||
use Wikibase\Repo\WikibaseRepo; | ||
|
||
/** | ||
* @covers \Wikibase\Repo\RestApi\Infrastructure\DataAccess\EntityUpdater | ||
* | ||
* @group Wikibase | ||
* @group Database | ||
* | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class EntityUpdaterIntegrationTest extends MediaWikiIntegrationTestCase { | ||
|
||
/** | ||
* @dataProvider provideStatementIdAndEntityWithStatement | ||
*/ | ||
public function testUpdate_removeStatementFromEntity( StatementGuid $statementId, StatementListProvidingEntity $entityToUpdate ): void { | ||
$this->saveNewEntity( $entityToUpdate ); | ||
|
||
$entityToUpdate->getStatements()->removeStatementsWithGuid( (string)$statementId ); | ||
|
||
$newRevision = $this->newEntityUpdater()->update( $entityToUpdate, $this->createStub( EditMetadata::class ) ); | ||
|
||
$this->assertCount( 0, $newRevision->getEntity()->getStatements() ); | ||
} | ||
|
||
/** | ||
* @dataProvider provideStatementIdAndEntityWithStatement | ||
*/ | ||
public function testUpdate_replaceStatementOnEntity( StatementGuid $statementId, StatementListProvidingEntity $entityToUpdate ): void { | ||
$newValue = 'new statement value'; | ||
$newStatement = NewStatement::forProperty( 'P321' ) | ||
->withGuid( $statementId ) | ||
->withValue( $newValue ) | ||
->build(); | ||
|
||
$this->saveNewEntity( $entityToUpdate ); | ||
|
||
$entityToUpdate->getStatements()->replaceStatement( $statementId, $newStatement ); | ||
|
||
$newRevision = $this->newEntityUpdater()->update( $entityToUpdate, $this->createStub( EditMetadata::class ) ); | ||
|
||
$statementList = $newRevision->getEntity()->getStatements(); | ||
$this->assertSame( | ||
$newValue, | ||
$statementList->getFirstStatementWithGuid( (string)$statementId )->getMainSnak()->getDataValue()->getValue() | ||
); | ||
} | ||
|
||
public function provideStatementIdAndEntityWithStatement(): Generator { | ||
$statementId = new StatementGuid( new ItemId( 'Q123' ), 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE' ); | ||
$statement = NewStatement::forProperty( 'P321' ) | ||
->withGuid( $statementId ) | ||
->withValue( 'a statement value' ) | ||
->build(); | ||
yield 'item with statement' => [ $statementId, NewItem::withStatement( $statement )->build() ]; | ||
|
||
$statementId = new StatementGuid( new NumericPropertyId( 'P123' ), 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE' ); | ||
$statement = NewStatement::forProperty( 'P321' ) | ||
->withGuid( $statementId ) | ||
->withValue( 'a statement value' ) | ||
->build(); | ||
$property = Property::newFromType( 'string' ); | ||
$property->setStatements( new StatementList( $statement ) ); | ||
yield 'property with statement' => [ $statementId, $property ]; | ||
} | ||
|
||
private function saveNewEntity( EntityDocument $entity ): void { | ||
WikibaseRepo::getEntityStore()->saveEntity( | ||
$entity, | ||
__METHOD__, | ||
$this->getTestUser()->getUser(), | ||
EDIT_NEW | ||
); | ||
} | ||
|
||
private function newEntityUpdater(): EntityUpdater { | ||
$permissionManager = $this->createStub( PermissionManager::class ); | ||
$permissionManager->method( $this->anything() )->willReturn( true ); | ||
|
||
return new EntityUpdater( | ||
RequestContext::getMain(), | ||
WikibaseRepo::getEditEntityFactory(), | ||
new NullLogger(), | ||
$this->createStub( EditSummaryFormatter::class ), | ||
$permissionManager | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.