From 19c6b69f3a75dfe9fac896a930e0ab78b01a561e Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Sat, 26 Sep 2026 19:45:51 +0200 Subject: [PATCH 1/2] Required vehicle fields are core plugin preferences, seeded from the former local file --- lib/GaletteAuto/Auto.php | 49 ++--- lib/GaletteAuto/AutoPreferences.php | 169 ++++++++++++++++++ lib/GaletteAuto/Controllers/Controller.php | 15 +- lib/GaletteAuto/PluginGaletteAuto.php | 13 +- tests/GaletteAuto/tests/units/Auto.php | 12 +- .../tests/units/AutoPreferences.php | 147 +++++++++++++++ 6 files changed, 358 insertions(+), 47 deletions(-) create mode 100644 lib/GaletteAuto/AutoPreferences.php create mode 100644 tests/GaletteAuto/tests/units/AutoPreferences.php diff --git a/lib/GaletteAuto/Auto.php b/lib/GaletteAuto/Auto.php index 5b781d0..d2f7364 100644 --- a/lib/GaletteAuto/Auto.php +++ b/lib/GaletteAuto/Auto.php @@ -64,21 +64,6 @@ class Auto private Plugins $plugins; private Db $zdb; - /** @var array */ - private array $required = [ - 'name' => 1, - 'model' => 1, - 'first_registration_date' => 1, - 'first_circulation_date' => 1, - 'color' => 1, - 'state' => 1, - 'registration' => 1, - 'body' => 1, - 'transmission' => 1, - 'finition' => 1, - 'fuel' => 1 - ]; - private ?int $id = null; private ?string $registration = null; private ?string $name = null; @@ -133,6 +118,8 @@ public function __construct(Plugins $plugins, Db $zdb, ?ArrayObject $args = null 'seats' => mb_strtolower(_T("Seats", "auto")), 'horsepower' => mb_strtolower(_T("Horsepower", "auto")), 'engine_size' => mb_strtolower(_T("Engine size", "auto")), + 'chassis_number' => mb_strtolower(_T("Chassis number", "auto")), + 'comment' => mb_strtolower(_T("Comment", "auto")), 'color' => mb_strtolower(_T("Color", "auto")), 'state' => mb_strtolower(_T("State", "auto")), 'finition' => mb_strtolower(_T("Finition", "auto")), @@ -320,20 +307,20 @@ public function getPropName(string $name): string /** * Check posted values validity * - * @param array $post All values to check, basically the $_POST array - * after sending the form - * @param VehicleAccess $access Access rules for current user + * @param array $post All values to check, basically the $_POST array + * after sending the form + * @param VehicleAccess $access Access rules for current user + * @param AutoPreferences $preferences Plugin preferences */ - public function check(array $post, VehicleAccess $access): bool + public function check(array $post, VehicleAccess $access, AutoPreferences $preferences): bool { $this->errors = []; //check for required fields, and correct values - $required = $this->getRequired(); foreach (self::POSTED_FIELDS as $prop) { $value = $post[$prop] ?? null; - if (($value == '' || $value == null) && in_array($prop, array_keys($required))) { + if (($value == '' || $value == null) && $preferences->isRequired($prop)) { $this->errors[] = str_replace( '%field', '' . $this->getPropName($prop) . '', @@ -414,7 +401,9 @@ public function check(array $post, VehicleAccess $access): bool break; //constants case 'fuel': - if (in_array((int)$value, array_keys($this->listFuels()), true)) { + if ($value === null || $value === '') { + $this->fuel = null; + } elseif (in_array((int)$value, array_keys($this->listFuels()), true)) { $this->fuel = (int)$value; } else { $this->errors[] = _T("- You must choose a fuel in the list", "auto"); @@ -480,22 +469,6 @@ public function getErrors(): array return $this->errors; } - /** - * Get required fields - * - * @return array - */ - public function getRequired(): array - { - $required = $this->required; - - if (file_exists(GALETTE_CONFIG_PATH . 'local_auto_required.inc.php')) { - $required = require GALETTE_CONFIG_PATH . 'local_auto_required.inc.php'; - } - - return $required; - } - /** * Handle car picture upload * diff --git a/lib/GaletteAuto/AutoPreferences.php b/lib/GaletteAuto/AutoPreferences.php new file mode 100644 index 0000000..d76ec74 --- /dev/null +++ b/lib/GaletteAuto/AutoPreferences.php @@ -0,0 +1,169 @@ + + */ +final class AutoPreferences +{ + public const string PREFIX = 'pref_auto_'; + /** Prefix of the yes/no preferences making a vehicle field required */ + public const string REQUIRED_PREFIX = self::PREFIX . 'required_'; + + /** + * Former local configuration file for required fields + * + * It only seeds the preferences when core creates them; it can be + * removed afterwards. + */ + public const string LEGACY_FILE = 'local_auto_required.inc.php'; + + /** + * Vehicle fields that are always required + * + * Database does not allow them to be empty. + * + * @var array + */ + public const array ALWAYS_REQUIRED = [ + 'model', + 'first_registration_date', + 'first_circulation_date', + 'color', + 'state', + 'body', + 'transmission', + 'finition', + ]; + + /** + * Vehicle fields that may be required, with their default + * + * @var array + */ + public const array OPTIONAL_FIELDS = [ + 'name' => true, + 'registration' => true, + 'fuel' => true, + 'mileage' => false, + 'seats' => false, + 'horsepower' => false, + 'engine_size' => false, + 'chassis_number' => false, + 'comment' => false, + ]; + + /** + * Constructor + * + * @param Preferences $preferences Core preferences + */ + public function __construct(private readonly Preferences $preferences) + { + } + + /** + * Get the preferences the plugin declares + * + * Defaults come from the former local configuration file when there is + * one, so its values are kept when core creates the preferences. + * + * @param string $config_path Configuration directory + * + * @return array> + */ + public static function getSchema(string $config_path = GALETTE_CONFIG_PATH): array + { + $legacy = self::getLegacyRequired($config_path); + + $schema = []; + foreach (self::OPTIONAL_FIELDS as $field => $default) { + $schema[self::REQUIRED_PREFIX . $field] = [ + 'type' => PreferencesSchema::TYPE_BOOL, + 'default' => $legacy !== null ? isset($legacy[$field]) : $default, + ]; + } + return $schema; + } + + /** + * Get required fields from the former local configuration file + * + * @param string $config_path Configuration directory + * + * @return ?array Null when there is no such file + */ + private static function getLegacyRequired(string $config_path): ?array + { + $file = $config_path . self::LEGACY_FILE; + if (!file_exists($file)) { + return null; + } + $required = require $file; + return is_array($required) ? $required : null; + } + + /** + * Is a vehicle field required? + * + * @param string $field Field name + */ + public function isRequired(string $field): bool + { + if (in_array($field, self::ALWAYS_REQUIRED, true)) { + return true; + } + if (!isset(self::OPTIONAL_FIELDS[$field])) { + return false; + } + //core hands a false boolean back as an empty string + return (bool)$this->preferences->getPluginValue(self::REQUIRED_PREFIX . $field); + } + + /** + * Get required vehicle fields + * + * @return array Field names as keys + */ + public function getRequired(): array + { + $required = []; + foreach (array_merge(self::ALWAYS_REQUIRED, array_keys(self::OPTIONAL_FIELDS)) as $field) { + if ($this->isRequired($field)) { + $required[$field] = true; + } + } + return $required; + } + + /** + * Get the yes/no preferences, named after their field, for templates + * + * @return array + */ + public function toArray(): array + { + $values = []; + foreach (array_keys(self::OPTIONAL_FIELDS) as $field) { + $values[$field] = $this->isRequired($field); + } + return $values; + } +} diff --git a/lib/GaletteAuto/Controllers/Controller.php b/lib/GaletteAuto/Controllers/Controller.php index b233e14..c0b0628 100644 --- a/lib/GaletteAuto/Controllers/Controller.php +++ b/lib/GaletteAuto/Controllers/Controller.php @@ -14,6 +14,7 @@ use Galette\Repository\Members; use GaletteAuto\AbstractObject; use GaletteAuto\Auto; +use GaletteAuto\AutoPreferences; use GaletteAuto\Body; use GaletteAuto\Brand; use GaletteAuto\Color; @@ -75,6 +76,14 @@ protected function accessDenied(Response $response, string $log): Response ); } + /** + * Get plugin preferences + */ + protected function getAutoPreferences(): AutoPreferences + { + return new AutoPreferences($this->preferences); + } + /** * Get vehicles repository */ @@ -366,7 +375,7 @@ public function showAddEditVehicle(Request $request, Response $response, string } if ($this->session->auto !== null) { - $auto->check($this->session->auto, $this->getAccess()); + $auto->check($this->session->auto, $this->getAccess(), $this->getAutoPreferences()); $this->session->auto = null; } @@ -397,7 +406,7 @@ public function showAddEditVehicle(Request $request, Response $response, string 'states' => $this->getProperties(State::class), 'fuels' => $auto->listFuels(), 'time' => time(), - 'required' => $auto->getRequired() + 'required' => $this->getAutoPreferences()->getRequired() ]; // members @@ -473,7 +482,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a } } - $res = $auto->check($post, $this->getAccess()); + $res = $auto->check($post, $this->getAccess(), $this->getAutoPreferences()); if ($res !== true) { $error_detected = $auto->getErrors(); } diff --git a/lib/GaletteAuto/PluginGaletteAuto.php b/lib/GaletteAuto/PluginGaletteAuto.php index 373d082..29bfbab 100644 --- a/lib/GaletteAuto/PluginGaletteAuto.php +++ b/lib/GaletteAuto/PluginGaletteAuto.php @@ -17,6 +17,7 @@ use Galette\Core\Plugins\InstallableInterface; use Galette\Core\Plugins\MemberActionProviderInterface; use Galette\Core\Plugins\MenuProviderInterface; +use Galette\Core\Plugins\PreferencesProviderInterface; use Galette\Core\Plugins\PublicPagesProviderInterface; use Galette\Entity\Adherent; use Galette\Core\GalettePlugin; @@ -27,7 +28,7 @@ * @author Johan Cwiklinski */ -class PluginGaletteAuto extends GalettePlugin implements MenuProviderInterface, DashboardProviderInterface, MemberActionProviderInterface, InstallableInterface, PublicPagesProviderInterface +class PluginGaletteAuto extends GalettePlugin implements MenuProviderInterface, DashboardProviderInterface, MemberActionProviderInterface, InstallableInterface, PublicPagesProviderInterface, PreferencesProviderInterface { #[Inject] private readonly Db $zdb; //@phpstan-ignore property.uninitializedReadonly,property.onlyRead (injected from DI) @@ -119,6 +120,16 @@ public function getMenus(): array return $menus; } + /** + * Get the preferences the plugin declares + * + * @return array> + */ + public function getPreferences(): array + { + return AutoPreferences::getSchema(); + } + /** * Get plugins public menus * diff --git a/tests/GaletteAuto/tests/units/Auto.php b/tests/GaletteAuto/tests/units/Auto.php index 256d3b9..fec1430 100644 --- a/tests/GaletteAuto/tests/units/Auto.php +++ b/tests/GaletteAuto/tests/units/Auto.php @@ -20,6 +20,7 @@ class Auto extends GaletteTestCase { protected int $seed = 20240212212207; + protected bool $load_plugins = true; protected \Galette\Core\Plugins $plugins; @@ -78,11 +79,12 @@ public function testCrud(): void $this->logSuperAdmin(); $access = new \GaletteAuto\VehicleAccess($this->zdb, $this->login, $this->preferences); + $prefs = new \GaletteAuto\AutoPreferences($this->preferences); $vehicles = new \GaletteAuto\Repository\Vehicles($this->plugins, $this->zdb, $this->login, $this->history); $auto = new \GaletteAuto\Auto($this->plugins, $this->zdb); $data = []; - $this->assertFalse($auto->check($data, $access)); + $this->assertFalse($auto->check($data, $access, $prefs)); $this->assertSame( [ '- Mandatory field registration empty.', @@ -109,7 +111,7 @@ public function testCrud(): void 'state' => $state_id, 'transmission' => $transmission_id, ]; - $this->assertFalse($auto->check($data, $access)); + $this->assertFalse($auto->check($data, $access, $prefs)); $this->assertSame( [ '- Mandatory field registration empty.', @@ -139,7 +141,7 @@ public function testCrud(): void 'transmission' => $transmission_id, 'owner_id' => $adh->id, ]; - $check = $auto->check($data, $access); + $check = $auto->check($data, $access, $prefs); $this->assertSame([], $auto->getErrors()); $this->assertTrue($check); @@ -199,7 +201,7 @@ public function testCrud(): void 'owner_id' => $adh2->id, 'change_owner' => true, ]; - $check = $auto->check($data, $access); + $check = $auto->check($data, $access, $prefs); $this->assertSame([], $auto->getErrors()); $this->assertTrue($check); @@ -235,7 +237,7 @@ public function testCrud(): void 'transmission' => $transmission_id, 'owner_id' => $adh->id, ]; - $check = $auto->check($data, $access); + $check = $auto->check($data, $access, $prefs); $this->assertSame([], $auto->getErrors()); $this->assertTrue($check); diff --git a/tests/GaletteAuto/tests/units/AutoPreferences.php b/tests/GaletteAuto/tests/units/AutoPreferences.php new file mode 100644 index 0000000..6b5f231 --- /dev/null +++ b/tests/GaletteAuto/tests/units/AutoPreferences.php @@ -0,0 +1,147 @@ + + */ +class AutoPreferences extends GaletteTestCase +{ + protected int $seed = 20260926190512; + protected bool $load_plugins = true; + + /** + * Restore default preferences + */ + public function tearDown(): void + { + foreach (\GaletteAuto\AutoPreferences::getSchema() as $name => $entry) { + $this->assertTrue($this->preferences->setValue($name, (int)$entry['default'], $this->login)); + } + $this->login->logout(); + parent::tearDown(); + } + + /** + * Preferences are declared to core, with former required fields as defaults + */ + public function testDefaults(): void + { + $this->assertSame( + [ + 'name' => true, + 'registration' => true, + 'fuel' => true, + 'mileage' => false, + 'seats' => false, + 'horsepower' => false, + 'engine_size' => false, + 'chassis_number' => false, + 'comment' => false, + ], + (new \GaletteAuto\AutoPreferences($this->preferences))->toArray() + ); + $this->assertSame( + ['type' => PreferencesSchema::TYPE_BOOL, 'default' => true, 'plugin' => 'auto'], + PreferencesSchema::get('pref_auto_required_fuel') + ); + + $required = (new \GaletteAuto\AutoPreferences($this->preferences))->getRequired(); + $this->assertSame( + [ + 'model', + 'first_registration_date', + 'first_circulation_date', + 'color', + 'state', + 'body', + 'transmission', + 'finition', + 'name', + 'registration', + 'fuel', + ], + array_keys($required) + ); + } + + /** + * Former local configuration file gives the defaults + */ + public function testLegacyFile(): void + { + $path = sys_get_temp_dir() . '/galette-auto-' . uniqid() . '/'; + mkdir($path); + $file = $path . \GaletteAuto\AutoPreferences::LEGACY_FILE; + file_put_contents( + $file, + " 1, 'model' => 1, 'mileage' => 1, 'comment' => 1];\n" + ); + + try { + $defaults = array_map( + fn(array $entry) => $entry['default'], + \GaletteAuto\AutoPreferences::getSchema($path) + ); + } finally { + unlink($file); + rmdir($path); + } + + $this->assertSame( + [ + 'pref_auto_required_name' => true, + 'pref_auto_required_registration' => false, + 'pref_auto_required_fuel' => false, + 'pref_auto_required_mileage' => true, + 'pref_auto_required_seats' => false, + 'pref_auto_required_horsepower' => false, + 'pref_auto_required_engine_size' => false, + 'pref_auto_required_chassis_number' => false, + 'pref_auto_required_comment' => true, + ], + $defaults + ); + } + + /** + * Stored preferences decide which fields are required + */ + public function testRequired(): void + { + $this->assertTrue($this->preferences->setValue('pref_auto_required_fuel', 0, $this->login)); + $this->assertTrue($this->preferences->setValue('pref_auto_required_comment', 1, $this->login)); + + $prefs = new \GaletteAuto\AutoPreferences($this->preferences); + $this->assertFalse($prefs->isRequired('fuel')); + $this->assertTrue($prefs->isRequired('comment')); + //always required, and not a field at all + $this->assertTrue($prefs->isRequired('model')); + $this->assertFalse($prefs->isRequired('owner_id')); + + //empty fields are checked accordingly + $this->logSuperAdmin(); + $auto = new \GaletteAuto\Auto($this->container->get(\Galette\Core\Plugins::class), $this->zdb); + $this->assertFalse( + $auto->check([], new \GaletteAuto\VehicleAccess($this->zdb, $this->login, $this->preferences), $prefs) + ); + $errors = $auto->getErrors(); + $this->assertContains('- Mandatory field comment empty.', $errors); + $this->assertNotContains('- Mandatory field fuel empty.', $errors); + $this->assertNotContains('- You must choose a fuel in the list', $errors); + $this->assertNull($auto->getFuel()); + } +} From 5b8176682054080b45e97f2be8eb1af0c096b070 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Sat, 26 Sep 2026 19:45:51 +0200 Subject: [PATCH 2/2] Add an administration page for required vehicle fields --- _define.php | 4 +- _routes.php | 11 ++ .../Controllers/PreferencesController.php | 88 ++++++++++++++ lib/GaletteAuto/PluginGaletteAuto.php | 7 ++ templates/default/preferences.html.twig | 42 +++++++ .../tests/units/PreferencesController.php | 112 ++++++++++++++++++ .../tests/units/PluginGaletteAuto.php | 1 + 7 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 lib/GaletteAuto/Controllers/PreferencesController.php create mode 100644 templates/default/preferences.html.twig create mode 100644 tests/GaletteAuto/Controllers/tests/units/PreferencesController.php diff --git a/_define.php b/_define.php index 18591b3..0efbf93 100644 --- a/_define.php +++ b/_define.php @@ -57,7 +57,9 @@ 'batch-propertieslist' => 'staff', 'removeProperty' => 'staff', 'removeProperties' => 'staff', - 'doRemoveProperty' => 'staff' + 'doRemoveProperty' => 'staff', + 'autoPreferences' => 'admin', + 'storeAutoPreferences' => 'admin' ], dbver: 1.00 ); diff --git a/_routes.php b/_routes.php index ffcc57a..9f191db 100644 --- a/_routes.php +++ b/_routes.php @@ -10,6 +10,7 @@ use Galette\Middleware\Authenticate; use GaletteAuto\Controllers\Controller; +use GaletteAuto\Controllers\PreferencesController; use GaletteAuto\Controllers\Crud\PropertiesController; use GaletteAuto\Controllers\Crud\ModelsController; @@ -219,3 +220,13 @@ '/{property:brand|color|state|finition|body|transmission}/remove[/{id:\d+}]', [PropertiesController::class, 'doRemoveProperty'] )->setName('doRemoveProperty')->add(Authenticate::class); + +$app->get( + '/preferences', + [PreferencesController::class, 'preferences'] +)->setName('autoPreferences')->add(Authenticate::class); + +$app->post( + '/preferences', + [PreferencesController::class, 'storePreferences'] +)->setName('storeAutoPreferences')->add(Authenticate::class); diff --git a/lib/GaletteAuto/Controllers/PreferencesController.php b/lib/GaletteAuto/Controllers/PreferencesController.php new file mode 100644 index 0000000..571fc33 --- /dev/null +++ b/lib/GaletteAuto/Controllers/PreferencesController.php @@ -0,0 +1,88 @@ + + */ +class PreferencesController extends AbstractPluginController +{ + /** + * @var array + */ + #[Inject("Plugin Galette Auto")] + protected array $module_info; + + /** + * Preferences page + */ + public function preferences(Request $request, Response $response): Response + { + $auto = new Auto($this->plugins, $this->zdb); + $fields = []; + foreach (array_keys(AutoPreferences::OPTIONAL_FIELDS) as $field) { + $fields[$field] = $auto->getPropName($field); + } + + $this->view->render( + $response, + $this->getTemplate('preferences'), + [ + 'page_title' => _T("Cars preferences", "auto"), + 'fields' => $fields, + 'required' => (new AutoPreferences($this->preferences))->toArray(), + ] + ); + return $response; + } + + /** + * Store preferences + * + * Every preference is a yes/no one: a missing one is an unchecked box, + * and is set off. + */ + public function storePreferences(Request $request, Response $response): Response + { + $post = $request->getParsedBody(); + $errors = []; + + foreach (array_keys(AutoPreferences::getSchema()) as $name) { + if (!$this->preferences->setValue($name, (int)isset($post[$name]), $this->login)) { + $errors = array_merge($errors, $this->preferences->getErrors()); + } + } + + if (count($errors) === 0) { + $this->flash->addMessage( + 'success_detected', + _T("Preferences have been successfully stored!", "auto") + ); + } else { + foreach (array_unique($errors) as $error) { + $this->flash->addMessage('error_detected', $error); + } + } + + return $response + ->withStatus(302) + ->withHeader('Location', $this->routeparser->urlFor('autoPreferences')); + } +} diff --git a/lib/GaletteAuto/PluginGaletteAuto.php b/lib/GaletteAuto/PluginGaletteAuto.php index 29bfbab..9fb453a 100644 --- a/lib/GaletteAuto/PluginGaletteAuto.php +++ b/lib/GaletteAuto/PluginGaletteAuto.php @@ -104,6 +104,13 @@ public function getMenus(): array ]; } + if ($login->isAdmin()) { + $menus['plugin_auto']['items'][] = [ + 'label' => _T("Preferences", "auto"), + 'route' => ['name' => 'autoPreferences'] + ]; + } + // Super Admin is not a regular user if (!$login->isSuperAdmin()) { $menus['myaccount'] = [ diff --git a/templates/default/preferences.html.twig b/templates/default/preferences.html.twig new file mode 100644 index 0000000..00ea0fe --- /dev/null +++ b/templates/default/preferences.html.twig @@ -0,0 +1,42 @@ +{# + # This file is part of Galette Auto plugin (https://galette.eu). + # SPDX-FileCopyrightText: Copyright © 2009-2026 The Galette Team + # SPDX-License-Identifier: GPL-3.0-or-later + #} + +{% extends 'page.html.twig' %} + +{% block content %} +
+
+

{{ _T("Required fields", "auto") }}

+

{{ _T("Model, brand, dates, color, state, body, transmission and finition are always required.", "auto") }}

+ {# labels in one column, toggles in the next: they line up whatever the label length #} + {% for field, label in fields %} +
+
+ +
+
+
+ + +
+
+
+ {% endfor %} +
+ +
+ +
+
+{% endblock %} diff --git a/tests/GaletteAuto/Controllers/tests/units/PreferencesController.php b/tests/GaletteAuto/Controllers/tests/units/PreferencesController.php new file mode 100644 index 0000000..883f0a6 --- /dev/null +++ b/tests/GaletteAuto/Controllers/tests/units/PreferencesController.php @@ -0,0 +1,112 @@ + + */ +class PreferencesController extends GaletteRoutingTestCase +{ + protected int $seed = 20260926191422; + protected bool $load_plugins = true; + + /** + * Restore default preferences + */ + public function tearDown(): void + { + foreach (AutoPreferences::getSchema() as $name => $entry) { + $this->assertTrue($this->preferences->setValue($name, (int)$entry['default'], $this->login)); + } + $this->login->logout(); + parent::tearDown(); + } + + /** + * Preferences page shows every toggle, checked or not + */ + public function testPage(): void + { + $this->logSuperAdmin(); + $test_response = $this->app->handle($this->createRequest('autoPreferences')); + $this->expectOK($test_response); + $body = (string)$test_response->getBody(); + + foreach (AutoPreferences::OPTIONAL_FIELDS as $field => $default) { + $this->assertMatchesRegularExpression( + '/name="pref_auto_required_' . $field . '"[^>]*value="1"\s*' . ($default ? 'checked' : '\/>') . '/', + $body, + $field + ); + } + $this->assertStringContainsString('', $body); + } + + /** + * Storing sets off the unchecked boxes + */ + public function testStore(): void + { + $this->logSuperAdmin(); + $request = $this->createRequest('storeAutoPreferences', [], 'POST') + ->withParsedBody([ + 'pref_auto_required_name' => '1', + 'pref_auto_required_mileage' => '1', + //not declared: ignored + 'pref_auto_required_model' => '0', + ]); + $test_response = $this->app->handle($request); + $this->assertSame( + ['Location' => [$this->routeparser->urlFor('autoPreferences')]], + $test_response->getHeaders() + ); + $this->expectFlashData(['success_detected' => ['Preferences have been successfully stored!']]); + + $this->preferences->load(); + $this->assertSame( + [ + 'name' => true, + 'registration' => false, + 'fuel' => false, + 'mileage' => true, + 'seats' => false, + 'horsepower' => false, + 'engine_size' => false, + 'chassis_number' => false, + 'comment' => false, + ], + (new AutoPreferences($this->preferences))->toArray() + ); + } + + /** + * Preferences are for administrators only + */ + public function testMemberAccess(): void + { + $this->getMemberOne(); + $mdata = $this->dataAdherentOne(); + $this->assertTrue($this->login->login($mdata['login_adh'], $mdata['mdp_adh'])); + $this->expectAuthMiddlewareRefused($this->app->handle($this->createRequest('autoPreferences'))); + $this->expectAuthMiddlewareRefused( + $this->app->handle( + $this->createRequest('storeAutoPreferences', [], 'POST') + ->withParsedBody(['pref_auto_required_name' => '1']) + ) + ); + $this->assertTrue((new AutoPreferences($this->preferences))->isRequired('fuel')); + } +} diff --git a/tests/GaletteAuto/tests/units/PluginGaletteAuto.php b/tests/GaletteAuto/tests/units/PluginGaletteAuto.php index a6ed1fe..f30c8f6 100644 --- a/tests/GaletteAuto/tests/units/PluginGaletteAuto.php +++ b/tests/GaletteAuto/tests/units/PluginGaletteAuto.php @@ -61,6 +61,7 @@ public function testGetMenus(): void 'brandsList', 'modelsList', 'vehiclesList', + 'autoPreferences', ], $this->getMenuRoutes() );