MediaFixtures.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types = 1);
  3. namespace App\Test\Fixtures;
  4. use App\Core\ActorLocalRoles;
  5. use App\Core\DB\DB;
  6. use App\Core\GSFile;
  7. use App\Entity\Activity;
  8. use App\Entity\Actor;
  9. use App\Entity\Note;
  10. use App\Util\TemporaryFile;
  11. use Component\Attachment\Entity\ActorToAttachment;
  12. use Component\Attachment\Entity\AttachmentToNote;
  13. use Component\Conversation\Conversation;
  14. use Doctrine\Bundle\FixturesBundle\Fixture;
  15. use Doctrine\Persistence\ObjectManager;
  16. use Exception;
  17. use Functional as F;
  18. class MediaFixtures extends Fixture
  19. {
  20. public function load(ObjectManager $manager)
  21. {
  22. DB::setManager($manager);
  23. $actor = Actor::create(['nickname' => 'attachment_test_actor', 'is_local' => false, 'roles' => ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR, 'type' => Actor::PERSON]);
  24. $manager->persist($actor);
  25. $note = Note::create(['actor_id' => $actor->getId(), 'content' => 'some other content', 'content_type' => 'text/plain', 'is_local' => true]);
  26. $manager->persist($note);
  27. $activity = Activity::create(['actor_id' => $actor->getId(), 'verb' => 'create', 'object_type' => 'note', 'object_id' => $note->getId(), 'source' => 'auto-test']);
  28. Conversation::assignLocalConversation($note, null);
  29. $manager->persist($activity);
  30. F\map(
  31. glob(INSTALLDIR . '/tests/sample-uploads/*'),
  32. function (string $filepath) use ($manager, $actor, $note) {
  33. $file = new TemporaryFile();
  34. $file->write(file_get_contents($filepath));
  35. try {
  36. $a = GSFile::storeFileAsAttachment($file);
  37. $manager->persist(ActorToAttachment::create(['attachment_id' => $a->getId(), 'actor_id' => $actor->getId()]));
  38. $manager->persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId(), 'title' => mb_substr($filepath, mb_strrpos($filepath, '/') + 1)]));
  39. $a->livesIncrementAndGet();
  40. } catch (Exception $e) {
  41. echo "Could not save file {$filepath}, failed with {$e}\n";
  42. } finally {
  43. unset($file);
  44. }
  45. },
  46. );
  47. $manager->flush();
  48. }
  49. }