Skip to content

Commit

Permalink
getting tests to involve FormatMgr
Browse files Browse the repository at this point in the history
  • Loading branch information
otrok7 committed Nov 6, 2024
1 parent 8c88b19 commit 80a3d13
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 45 deletions.
32 changes: 17 additions & 15 deletions includes/class-bread-bmlt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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",
Expand Down Expand Up @@ -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) {
Expand All @@ -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"];
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions public/class-bread-content-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
25 changes: 20 additions & 5 deletions public/class-bread-format-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -175,4 +183,11 @@ public function write_formats(string $lang, bool $isAll, int $lineHeight, int $f
$data .= "</table>";
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;
}
}
6 changes: 1 addition & 5 deletions public/class-bread-meeting-enhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
9 changes: 3 additions & 6 deletions public/class-bread-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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']);
Expand Down
31 changes: 21 additions & 10 deletions tests/BreadMeetinglistStructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php
require_once __DIR__.'/mock/class-bread.php';
//require_once __DIR__.'/../includes/class-bread-bmlt.php';
require_once __DIR__.'/mock/class-bread-bmlt.php';
require_once __DIR__.'/../public/class-bread-meetingslist-structure.php';
require_once __DIR__.'/../public/class-bread-meeting-enhancer.php';
require_once __DIR__.'/../public/class-bread-format-manager.php';
function apply_filters( $hook_name, $value, $args ) {
return $value;
}
function plugin_dir_url( $file) {
return './';
}
Loading

0 comments on commit 80a3d13

Please sign in to comment.