191 lines
9.9 KiB
PHP
191 lines
9.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Services;
|
||
|
|
|
||
|
|
use App\Services\Article\ValidationService;
|
||
|
|
use Tests\TestCase;
|
||
|
|
use ReflectionClass;
|
||
|
|
use ReflectionMethod;
|
||
|
|
|
||
|
|
class ValidationServiceKeywordTest extends TestCase
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Helper method to access private validateByKeywords method
|
||
|
|
*/
|
||
|
|
private function getValidateByKeywordsMethod(): ReflectionMethod
|
||
|
|
{
|
||
|
|
$reflection = new ReflectionClass(ValidationService::class);
|
||
|
|
$method = $reflection->getMethod('validateByKeywords');
|
||
|
|
$method->setAccessible(true);
|
||
|
|
return $method;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_validates_belgian_political_keywords(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, 'This article discusses N-VA party policies.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Bart De Wever made a statement today.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Frank Vandenbroucke announced new healthcare policies.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Alexander De Croo addressed the nation.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The Vooruit party proposed new legislation.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Open Vld supports the new budget.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'CD&V members voted on the proposal.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Vlaams Belang criticized the decision.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'PTB organized a protest yesterday.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'PVDA released a statement.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_validates_belgian_location_keywords(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, 'This event took place in Belgium.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The Belgian government announced new policies.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Flanders saw increased tourism this year.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The Flemish government supports this initiative.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Wallonia will receive additional funding.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Brussels hosted the international conference.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Antwerp Pride attracted thousands of participants.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Ghent University published the research.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Bruges tourism numbers increased.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Leuven students organized the protest.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Mechelen city council voted on the proposal.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Namur hosted the cultural event.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Liège airport saw increased traffic.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Charleroi industrial zone expanded.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_validates_government_keywords(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, 'Parliament voted on the new legislation.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The government announced budget cuts.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The minister addressed concerns about healthcare.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'New policy changes will take effect next month.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The law was passed with majority support.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'New legislation affects education funding.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_validates_news_topic_keywords(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, 'The economy showed signs of recovery.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Economic indicators improved this quarter.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Education reforms were announced today.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Healthcare workers received additional support.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Transport infrastructure will be upgraded.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Climate change policies were discussed.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Energy prices have increased significantly.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'European Union voted on trade agreements.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'EU sanctions were extended.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Migration policies need urgent review.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Security measures were enhanced.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Justice system reforms are underway.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Culture festivals received government funding.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Police reported 18 administrative detentions.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_case_insensitive_keyword_matching(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, 'This article mentions ANTWERP in capital letters.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'brussels is mentioned in lowercase.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'BeLgIuM is mentioned in mixed case.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The FLEMISH government announced policies.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'n-va party policies were discussed.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'EUROPEAN union directives apply.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_rejects_content_without_belgian_keywords(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertFalse($method->invoke(null, 'This article discusses random topics.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'International news from other countries.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'Technology updates and innovations.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'Sports results from around the world.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'Entertainment news and celebrity gossip.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'Weather forecast for next week.'));
|
||
|
|
$this->assertFalse($method->invoke(null, 'Stock market analysis and trends.'));
|
||
|
|
}
|
||
|
|
|
||
|
|
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.
|
||
|
|
';
|
||
|
|
|
||
|
|
$this->assertTrue($method->invoke(null, $longText));
|
||
|
|
|
||
|
|
$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.
|
||
|
|
';
|
||
|
|
|
||
|
|
$this->assertFalse($method->invoke(null, $longTextWithoutKeywords));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_empty_content_returns_false(): void
|
||
|
|
{
|
||
|
|
$method = $this->getValidateByKeywordsMethod();
|
||
|
|
|
||
|
|
$this->assertFalse($method->invoke(null, ''));
|
||
|
|
$this->assertFalse($method->invoke(null, ' '));
|
||
|
|
$this->assertFalse($method->invoke(null, "\n\n\t"));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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}.";
|
||
|
|
$result = $method->invoke(null, $testContent);
|
||
|
|
|
||
|
|
$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
|
||
|
|
$this->assertTrue($method->invoke(null, 'Anti-government protesters gathered.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The policeman directed traffic.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Educational reforms are needed.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'Economic growth accelerated.'));
|
||
|
|
$this->assertTrue($method->invoke(null, 'The European directive was implemented.'));
|
||
|
|
}
|
||
|
|
}
|