CoreFixtures.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types = 1);
  3. namespace App\Test\Fixtures;
  4. use App\Core\ActorLocalRoles;
  5. use App\Core\VisibilityScope;
  6. use App\Entity\Activity;
  7. use App\Entity\Actor;
  8. use App\Entity\LocalUser;
  9. use App\Entity\Note;
  10. use Component\Conversation\Conversation;
  11. use Component\Group\Entity\GroupMember;
  12. use Component\Group\Entity\LocalGroup;
  13. use Component\Notification\Entity\Attention;
  14. use Component\Notification\Entity\Notification;
  15. use Component\Subscription\Entity\ActorSubscription;
  16. use Doctrine\Bundle\FixturesBundle\Fixture;
  17. use Doctrine\Persistence\ObjectManager;
  18. class CoreFixtures extends Fixture
  19. {
  20. public function load(ObjectManager $manager)
  21. {
  22. $actors = [];
  23. $local_entities = [];
  24. foreach ([
  25. 'taken_user' => [
  26. LocalUser::class,
  27. 'setId',
  28. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'taken_user@provider.any'],
  29. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  30. ],
  31. 'some_user' => [
  32. LocalUser::class,
  33. 'setId',
  34. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'some_user@provider.any'],
  35. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  36. ],
  37. 'admin' => [
  38. LocalUser::class,
  39. 'setId',
  40. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'admin@provider.any'],
  41. ['roles' => ActorLocalRoles::OPERATOR | ActorLocalRoles::MODERATOR | ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  42. ],
  43. 'local_user_test_user' => [
  44. LocalUser::class,
  45. 'setId',
  46. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'local_user_test_user@provider.any'],
  47. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  48. ],
  49. 'form_personal_info_test_user' => [
  50. LocalUser::class,
  51. 'setId',
  52. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_personal_info_test_user@provider.any'],
  53. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  54. ],
  55. 'form_account_test_user' => [
  56. LocalUser::class,
  57. 'setId',
  58. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_account_test_user@provider.any'],
  59. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  60. ],
  61. 'taken_public_group' => [
  62. LocalGroup::class,
  63. 'setActorId',
  64. [],
  65. ['roles' => ActorLocalRoles::VISITOR, 'type' => Actor::GROUP],
  66. ],
  67. 'taken_private_group' => [
  68. LocalGroup::class,
  69. 'setActorId',
  70. [],
  71. ['roles' => ActorLocalRoles::VISITOR | ActorLocalRoles::PRIVATE_GROUP, 'type' => Actor::GROUP],
  72. ],
  73. ] as $nick => [$entity, $method, $extra_create, $extra_create_actor]) {
  74. $actor = Actor::create(array_merge(['nickname' => $nick, 'is_local' => true], $extra_create_actor));
  75. $manager->persist($actor);
  76. // cannot use array spread for arrays with string keys
  77. $ent = $entity::create(array_merge(['nickname' => $nick], $extra_create));
  78. $ent->{$method}($actor->getId());
  79. $local_entities[$nick] = $ent;
  80. $manager->persist($ent);
  81. // Add self subscriptions
  82. $manager->persist(ActorSubscription::create(['subscriber_id' => $actor->getId(), 'subscribed_id' => $actor->getId()]));
  83. $actors[$nick] = $actor;
  84. }
  85. $notes = [];
  86. $notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => true]);
  87. $notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'private note', 'scope' => VisibilityScope::MESSAGE, 'content_type' => 'text/plain', 'is_local' => false]);
  88. foreach ($notes as $note) {
  89. $manager->persist($note);
  90. $activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
  91. Conversation::assignLocalConversation($note, null);
  92. $manager->persist($activity);
  93. }
  94. $group_notes = [];
  95. $group_notes[] = $public_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note public', 'scope' => VisibilityScope::EVERYWHERE, 'content_type' => 'text/plain', 'is_local' => true]);
  96. $group_notes[] = $private_group_note = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'group note private', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
  97. foreach ($group_notes as $note) {
  98. $manager->persist($note);
  99. $activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
  100. Conversation::assignLocalConversation($note, null);
  101. $manager->persist($activity);
  102. $manager->persist(Notification::create(['activity_id' => $activity->getId(), 'target_id' => $local_entities['taken_public_group']->getActorId(), 'reason' => 'testing']));
  103. $manager->persist(Notification::create(['activity_id' => $activity->getId(), 'target_id' => $local_entities['taken_private_group']->getActorId(), 'reason' => 'testing']));
  104. }
  105. $manager->persist(Attention::create(['note_id' => $public_group_note->getId(), 'target_id' => $local_entities['taken_public_group']->getActorId()]));
  106. $manager->persist(GroupMember::create(['group_id' => $local_entities['taken_private_group']->getActorId(), 'actor_id' => $actors['some_user']->getId()]));
  107. $manager->persist(Attention::create(['note_id' => $private_group_note->getId(), 'target_id' => $local_entities['taken_private_group']->getActorId()]));
  108. $manager->flush();
  109. }
  110. }