371 lines
12 KiB
PHP
371 lines
12 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\Article;
|
||
|
|
use App\Models\ArticlePublication;
|
||
|
|
use App\Models\Feed;
|
||
|
|
use App\Models\Keyword;
|
||
|
|
use App\Models\Language;
|
||
|
|
use App\Models\Log;
|
||
|
|
use App\Models\PlatformAccount;
|
||
|
|
use App\Models\PlatformChannel;
|
||
|
|
use App\Models\PlatformChannelPost;
|
||
|
|
use App\Models\PlatformInstance;
|
||
|
|
use App\Models\Route;
|
||
|
|
use App\Models\Setting;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class DatabaseIntegrationTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_user_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'name' => 'Test User',
|
||
|
|
'email' => 'test@example.com'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('users', [
|
||
|
|
'name' => 'Test User',
|
||
|
|
'email' => 'test@example.com'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals('Test User', $user->name);
|
||
|
|
$this->assertEquals('test@example.com', $user->email);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_language_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create([
|
||
|
|
'name' => 'English',
|
||
|
|
'code' => 'en'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('languages', [
|
||
|
|
'name' => 'English',
|
||
|
|
'code' => 'en'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_instance_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$instance = PlatformInstance::factory()->create([
|
||
|
|
'name' => 'Test Instance',
|
||
|
|
'url' => 'https://test.lemmy.world'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('platform_instances', [
|
||
|
|
'name' => 'Test Instance',
|
||
|
|
'url' => 'https://test.lemmy.world'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_account_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$instance = PlatformInstance::factory()->create();
|
||
|
|
|
||
|
|
$account = PlatformAccount::factory()->create([
|
||
|
|
'platform_instance_id' => $instance->id,
|
||
|
|
'username' => 'testuser',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('platform_accounts', [
|
||
|
|
'platform_instance_id' => $instance->id,
|
||
|
|
'username' => 'testuser',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($instance->id, $account->platformInstance->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_channel_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$account = PlatformAccount::factory()->create();
|
||
|
|
|
||
|
|
$channel = PlatformChannel::factory()->create([
|
||
|
|
'platform_account_id' => $account->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'name' => 'Test Channel',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('platform_channels', [
|
||
|
|
'platform_account_id' => $account->id,
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'name' => 'Test Channel',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($account->id, $channel->platformAccount->id);
|
||
|
|
$this->assertEquals($language->id, $channel->language->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_feed_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
|
||
|
|
$feed = Feed::factory()->create([
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'name' => 'Test Feed',
|
||
|
|
'url' => 'https://example.com/feed.rss',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('feeds', [
|
||
|
|
'language_id' => $language->id,
|
||
|
|
'name' => 'Test Feed',
|
||
|
|
'url' => 'https://example.com/feed.rss',
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($language->id, $feed->language->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_article_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
|
||
|
|
$article = Article::factory()->create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'title' => 'Test Article',
|
||
|
|
'url' => 'https://example.com/article',
|
||
|
|
'approval_status' => 'pending'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('articles', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'title' => 'Test Article',
|
||
|
|
'url' => 'https://example.com/article',
|
||
|
|
'approval_status' => 'pending'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($feed->id, $article->feed->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_article_publication_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$article = Article::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create();
|
||
|
|
|
||
|
|
$publication = ArticlePublication::create([
|
||
|
|
'article_id' => $article->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'post_id' => 'test-post-123',
|
||
|
|
'published_at' => now(),
|
||
|
|
'published_by' => 'test-user'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('article_publications', [
|
||
|
|
'article_id' => $article->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'post_id' => 'test-post-123',
|
||
|
|
'published_by' => 'test-user'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($article->id, $publication->article->id);
|
||
|
|
$this->assertEquals($channel->id, $publication->platformChannel->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_route_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$channel = PlatformChannel::factory()->create();
|
||
|
|
|
||
|
|
$route = Route::factory()->create([
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('routes', [
|
||
|
|
'feed_id' => $feed->id,
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($feed->id, $route->feed->id);
|
||
|
|
$this->assertEquals($channel->id, $route->platformChannel->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_channel_post_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$channel = PlatformChannel::factory()->create();
|
||
|
|
|
||
|
|
$post = PlatformChannelPost::create([
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'post_id' => 'external-post-123',
|
||
|
|
'title' => 'Test Post',
|
||
|
|
'content' => 'Test content',
|
||
|
|
'url' => 'https://example.com/post',
|
||
|
|
'created_at' => now(),
|
||
|
|
'updated_at' => now()
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('platform_channel_posts', [
|
||
|
|
'platform_channel_id' => $channel->id,
|
||
|
|
'post_id' => 'external-post-123',
|
||
|
|
'title' => 'Test Post'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertEquals($channel->id, $post->platformChannel->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_keyword_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$keyword = Keyword::factory()->create([
|
||
|
|
'keyword' => 'test keyword',
|
||
|
|
'is_blocked' => false
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('keywords', [
|
||
|
|
'keyword' => 'test keyword',
|
||
|
|
'is_blocked' => false
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_log_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$log = Log::create([
|
||
|
|
'level' => 'info',
|
||
|
|
'message' => 'Test log message',
|
||
|
|
'context' => json_encode(['key' => 'value']),
|
||
|
|
'logged_at' => now()
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('logs', [
|
||
|
|
'level' => 'info',
|
||
|
|
'message' => 'Test log message'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_setting_model_creates_successfully(): void
|
||
|
|
{
|
||
|
|
$setting = Setting::factory()->create([
|
||
|
|
'key' => 'test_setting',
|
||
|
|
'value' => 'test_value'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('settings', [
|
||
|
|
'key' => 'test_setting',
|
||
|
|
'value' => 'test_value'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_feed_articles_relationship(): void
|
||
|
|
{
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$articles = Article::factory()->count(3)->create(['feed_id' => $feed->id]);
|
||
|
|
|
||
|
|
$this->assertCount(3, $feed->articles);
|
||
|
|
|
||
|
|
foreach ($articles as $article) {
|
||
|
|
$this->assertTrue($feed->articles->contains($article));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_article_publications_relationship(): void
|
||
|
|
{
|
||
|
|
$article = Article::factory()->create();
|
||
|
|
$publications = ArticlePublication::factory()->count(2)->create(['article_id' => $article->id]);
|
||
|
|
|
||
|
|
$this->assertCount(2, $article->publications);
|
||
|
|
|
||
|
|
foreach ($publications as $publication) {
|
||
|
|
$this->assertTrue($article->publications->contains($publication));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_account_channels_relationship(): void
|
||
|
|
{
|
||
|
|
$account = PlatformAccount::factory()->create();
|
||
|
|
$channels = PlatformChannel::factory()->count(2)->create(['platform_account_id' => $account->id]);
|
||
|
|
|
||
|
|
$this->assertCount(2, $account->channels);
|
||
|
|
|
||
|
|
foreach ($channels as $channel) {
|
||
|
|
$this->assertTrue($account->channels->contains($channel));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_platform_channel_routes_relationship(): void
|
||
|
|
{
|
||
|
|
$channel = PlatformChannel::factory()->create();
|
||
|
|
$routes = Route::factory()->count(2)->create(['platform_channel_id' => $channel->id]);
|
||
|
|
|
||
|
|
$this->assertCount(2, $channel->routes);
|
||
|
|
|
||
|
|
foreach ($routes as $route) {
|
||
|
|
$this->assertTrue($channel->routes->contains($route));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_language_platform_instances_relationship(): void
|
||
|
|
{
|
||
|
|
$language = Language::factory()->create();
|
||
|
|
$instances = PlatformInstance::factory()->count(2)->create();
|
||
|
|
|
||
|
|
// Attach language to instances via pivot table
|
||
|
|
foreach ($instances as $instance) {
|
||
|
|
$language->platformInstances()->attach($instance->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->assertCount(2, $language->platformInstances);
|
||
|
|
|
||
|
|
foreach ($instances as $instance) {
|
||
|
|
$this->assertTrue($language->platformInstances->contains($instance));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_model_soft_deletes_work_correctly(): void
|
||
|
|
{
|
||
|
|
// Test models that might use soft deletes
|
||
|
|
$feed = Feed::factory()->create();
|
||
|
|
$feedId = $feed->id;
|
||
|
|
|
||
|
|
$feed->delete();
|
||
|
|
|
||
|
|
// Should not find with normal query if soft deleted
|
||
|
|
$this->assertNull(Feed::find($feedId));
|
||
|
|
|
||
|
|
// Should find with withTrashed if model uses soft deletes
|
||
|
|
if (method_exists($feed, 'withTrashed')) {
|
||
|
|
$this->assertNotNull(Feed::withTrashed()->find($feedId));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_database_constraints_are_enforced(): void
|
||
|
|
{
|
||
|
|
// Test foreign key constraints
|
||
|
|
$this->expectException(\Illuminate\Database\QueryException::class);
|
||
|
|
|
||
|
|
// Try to create article with non-existent feed_id
|
||
|
|
Article::factory()->create(['feed_id' => 99999]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_all_factories_work_correctly(): void
|
||
|
|
{
|
||
|
|
// Test that all model factories can create valid records
|
||
|
|
$models = [
|
||
|
|
User::factory()->make(),
|
||
|
|
Language::factory()->make(),
|
||
|
|
PlatformInstance::factory()->make(),
|
||
|
|
PlatformAccount::factory()->make(),
|
||
|
|
PlatformChannel::factory()->make(),
|
||
|
|
Feed::factory()->make(),
|
||
|
|
Article::factory()->make(),
|
||
|
|
Route::factory()->make(),
|
||
|
|
Keyword::factory()->make(),
|
||
|
|
];
|
||
|
|
|
||
|
|
foreach ($models as $model) {
|
||
|
|
$this->assertNotNull($model);
|
||
|
|
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Model::class, $model);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|