2021-08-14 20:43:35 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2022-03-13 18:00:21 +00:00
|
|
|
namespace App\Test\Fixtures;
|
2021-08-14 20:43:35 +01:00
|
|
|
|
2022-03-06 20:16:09 +00:00
|
|
|
use App\Core\ActorLocalRoles;
|
2022-03-27 15:19:09 +01:00
|
|
|
use App\Core\DB;
|
2021-08-14 20:43:35 +01:00
|
|
|
use App\Core\GSFile;
|
2022-03-06 20:16:09 +00:00
|
|
|
use App\Entity\Activity;
|
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Entity\Note;
|
2021-08-19 12:54:06 +01:00
|
|
|
use App\Util\TemporaryFile;
|
2022-03-06 20:16:09 +00:00
|
|
|
use Component\Attachment\Entity\ActorToAttachment;
|
|
|
|
use Component\Attachment\Entity\AttachmentToNote;
|
|
|
|
use Component\Conversation\Conversation;
|
2021-08-14 20:43:35 +01:00
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
|
|
|
use Doctrine\Persistence\ObjectManager;
|
2021-08-19 01:44:03 +01:00
|
|
|
use Exception;
|
2021-08-14 21:46:44 +01:00
|
|
|
use Functional as F;
|
2021-08-14 20:43:35 +01:00
|
|
|
|
|
|
|
class MediaFixtures extends Fixture
|
|
|
|
{
|
|
|
|
public function load(ObjectManager $manager)
|
|
|
|
{
|
|
|
|
DB::setManager($manager);
|
2022-03-06 20:16:09 +00:00
|
|
|
$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);
|
2021-10-10 09:26:18 +01:00
|
|
|
F\map(
|
|
|
|
glob(INSTALLDIR . '/tests/sample-uploads/*'),
|
2022-03-06 20:16:09 +00:00
|
|
|
function (string $filepath) use ($manager, $actor, $note) {
|
2021-10-10 09:26:18 +01:00
|
|
|
$file = new TemporaryFile();
|
|
|
|
$file->write(file_get_contents($filepath));
|
|
|
|
try {
|
2022-03-06 20:16:09 +00:00
|
|
|
$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();
|
2021-10-10 09:26:18 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
echo "Could not save file {$filepath}, failed with {$e}\n";
|
|
|
|
} finally {
|
|
|
|
unset($file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-08-14 20:43:35 +01:00
|
|
|
$manager->flush();
|
|
|
|
}
|
|
|
|
}
|