Http::response('Test content with Belgium news', 200) ]); $feed = Feed::factory()->create(); $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://example.com/article', 'is_valid' => null, 'validated_at' => null ]); $result = ValidationService::validate($article); $this->assertInstanceOf(Article::class, $result); $this->assertNotNull($result->validated_at); $this->assertIsBool($result->is_valid); } public function test_validate_marks_article_invalid_when_missing_data(): void { // Mock HTTP requests to return HTML without article content Http::fake([ 'https://invalid-url-without-parser.com/article' => Http::response('Empty', 200) ]); $feed = Feed::factory()->create(); $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://invalid-url-without-parser.com/article', 'is_valid' => null, 'validated_at' => null ]); $result = ValidationService::validate($article); $this->assertFalse($result->is_valid); $this->assertNotNull($result->validated_at); } public function test_validate_with_supported_article_content(): void { // Mock HTTP requests Http::fake([ 'https://example.com/article' => Http::response('Article content', 200) ]); $feed = Feed::factory()->create(); $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://example.com/article', 'is_valid' => null, 'validated_at' => null ]); $result = ValidationService::validate($article); // Since we can't fetch real content in tests, it should be marked invalid $this->assertFalse($result->is_valid); $this->assertNotNull($result->validated_at); } public function test_validate_updates_article_in_database(): void { // Mock HTTP requests Http::fake([ 'https://example.com/article' => Http::response('Article content', 200) ]); $feed = Feed::factory()->create(); $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://example.com/article', 'is_valid' => null, 'validated_at' => null ]); $originalId = $article->id; ValidationService::validate($article); // Check that the article was updated in the database $updatedArticle = Article::find($originalId); $this->assertNotNull($updatedArticle->validated_at); $this->assertNotNull($updatedArticle->is_valid); } public function test_validate_handles_article_with_existing_validation(): void { // Mock HTTP requests Http::fake([ 'https://example.com/article' => Http::response('Article content', 200) ]); $feed = Feed::factory()->create(); $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://example.com/article', 'is_valid' => true, 'validated_at' => now()->subHour() ]); $originalValidatedAt = $article->validated_at; $result = ValidationService::validate($article); // Should re-validate and update timestamp $this->assertNotEquals($originalValidatedAt, $result->validated_at); } public function test_validate_keyword_checking_logic(): void { // Mock HTTP requests with content that contains Belgian keywords Http::fake([ 'https://example.com/article-about-bart-de-wever' => Http::response( '
Article about Bart De Wever and Belgian politics
', 200 ) ]); $feed = Feed::factory()->create(); // Create an article that would match the validation keywords if content was available $article = Article::factory()->create([ 'feed_id' => $feed->id, 'url' => 'https://example.com/article-about-bart-de-wever', 'is_valid' => null, 'validated_at' => null ]); $result = ValidationService::validate($article); // The service looks for keywords in the full_article content // Since we can't fetch real content, it will be marked invalid $this->assertFalse($result->is_valid); } }