Files

30 lines
3.9 KiB
PHP

<?php
if(!defined('WP_CLI')||!WP_CLI)exit;
function hgcf_daypart_assert($ok,$label){if(!$ok)WP_CLI::error($label);WP_CLI::log('PASS '.$label);}
$config=hgcf_normalize_settings(['recipient'=>'recipient@example.com','site_name'=>'Example','show_consent'=>false]);
$flow=hgcf_workflow_defaults('request');$flow['time']='optional';$flow['time_mode']='daypart';$flow['service']='optional';$flow['message_required']=false;$flow['services']=['combo'=>['nl'=>'Combinatie','en'=>'Combination']];
$config['workflows']=['request'=>$flow];$override=function()use(&$config){return $config;};add_filter('pre_option_hgcf_settings',$override);
$source=['name'=>'Test Visitor','email'=>'visitor@example.com','mode'=>'request','massage'=>'combo','preferred_time'=>'afternoon'];
hgcf_daypart_assert(!is_wp_error(hgcf_validate($source)),'daypart and combination accepted without hidden consent');
foreach(['morning','afternoon','evening',''] as $time)hgcf_daypart_assert(!is_wp_error(hgcf_validate(array_replace($source,['preferred_time'=>$time]))),'optional time of day '.$time);
foreach(['11:30','night','<script>','AFTERNOON',['array']] as $time)hgcf_daypart_assert(is_wp_error(hgcf_validate(array_replace($source,['preferred_time'=>$time]))),'forged or malformed daypart rejected');
$config['workflows']['request']['time']='required';hgcf_daypart_assert(is_wp_error(hgcf_validate(array_replace($source,['preferred_time'=>'']))),'required daypart enforced');$config['workflows']['request']['time']='optional';
$config['workflows']['request']['opening_time']='12:00';$config['workflows']['request']['closing_time']='15:00';
hgcf_daypart_assert(array_keys(hgcf_dayparts($config['workflows']['request']))===['afternoon'],'only overlapping dayparts offered');
hgcf_daypart_assert(is_wp_error(hgcf_validate(array_replace($source,['preferred_time'=>'morning']))),'daypart outside opening range rejected');
$config['workflows']['request']=$flow;
$messages=[];$mock=function($result,$atts)use(&$messages){$messages[]=$atts;return true;};add_filter('pre_wp_mail',$mock,PHP_INT_MAX,2);
foreach(['nl'=>'Voorkeursdagdeel: Middag','en'=>'Preferred time of day: Afternoon'] as $lang=>$expected){hgcf_daypart_assert(hgcf_send(hgcf_validate($source,$lang),$lang),'mock send '.$lang);hgcf_daypart_assert(str_contains(end($messages)['message'],$expected),'mail contains readable daypart '.$lang);}
remove_filter('pre_wp_mail',$mock,PHP_INT_MAX);
$html=hgcf_render(['lang'=>'nl']);preg_match('/data-hgcf-workflows="([^"]+)"/',$html,$match);$frontend=json_decode(html_entity_decode($match[1],ENT_QUOTES),true);
hgcf_daypart_assert(array_column($frontend['request']['services'],'value')===['combo','unsure'],'unsure option is last');
hgcf_daypart_assert($frontend['request']['timeMode']==='daypart'&&$frontend['request']['timeLabel']==='Voorkeursdagdeel','localized daypart config');
hgcf_daypart_assert(str_contains($html,'id="mlhc-daypart"')&&!str_contains($html,'name="mlhc_consent"'),'daypart control and hidden consent render');
$config['workflows']['request']['time_mode']='exact';hgcf_daypart_assert(!is_wp_error(hgcf_validate(array_replace($source,['preferred_time'=>'11:30']))),'exact-time workflow remains compatible');
hgcf_daypart_assert(is_wp_error(hgcf_validate($source)),'exact-time workflow rejects a daypart');
$row=$flow;$row['enabled']='1';$row['message_required']='0';$row['services']=[['id'=>'combo','labels'=>['nl'=>'Combinatie','en'=>'Combination']]];
$clean=hgcf_sanitize_workflows([$row],$config['workflows'],['nl'=>'Nederlands','en'=>'English']);hgcf_daypart_assert(!is_wp_error($clean)&&$clean['request']['time_mode']==='daypart','daypart mode saves in admin');
$row['time_mode']=['bad'];hgcf_daypart_assert(is_wp_error(hgcf_sanitize_workflows([$row],$config['workflows'],['nl'=>'Nederlands','en'=>'English'])),'malformed time mode rejected');
remove_filter('pre_option_hgcf_settings',$override);
WP_CLI::success('Dayparts, exact-time compatibility, ordering and mail checks passed; no real mail sent.');