EventConfig: Merge event configuration from database to global config

main
Igor Scheller 6 years ago committed by msquare
parent 63d1292bf8
commit 7f61dc95be

@ -1,5 +1,8 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Models\EventConfig;
/** /**
* @return string * @return string
*/ */
@ -20,22 +23,17 @@ function event_config_edit_controller()
} }
$request = request(); $request = request();
$event_name = null; $config = config();
$event_welcome_msg = null; $event_name = $config->get('name');
$buildup_start_date = null; $event_welcome_msg = $config->get('welcome_msg');
$event_start_date = null; /** @var Carbon $buildup_start_date */
$event_end_date = null; $buildup_start_date = $config->get('buildup_start');
$teardown_end_date = null; /** @var Carbon $event_start_date */
$event_start_date = $config->get('event_start');
$event_config = EventConfig(); /** @var Carbon $event_end_date */
if (!empty($event_config)) { $event_end_date = $config->get('event_end');
$event_name = $event_config['event_name']; /** @var Carbon $teardown_end_date */
$buildup_start_date = $event_config['buildup_start_date']; $teardown_end_date = $config->get('teardown_end');
$event_start_date = $event_config['event_start_date'];
$event_end_date = $event_config['event_end_date'];
$teardown_end_date = $event_config['teardown_end_date'];
$event_welcome_msg = $event_config['event_welcome_msg'];
}
if ($request->has('submit')) { if ($request->has('submit')) {
$valid = true; $valid = true;
@ -91,24 +89,34 @@ function event_config_edit_controller()
} }
if ($valid) { if ($valid) {
EventConfig_update( $eventConfig = new EventConfig();
$event_name,
$buildup_start_date, foreach (
$event_start_date, [
$event_end_date, 'name' => $event_name,
$teardown_end_date, 'welcome_msg' => $event_welcome_msg,
$event_welcome_msg 'buildup_start' => $buildup_start_date,
); 'event_start' => $event_start_date,
'event_end' => $event_end_date,
'teardown_end' => $teardown_end_date,
] as $key => $value
) {
$eventConfig
->findOrNew($key)
->setAttribute('name', $key)
->setAttribute('value', $value)
->save();
}
engelsystem_log( engelsystem_log(
sprintf( sprintf(
'Changed event config: %s, %s, %s, %s, %s, %s', 'Changed event config: %s, %s, %s, %s, %s, %s',
$event_name, $event_name,
$event_welcome_msg, $event_welcome_msg,
date('Y-m-d', $buildup_start_date), $buildup_start_date ? $buildup_start_date->format('Y-m-d') : '',
date('Y-m-d', $event_start_date), $event_start_date ? $event_start_date->format('Y-m-d') : '',
date('Y-m-d', $event_end_date), $event_end_date ? $event_end_date->format('Y-m-d') : '',
date('Y-m-d', $teardown_end_date) $teardown_end_date ? $teardown_end_date->format('Y-m-d') : ''
) )
); );
success(__('Settings saved.')); success(__('Settings saved.'));

@ -12,7 +12,6 @@ $includeFiles = [
__DIR__ . '/../includes/sys_template.php', __DIR__ . '/../includes/sys_template.php',
__DIR__ . '/../includes/model/AngelType_model.php', __DIR__ . '/../includes/model/AngelType_model.php',
__DIR__ . '/../includes/model/EventConfig_model.php',
__DIR__ . '/../includes/model/Message_model.php', __DIR__ . '/../includes/model/Message_model.php',
__DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php',
__DIR__ . '/../includes/model/Room_model.php', __DIR__ . '/../includes/model/Room_model.php',

@ -1,78 +0,0 @@
<?php
use Engelsystem\Database\DB;
/**
* Get event config.
*
* @return array|null
*/
function EventConfig()
{
$config = DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1');
return empty($config) ? null : $config;
}
/**
* Update event config.
*
* @param string $event_name
* @param int $buildup_start_date
* @param int $event_start_date
* @param int $event_end_date
* @param int $teardown_end_date
* @param string $event_welcome_msg
* @return bool
*/
function EventConfig_update(
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg
) {
$eventConfig = EventConfig();
if (empty($eventConfig)) {
return DB::insert('
INSERT INTO `EventConfig` (
`event_name`,
`buildup_start_date`,
`event_start_date`,
`event_end_date`,
`teardown_end_date`,
`event_welcome_msg`
)
VALUES (?, ?, ?, ?, ?, ?)
',
[
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg
]
);
}
return (bool)DB::update('
UPDATE `EventConfig` SET
`event_name` = ?,
`buildup_start_date` = ?,
`event_start_date` = ?,
`event_end_date` = ?,
`teardown_end_date` = ?,
`event_welcome_msg` = ?
',
[
$event_name,
$buildup_start_date,
$event_start_date,
$event_end_date,
$teardown_end_date,
$event_welcome_msg,
]
);
}

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
/** /**
@ -128,10 +129,13 @@ function UserWorkLog_create($userWorkLog)
function UserWorkLog_new($user) function UserWorkLog_new($user)
{ {
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time())); $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
$event_config = EventConfig();
if (!empty($event_config['buildup_start_date'])) { /** @var Carbon $buildup */
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', $event_config['buildup_start_date'])); $buildup = $buildup = config('buildup_start');
if (!empty($buildup)) {
$work_date = $buildup->format('Y-m-d H:i');
} }
return [ return [
'user_id' => $user['UID'], 'user_id' => $user['UID'],
'work_timestamp' => $work_date, 'work_timestamp' => $work_date,

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\ValidationResult; use Engelsystem\ValidationResult;
@ -359,19 +360,23 @@ function User_validate_planned_arrival_date($planned_arrival_date)
// null is not okay // null is not okay
return new ValidationResult(false, time()); return new ValidationResult(false, time());
} }
$event_config = EventConfig();
if (empty($event_config)) { $config = config();
// Nothing to validate against $buildup = $config->get('buildup_start');
return new ValidationResult(true, $planned_arrival_date); $teardown = $config->get('teardown_end');
}
if (isset($event_config['buildup_start_date']) && $planned_arrival_date < $event_config['buildup_start_date']) { /** @var Carbon $buildup */
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_arrival_date))) {
// Planned arrival can not be before buildup start date // Planned arrival can not be before buildup start date
return new ValidationResult(false, $event_config['buildup_start_date']); return new ValidationResult(false, $buildup->getTimestamp());
} }
if (isset($event_config['teardown_end_date']) && $planned_arrival_date > $event_config['teardown_end_date']) {
/** @var Carbon $teardown */
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_arrival_date))) {
// Planned arrival can not be after teardown end date // Planned arrival can not be after teardown end date
return new ValidationResult(false, $event_config['teardown_end_date']); return new ValidationResult(false, $teardown->getTimestamp());
} }
return new ValidationResult(true, $planned_arrival_date); return new ValidationResult(true, $planned_arrival_date);
} }
@ -388,23 +393,28 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de
// null is okay // null is okay
return new ValidationResult(true, null); return new ValidationResult(true, null);
} }
if ($planned_arrival_date > $planned_departure_date) { if ($planned_arrival_date > $planned_departure_date) {
// departure cannot be before arrival // departure cannot be before arrival
return new ValidationResult(false, $planned_arrival_date); return new ValidationResult(false, $planned_arrival_date);
} }
$event_config = EventConfig();
if (empty($event_config)) { $config = config();
// Nothing to validate against $buildup = $config->get('buildup_start');
return new ValidationResult(true, $planned_departure_date); $teardown = $config->get('teardown_end');
}
if (isset($event_config['buildup_start_date']) && $planned_departure_date < $event_config['buildup_start_date']) { /** @var Carbon $buildup */
if (!empty($buildup) && $buildup->greaterThan(Carbon::createFromTimestamp($planned_departure_date))) {
// Planned arrival can not be before buildup start date // Planned arrival can not be before buildup start date
return new ValidationResult(false, $event_config['buildup_start_date']); return new ValidationResult(false, $buildup->getTimestamp());
} }
if (isset($event_config['teardown_end_date']) && $planned_departure_date > $event_config['teardown_end_date']) {
/** @var Carbon $teardown */
if (!empty($teardown) && $teardown->lessThan(Carbon::createFromTimestamp($planned_departure_date))) {
// Planned arrival can not be after teardown end date // Planned arrival can not be after teardown end date
return new ValidationResult(false, $event_config['teardown_end_date']); return new ValidationResult(false, $teardown->getTimestamp());
} }
return new ValidationResult(true, $planned_departure_date); return new ValidationResult(true, $planned_departure_date);
} }

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
/** /**
@ -37,7 +38,7 @@ function guest_register()
$tshirt_sizes = config('tshirt_sizes'); $tshirt_sizes = config('tshirt_sizes');
$enable_tshirt_size = config('enable_tshirt_size'); $enable_tshirt_size = config('enable_tshirt_size');
$min_password_length = config('min_password_length'); $min_password_length = config('min_password_length');
$event_config = EventConfig(); $config = config();
$request = request(); $request = request();
$session = session(); $session = session();
@ -273,8 +274,8 @@ function guest_register()
} }
// If a welcome message is present, display registration success page. // If a welcome message is present, display registration success page.
if (!empty($event_config) && !empty($event_config['event_welcome_msg'])) { if ($message = $config->get('welcome_msg')) {
return User_registration_success_view($event_config['event_welcome_msg']); return User_registration_success_view($message);
} }
redirect(page_link_to('/')); redirect(page_link_to('/'));
@ -283,13 +284,14 @@ function guest_register()
$buildup_start_date = time(); $buildup_start_date = time();
$teardown_end_date = null; $teardown_end_date = null;
if (!empty($event_config)) { if ($buildup = $config->get('buildup_start')) {
if (isset($event_config['buildup_start_date'])) { /** @var Carbon $buildup */
$buildup_start_date = $event_config['buildup_start_date']; $buildup_start_date = $buildup->getTimestamp();
} }
if (isset($event_config['teardown_end_date'])) {
$teardown_end_date = $event_config['teardown_end_date']; if ($teardown = $config->get('teardown_end')) {
} /** @var Carbon $teardown */
$teardown_end_date = $teardown->getTimestamp();
} }
return page_with_title(register_title(), [ return page_with_title(register_title(), [
@ -452,12 +454,10 @@ function guest_login()
} }
} }
$event_config = EventConfig();
return page([ return page([
div('col-md-12', [ div('col-md-12', [
div('row', [ div('row', [
EventConfig_countdown_page($event_config) EventConfig_countdown_page()
]), ]),
div('row', [ div('row', [
div('col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4', [ div('col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4', [

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
/** /**
@ -206,6 +207,7 @@ function user_settings()
{ {
global $user; global $user;
$request = request(); $request = request();
$config = config();
$themes = config('available_themes'); $themes = config('available_themes');
$enable_tshirt_size = config('enable_tshirt_size'); $enable_tshirt_size = config('enable_tshirt_size');
@ -214,14 +216,15 @@ function user_settings()
$buildup_start_date = null; $buildup_start_date = null;
$teardown_end_date = null; $teardown_end_date = null;
$event_config = EventConfig();
if (!empty($event_config)) { if ($buildup = $config->get('buildup_start')) {
if (isset($event_config['buildup_start_date'])) { /** @var Carbon $buildup */
$buildup_start_date = $event_config['buildup_start_date']; $buildup_start_date = $buildup->getTimestamp();
} }
if (isset($event_config['teardown_end_date'])) {
$teardown_end_date = $event_config['teardown_end_date']; if ($teardown = $config->get('teardown_end')) {
} /** @var Carbon $teardown */
$teardown_end_date = $teardown->getTimestamp();
} }
$user_source = $user; $user_source = $user;

@ -1,5 +1,6 @@
<?php <?php
// Methods to build a html form. // Methods to build a html form.
use Carbon\Carbon;
/** /**
* Renders a hidden input * Renders a hidden input
@ -63,6 +64,7 @@ function form_spinner($name, $label, $value)
function form_date($name, $label, $value, $start_date = '', $end_date = '') function form_date($name, $label, $value, $start_date = '', $end_date = '')
{ {
$dom_id = $name . '-date'; $dom_id = $name . '-date';
$value = ($value instanceof Carbon) ? $value->getTimestamp() : $value;
$value = is_numeric($value) ? date('Y-m-d', $value) : ''; $value = is_numeric($value) ? date('Y-m-d', $value) : '';
$start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : ''; $start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : '';
$end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : ''; $end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : '';

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\ValidationResult; use Engelsystem\ValidationResult;
/** /**
@ -134,9 +135,16 @@ function check_request_date($name, $error_message = null, $null_allowed = false)
*/ */
function check_date($input, $error_message = null, $null_allowed = false) function check_date($input, $error_message = null, $null_allowed = false)
{ {
if ($tmp = parse_date('Y-m-d H:i', trim($input) . ' 00:00')) { try {
return new ValidationResult(true, $tmp); $time = Carbon::createFromFormat('Y-m-d', trim($input));
} catch (InvalidArgumentException $e) {
$time = null;
} }
if ($time) {
return new ValidationResult(true, $time);
}
if ($null_allowed) { if ($null_allowed) {
return new ValidationResult(true, null); return new ValidationResult(true, null);
} }

@ -1,59 +1,62 @@
<?php <?php
use Carbon\Carbon;
/** /**
* Shows basic event infos and countdowns. * Shows basic event infos and countdowns.
* *
* @param array $event_config The event configuration
* @return string * @return string
*/ */
function EventConfig_countdown_page($event_config) function EventConfig_countdown_page()
{ {
if (empty($event_config)) { $config = config();
return div('col-md-12 text-center', [ $name = $config->get('name', '');
heading(sprintf(__('Welcome to the %s!'), '<span class="icon-icon_angel"></span> ENGELSYSTEM'), 2) /** @var Carbon $buildup */
]); $buildup = $config->get('buildup_start');
} /** @var Carbon $start */
$start = $config->get('event_start');
/** @var Carbon $end */
$end = $config->get('event_end');
/** @var Carbon $teardown */
$teardown = $config->get('teardown_end');
$elements = []; $elements = [];
if (!is_null($event_config['event_name'])) { $elements[] = div('col-sm-12 text-center', [
$elements[] = div('col-sm-12 text-center', [ heading(sprintf(
heading(sprintf( __('Welcome to the %s!'),
__('Welcome to the %s!'), $name . ' <span class="icon-icon_angel"></span> ENGELSYSTEM'
$event_config['event_name'] . ' <span class="icon-icon_angel"></span> ENGELSYSTEM' ), 2)
), 2) ]);
]);
}
if (!is_null($event_config['buildup_start_date']) && time() < $event_config['buildup_start_date']) { if (!empty($buildup) && $buildup->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [ $elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Buildup starts'), 4), heading(__('Buildup starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['buildup_start_date'] . '">%c</span>', '<span class="moment-countdown text-big" data-timestamp="' . $buildup->getTimestamp() . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['buildup_start_date']) . '</small>' '<small>' . $buildup->format(__('Y-m-d')) . '</small>'
]); ]);
} }
if (!is_null($event_config['event_start_date']) && time() < $event_config['event_start_date']) { if (!empty($start) && $start->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [ $elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event starts'), 4), heading(__('Event starts'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['event_start_date'] . '">%c</span>', '<span class="moment-countdown text-big" data-timestamp="' . $start->getTimestamp() . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['event_start_date']) . '</small>' '<small>' . $start->format(__('Y-m-d')) . '</small>'
]); ]);
} }
if (!is_null($event_config['event_end_date']) && time() < $event_config['event_end_date']) { if (!empty($end) && $end->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [ $elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Event ends'), 4), heading(__('Event ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['event_end_date'] . '">%c</span>', '<span class="moment-countdown text-big" data-timestamp="' . $end->getTimestamp() . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['event_end_date']) . '</small>' '<small>' . $end->format(__('Y-m-d')) . '</small>'
]); ]);
} }
if (!is_null($event_config['teardown_end_date']) && time() < $event_config['teardown_end_date']) { if (!empty($teardown) && $teardown->greaterThan(new Carbon())) {
$elements[] = div('col-sm-3 text-center hidden-xs', [ $elements[] = div('col-sm-3 text-center hidden-xs', [
heading(__('Teardown ends'), 4), heading(__('Teardown ends'), 4),
'<span class="moment-countdown text-big" data-timestamp="' . $event_config['teardown_end_date'] . '">%c</span>', '<span class="moment-countdown text-big" data-timestamp="' . $teardown->getTimestamp() . '">%c</span>',
'<small>' . date(__('Y-m-d'), $event_config['teardown_end_date']) . '</small>' '<small>' . $teardown->format(__('Y-m-d')) . '</small>'
]); ]);
} }

@ -1,5 +1,7 @@
<?php <?php
use Carbon\Carbon;
/** /**
* Renders user settings page * Renders user settings page
* *
@ -887,10 +889,9 @@ function render_user_arrived_hint()
global $user; global $user;
if ($user['Gekommen'] == 0) { if ($user['Gekommen'] == 0) {
$event_config = EventConfig(); /** @var Carbon $buildup */
if (!empty($event_config) $buildup = config('buildup_start');
&& !is_null($event_config['buildup_start_date']) if (!empty($buildup) && $buildup->lessThan(new Carbon())) {
&& time() > $event_config['buildup_start_date']) {
return __('You are not marked as arrived. Please go to heaven\'s desk, get your angel badge and/or tell them that you arrived already.'); return __('You are not marked as arrived. Please go to heaven\'s desk, get your angel badge and/or tell them that you arrived already.');
} }
} }

@ -2,25 +2,25 @@
<hr/> <hr/>
<div class="text-center footer" style="margin-bottom: 10px;"> <div class="text-center footer" style="margin-bottom: 10px;">
{% block eventinfo %} {% block eventinfo %}
{% if event_config.event_name %} {% if config('name') %}
{% if event_config.event_start_date and event_config.event_end_date %} {% if config('event_start') and config('event_end') %}
{{ __('%s, from %s to %s', [ {{ __('%s, from %s to %s', [
event_config.event_name, config('name'),
date(event_config.event_start_date).format(__('Y-m-d')), config('event_start').format(__('Y-m-d')),
date(event_config.event_end_date).format(__('Y-m-d')) config('event_end').format(__('Y-m-d'))
]) }} ]) }}
{% elseif event_config.event_start_date %} {% elseif config('event_start') %}
{{ __('%s, starting %s', [ {{ __('%s, starting %s', [
event_config.event_name, config('name'),
date(event_config.event_start_date).format(__('Y-m-d')) config('event_start').format(__('Y-m-d'))
]) }} ]) }}
{% else %} {% else %}
{{ event_config.event_name }} {{ config('name') }}
{% endif %} <br> {% endif %} <br>
{% elseif event_config.event_start_date and event_config.event_end_date %} {% elseif config('event_start') and config('event_end') %}
{{ __('Event from %s to %s', [ {{ __('Event from %s to %s', [
date(event_config.event_start_date).format(__('Y-m-d')), config('event_start').format(__('Y-m-d')),
date(event_config.event_end_date).format(__('Y-m-d')) config('event_end').format(__('Y-m-d'))
]) }} <br> ]) }} <br>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

@ -2,14 +2,31 @@
namespace Engelsystem\Config; namespace Engelsystem\Config;
use Engelsystem\Application;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Engelsystem\Models\EventConfig;
use Exception; use Exception;
use Illuminate\Database\QueryException;
class ConfigServiceProvider extends ServiceProvider class ConfigServiceProvider extends ServiceProvider
{ {
/** @var array */ /** @var array */
protected $configFiles = ['config.default.php', 'config.php']; protected $configFiles = ['config.default.php', 'config.php'];
/** @var EventConfig */
protected $eventConfig;
/**
* @param Application $app
* @param EventConfig $eventConfig
*/
public function __construct(Application $app, EventConfig $eventConfig = null)
{
parent::__construct($app);
$this->eventConfig = $eventConfig;
}
public function register() public function register()
{ {
$config = $this->app->make(Config::class); $config = $this->app->make(Config::class);
@ -17,7 +34,7 @@ class ConfigServiceProvider extends ServiceProvider
$this->app->instance('config', $config); $this->app->instance('config', $config);
foreach ($this->configFiles as $file) { foreach ($this->configFiles as $file) {
$file = config_path($file); $file = $this->getConfigPath($file);
if (!file_exists($file)) { if (!file_exists($file)) {
continue; continue;
@ -33,4 +50,44 @@ class ConfigServiceProvider extends ServiceProvider
throw new Exception('Configuration not found'); throw new Exception('Configuration not found');
} }
} }
public function boot()
{
if (!$this->eventConfig) {
return;
}
/** @var Config $config */
$config = $this->app->get('config');
/** @var EventConfig[] $values */
try {
$values = $this->eventConfig->newQuery()->get(['name', 'value']);
} catch (QueryException $e) {
return;
}
foreach ($values as $option) {
$data = $option->value;
if (is_array($data) && $config->has($option->name)) {
$data = array_replace_recursive(
$config->get($option->name),
$data
);
}
$config->set($option->name, $data);
}
}
/**
* Get the config path
*
* @param string $path
* @return string
*/
protected function getConfigPath($path = ''): string
{
return config_path($path);
}
} }

@ -2,7 +2,6 @@
namespace Engelsystem\Renderer\Twig\Extensions; namespace Engelsystem\Renderer\Twig\Extensions;
use Carbon\Carbon;
use Twig_Extension as TwigExtension; use Twig_Extension as TwigExtension;
use Twig_Extension_GlobalsInterface as GlobalsInterface; use Twig_Extension_GlobalsInterface as GlobalsInterface;
@ -17,45 +16,8 @@ class Globals extends TwigExtension implements GlobalsInterface
{ {
global $user; global $user;
$eventConfig = $this->getEventConfig();
if (empty($eventConfig)) {
$eventConfig = [];
}
return [ return [
'user' => isset($user) ? $user : [], 'user' => isset($user) ? $user : [],
'event_config' => $this->filterEventConfig($eventConfig),
]; ];
} }
/**
* @return array
* @codeCoverageIgnore
*/
protected function getEventConfig()
{
return EventConfig();
}
/**
* @param $eventConfig
* @return mixed
*/
protected function filterEventConfig($eventConfig)
{
array_walk($eventConfig, function (&$value, $key) {
if (is_null($value) || !in_array($key, [
'buildup_start_date',
'event_start_date',
'event_end_date',
'teardown_end_date',
])) {
return;
}
$value = Carbon::createFromTimestamp($value);
});
return $eventConfig;
}
} }

@ -5,18 +5,22 @@ namespace Engelsystem\Test\Unit\Config;
use Engelsystem\Application; use Engelsystem\Application;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Config\ConfigServiceProvider; use Engelsystem\Config\ConfigServiceProvider;
use Engelsystem\Models\EventConfig;
use Engelsystem\Test\Unit\ServiceProviderTest; use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception; use Exception;
use PHPUnit_Framework_MockObject_MockObject; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\QueryException;
use PHPUnit\Framework\MockObject\MockObject;
class ConfigServiceProviderTest extends ServiceProviderTest class ConfigServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Config\ConfigServiceProvider::register() * @covers \Engelsystem\Config\ConfigServiceProvider::register
* @covers \Engelsystem\Config\ConfigServiceProvider::getConfigPath
*/ */
public function testRegister() public function testRegister()
{ {
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */ /** @var MockObject|Config $config */
$config = $this->getMockBuilder(Config::class) $config = $this->getMockBuilder(Config::class)
->getMock(); ->getMock();
@ -53,11 +57,11 @@ class ConfigServiceProviderTest extends ServiceProviderTest
} }
/** /**
* @covers \Engelsystem\Config\ConfigServiceProvider::register() * @covers \Engelsystem\Config\ConfigServiceProvider::register
*/ */
public function testRegisterException() public function testRegisterException()
{ {
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */ /** @var MockObject|Config $config */
$config = $this->getMockBuilder(Config::class) $config = $this->getMockBuilder(Config::class)
->getMock(); ->getMock();
@ -68,8 +72,8 @@ class ConfigServiceProviderTest extends ServiceProviderTest
$app->expects($this->exactly(2)) $app->expects($this->exactly(2))
->method('instance') ->method('instance')
->withConsecutive( ->withConsecutive(
[Config::class, $config], [Config::class, $config],
['config', $config] ['config', $config]
); );
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce()); $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce());
@ -81,4 +85,67 @@ class ConfigServiceProviderTest extends ServiceProviderTest
$serviceProvider = new ConfigServiceProvider($app); $serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();
} }
/**
* @covers \Engelsystem\Config\ConfigServiceProvider::__construct
* @covers \Engelsystem\Config\ConfigServiceProvider::boot
*/
public function testBoot()
{
$app = $this->getApp(['get']);
/** @var EventConfig|MockObject $eventConfig */
$eventConfig = $this->createMock(EventConfig::class);
/** @var EloquentBuilder|MockObject $eloquentBuilder */
$eloquentBuilder = $this->getMockBuilder(EloquentBuilder::class)
->disableOriginalConstructor()
->getMock();
$config = new Config(['foo' => 'bar', 'lorem' => ['ipsum' => 'dolor', 'bla' => 'foo']]);
$configs = [
new EventConfig(['name' => 'test', 'value' => 'testing']),
new EventConfig(['name' => 'lorem', 'value' => ['ipsum' => 'tester']]),
];
$returnValue = $eloquentBuilder;
$eventConfig
->expects($this->exactly(3))
->method('newQuery')
->willReturnCallback(function () use (&$returnValue) {
if ($returnValue instanceof EloquentBuilder) {
$return = $returnValue;
$returnValue = null;
return $return;
}
if (is_null($returnValue)) {
throw new QueryException('', [], new Exception());
}
return null;
});
$this->setExpects($eloquentBuilder, 'get', [['name', 'value']], $configs);
$this->setExpects($app, 'get', ['config'], $config, $this->exactly(3));
$serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->boot();
$serviceProvider = new ConfigServiceProvider($app, $eventConfig);
$serviceProvider->boot();
$serviceProvider->boot();
$serviceProvider->boot();
$this->assertArraySubset(
[
'foo' => 'bar',
'lorem' => [
'ipsum' => 'tester',
'bla' => 'foo',
],
'test' => 'testing',
],
$config->get(null)
);
}
} }

