From 1cf0f9858a2301ef7736e6060184e97e1629dfa9 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 12 Feb 2025 16:02:04 +0100 Subject: [PATCH] Add tests for loading translations in NodaWikidataFetcher --- tests/NodaWikidataFetcherTest.php | 210 ++++++++++++++++++++++++++++-- 1 file changed, 200 insertions(+), 10 deletions(-) diff --git a/tests/NodaWikidataFetcherTest.php b/tests/NodaWikidataFetcherTest.php index 853b652..f23d5bb 100644 --- a/tests/NodaWikidataFetcherTest.php +++ b/tests/NodaWikidataFetcherTest.php @@ -6,19 +6,27 @@ */ declare(strict_types = 1); use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Medium; +use PHPUnit\Framework\Attributes\DataProvider; + +require_once __DIR__ . '/../../MDMysqli/test_connections.conf.php'; /** * This script contains tests for the Wikidata fetcher. - * - * @covers \NodaWikidataFetcher */ +#[medium] +#[CoversClass(\NodaWikidataFetcher::class)] final class NodaWikidataFetcherTest extends TestCase { + + // Test for getting translations: Telugu + private const TEST_LANG = 'te'; + /** * Test to check whether the HTML page is correctly generated. * * @author Joshua Ramon Enslin * @group ValidOutput - * @small * * @return void */ @@ -34,7 +42,6 @@ final class NodaWikidataFetcherTest extends TestCase { * * @author Joshua Ramon Enslin * @group ValidOutput - * @small * * @return void */ @@ -47,9 +54,7 @@ final class NodaWikidataFetcherTest extends TestCase { /** * Test to check whether the HTML page is correctly generated. * - * @author Joshua Ramon Enslin * @group ValidOutput - * @small * * @return void */ @@ -62,9 +67,7 @@ final class NodaWikidataFetcherTest extends TestCase { /** * Test for cleaning wikidata info. * - * @author Joshua Ramon Enslin * @group ValidOutput - * @small * * @return void */ @@ -265,9 +268,7 @@ Transclusion expansion time report (%,ms,calls,template) /** * Test for cleaning wikidata info. * - * @author Joshua Ramon Enslin * @group ValidOutput - * @small * * @return void */ @@ -281,4 +282,193 @@ Transclusion expansion time report (%,ms,calls,template) ); } + + /** + * Data provider for an actor that has a wikidata link and a Telugu translation. + * + * @return array + */ + public static function actorWithTlAndWikidataLinkProvider():array { + + $mysqli = md_main_mysqli_connect(); + + $result = $mysqli->do_read_query("SELECT `persinst_id`, `noda_nrinsource` + FROM `" . DATABASENAME_NODA . "`.`noda` + WHERE `noda_source` = 'Wikidata' + AND EXISTS (SELECT 1 FROM `" . DATABASENAME_NODA . "`.`persinst_translation` + WHERE `persinst_translation`.`persinst_id` = `noda`.`persinst_id` + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "')"); + + if (!$cur = $result->fetch_row()) { + throw new Exception("Failed to identify an entry that has a wikidata entry and a translation for language " . self::TEST_LANG); + } + $result->close(); + $mysqli->close(); + + return [ + 'Actor with wikidata and translation' => [$cur[0], $cur[1]], + ]; + + } + + /** + * Data provider for a place that has a wikidata link and a Telugu translation. + * + * @return array + */ + public static function placeWithTlAndWikidataLinkProvider():array { + + $mysqli = md_main_mysqli_connect(); + + $result = $mysqli->do_read_query("SELECT `ort_id`, `noda_nrinsource` + FROM `" . DATABASENAME_NODA . "`.`noda_orte` + WHERE `noda_source` = 'Wikidata' + AND EXISTS (SELECT 1 FROM `" . DATABASENAME_NODA . "`.`ort_translation` + WHERE `ort_translation`.`ort_id` = `noda_orte`.`ort_id` + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "')"); + + if (!$cur = $result->fetch_row()) { + throw new Exception("Failed to identify an entry that has a wikidata entry and a translation for language " . self::TEST_LANG); + } + $result->close(); + $mysqli->close(); + + return [ + 'Place with wikidata and translation' => [$cur[0], $cur[1]], + ]; + + } + + /** + * Data provider for an tag that has a wikidata link and a Telugu translation. + * + * @return array + */ + public static function tagWithTlAndWikidataLinkProvider():array { + + $mysqli = md_main_mysqli_connect(); + + $result = $mysqli->do_read_query("SELECT `tag_id`, `noda_nrinsource` + FROM `" . DATABASENAME_NODA . "`.`noda_tag` + WHERE `noda_source` = 'Wikidata' + AND EXISTS (SELECT 1 FROM `" . DATABASENAME_NODA . "`.`tag_translation` + WHERE `tag_translation`.`tag_id` = `noda_tag`.`tag_id` + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "')"); + + if (!$cur = $result->fetch_row()) { + throw new Exception("Failed to identify an entry that has a wikidata entry and a translation for language " . self::TEST_LANG); + } + $result->close(); + $mysqli->close(); + + return [ + 'Tag with wikidata and translation' => [$cur[0], $cur[1]], + ]; + + } + + /** + * Test for fetching and recording translations for an actor. + * + * @param integer $actor_id Actor ID. + * @param string $wikidata_id Wikidata ID. + * + * @return void + */ + #[DataProvider('actorWithTlAndWikidataLinkProvider')] + public function testFetchingTranslationForPersinst(int $actor_id, string $wikidata_id):void { + + $mysqli = md_main_mysqli_connect(); + + $mysqli->do_update_query("DELETE FROM `" . DATABASENAME_NODA . "`.`persinst_translation` + WHERE `persinst_id` = " . $actor_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'"); + + self::assertEquals(0, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`persinst_translation` + WHERE `persinst_id` = " . $actor_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $data = NodaWikidataFetcher::getWikidataEntity($wikidata_id); + $fetcher = new NodaWikidataFetcher($mysqli); + $fetcher->getWikidataTranslationsForPersinst($data, $actor_id, [self::TEST_LANG]); + + self::assertEquals(1, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`persinst_translation` + WHERE `persinst_id` = " . $actor_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $mysqli->close(); + + } + + /** + * Test for fetching and recording translations for an place. + * + * @param integer $place_id Place ID. + * @param string $wikidata_id Wikidata ID. + * + * @return void + */ + #[DataProvider('placeWithTlAndWikidataLinkProvider')] + public function testFetchingTranslationForPlace(int $place_id, string $wikidata_id):void { + + $mysqli = md_main_mysqli_connect(); + + $mysqli->do_update_query("DELETE FROM `" . DATABASENAME_NODA . "`.`ort_translation` + WHERE `ort_id` = " . $place_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'"); + + self::assertEquals(0, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`ort_translation` + WHERE `ort_id` = " . $place_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $data = NodaWikidataFetcher::getWikidataEntity($wikidata_id); + $fetcher = new NodaWikidataFetcher($mysqli); + $fetcher->getWikidataTranslationsForPlace($data, $place_id, [self::TEST_LANG]); + + self::assertEquals(1, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`ort_translation` + WHERE `ort_id` = " . $place_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $mysqli->close(); + + } + + /** + * Test for fetching and recording translations for an tag. + * + * @param integer $tag_id Tag ID. + * @param string $wikidata_id Wikidata ID. + * + * @return void + */ + #[DataProvider('tagWithTlAndWikidataLinkProvider')] + public function testFetchingTranslationForTag(int $tag_id, string $wikidata_id):void { + + $mysqli = md_main_mysqli_connect(); + + $mysqli->do_update_query("DELETE FROM `" . DATABASENAME_NODA . "`.`tag_translation` + WHERE `tag_id` = " . $tag_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'"); + + self::assertEquals(0, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`tag_translation` + WHERE `tag_id` = " . $tag_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $data = NodaWikidataFetcher::getWikidataEntity($wikidata_id); + $fetcher = new NodaWikidataFetcher($mysqli); + $fetcher->getWikidataTranslationsForTag($data, $tag_id, [self::TEST_LANG]); + + self::assertEquals(1, MDMysqliTesting::queryNumRows($mysqli, " + FROM `" . DATABASENAME_NODA . "`.`tag_translation` + WHERE `tag_id` = " . $tag_id . " + AND `trans_language` = '" . $mysqli->escape_string(self::TEST_LANG) . "'")); + + $mysqli->close(); + + } }