diff --git a/lib/features/exercises/validators.dart b/lib/features/exercises/validators.dart index 46bb5f31a..f6f58ee5f 100644 --- a/lib/features/exercises/validators.dart +++ b/lib/features/exercises/validators.dart @@ -2,7 +2,10 @@ import 'package:material_ui/material_ui.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; /// The amount of characters an exercise description needs to have -const MIN_CHARS_DESCRIPTION = 40; +const MIN_CHARS_DESCRIPTION = 120; + +/// The amount of words an exercise description needs to have +const MIN_WORDS_DESCRIPTION = 15; /// The amount of characters an exercise name needs to have const MIN_CHARS_NAME = 5; @@ -39,5 +42,12 @@ String? validateExerciseDescription(String? name, BuildContext context) { return AppLocalizations.of(context).enterMinCharacters(MIN_CHARS_DESCRIPTION.toString()); } + // Words are tokens with at least one letter, so markdown list markers don't count + final letter = RegExp(r'\p{L}', unicode: true); + final words = name.split(RegExp(r'\s+')).where((token) => letter.hasMatch(token)).length; + if (words < MIN_WORDS_DESCRIPTION) { + return AppLocalizations.of(context).enterMinWords(MIN_WORDS_DESCRIPTION.toString()); + } + return null; } diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index 2453a5e92..fd86e3b3a 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -509,6 +509,7 @@ "incline_bench": "Schrägbank", "lats": "Latissimus", "enterMinCharacters": "Bitte gebe mindestens {min} Zeichen ein", + "enterMinWords": "Bitte gib mindestens {min} Wörter ein", "@enterMinCharacters": { "description": "Error message when the user hasn't entered the minimum amount characters in a form", "type": "text", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 284e9a4db..f6bcd04ad 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -1194,6 +1194,16 @@ } } }, + "enterMinWords": "Please enter at least {min} words", + "@enterMinWords": { + "description": "Error message when the user hasn't entered the minimum amount of words in a form", + "type": "text", + "placeholders": { + "min": { + "type": "String" + } + } + }, "enterMaxCharacters": "Please enter at most {max} characters", "@enterMaxCharacters": { "description": "Error message when the user has entered more than the allowed amount of characters in a form", diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index adae245c6..adba85617 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -466,6 +466,7 @@ }, "back": "Dos", "enterMinCharacters": "Veuillez entrer au moins {min} caractères", + "enterMinWords": "Veuillez saisir au moins {min} mots", "@enterMinCharacters": { "description": "Error message when the user hasn't entered the minimum amount characters in a form", "type": "text", diff --git a/test/features/exercises/validators_test.dart b/test/features/exercises/validators_test.dart new file mode 100644 index 000000000..04d023317 --- /dev/null +++ b/test/features/exercises/validators_test.dart @@ -0,0 +1,84 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:wger/features/exercises/validators.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/l10n/localizations_delegates.dart'; + +const _fifteenWords = + 'Stand upright and hold the dumbbells at your sides, then curl both weights up slowly.'; +const _longEnough = + '$_fifteenWords Lower them again under control and keep your elbows close to your body.'; + +void main() { + late BuildContext context; + late AppLocalizations i18n; + + Future pumpContext(WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: appLocalizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Builder( + builder: (ctx) { + context = ctx; + return const SizedBox(); + }, + ), + ), + ); + i18n = AppLocalizations.of(context); + } + + group('validateExerciseDescription', () { + testWidgets('accepts a description with enough characters and words', (tester) async { + await pumpContext(tester); + expect(validateExerciseDescription(_longEnough, context), isNull); + }); + + testWidgets('rejects a description under 120 characters', (tester) async { + await pumpContext(tester); + expect(_fifteenWords.length, lessThan(MIN_CHARS_DESCRIPTION)); + expect( + validateExerciseDescription(_fifteenWords, context), + i18n.enterMinCharacters('$MIN_CHARS_DESCRIPTION'), + ); + }); + + testWidgets('rejects a long description with too few words', (tester) async { + await pumpContext(tester); + final fewWords = 'Supercalifragilisticexpialidocious ' * 5; + expect( + validateExerciseDescription(fewWords, context), + i18n.enterMinWords('$MIN_WORDS_DESCRIPTION'), + ); + }); + + testWidgets("markdown list markers don't count as words", (tester) async { + await pumpContext(tester); + final padded = '${'- 1. * - ' * 14}Hold the bar and pull yourself up.'; + expect(padded.length, greaterThanOrEqualTo(MIN_CHARS_DESCRIPTION)); + expect( + validateExerciseDescription(padded, context), + i18n.enterMinWords('$MIN_WORDS_DESCRIPTION'), + ); + }); + }); +}