[TESTS] Fix DataFixtures

This commit is contained in:
Diogo Peralta Cordeiro 2022-03-06 20:16:09 +00:00
parent 9a0c74cb0c
commit a37ce86d05
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 77 additions and 18 deletions

View File

@ -21,6 +21,7 @@ declare(strict_types = 1);
namespace Component\Group\Entity;
use App\Core\ActorLocalRoles;
use App\Core\Entity;
use DateTimeInterface;
@ -44,7 +45,7 @@ class GroupMember extends Entity
// @codeCoverageIgnoreStart
private int $group_id;
private int $actor_id;
private int $roles;
private int $roles = ActorLocalRoles::VISITOR;
private ?string $uri = null;
private DateTimeInterface $created;
private DateTimeInterface $modified;
@ -125,7 +126,7 @@ class GroupMember extends Entity
'fields' => [
'group_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'group_member_group_id_fkey', 'not null' => true, 'description' => 'foreign key to group table'],
'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'name' => 'group_member_actor_id_fkey', 'not null' => true, 'description' => 'foreign key to actor table'],
'roles' => ['type' => 'int', 'not null' => true, 'description' => 'Bitmap of permissions this actor has'],
'roles' => ['type' => 'int', 'not null' => true, 'default' => ActorLocalRoles::VISITOR, 'description' => 'Bitmap of permissions this actor has'],
'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'],
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],

View File

@ -6,12 +6,15 @@ namespace App\DataFixtures;
use App\Core\ActorLocalRoles;
use App\Core\VisibilityScope;
use App\Entity\Activity;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note;
use Component\Group\Entity\GroupInbox;
use Component\Conversation\Conversation;
use Component\Group\Entity\GroupMember;
use Component\Group\Entity\LocalGroup;
use Component\Notification\Entity\Attention;
use Component\Notification\Entity\Notification;
use Component\Subscription\Entity\ActorSubscription;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
@ -23,37 +26,75 @@ class CoreFixtures extends Fixture
$actors = [];
$local_entities = [];
foreach ([
'taken_user' => [LocalUser::class, 'setId', ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'email@provider'], []],
'some_user' => [LocalUser::class, 'setId', [], []],
'admin' => [LocalUser::class, 'setId', [], ['roles' => ActorLocalRoles::OPERATOR | ActorLocalRoles::MODERATOR | ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR]],
'local_user_test_user' => [LocalUser::class, 'setId', ['password' => LocalUser::hashPassword('foobar')], []],
'form_personal_info_test_user' => [LocalUser::class, 'setId', [], []],
'form_account_test_user' => [LocalUser::class, 'setId', ['password' => LocalUser::hashPassword('some password')], []],
'taken_group' => [LocalGroup::class, 'setGroupId', [], []],
'taken_user' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'taken_user@provider.any'],
['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'some_user' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'some_user@provider.any'],
['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'admin' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'admin@provider.any'],
['roles' => ActorLocalRoles::OPERATOR | ActorLocalRoles::MODERATOR | ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'local_user_test_user' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'local_user_test_user@provider.any'],
['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'form_personal_info_test_user' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_personal_info_test_user@provider.any'],
['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'form_account_test_user' => [
LocalUser::class,
'setId',
['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_account_test_user@provider.any'],
['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
],
'taken_group' => [
LocalGroup::class,
'setActorId',
[],
['roles' => ActorLocalRoles::VISITOR, 'type' => Actor::GROUP],
],
] as $nick => [$entity, $method, $extra_create, $extra_create_actor]) {
$actor = Actor::create(array_merge(['nickname' => $nick, 'is_local' => true], $extra_create_actor));
$manager->persist($actor);
$ent = $entity::create(array_merge(['nickname' => $nick], $extra_create)); // cannot use array spread for arrays with string keys
// cannot use array spread for arrays with string keys
$ent = $entity::create(array_merge(['nickname' => $nick], $extra_create));
$ent->{$method}($actor->getId());
$local_entities[$nick] = $ent;
$manager->persist($ent);
// Add self subscriptions
$manager->persist(ActorSubscription::create(['subscriber' => $actor->getId(), 'subscribed' => $actor->getId()]));
$manager->persist(ActorSubscription::create(['subscriber_id' => $actor->getId(), 'subscribed_id' => $actor->getId()]));
$actors[$nick] = $actor;
}
$n = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'some content', 'content_type' => 'text/plain', 'is_local' => true]);
$manager->persist($n);
$notes = [];
$notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => true]);
$notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'private note', 'scope' => VisibilityScope::COLLECTION, 'content_type' => 'text/plain', 'is_local' => false]);
$notes[] = $group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
foreach ($notes as $note) {
$manager->persist($note);
$activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
Conversation::assignLocalConversation($note, null);
$manager->persist($activity);
}
$manager->persist(GroupMember::create(['group_id' => $local_entities['taken_group']->getGroupId(), 'actor_id' => $actors['some_user']->getId()]));
$manager->persist(GroupInbox::create(['group_id' => $local_entities['taken_group']->getGroupId(), 'activity_id' => $group_note->getId()]));
$manager->persist(GroupMember::create(['group_id' => $local_entities['taken_group']->getActorId(), 'actor_id' => $actors['some_user']->getId()]));
$manager->persist(Attention::create(['note_id' => $group_note->getId(), 'target_id' => $local_entities['taken_group']->getActorId()]));
$manager->persist(Notification::create(['activity_id' => $activity->getId(), 'target_id' => $local_entities['taken_group']->getActorId(), 'reason' => 'testing']));
$manager->flush();
}
}

View File

@ -4,9 +4,16 @@ declare(strict_types = 1);
namespace App\DataFixtures;
use App\Core\ActorLocalRoles;
use App\Core\DB\DB;
use App\Core\GSFile;
use App\Entity\Activity;
use App\Entity\Actor;
use App\Entity\Note;
use App\Util\TemporaryFile;
use Component\Attachment\Entity\ActorToAttachment;
use Component\Attachment\Entity\AttachmentToNote;
use Component\Conversation\Conversation;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Exception;
@ -17,13 +24,23 @@ class MediaFixtures extends Fixture
public function load(ObjectManager $manager)
{
DB::setManager($manager);
$actor = Actor::create(['nickname' => 'attachment_test_actor', 'is_local' => false, 'roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON]);
$manager->persist($actor);
$note = Note::create(['actor_id' => $actor->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => true]);
$manager->persist($note);
$activity = Activity::create(['actor_id' => $actor->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
Conversation::assignLocalConversation($note, null);
$manager->persist($activity);
F\map(
glob(INSTALLDIR . '/tests/sample-uploads/*'),
function (string $filepath) {
function (string $filepath) use ($manager, $actor, $note) {
$file = new TemporaryFile();
$file->write(file_get_contents($filepath));
try {
GSFile::storeFileAsAttachment($file);
$a = GSFile::storeFileAsAttachment($file);
$manager->persist(ActorToAttachment::create(['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]));
$manager->persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => mb_substr($filepath, mb_strrpos($filepath, '/') + 1)]));
$a->livesIncrementAndGet();
} catch (Exception $e) {
echo "Could not save file {$filepath}, failed with {$e}\n";
} finally {