Add tests for Wikipedia class
This commit is contained in:
parent
217ffa88de
commit
096a76a782
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "fwdekker/death-notifier",
|
||||
"description": "Get notified when a famous person dies.",
|
||||
"version": "0.19.12", "_comment_version": "Also update version in `package.json`!",
|
||||
"version": "0.19.13", "_comment_version": "Also update version in `package.json`!",
|
||||
"type": "project",
|
||||
"license": "MIT",
|
||||
"homepage": "https://git.fwdekker.com/tools/death-notifier",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "death-notifier",
|
||||
"version": "0.19.12", "_comment_version": "Also update version in `composer.json`!",
|
||||
"version": "0.19.13", "_comment_version": "Also update version in `composer.json`!",
|
||||
"description": "Get notified when a famous person dies.",
|
||||
"author": "Florine W. Dekker",
|
||||
"browser": "dist/bundle.js",
|
||||
|
|
|
@ -72,6 +72,7 @@ class AddTrackingAction extends Action
|
|||
],
|
||||
]))->check($inputs);
|
||||
|
||||
// TODO: If other user has already added article, do not contact Wikipedia, just add it directly
|
||||
[$normalized_name, $status] = $this->get_and_validate_page_info(strval($inputs["person_name"]));
|
||||
$this->tracking_list->transaction(function () use ($normalized_name, $status) {
|
||||
if ($this->tracking_list->has_tracking($_SESSION["uuid"], $normalized_name))
|
||||
|
@ -94,7 +95,7 @@ class AddTrackingAction extends Action
|
|||
private function get_and_validate_page_info(string $person_name): array
|
||||
{
|
||||
try {
|
||||
$info = $this->wikipedia->query_person_info([$person_name], resolve_moves: false);
|
||||
$info = $this->wikipedia->query_people_info([$person_name], resolve_moves: false);
|
||||
|
||||
$normalized_name = $info->redirects->resolve($person_name);
|
||||
} catch (WikipediaException $exception) {
|
||||
|
|
|
@ -85,7 +85,7 @@ class UpdateTrackingsAction extends Action
|
|||
return;
|
||||
|
||||
try {
|
||||
$people_statuses = $this->wikipedia->query_person_info($names, resolve_moves: true);
|
||||
$people_statuses = $this->wikipedia->query_people_info($names, resolve_moves: true);
|
||||
} catch (WikipediaException $exception) {
|
||||
$this->logger->error("Failed to query page info.", ["cause" => $exception, "pages" => $names]);
|
||||
throw new UnexpectedException("Could not reach Wikipedia. Maybe the website is down?");
|
||||
|
|
|
@ -288,7 +288,7 @@ class Wikipedia
|
|||
* `PersonStatus`
|
||||
* @throws WikipediaException if the query fails
|
||||
*/
|
||||
public function query_person_info(array $names, bool $resolve_moves): QueryOutput
|
||||
public function query_people_info(array $names, bool $resolve_moves): QueryOutput
|
||||
{
|
||||
$output = $this->api_query_batched_resolve_moves(
|
||||
params: ["prop" => "categories", "cllimit" => strval(self::CATS_PER_QUERY)],
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
<?php /** @noinspection PhpUnhandledExceptionInspection */
|
||||
|
||||
namespace com\fwdekker\deathnotifier\wikipedia;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@see Wikipedia}.
|
||||
*
|
||||
* These tests are slow because they directly communicate with Wikipedia. No mocking going on here. This also means that
|
||||
* in several years, some of these tests will fail.
|
||||
*/
|
||||
class WikipediaTest extends TestCase
|
||||
{
|
||||
private Wikipedia $wikipedia;
|
||||
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->wikipedia = new Wikipedia();
|
||||
}
|
||||
|
||||
|
||||
public function test_query_returns_missing_for_nonsense_title(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["This article does not exist."], resolve_moves: false);
|
||||
|
||||
self::assertEmpty($output->results);
|
||||
self::assertEmpty($output->redirects->getIterator());
|
||||
self::assertContains("This article does not exist.", $output->missing);
|
||||
}
|
||||
|
||||
public function test_query_returns_redirected_for_normalized_title(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["donald Trump"], resolve_moves: false);
|
||||
|
||||
self::assertArrayHasKey("Donald Trump", $output->results);
|
||||
self::assertEquals("Donald Trump", $output->redirects->resolve("donald Trump"));
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
public function test_query_returns_redirected_for_alternative_title(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Donald J. Trump"], resolve_moves: false);
|
||||
|
||||
self::assertArrayHasKey("Donald Trump", $output->results);
|
||||
self::assertEquals("Donald Trump", $output->redirects->resolve("Donald J. Trump"));
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
public function test_query_returns_redirected_for_normalized_alternative_title(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["donald J. Trump"], resolve_moves: false);
|
||||
|
||||
self::assertArrayHasKey("Donald Trump", $output->results);
|
||||
self::assertEquals("Donald Trump", $output->redirects->resolve("donald J. Trump"));
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
public function test_query_returns_no_missing_or_redirects_for_correct_title(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Donald Trump"], resolve_moves: false);
|
||||
|
||||
self::assertArrayHasKey("Donald Trump", $output->results);
|
||||
self::assertEmpty($output->redirects->getIterator());
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
public function test_query_returns_missing_if_page_deleted_and_moved_but_resolve_is_disabled(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["MILK FART"], resolve_moves: false);
|
||||
|
||||
self::assertEmpty($output->results);
|
||||
self::assertEmpty($output->redirects->getIterator());
|
||||
self::assertContains("MILK FART", $output->missing);
|
||||
}
|
||||
|
||||
public function test_query_resolves_move_if_page_was_deleted(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["MILK FART"], resolve_moves: true);
|
||||
|
||||
self::assertArrayHasKey("Ben Shapiro", $output->results);
|
||||
self::assertEquals("Ben Shapiro", $output->redirects->resolve("MILK FART"));
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
public function test_query_resolves_move_and_redirect_if_page_was_deleted(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Asdfasdf"], resolve_moves: true);
|
||||
|
||||
self::assertArrayHasKey("Tom E. Beer", $output->results);
|
||||
self::assertEquals("Tom E. Beer", $output->redirects->resolve("Asdfasdf"));
|
||||
self::assertEmpty($output->missing);
|
||||
}
|
||||
|
||||
|
||||
public function test_query_detects_non_person_page(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Milk"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(ArticleType::Other, $output->results["Milk"]["type"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_disambiguation_page(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Joseph"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(ArticleType::Disambiguation, $output->results["Joseph"]["type"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_index_page(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["ASDF"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(ArticleType::Disambiguation, $output->results["ASDF"]["type"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_person_page(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Joe Biden"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(ArticleType::Person, $output->results["Joe Biden"]["type"]);
|
||||
}
|
||||
|
||||
|
||||
public function test_query_detects_alive_person(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["ContraPoints"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(PersonStatus::Alive, $output->results["ContraPoints"]["status"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_dead_person(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Adolf Hitler"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(PersonStatus::Dead, $output->results["Adolf Hitler"]["status"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_missing_person(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Sada Abe"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(PersonStatus::Dead, $output->results["Sada Abe"]["status"]);
|
||||
}
|
||||
|
||||
public function test_query_detects_possibly_living_person(): void
|
||||
{
|
||||
$output = $this->wikipedia->query_people_info(["Judge Edward Aaron"], resolve_moves: false);
|
||||
|
||||
self::assertEquals(PersonStatus::PossiblyAlive, $output->results["Judge Edward Aaron"]["status"]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue