diff --git a/includes/class-bread-bmlt.php b/includes/class-bread-bmlt.php index cb73b53..32ee30b 100644 --- a/includes/class-bread-bmlt.php +++ b/includes/class-bread-bmlt.php @@ -42,9 +42,17 @@ public static function get_root_server_request(string $url) return Bread_Bmlt::get($url, $cookies); } - public static function get_configured_root_server_request($url) + public static function get_configured_root_server_request($url, $raw = false) { - return Bread_Bmlt::get_root_server_request(Bread::getOption('root_server')."/".$url); + $results = Bread_Bmlt::get_root_server_request(Bread::getOption('root_server')."/".$url); + if ($raw) { + return $results; + } + return json_decode(wp_remote_retrieve_body($results), true); + } + public static function get_formats_by_language(string $lang) + { + return Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetFormats$lang"); } /** * Undocumented function @@ -72,8 +80,7 @@ private static function get(string $url, array $cookies = array()) : WP_Error | public static function get_all_meetings() { $lang = Bread_Bmlt::get_bmlt_server_lang(); - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults&data_field_key=weekday_tinyint,start_time,service_body_bigint,id_bigint,meeting_name,location_text,email_contact&sort_keys=meeting_name,service_body_bigint,weekday_tinyint,start_time"); - $result = json_decode(wp_remote_retrieve_body($results), true); + $result = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults&data_field_key=weekday_tinyint,start_time,service_body_bigint,id_bigint,meeting_name,location_text,email_contact&sort_keys=meeting_name,service_body_bigint,weekday_tinyint,start_time"); $unique_areas = Bread_Bmlt::get_areas(); $all_meetings = array(); @@ -94,8 +101,7 @@ public static function get_all_meetings() } public static function get_fieldkeys() { - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetFieldKeys"); - return json_decode(wp_remote_retrieve_body($results), true); + return Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetFieldKeys"); } private static $standard_keys = array( "id_bigint","worldid_mixed","service_body_bigint", @@ -129,8 +135,7 @@ public static function get_areas() if (!empty(Bread_Bmlt::$unique_areas)) { return Bread_Bmlt::$unique_areas; } - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetServiceBodies"); - $result = json_decode(wp_remote_retrieve_body($results), true); + $result = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetServiceBodies"); Bread_Bmlt::$unique_areas = array(); foreach ($result as $value) { @@ -156,9 +161,8 @@ public static function get_areas() public static function get_bmlt_server_lang() : string { if (Bread_Bmlt::$bmlt_server_lang == '') { - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetServerInfo"); - $result = json_decode(wp_remote_retrieve_body($results), true); - if ($result==null) { + $result = Bread_Bmlt::testRootServer(); + if (!($result && is_array($result) && is_array($result[0]))) { return 'en'; } Bread_Bmlt::$bmlt_server_lang = ($result==null) ? 'en' : $result["0"]["nativeLang"]; @@ -174,9 +178,9 @@ public static function get_bmlt_server_lang() : string public static function testRootServer(string $override_root_server = null) : array { if ($override_root_server == null) { - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetServerInfo"); + $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetServerInfo", true); } else { - $results = Bread_Bmlt::get_root_server_request($override_root_server."/client_interface/json/?switcher=GetServerInfo"); + $results = Bread_Bmlt::get_root_server_request($override_root_server."/client_interface/json/?switcher=GetServerInfo", true); } if ($results instanceof WP_Error) { Bread_Bmlt::$connection_error = $results->get_error_message(); @@ -200,7 +204,6 @@ public static function getFormatsForSelect(bool $all = false): array { if ($all) { $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetFormats"); - $results = json_decode(wp_remote_retrieve_body($results), true); Bread_Bmlt::sortBySubkey($results, 'key_string'); return $results; } @@ -217,7 +220,6 @@ public static function getFormatsForSelect(bool $all = false): array $queryUrl = "client_interface/json/?switcher=GetSearchResults$services&get_formats_only"; } $results = Bread_Bmlt::get_configured_root_server_request($queryUrl); - $results = json_decode(wp_remote_retrieve_body($results), true); $results = empty($service_body_id) ? $results : $results['formats']; Bread_Bmlt::sortBySubkey($results, 'key_string'); return $results; diff --git a/public/class-bread-content-generator.php b/public/class-bread-content-generator.php index 48dab51..23c17ef 100644 --- a/public/class-bread-content-generator.php +++ b/public/class-bread-content-generator.php @@ -583,8 +583,7 @@ function write_additional_meetinglist() if ($this->options['additional_list_format_key'] === 'additional_list') { $additional_list_query .= "&advanced_published=0"; } - $results = Bread_Bmlt::get_configured_root_server_request($additional_list_query); - $additional_meetinglist_result = json_decode(wp_remote_retrieve_body($results), true); + $additional_meetinglist_result = Bread_Bmlt::get_configured_root_server_request($additional_list_query); $this->adjust_timezone($additional_meetinglist_result, $this->target_timezone); } if ($additional_list_query || $this->options['weekday_language'] != $this->options['additional_list_language']) { diff --git a/public/class-bread-format-manager.php b/public/class-bread-format-manager.php index 33e8cc5..e685fc4 100644 --- a/public/class-bread-format-manager.php +++ b/public/class-bread-format-manager.php @@ -24,11 +24,12 @@ class Bread_FormatsManager */ private $hashedFormats = array(); /** - * The default languages. + * The default language. * - * @var array + * @var string */ - private $defaultLang; + private string $defaultLang; + private array|null $wheelchairFormat = array(); /** * The info regarding the formats used is available already during construction because it is returned by the initial root server query. * @@ -75,11 +76,18 @@ private function loadFormats(string $lang): void if (isset($this->allFormats[$lang])) { return; } - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetFormats$lang"); - $this->allFormats[$lang] = json_decode(wp_remote_retrieve_body($results), true); + $this->allFormats[$lang] = Bread_Bmlt::get_formats_by_language($lang); Bread_Bmlt::sortBySubkey($this->allFormats[$lang], 'key_string'); $this->hashedFormats[$lang] = $this->hashFormats($this->allFormats[$lang]); } + /** + * Undocumented function + * + * @param string $lang + * @param string $field + * @param string $id + * @return array + */ public function getFormatFromField(string $lang, string $field, string $id): array { if (!isset($this->allFormats[$lang])) { @@ -175,4 +183,11 @@ public function write_formats(string $lang, bool $isAll, int $lineHeight, int $f $data .= ""; return $data; } + public function getWheelchairFormat($lang) + { + if (is_array($this->wheelchairFormat) && empty($this->wheelchairFormat)) { + $this->wheelchairFormat = $this->getFormatFromField($lang, 'world_id', 'WCHR'); + } + return $this->wheelchairFormat; + } } diff --git a/public/class-bread-meeting-enhancer.php b/public/class-bread-meeting-enhancer.php index df50c5d..b3f1c14 100644 --- a/public/class-bread-meeting-enhancer.php +++ b/public/class-bread-meeting-enhancer.php @@ -77,11 +77,7 @@ public function enhance_meeting(&$meeting_value, $lang, $formatsManager) $meeting_value['area_i'] = substr($area_name, 0, 1); $meeting_value['wheelchair'] = ''; - // This is just to make unit testing easier. In real life, $formatsManager should always be there - if ($formatsManager==null) { - return $meeting_value; - } - $wheelchair_format = $formatsManager->getFormatFromField($this->options['weekday_language'], 'world_id', 'WCHR'); + $wheelchair_format = $formatsManager->getWheelchairFormat($this->options['weekday_language']); if (!is_null($wheelchair_format)) { $fmts = explode(',', $meeting_value['format_shared_id_list']); if (in_array($wheelchair_format['id'], $fmts)) { diff --git a/public/class-bread-public.php b/public/class-bread-public.php index 77628c7..6f50a6e 100644 --- a/public/class-bread-public.php +++ b/public/class-bread-public.php @@ -161,12 +161,10 @@ public function bmlt_meeting_list($atts = null, $content = null) $services = $this->options['custom_query']; } if ($this->options['used_format_1'] == '') { - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults$services&sort_keys=$sort_keys$get_used_formats$select_language"); + $result = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults$services&sort_keys=$sort_keys$get_used_formats$select_language"); } elseif ($this->options['used_format_1'] != '') { - $results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults$services&sort_keys=$sort_keys&get_used_formats&formats[]=" . $this->options['used_format_1'] . $select_language); + $result = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults$services&sort_keys=$sort_keys&get_used_formats&formats[]=" . $this->options['used_format_1'] . $select_language); } - - $result = json_decode(wp_remote_retrieve_body($results), true); if (!empty($this->options['extra_meetings'])) { $extras = ""; foreach ((array)$this->options['extra_meetings'] as $value) { @@ -175,8 +173,7 @@ public function bmlt_meeting_list($atts = null, $content = null) $extras .= "&meeting_ids[]=" . $value; } - $extra_results = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults&sort_keys=" . $sort_keys . "" . $extras . "" . $get_used_formats . $select_language); - $extra_result = json_decode(wp_remote_retrieve_body($extra_results), true); + $extra_result = Bread_Bmlt::get_configured_root_server_request("client_interface/json/?switcher=GetSearchResults&sort_keys=" . $sort_keys . "" . $extras . "" . $get_used_formats . $select_language); $formatsManager = null; if ($extra_result <> null) { $result_meetings = array_merge($result['meetings'], $extra_result['meetings']); diff --git a/tests/BreadMeetinglistStructureTest.php b/tests/BreadMeetinglistStructureTest.php index 5647ac5..c2bb516 100644 --- a/tests/BreadMeetinglistStructureTest.php +++ b/tests/BreadMeetinglistStructureTest.php @@ -15,11 +15,20 @@ private function getMeetings(string $service_body): array $json = file_get_contents('tests/serviceBodies/'.$service_body.".json"); return json_decode($json, true); } - private function enhanceMeetings(&$meetings, $options) + private function getFormats($formats) + { + $json = file_get_contents('tests/formats/'.$formats.".json"); + return json_decode($json, true); + } + private function getFormatMgr($usedFormat, $lang) + { + return new Bread_FormatsManager($this->getFormats($usedFormat), $lang); + } + private function enhanceMeetings(&$meetings, $options, $formatMgr) { $enhancer = new Bread_Meeting_Enhancer($options, array()); foreach ($meetings as &$meeting) { - $meeting = $enhancer->enhance_meeting($meeting, 'de', null); + $meeting = $enhancer->enhance_meeting($meeting, 'de', $formatMgr); } } public function calculateExpectedHeadingStyle($options): string @@ -42,46 +51,48 @@ public function calculateExpectedHeadingStyle($options): string } public function testBerlinByDayMain() { - $this->doTest('berlin-booklet', 'berlin', -1, + $this->doTest('berlin-booklet', 'berlin', 'berlin-formats-de', 'german-formats', -1, ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'], [[0],[0],[0],[0],[0],[0],[0]], 'de'); } public function testBerlinByDayAdditional() { - $this->doTest('berlin-booklet', 'berlin', 1, + $this->doTest('berlin-booklet', 'berlin', 'berlin-formats-de', 'german-formats', 1, ['','','','','','',''], [[0],[0],[0],[0],[0],[0],[0]], 'de'); } public function testBerlinByCityPlusDayMain() { - $this->doTest('berlin-by-city-plus-day', 'berlin', -1, + $this->doTest('berlin-by-city-plus-day', 'berlin', 'berlin-formats-de', 'german-formats', -1, ['Berlin','Dallgow-Döberitz','Eberswalde','Potsdam','Rathenow'], [[0],[0],[0],[0],[0]], 'de'); } public function testBerlinByCityPlusDayAdditional() { - $this->doTest('berlin-booklet', 'berlin', 1, + $this->doTest('berlin-booklet', 'berlin', 'berlin-formats-de', 'german-formats', 1, ['','','','','','',''], [[0],[0],[0],[0],[0],[0],[0]], 'de'); } public function testBerlinByDayThenCityPlusDayAdditionalMain() { - $this->doTest('berlin-by-day-then-city-plus-day', 'berlin', -1, + $this->doTest('berlin-by-day-then-city-plus-day', 'berlin', 'berlin-formats-de', 'german-formats', -1, ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'], [['Berlin',],['Berlin','Potsdam','Rathenow'],['Berlin','Dallgow-Döberitz','Eberswalde'],['Berlin','Potsdam'],['Berlin',],['Berlin','Potsdam'],['Berlin','Potsdam']], 'de'); } public function testBerlinByDayThenCityPlusDayAdditional() { - $this->doTest('berlin-by-day-then-city-plus-day', 'berlin', 1, + $this->doTest('berlin-by-day-then-city-plus-day', 'berlin', 'berlin-formats-de', 'german-formats', 1, ['','','','','','',''], [[0],[0],[0],[0],[0],[0],[0]], 'de'); } - public function doTest($config, $meetingJson, $include, $expectedHeading, $expectedSubHeading, $lang): void + public function doTest($config, $meetingJson, $usedFormats, $formatBase, $include, $expectedHeading, $expectedSubHeading, $lang): void { new Bread(); $options = $this->getConfiguration($config); $meetings = $this->getMeetings($meetingJson); - $this->enhanceMeetings($meetings, $options); + $formatMgr = $this->getFormatMgr($usedFormats, $lang); + Bread_Bmlt::setFormatBase($formatBase); + $this->enhanceMeetings($meetings, $options,$formatMgr); $bms = new Bread_Meetingslist_Structure($options, $meetings, $lang, $include); $knt = 0; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d8013f5..37b0c9f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,12 @@ 1) { + $text = $exploded[$knt-1]; + } + } + return $text; + } + public static function get_formats_by_language(string $lang) + { + $json = file_get_contents('tests/formats/'.Bread_Bmlt::$format_base.'-'.$lang.".json"); + return json_decode($json, true); + } + public static function sortBySubkey(array &$array, string $subkey, int $sortType = SORT_ASC): void + { + if (empty($array)) { + return; + } + foreach ($array as $subarray) { + $keys[] = $subarray[$subkey]; + } + array_multisort($keys, $sortType, $array); + } } \ No newline at end of file