fedi-feed-router/backend/tests/Unit/Services/ValidationServiceKeywordTest.php

210 lines
12 KiB
PHP
Raw Normal View History

2025-08-09 21:32:46 +02:00
<?php
namespace Tests\Unit\Services;
2025-08-15 16:39:18 +02:00
use Domains\Article\Services\ValidationService;
2025-08-09 21:32:46 +02:00
use Tests\TestCase;
2025-08-15 02:50:42 +02:00
use Tests\Traits\CreatesArticleFetcher;
2025-08-09 21:32:46 +02:00
use ReflectionClass;
use ReflectionMethod;
2025-08-15 02:50:42 +02:00
use Mockery;
2025-08-09 21:32:46 +02:00
class ValidationServiceKeywordTest extends TestCase
{
2025-08-15 02:50:42 +02:00
use CreatesArticleFetcher;
private ValidationService $validationService;
protected function setUp(): void
{
parent::setUp();
$articleFetcher = $this->createArticleFetcher();
$this->validationService = new ValidationService($articleFetcher);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
2025-08-09 21:32:46 +02:00
/**
* Helper method to access private validateByKeywords method
*/
private function getValidateByKeywordsMethod(): ReflectionMethod
{
2025-08-15 02:50:42 +02:00
$reflection = new ReflectionClass($this->validationService);
2025-08-09 21:32:46 +02:00
$method = $reflection->getMethod('validateByKeywords');
$method->setAccessible(true);
return $method;
}
public function test_validates_belgian_political_keywords(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'This article discusses N-VA party policies.'));
$this->assertTrue($method->invoke($this->validationService, 'Bart De Wever made a statement today.'));
$this->assertTrue($method->invoke($this->validationService, 'Frank Vandenbroucke announced new healthcare policies.'));
$this->assertTrue($method->invoke($this->validationService, 'Alexander De Croo addressed the nation.'));
$this->assertTrue($method->invoke($this->validationService, 'The Vooruit party proposed new legislation.'));
$this->assertTrue($method->invoke($this->validationService, 'Open Vld supports the new budget.'));
$this->assertTrue($method->invoke($this->validationService, 'CD&V members voted on the proposal.'));
$this->assertTrue($method->invoke($this->validationService, 'Vlaams Belang criticized the decision.'));
$this->assertTrue($method->invoke($this->validationService, 'PTB organized a protest yesterday.'));
$this->assertTrue($method->invoke($this->validationService, 'PVDA released a statement.'));
2025-08-09 21:32:46 +02:00
}
public function test_validates_belgian_location_keywords(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'This event took place in Belgium.'));
$this->assertTrue($method->invoke($this->validationService, 'The Belgian government announced new policies.'));
$this->assertTrue($method->invoke($this->validationService, 'Flanders saw increased tourism this year.'));
$this->assertTrue($method->invoke($this->validationService, 'The Flemish government supports this initiative.'));
$this->assertTrue($method->invoke($this->validationService, 'Wallonia will receive additional funding.'));
$this->assertTrue($method->invoke($this->validationService, 'Brussels hosted the international conference.'));
$this->assertTrue($method->invoke($this->validationService, 'Antwerp Pride attracted thousands of participants.'));
$this->assertTrue($method->invoke($this->validationService, 'Ghent University published the research.'));
$this->assertTrue($method->invoke($this->validationService, 'Bruges tourism numbers increased.'));
$this->assertTrue($method->invoke($this->validationService, 'Leuven students organized the protest.'));
$this->assertTrue($method->invoke($this->validationService, 'Mechelen city council voted on the proposal.'));
$this->assertTrue($method->invoke($this->validationService, 'Namur hosted the cultural event.'));
$this->assertTrue($method->invoke($this->validationService, 'Liège airport saw increased traffic.'));
$this->assertTrue($method->invoke($this->validationService, 'Charleroi industrial zone expanded.'));
2025-08-09 21:32:46 +02:00
}
public function test_validates_government_keywords(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'Parliament voted on the new legislation.'));
$this->assertTrue($method->invoke($this->validationService, 'The government announced budget cuts.'));
$this->assertTrue($method->invoke($this->validationService, 'The minister addressed concerns about healthcare.'));
$this->assertTrue($method->invoke($this->validationService, 'New policy changes will take effect next month.'));
$this->assertTrue($method->invoke($this->validationService, 'The law was passed with majority support.'));
$this->assertTrue($method->invoke($this->validationService, 'New legislation affects education funding.'));
2025-08-09 21:32:46 +02:00
}
public function test_validates_news_topic_keywords(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'The economy showed signs of recovery.'));
$this->assertTrue($method->invoke($this->validationService, 'Economic indicators improved this quarter.'));
$this->assertTrue($method->invoke($this->validationService, 'Education reforms were announced today.'));
$this->assertTrue($method->invoke($this->validationService, 'Healthcare workers received additional support.'));
$this->assertTrue($method->invoke($this->validationService, 'Transport infrastructure will be upgraded.'));
$this->assertTrue($method->invoke($this->validationService, 'Climate change policies were discussed.'));
$this->assertTrue($method->invoke($this->validationService, 'Energy prices have increased significantly.'));
$this->assertTrue($method->invoke($this->validationService, 'European Union voted on trade agreements.'));
$this->assertTrue($method->invoke($this->validationService, 'EU sanctions were extended.'));
$this->assertTrue($method->invoke($this->validationService, 'Migration policies need urgent review.'));
$this->assertTrue($method->invoke($this->validationService, 'Security measures were enhanced.'));
$this->assertTrue($method->invoke($this->validationService, 'Justice system reforms are underway.'));
$this->assertTrue($method->invoke($this->validationService, 'Culture festivals received government funding.'));
$this->assertTrue($method->invoke($this->validationService, 'Police reported 18 administrative detentions.'));
2025-08-09 21:32:46 +02:00
}
public function test_case_insensitive_keyword_matching(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'This article mentions ANTWERP in capital letters.'));
$this->assertTrue($method->invoke($this->validationService, 'brussels is mentioned in lowercase.'));
$this->assertTrue($method->invoke($this->validationService, 'BeLgIuM is mentioned in mixed case.'));
$this->assertTrue($method->invoke($this->validationService, 'The FLEMISH government announced policies.'));
$this->assertTrue($method->invoke($this->validationService, 'n-va party policies were discussed.'));
$this->assertTrue($method->invoke($this->validationService, 'EUROPEAN union directives apply.'));
2025-08-09 21:32:46 +02:00
}
public function test_rejects_content_without_belgian_keywords(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertFalse($method->invoke($this->validationService, 'This article discusses random topics.'));
$this->assertFalse($method->invoke($this->validationService, 'International news from other countries.'));
$this->assertFalse($method->invoke($this->validationService, 'Technology updates and innovations.'));
$this->assertFalse($method->invoke($this->validationService, 'Sports results from around the world.'));
$this->assertFalse($method->invoke($this->validationService, 'Entertainment news and celebrity gossip.'));
$this->assertFalse($method->invoke($this->validationService, 'Weather forecast for next week.'));
$this->assertFalse($method->invoke($this->validationService, 'Stock market analysis and trends.'));
2025-08-09 21:32:46 +02:00
}
public function test_keyword_matching_in_longer_text(): void
{
$method = $this->getValidateByKeywordsMethod();
$longText = '
This is a comprehensive article about various topics.
It covers international relations, global economics, and regional policies.
However, it specifically mentions that Antwerp hosted a major conference
last week with participants from around the world. The event was
considered highly successful and will likely be repeated next year.
';
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, $longText));
2025-08-09 21:32:46 +02:00
$longTextWithoutKeywords = '
This is a comprehensive article about various topics.
It covers international relations, global finance, and commercial matters.
The conference was held in a major international city and attracted
participants from around the world. The event was considered highly
successful and will likely be repeated next year.
';
2025-08-15 02:50:42 +02:00
$this->assertFalse($method->invoke($this->validationService, $longTextWithoutKeywords));
2025-08-09 21:32:46 +02:00
}
public function test_empty_content_returns_false(): void
{
$method = $this->getValidateByKeywordsMethod();
2025-08-15 02:50:42 +02:00
$this->assertFalse($method->invoke($this->validationService, ''));
$this->assertFalse($method->invoke($this->validationService, ' '));
$this->assertFalse($method->invoke($this->validationService, "\n\n\t"));
2025-08-09 21:32:46 +02:00
}
/**
* Test comprehensive keyword coverage to ensure all expected keywords work
*/
public function test_all_keywords_are_functional(): void
{
$method = $this->getValidateByKeywordsMethod();
$expectedKeywords = [
// Political parties and leaders
'N-VA', 'Bart De Wever', 'Frank Vandenbroucke', 'Alexander De Croo',
'Vooruit', 'Open Vld', 'CD&V', 'Vlaams Belang', 'PTB', 'PVDA',
// Belgian locations and institutions
'Belgium', 'Belgian', 'Flanders', 'Flemish', 'Wallonia', 'Brussels',
'Antwerp', 'Ghent', 'Bruges', 'Leuven', 'Mechelen', 'Namur', 'Liège', 'Charleroi',
'parliament', 'government', 'minister', 'policy', 'law', 'legislation',
// Common Belgian news topics
'economy', 'economic', 'education', 'healthcare', 'transport', 'climate', 'energy',
'European', 'EU', 'migration', 'security', 'justice', 'culture', 'police'
];
foreach ($expectedKeywords as $keyword) {
$testContent = "This article contains the keyword: {$keyword}.";
2025-08-15 02:50:42 +02:00
$result = $method->invoke($this->validationService, $testContent);
2025-08-09 21:32:46 +02:00
$this->assertTrue($result, "Keyword '{$keyword}' should match but didn't");
}
}
public function test_partial_keyword_matches_work(): void
{
$method = $this->getValidateByKeywordsMethod();
// Keywords should match when they appear as part of larger words or phrases
2025-08-15 02:50:42 +02:00
$this->assertTrue($method->invoke($this->validationService, 'Anti-government protesters gathered.'));
$this->assertTrue($method->invoke($this->validationService, 'The policeman directed traffic.'));
$this->assertTrue($method->invoke($this->validationService, 'Educational reforms are needed.'));
$this->assertTrue($method->invoke($this->validationService, 'Economic growth accelerated.'));
$this->assertTrue($method->invoke($this->validationService, 'The European directive was implemented.'));
2025-08-09 21:32:46 +02:00
}
}