[WIP] Test update handler

This commit is contained in:
Jeremy Dormitzer 2019-01-30 11:27:47 -05:00
parent 16479f4094
commit e3674a31e8
3 changed files with 134 additions and 28 deletions

View File

@ -246,27 +246,44 @@ class ObjectsService
if ( ! $object ) {
return;
}
foreach( $object->getFields() as $field ) {
if ( array_key_exists( $field->getName(), $updatedFields ) ) {
$newValue = $updatedFields[$field->getName()];
if ( is_array( $newValue ) ) {
$referencedObject = $this->persist( $newValue, 'objects-service.update' );
$oldTargetObject = $field->getTargetObject();
$field->setTargetObject(
$referencedObject, $this->dateTimeProvider->getTime( 'objects-service.update' )
);
$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 );
foreach( $updatedFields as $fieldName => $newValue ) {
if ( $newValue === null && $object->hasField( $fieldName ) ) {
$field = $object->getField( $fieldName );
if ( $field->hasTargetObject() && ! $field->getTargetObject()->hasField( 'id' ) ) {
$targetObject = $field->getTargetObject();
// Clear the target object by setting a dummy value
$field->setValue( '' );
$this->entityManager->remove( $targetObject );
}
$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' ) );
@ -289,15 +306,12 @@ class ObjectsService
if ( ! $existing ) {
return;
}
$delta = array();
foreach ( $existing->getFields() as $field ) {
if ( array_key_exists( $field->getName(), $replacement ) ) {
$delta[$field->getName()] = $replacement[$field->getName()];
} else {
$delta[$field->getName()] = null;
if ( ! array_key_exists( $field->getName(), $replacement ) ) {
$replacement[$field->getName()] = null;
}
}
return $this->update( $id, $delta );
return $this->update( $id, $replacement );
}
}
?>

View File

@ -10,6 +10,7 @@ use ActivityPub\Entities\Field;
use Doctrine\ORM\EntityManager;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Database\PrefixNamingStrategy;
use ActivityPub\Test\TestUtils\TestActivityPubObject;
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
use Doctrine\ORM\Tools\Setup;
use GuzzleHttp\Client;
@ -1199,8 +1200,98 @@ class ObjectsServiceTest extends SQLiteTestCase
public function testReplace()
{
// TODO implement me
$this->assertTrue( false );
$testCases = array(
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 );
}
}
}
}
?>

View File

@ -1,6 +1,7 @@
<?php
namespace ActivityPub\Test\TestConfig;
use InvalidArgumentException;
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\DefaultTable;