CoreFixtures.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Language\Entity\Language;
  14. use Component\Notification\Entity\Attention;
  15. use Component\Notification\Entity\Notification;
  16. use Component\Subscription\Entity\ActorSubscription;
  17. use Doctrine\Bundle\FixturesBundle\Fixture;
  18. use Doctrine\Persistence\ObjectManager;
  19. use Symfony\Component\Intl\Locales;
  20. class CoreFixtures extends Fixture
  21. {
  22. public function load(ObjectManager $manager)
  23. {
  24. // Populate Initial Language values
  25. foreach (Locales::getNames() as $key => $name) {
  26. $manager->persist(Language::create(['locale' => $key, 'short_display' => $key, 'long_display' => $name]));
  27. }
  28. $manager->flush();
  29. $actors = [];
  30. $local_entities = [];
  31. foreach ([
  32. 'taken_user' => [
  33. LocalUser::class,
  34. 'setId',
  35. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'taken_user@provider.any'],
  36. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  37. ],
  38. 'some_user' => [
  39. LocalUser::class,
  40. 'setId',
  41. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'some_user@provider.any'],
  42. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  43. ],
  44. 'admin' => [
  45. LocalUser::class,
  46. 'setId',
  47. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'admin@provider.any'],
  48. ['roles' => ActorLocalRoles::OPERATOR | ActorLocalRoles::MODERATOR | ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  49. ],
  50. 'local_user_test_user' => [
  51. LocalUser::class,
  52. 'setId',
  53. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'local_user_test_user@provider.any'],
  54. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  55. ],
  56. 'form_personal_info_test_user' => [
  57. LocalUser::class,
  58. 'setId',
  59. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_personal_info_test_user@provider.any'],
  60. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  61. ],
  62. 'form_account_test_user' => [
  63. LocalUser::class,
  64. 'setId',
  65. ['password' => LocalUser::hashPassword('foobar'), 'outgoing_email' => 'form_account_test_user@provider.any'],
  66. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  67. ],
  68. 'taken_public_group' => [
  69. LocalGroup::class,
  70. 'setActorId',
  71. [],
  72. ['roles' => ActorLocalRoles::VISITOR, 'type' => Actor::GROUP],
  73. ],
  74. 'taken_private_group' => [
  75. LocalGroup::class,
  76. 'setActorId',
  77. [],
  78. ['roles' => ActorLocalRoles::VISITOR | ActorLocalRoles::PRIVATE_GROUP, 'type' => Actor::GROUP],
  79. ],
  80. 'local_user_note_test' => [
  81. LocalUser::class,
  82. 'setId',
  83. [],
  84. ['roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON],
  85. ],
  86. ] as $nick => [$entity, $method, $extra_create, $extra_create_actor]) {
  87. $actor = Actor::create(array_merge(['nickname' => $nick, 'is_local' => true], $extra_create_actor));
  88. $manager->persist($actor);
  89. // cannot use array spread for arrays with string keys
  90. $ent = $entity::create(array_merge(['nickname' => $nick], $extra_create));
  91. $ent->{$method}($actor->getId());
  92. $local_entities[$nick] = $ent;
  93. $manager->persist($ent);
  94. // Add self subscriptions
  95. $manager->persist(ActorSubscription::create(['subscriber_id' => $actor->getId(), 'subscribed_id' => $actor->getId()]));
  96. $actors[$nick] = $actor;
  97. }
  98. $notes = [];
  99. $notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => true]);
  100. $notes[] = Note::create(['actor_id' => $actors['taken_user']->getId(), 'content' => 'private note', 'scope' => VisibilityScope::MESSAGE, 'content_type' => 'text/plain', 'is_local' => false]);
  101. $notes[] = Note::create(['actor_id' => $actors['local_user_note_test']->getId(), 'content' => 'message', 'scope' => VisibilityScope::MESSAGE, 'content_type' => 'text/plain', 'is_local' => false]);
  102. $notes[] = Note::create(['actor_id' => $actors['local_user_note_test']->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => false]);
  103. foreach ($notes as $note) {
  104. $manager->persist($note);
  105. $activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
  106. Conversation::assignLocalConversation($note, null);
  107. $manager->persist($activity);
  108. }
  109. $group_notes = [];
  110. $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]);
  111. $group_notes[] = $private_group_note = Note::create(['actor_id' => $actors['local_user_note_test']->getId(), 'content' => 'group note private', 'scope' => VisibilityScope::GROUP, 'content_type' => 'text/plain', 'is_local' => true]);
  112. foreach ($group_notes as $note) {
  113. $manager->persist($note);
  114. $activity = Activity::create(['actor_id' => $actors['taken_user']->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
  115. Conversation::assignLocalConversation($note, null);
  116. $manager->persist($activity);
  117. $manager->persist(Notification::create(['activity_id' => $activity->getId(), 'target_id' => $local_entities['taken_public_group']->getActorId(), 'reason' => 'testing']));
  118. $manager->persist(Notification::create(['activity_id' => $activity->getId(), 'target_id' => $local_entities['taken_private_group']->getActorId(), 'reason' => 'testing']));
  119. }
  120. $manager->persist(Attention::create(['object_type' => Note::schemaName(), 'object_id' => $public_group_note->getId(), 'target_id' => $local_entities['taken_public_group']->getActorId()]));
  121. $manager->persist(GroupMember::create(['group_id' => $local_entities['taken_private_group']->getActorId(), 'actor_id' => $actors['some_user']->getId()]));
  122. $manager->persist(Attention::create(['object_type' => Note::schemaName(), 'object_id' => $private_group_note->getId(), 'target_id' => $local_entities['taken_private_group']->getActorId()]));
  123. $manager->flush();
  124. }
  125. }