@ -2,54 +2,25 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use Carbon\Carbon;
use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Globals;
use PHPUnit\Framework\MockObject\MockObject;
class GlobalsTest extends ExtensionTest class GlobalsTest extends ExtensionTest
{ {
/** /**
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::filterEventConfig
*/ */
public function testGetGlobals() public function testGetGlobals()
{ {
global $user; global $user;
$user = []; $user = [];
/** @var Globals|MockObject $extension */ $extension = new Globals();
$extension = $this->getMockBuilder(Globals::class)
->setMethods(['getEventConfig'])
->getMock();
$extension->expects($this->exactly(2))
->method('getEventConfig')
->willReturnOnConsecutiveCalls(
null,
[
'lorem' => 'ipsum',
'event_end_date' => 1234567890,
]
);
$globals = $extension->getGlobals(); $globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals); $this->assertGlobalsExists('user', [], $globals);
$this->assertGlobalsExists('event_config', [], $globals);
$user['foo'] = 'bar'; $user['foo'] = 'bar';
$globals = $extension->getGlobals(); $globals = $extension->getGlobals();
$this->assertGlobalsExists('user', ['foo' => 'bar'], $globals); $this->assertGlobalsExists('user', ['foo' => 'bar'], $globals);
$this->assertGlobalsExists('event_config', ['lorem' => 'ipsum'], $globals);
$config = $globals['event_config'];
$this->assertArrayHasKey('event_end_date', $config);
/** @var Carbon $eventEndDate */
$eventEndDate = $config['event_end_date'];
$this->assertInstanceOf(Carbon::class, $eventEndDate);
$eventEndDate->setTimezone('UTC');
$this->assertEquals('2009-02-13 23:31:30', $eventEndDate->format('Y-m-d H:i:s'));
} }
} }

Loading…
Cancel
Save