Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

3.4 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Enums\Media\Type as MediaType;
use App\Models\Media;
use App\Models\Workspace;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

beforeEach(function () {
    Storage::fake();
});

test('media belongs to mediable', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');

    expect($media->mediable->id)->toBe($workspace->id);
});

test('media has url attribute', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');

    expect($media->url)->not->toBeEmpty();
});

test('media casts type to enum', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');

    expect($media->type)->toBeInstanceOf(MediaType::class);
});

test('media casts size to integer', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');

    expect($media->size)->toBeInt();
});

test('media casts meta to array', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo', ['key' => 'value']);

    expect($media->meta)->toBeArray();
    expect($media->meta['key'])->toBe('value');
});

test('media deletes file from storage when deleted', function () {
    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');
    $path = $media->path;

    Storage::assertExists($path);

    $media->delete();

    Storage::assertMissing($path);
});

test('a media delete rolled back with its transaction keeps the file', function () {
    $workspace = Workspace::factory()->create();
    $media = $workspace->addMedia(UploadedFile::fake()->image('logo.jpg', 100, 100), 'logo');

    try {
        DB::transaction(function () use ($media): void {
            $media->delete();

            throw new RuntimeException('rolled back');
        });
    } catch (RuntimeException) {
    }

    expect(Media::query()->find($media->id))->not->toBeNull();
    Storage::assertExists($media->path);
});

test('a media file another row still uses is kept when one row is deleted', function () {
    $workspace = Workspace::factory()->create();
    $media = $workspace->addMedia(UploadedFile::fake()->image('logo.jpg', 100, 100), 'logo');
    $twin = $media->replicate();
    $twin->mediable_id = Workspace::factory()->create()->id;
    $twin->workspace_id = $twin->mediable_id;
    $twin->save();

    $media->delete();

    Storage::assertExists($media->path);
});

test('media can get temporary url', function () {
    // Use a driver that supports temporary URLs
    Storage::fake('s3');
    config(['filesystems.default' => 's3']);

    $workspace = Workspace::factory()->create();
    $file = UploadedFile::fake()->image('logo.jpg', 100, 100);
    $media = $workspace->addMedia($file, 'logo');

    // The fake driver returns a basic URL format
    $url = $media->getTemporaryUrl(30);

    expect($url)->toBeString();
});