2018-12-21 14:51:58 +00:00
|
|
|
<?php
|
2019-01-08 15:14:42 +00:00
|
|
|
namespace ActivityPub\Test\Entities;
|
2018-12-21 14:51:58 +00:00
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use ActivityPub\Crypto\RsaKeypair;
|
|
|
|
use ActivityPub\Entities\ActivityPubObject;
|
|
|
|
use ActivityPub\Database\PrefixNamingStrategy;
|
2019-01-20 03:37:07 +00:00
|
|
|
use ActivityPub\Test\TestConfig\ArrayDataSet;
|
|
|
|
use ActivityPub\Test\TestConfig\SQLiteTestCase;
|
2018-12-21 14:51:58 +00:00
|
|
|
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\Tools\Setup;
|
|
|
|
|
|
|
|
class EntityTest extends SQLiteTestCase
|
|
|
|
{
|
|
|
|
protected $entityManager;
|
|
|
|
protected $dateTimeProvider;
|
|
|
|
|
|
|
|
protected function getDataSet()
|
|
|
|
{
|
|
|
|
return new ArrayDataSet( array(
|
|
|
|
'objects' => array(),
|
|
|
|
'fields' => array(),
|
|
|
|
'keys' => array(),
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$dbConfig = Setup::createAnnotationMetadataConfiguration(
|
2019-01-08 15:14:42 +00:00
|
|
|
array( __DIR__ . '/../../src/Entities' ), true
|
2018-12-21 14:51:58 +00:00
|
|
|
);
|
|
|
|
$namingStrategy = new PrefixNamingStrategy( '' );
|
|
|
|
$dbConfig->setNamingStrategy( $namingStrategy );
|
|
|
|
$dbParams = array(
|
|
|
|
'driver' => 'pdo_sqlite',
|
2019-01-08 15:14:42 +00:00
|
|
|
'path' => $this->getDbPath(),
|
2018-12-21 14:51:58 +00:00
|
|
|
);
|
|
|
|
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
|
2019-01-20 22:20:24 +00:00
|
|
|
$this->dateTimeProvider = new TestDateTimeProvider( array(
|
|
|
|
'objects-service.create' => new DateTime( "12:00" ),
|
|
|
|
'objects-service.update' => new DateTime( "12:01" ),
|
|
|
|
) );
|
2018-12-21 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTime( $context ) {
|
|
|
|
return $this->dateTimeProvider
|
|
|
|
->getTime( $context )
|
|
|
|
->format( "Y-m-d H:i:s" );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCreatesAnObjectWithAPrivateKey()
|
|
|
|
{
|
2019-01-20 22:20:24 +00:00
|
|
|
$object = new ActivityPubObject( $this->dateTimeProvider->getTime( 'objects-service.create' ) );
|
2018-12-21 14:51:58 +00:00
|
|
|
$privateKey = 'a private key';
|
|
|
|
$object->setPrivateKey( $privateKey );
|
|
|
|
$this->entityManager->persist( $object );
|
|
|
|
$this->entityManager->flush();
|
2019-01-20 22:20:24 +00:00
|
|
|
$now = $this->getTime( 'objects-service.create' );
|
2018-12-21 14:51:58 +00:00
|
|
|
$expected = new ArrayDataSet( array(
|
|
|
|
'objects' => array(
|
|
|
|
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
|
|
|
|
),
|
|
|
|
'keys' => array(
|
|
|
|
array( 'id' => 1, 'object_id' => 1, 'key' => $privateKey )
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
$expectedObjectsTable = $expected->getTable( 'objects' );
|
|
|
|
$expectedKeysTable = $expected->getTable( 'keys' );
|
|
|
|
$objectsQueryTable = $this->getConnection()->createQueryTable(
|
|
|
|
'objects', 'SELECT * FROM objects'
|
|
|
|
);
|
|
|
|
$keysQueryTable = $this->getConnection()->createQueryTable(
|
|
|
|
'keys', 'SELECT * FROM keys'
|
|
|
|
);
|
|
|
|
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
|
|
|
|
$this->assertTablesEqual( $expectedKeysTable, $keysQueryTable );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function itUpdatesAPrivateKey()
|
|
|
|
{
|
2019-01-20 22:20:24 +00:00
|
|
|
$object = new ActivityPubObject( $this->dateTimeProvider->getTime( 'objects-service.create' ) );
|
2018-12-21 14:51:58 +00:00
|
|
|
$privateKey = 'a private key';
|
|
|
|
$object->setPrivateKey( $privateKey );
|
|
|
|
$this->entityManager->persist( $object );
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$newPrivateKey = 'a new private key';
|
|
|
|
$object->setPrivateKey( $newPrivateKey );
|
|
|
|
$this->entityManager->persiste( $object );
|
|
|
|
$this->entityManager->flush();
|
2019-01-20 22:20:24 +00:00
|
|
|
$now = $this->getTime( 'objects-service.create' );
|
2018-12-21 14:51:58 +00:00
|
|
|
$expected = new ArrayDataSet( array(
|
|
|
|
'objects' => array(
|
|
|
|
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
|
|
|
|
),
|
|
|
|
'keys' => array(
|
|
|
|
array( 'id' => 1, 'object_id' => 1, 'key' => $newPrivateKey )
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
$expectedObjectsTable = $expected->getTable( 'objects' );
|
|
|
|
$expectedKeysTable = $expected->getTable( 'keys' );
|
|
|
|
$objectsQueryTable = $this->getConnection()->createQueryTable(
|
|
|
|
'objects', 'SELECT * FROM objects'
|
|
|
|
);
|
|
|
|
$keysQueryTable = $this->getConnection()->createQueryTable(
|
|
|
|
'keys', 'SELECT * FROM keys'
|
|
|
|
);
|
|
|
|
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
|
|
|
|
$this->assertTablesEqual( $expectedKeysTable, $keysQueryTable );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|