[WIP] Test update handler
This commit is contained in:
parent
16479f4094
commit
e3674a31e8
@ -246,27 +246,44 @@ class ObjectsService
|
|||||||
if ( ! $object ) {
|
if ( ! $object ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach( $object->getFields() as $field ) {
|
foreach( $updatedFields as $fieldName => $newValue ) {
|
||||||
if ( array_key_exists( $field->getName(), $updatedFields ) ) {
|
if ( $newValue === null && $object->hasField( $fieldName ) ) {
|
||||||
$newValue = $updatedFields[$field->getName()];
|
$field = $object->getField( $fieldName );
|
||||||
if ( is_array( $newValue ) ) {
|
if ( $field->hasTargetObject() && ! $field->getTargetObject()->hasField( 'id' ) ) {
|
||||||
$referencedObject = $this->persist( $newValue, 'objects-service.update' );
|
$targetObject = $field->getTargetObject();
|
||||||
$oldTargetObject = $field->getTargetObject();
|
// Clear the target object by setting a dummy value
|
||||||
$field->setTargetObject(
|
$field->setValue( '' );
|
||||||
$referencedObject, $this->dateTimeProvider->getTime( 'objects-service.update' )
|
$this->entityManager->remove( $targetObject );
|
||||||
);
|
|
||||||
$this->entityManager->persist( $field );
|
|
||||||
if ( $oldTargetObject && ! $oldTargetObject->hasField( 'id' ) ) {
|
|
||||||
$this->entityManager->remove( $oldTargetObject );
|
|
||||||
}
|
|
||||||
} else if ( $newValue === null ) {
|
|
||||||
$object->removeField( $field );
|
|
||||||
$this->entityManager->persist( $object );
|
|
||||||
$this->entityManager->remove( $field );
|
|
||||||
} else {
|
|
||||||
$field->setValue( $newValue, $this->dateTimeProvider->getTime( 'objects-service.update' ) );
|
|
||||||
$this->entityManager->persist( $field );
|
|
||||||
}
|
}
|
||||||
|
$object->removeField( $field );
|
||||||
|
$this->entityManager->persist( $object );
|
||||||
|
$this->entityManager->remove( $field );
|
||||||
|
} else if ( $object->hasField( $fieldName ) ) {
|
||||||
|
$field = $object->getField( $fieldName );
|
||||||
|
$oldTargetObject = $field->getTargetObject();
|
||||||
|
if ( is_array( $newValue ) ) {
|
||||||
|
$newTargetObject = $this->persist( $newValue, 'objects-service.update' );
|
||||||
|
$field->setTargetObject(
|
||||||
|
$newTargetObject,
|
||||||
|
$this->dateTimeProvider->getTime( 'objects-service.update' )
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$field->setValue(
|
||||||
|
$newValue, $this->dateTimeProvider->getTime( 'objects-service.update' )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ( $oldTargetObject && ! $oldTargetObject->hasField( 'id' ) ) {
|
||||||
|
$this->entityManager->remove( $oldTargetObject );
|
||||||
|
}
|
||||||
|
$this->entityManager->persist( $field );
|
||||||
|
} else {
|
||||||
|
if ( is_array( $newValue ) ) {
|
||||||
|
$newTargetObject = $this->persist( $newValue );
|
||||||
|
$field = Field::withObject( $object, $fieldName, $newTargetObject );
|
||||||
|
} else {
|
||||||
|
$field = Field::withValue( $object, $fieldName, $newValue );
|
||||||
|
}
|
||||||
|
$this->entityManager->persist( $field );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$object->setLastUpdated( $this->dateTimeProvider->getTime( 'objects-service.update' ) );
|
$object->setLastUpdated( $this->dateTimeProvider->getTime( 'objects-service.update' ) );
|
||||||
@ -289,15 +306,12 @@ class ObjectsService
|
|||||||
if ( ! $existing ) {
|
if ( ! $existing ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$delta = array();
|
|
||||||
foreach ( $existing->getFields() as $field ) {
|
foreach ( $existing->getFields() as $field ) {
|
||||||
if ( array_key_exists( $field->getName(), $replacement ) ) {
|
if ( ! array_key_exists( $field->getName(), $replacement ) ) {
|
||||||
$delta[$field->getName()] = $replacement[$field->getName()];
|
$replacement[$field->getName()] = null;
|
||||||
} else {
|
|
||||||
$delta[$field->getName()] = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this->update( $id, $delta );
|
return $this->update( $id, $replacement );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -10,6 +10,7 @@ use ActivityPub\Entities\Field;
|
|||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use ActivityPub\Objects\ObjectsService;
|
use ActivityPub\Objects\ObjectsService;
|
||||||
use ActivityPub\Database\PrefixNamingStrategy;
|
use ActivityPub\Database\PrefixNamingStrategy;
|
||||||
|
use ActivityPub\Test\TestUtils\TestActivityPubObject;
|
||||||
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
|
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
|
||||||
use Doctrine\ORM\Tools\Setup;
|
use Doctrine\ORM\Tools\Setup;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
@ -1199,8 +1200,98 @@ class ObjectsServiceTest extends SQLiteTestCase
|
|||||||
|
|
||||||
public function testReplace()
|
public function testReplace()
|
||||||
{
|
{
|
||||||
// TODO implement me
|
$testCases = array(
|
||||||
$this->assertTrue( false );
|
array(
|
||||||
|
'id' => 'basicTest',
|
||||||
|
'object' => array(
|
||||||
|
'id' => 'https://example.com/objects/1',
|
||||||
|
'type' => 'Article',
|
||||||
|
'summary' => 'An article',
|
||||||
|
),
|
||||||
|
'replacementId' => 'https://example.com/objects/1',
|
||||||
|
'replacement' => array(
|
||||||
|
'id' => 'https://example.com/objects/1',
|
||||||
|
'type' => 'Note',
|
||||||
|
'contents' => 'This is a note',
|
||||||
|
),
|
||||||
|
'expectedObject' => array(
|
||||||
|
'id' => 'https://example.com/objects/1',
|
||||||
|
'type' => 'Note',
|
||||||
|
'contents' => 'This is a note',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'itUpdatesTargetObject',
|
||||||
|
'object' => array(
|
||||||
|
'id' => 'https://example.com/objects/2',
|
||||||
|
'type' => 'Article',
|
||||||
|
'attributedTo' => array(
|
||||||
|
'id' => 'https://example.com/actors/1'
|
||||||
|
),
|
||||||
|
'foo' => array(
|
||||||
|
'bar' => 'baz',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'replacementId' => 'https://example.com/objects/2',
|
||||||
|
'replacement' => array(
|
||||||
|
'id' => 'https://example.com/objects/2',
|
||||||
|
'type' => 'Article',
|
||||||
|
'attributedTo' => 'https://example.com/actors/1',
|
||||||
|
),
|
||||||
|
'expectedObject' => array(
|
||||||
|
'id' => 'https://example.com/objects/2',
|
||||||
|
'type' => 'Article',
|
||||||
|
'attributedTo' => 'https://example.com/actors/1',
|
||||||
|
),
|
||||||
|
'expectedDatabaseState' => array(
|
||||||
|
'objects' => array(
|
||||||
|
array(
|
||||||
|
'id' => 1,
|
||||||
|
'created' => $this->getTime( 'objects-service.create' ),
|
||||||
|
'updated' => $this->getTime( 'objects-service.update' ),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 2,
|
||||||
|
'created' => $this->getTime( 'objects-service.create' ),
|
||||||
|
'updated' => $this->getTime( 'objects-service.update' ),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 3,
|
||||||
|
'created' => $this->getTime( 'objects-service.create' ),
|
||||||
|
'updated' => $this->getTime( 'objects-service.update' ),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'fields' => array(
|
||||||
|
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
foreach ( $testCases as $testCase ) {
|
||||||
|
$this->objectsService->persist( $testCase['object'] );
|
||||||
|
$replacement = $this->objectsService->replace(
|
||||||
|
$testCase['replacementId'],
|
||||||
|
$testCase['replacement']
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
$testCase['expectedObject'],
|
||||||
|
$replacement->asArray(),
|
||||||
|
"Error on test $testCase[id]"
|
||||||
|
);
|
||||||
|
if ( array_key_exists( 'expectedDatabaseState', $testCase ) ) {
|
||||||
|
$expectedDb = new ArrayDataSet( $testCase['expectedDatabaseState'] );
|
||||||
|
$expectedObjectsTable = $expectedDb->getTable('objects');
|
||||||
|
$expectedFieldsTable = $expectedDb->getTable('fields');
|
||||||
|
$objectsQueryTable = $this->getConnection()->createQueryTable(
|
||||||
|
'objects', 'SELECT * FROM objects'
|
||||||
|
);
|
||||||
|
$fieldsQueryTable = $this->getConnection()->createQueryTable(
|
||||||
|
'fields', 'SELECT * FROM fields'
|
||||||
|
);
|
||||||
|
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
|
||||||
|
$this->assertTablesEqual( $expectedFieldsTable, $fieldsQueryTable );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ActivityPub\Test\TestConfig;
|
namespace ActivityPub\Test\TestConfig;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
|
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
|
||||||
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
|
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
|
||||||
use PHPUnit\DbUnit\DataSet\DefaultTable;
|
use PHPUnit\DbUnit\DataSet\DefaultTable;
|
||||||
|
Loading…
Reference in New Issue
Block a user