From 096a76a78288db93b7f61e3bcc539bf75722c9fe Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Sat, 17 Dec 2022 16:28:15 +0100 Subject: [PATCH] Add tests for Wikipedia class --- composer.json | 2 +- package.json | 2 +- .../tracking/AddTrackingAction.php | 3 +- .../tracking/UpdateTrackingsAction.php | 2 +- .../deathnotifier/wikipedia/Wikipedia.php | 2 +- .../deathnotifier/wikipedia/WikipediaTest.php | 154 ++++++++++++++++++ 6 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 src/test/php/com/fwdekker/deathnotifier/wikipedia/WikipediaTest.php diff --git a/composer.json b/composer.json index 8d4fb26..a5ce7f2 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/package.json b/package.json index f11284f..2de37a7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main/php/com/fwdekker/deathnotifier/tracking/AddTrackingAction.php b/src/main/php/com/fwdekker/deathnotifier/tracking/AddTrackingAction.php index 8dd7cda..b3146fa 100644 --- a/src/main/php/com/fwdekker/deathnotifier/tracking/AddTrackingAction.php +++ b/src/main/php/com/fwdekker/deathnotifier/tracking/AddTrackingAction.php @@ -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) { diff --git a/src/main/php/com/fwdekker/deathnotifier/tracking/UpdateTrackingsAction.php b/src/main/php/com/fwdekker/deathnotifier/tracking/UpdateTrackingsAction.php index 9af91a5..ee2393f 100644 --- a/src/main/php/com/fwdekker/deathnotifier/tracking/UpdateTrackingsAction.php +++ b/src/main/php/com/fwdekker/deathnotifier/tracking/UpdateTrackingsAction.php @@ -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?"); diff --git a/src/main/php/com/fwdekker/deathnotifier/wikipedia/Wikipedia.php b/src/main/php/com/fwdekker/deathnotifier/wikipedia/Wikipedia.php index 3b324d7..0370603 100644 --- a/src/main/php/com/fwdekker/deathnotifier/wikipedia/Wikipedia.php +++ b/src/main/php/com/fwdekker/deathnotifier/wikipedia/Wikipedia.php @@ -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)], diff --git a/src/test/php/com/fwdekker/deathnotifier/wikipedia/WikipediaTest.php b/src/test/php/com/fwdekker/deathnotifier/wikipedia/WikipediaTest.php new file mode 100644 index 0000000..4fd1823 --- /dev/null +++ b/src/test/php/com/fwdekker/deathnotifier/wikipedia/WikipediaTest.php @@ -0,0 +1,154 @@ +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"]); + } +}