activitypub-php/test/ObjectsServiceTest.php

467 lines
16 KiB
PHP
Raw Normal View History

2018-11-21 03:34:39 +00:00
<?php
2018-11-24 03:25:56 +00:00
namespace ActivityPub\Test;
2018-11-21 03:34:39 +00:00
use DateTime;
2018-11-24 03:25:56 +00:00
use ActivityPub\Test\Config\SQLiteTestCase;
use ActivityPub\Test\Config\ArrayDataSet;
use ActivityPub\Entities\Field;
2018-11-21 03:34:39 +00:00
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Database\PrefixNamingStrategy;
use PHPUnit\DbUnit\TestCaseTrait;
2018-11-23 13:28:46 +00:00
class ObjectsServiceTest extends SQLiteTestCase
2018-11-21 03:34:39 +00:00
{
protected $entityManager;
protected $objectsService;
protected function getDataSet()
{
2018-11-23 13:28:46 +00:00
return new ArrayDataSet( array( 'objects' => array(), 'fields' => array() ) );
}
protected function setUp()
{
parent::setUp();
$dbConfig = Setup::createAnnotationMetadataConfiguration(
array( __DIR__ . '/../src/Entities' ), true
);
$namingStrategy = new PrefixNamingStrategy( '' );
$dbConfig->setNamingStrategy( $namingStrategy );
$dbParams = array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
);
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
$this->objectsService = new ObjectsService( $this->entityManager );
}
private static function getNow() {
return date("Y-m-d H:i:s");
}
public function testItCreatesObject()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
);
$now = self::getNow();
$object = $this->objectsService->createObject( $fields );
$expected = new ArrayDataSet( array(
2018-11-21 03:34:39 +00:00
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now )
2018-11-21 03:34:39 +00:00
),
'fields' => array(
2018-11-21 03:34:39 +00:00
array(
'id' => 1,
'object_id' => 1,
'name' => 'id',
2018-11-21 03:34:39 +00:00
'value' => 'https://example.com/notes/1',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
2018-11-21 03:34:39 +00:00
),
array(
'id' => 2,
'object_id' => 1,
'name' => 'type',
2018-11-21 03:34:39 +00:00
'value' => 'Note',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
2018-11-21 03:34:39 +00:00
),
array(
'id' => 3,
'object_id' => 1,
'name' => 'content',
2018-11-21 03:34:39 +00:00
'value' => 'This is a note',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
2018-11-21 03:34:39 +00:00
),
),
) );
$expectedObjectsTable = $expected->getTable('objects');
$expectedFieldsTable = $expected->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 );
2018-11-21 03:34:39 +00:00
}
public function testObjectFieldsSet()
2018-11-21 03:34:39 +00:00
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
2018-11-21 03:34:39 +00:00
);
$object = $this->objectsService->createObject( $fields );
$this->assertCount( 3, $object->getFields() );
$this->assertEmpty( $object->getReferencingFields() );
}
public function testSerializesToArray()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
2018-11-21 03:34:39 +00:00
);
$object = $this->objectsService->createObject( $fields );
$this->assertEquals( $fields, $object->asArray() );
2018-11-21 03:34:39 +00:00
}
2018-11-23 13:28:46 +00:00
public function testDbStartsEmpty()
{
$this->assertSame( 0, $this->getConnection()->getRowCount( 'objects' ) );
$this->assertSame( 0, $this->getConnection()->getRowCount( 'fields' ) );
}
public function testItCreatesNestedObjects()
2018-11-21 03:34:39 +00:00
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
'attributedTo' => array(
'id' => 'https://example.com/actors/1',
'type' => 'Person',
),
2018-11-21 03:34:39 +00:00
);
$now = self::getNow();
$object = $this->objectsService->createObject( $fields );
$expected = new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
array( 'id' => 2, 'created' => $now, 'lastUpdated' => $now ),
),
'fields' => array(
array(
'id' => 1,
'object_id' => 1,
'name' => 'id',
'value' => 'https://example.com/notes/1',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
),
array(
'id' => 2,
'object_id' => 1,
'name' => 'type',
'value' => 'Note',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
),
array(
'id' => 3,
'object_id' => 1,
'name' => 'content',
'value' => 'This is a note',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
),
array(
'id' => 4,
2018-11-23 13:28:46 +00:00
'object_id' => 2,
'name' => 'id',
'value' => 'https://example.com/actors/1',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 5,
'object_id' => 2,
'name' => 'type',
'value' => 'Person',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 6,
'object_id' => 1,
'name' => 'attributedTo',
'value' => null,
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => 2,
),
2018-11-23 13:28:46 +00:00
),
) );
$expectedObjectsTable = $expected->getTable('objects');
$expectedFieldsTable = $expected->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 );
}
public function testItSerializesNestedObjects()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
'attributedTo' => array(
'id' => 'https://example.com/actors/1',
'type' => 'Person',
),
);
$object = $this->objectsService->createObject( $fields );
$this->assertEquals( $fields, $object->asArray() );
}
public function testItCreatesObjectsWithArray()
{
$fields = array(
'id' => 'https://example.com/collections/1',
'type' => 'Collection',
'items' => array(
"https://example.com/notes/1",
"https://example.com/notes/2",
),
);
$now = self::getNow();
2018-11-23 13:28:46 +00:00
$object = $this->objectsService->createObject( $fields );
$expected = new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
2018-11-23 13:28:46 +00:00
),
'fields' => array(
array(
2018-11-23 13:28:46 +00:00
'id' => 1,
'object_id' => 1,
'name' => 'id',
'value' => 'https://example.com/collections/1',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 2,
'object_id' => 1,
'name' => 'type',
'value' => 'Collection',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 3,
'object_id' => 1,
'name' => 'items',
'value' => 'https://example.com/notes/1',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 4,
'object_id' => 1,
'name' => 'items',
'value' => 'https://example.com/notes/2',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
),
) );
$expectedObjectsTable = $expected->getTable('objects');
$expectedFieldsTable = $expected->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 );
}
public function testItSerializesArrayFields()
{
$fields = array(
'id' => 'https://example.com/collections/1',
'type' => 'Collection',
'items' => array(
"https://example.com/notes/1",
"https://example.com/notes/2",
),
);
$object = $this->objectsService->createObject( $fields );
$this->assertEquals( $fields, $object->asArray() );
}
public function testItCreatesNestedObjectArrayFields()
{
$fields = array(
'id' => 'https://example.com/collections/1',
'type' => 'Collection',
'items' => array(
array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
),
array(
'id' => 'https://example.com/notes/2',
'type' => 'Note',
'content' => 'This is another note',
),
),
);
$now = self::getNow();
2018-11-23 13:28:46 +00:00
$object = $this->objectsService->createObject( $fields );
$expected = new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
array( 'id' => 2, 'created' => $now, 'lastUpdated' => $now ),
array( 'id' => 3, 'created' => $now, 'lastUpdated' => $now ),
2018-11-23 13:28:46 +00:00
),
'fields' => array(
array(
'id' => 1,
'object_id' => 1,
'name' => 'id',
'value' => 'https://example.com/collections/1',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 2,
'object_id' => 1,
'name' => 'type',
'value' => 'Collection',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 3,
'object_id' => 2,
'name' => 'id',
2018-11-23 13:28:46 +00:00
'value' => 'https://example.com/notes/1',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
),
array(
2018-11-23 13:28:46 +00:00
'id' => 4,
'object_id' => 2,
'name' => 'type',
2018-11-23 13:28:46 +00:00
'value' => 'Note',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 5,
'object_id' => 2,
'name' => 'content',
'value' => 'This is a note',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 6,
'object_id' => 1,
'name' => 'items',
'value' => null,
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => 2,
),
array(
2018-11-23 13:28:46 +00:00
'id' => 7,
'object_id' => 3,
'name' => 'id',
'value' => 'https://example.com/notes/2',
'created' => $now,
'lastUpdated' => $now,
'targetObject_id' => null,
),
2018-11-23 13:28:46 +00:00
array(
'id' => 8,
'object_id' => 3,
'name' => 'type',
'value' => 'Note',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 9,
'object_id' => 3,
'name' => 'content',
'value' => 'This is another note',
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => null,
),
array(
'id' => 10,
'object_id' => 1,
'name' => 'items',
'value' => null,
'created' => $now,
'lastUpdated' => $now,
2018-11-23 13:28:46 +00:00
'targetObject_id' => 3,
),
),
) );
$expectedObjectsTable = $expected->getTable('objects');
$expectedFieldsTable = $expected->getTable('fields');
$objectsQueryTable = $this->getConnection()->createQueryTable(
2018-11-21 03:34:39 +00:00
'objects', 'SELECT * FROM objects'
);
$fieldsQueryTable = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
$this->assertTablesEqual( $expectedFieldsTable, $fieldsQueryTable );
2018-11-21 03:34:39 +00:00
}
2018-11-23 13:28:46 +00:00
public function testItSerializesNestedObjectsInArrays()
{
$fields = array(
'id' => 'https://example.com/collections/1',
'type' => 'Collection',
'items' => array(
array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
),
array(
'id' => 'https://example.com/notes/2',
'type' => 'Note',
'content' => 'This is another note',
),
),
);
$object = $this->objectsService->createObject( $fields );
$arr = $object->asArray();
$this->assertEquals( $fields, $arr );
}
2018-11-21 03:34:39 +00:00
}
?>