diff --git a/src/app/Models/Document.php b/src/app/Models/Document.php index e7d6eb8..9de8f9a 100644 --- a/src/app/Models/Document.php +++ b/src/app/Models/Document.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Helpers\SlugHelper; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -17,7 +18,7 @@ class Document extends Model { - use SoftDeletes; + use HasFactory, SoftDeletes; /** * Get the route key for the model. diff --git a/src/database/factories/DocumentFactory.php b/src/database/factories/DocumentFactory.php new file mode 100644 index 0000000..017a6c2 --- /dev/null +++ b/src/database/factories/DocumentFactory.php @@ -0,0 +1,62 @@ + + */ +class DocumentFactory extends Factory +{ + protected $model = Document::class; + + public function definition(): array + { + $title = fake()->unique()->sentence(3); + + return [ + 'path' => $title . '.md', + 'slug' => SlugHelper::generate($title), + 'default_locale' => 'en', + 'file_size' => 0, + 'file_hash' => str_repeat('0', 64), + 'file_modified_at' => now(), + ]; + } + + /** + * After creating, attach a translation in the document's default_locale. + * Pass an explicit ['title' => ..., 'content' => ...] via withTranslation() to override. + */ + public function configure(): static + { + return $this->afterCreating(function (Document $document) { + if ($document->translations()->count() === 0) { + DocumentTranslation::factory()->create([ + 'document_id' => $document->id, + 'locale' => $document->default_locale, + ]); + } + }); + } + + /** + * Override the default_locale and create the corresponding translation. + */ + public function defaultLocale(string $locale): static + { + return $this->state(['default_locale' => $locale]); + } + + /** + * Suppress automatic translation creation (caller will create manually). + */ + public function withoutTranslations(): static + { + return $this->afterCreating(fn () => null); + } +} diff --git a/src/database/factories/DocumentTranslationFactory.php b/src/database/factories/DocumentTranslationFactory.php new file mode 100644 index 0000000..df57ee2 --- /dev/null +++ b/src/database/factories/DocumentTranslationFactory.php @@ -0,0 +1,29 @@ + + */ +class DocumentTranslationFactory extends Factory +{ + protected $model = DocumentTranslation::class; + + public function definition(): array + { + $title = fake()->sentence(3); + $content = fake()->paragraphs(3, true); + + return [ + 'document_id' => Document::factory()->withoutTranslations(), + 'locale' => 'en', + 'title' => $title, + 'content' => $content, + 'rendered_html' => '

' . e($content) . '

', + ]; + } +}