diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/VersionCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/VersionCommand.java index 4169940b..eb68cf3c 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/VersionCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/VersionCommand.java @@ -12,8 +12,6 @@ import javax.inject.Inject; import java.util.Collection; import java.util.List; -import static fr.xephi.authme.settings.properties.PluginSettings.HELP_HEADER; - public class VersionCommand implements ExecutableCommand { @Inject @@ -25,8 +23,7 @@ public class VersionCommand implements ExecutableCommand { @Override public void executeCommand(CommandSender sender, List arguments) { // Show some version info - sender.sendMessage(ChatColor.GOLD + "==========[ " + commandService.getProperty(HELP_HEADER) - + " ABOUT ]=========="); + sender.sendMessage(ChatColor.GOLD + "==========[ " + AuthMe.getPluginName() + " ABOUT ]=========="); sender.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.WHITE + AuthMe.getPluginName() + " v" + AuthMe.getPluginVersion() + ChatColor.GRAY + " (build: " + AuthMe.getPluginBuildNumber() + ")"); sender.sendMessage(ChatColor.GOLD + "Developers:"); diff --git a/src/main/java/fr/xephi/authme/command/help/HelpMessage.java b/src/main/java/fr/xephi/authme/command/help/HelpMessage.java index 0c940436..82023beb 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpMessage.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpMessage.java @@ -6,6 +6,8 @@ package fr.xephi.authme.command.help; */ public enum HelpMessage { + HEADER("header"), + OPTIONAL("optional"), HAS_PERMISSION("hasPermission"), diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 8f681bf2..8fc94161 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -6,12 +6,10 @@ import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.FoundCommandResult; -import fr.xephi.authme.initialization.SettingsDependent; +import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.permission.DefaultPermission; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsManager; -import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.settings.properties.PluginSettings; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -27,7 +25,7 @@ import static java.util.Collections.singletonList; /** * Help syntax generator for AuthMe commands. */ -public class HelpProvider implements SettingsDependent { +public class HelpProvider implements Reloadable { // --- Bit flags --- /** Set to show a command overview. */ @@ -50,15 +48,13 @@ public class HelpProvider implements SettingsDependent { private final PermissionsManager permissionsManager; private final HelpMessagesService helpMessagesService; - private String helpHeader; /** int with bit flags set corresponding to the above constants for enabled sections. */ private Integer enabledSections; @Inject - HelpProvider(PermissionsManager permissionsManager, HelpMessagesService helpMessagesService, Settings settings) { + HelpProvider(PermissionsManager permissionsManager, HelpMessagesService helpMessagesService) { this.permissionsManager = permissionsManager; this.helpMessagesService = helpMessagesService; - reload(settings); } private List printHelp(CommandSender sender, FoundCommandResult result, int options) { @@ -72,7 +68,10 @@ public class HelpProvider implements SettingsDependent { // Return directly if no options are enabled so we don't include the help header return lines; } - lines.add(ChatColor.GOLD + "==========[ " + helpHeader + " HELP ]=========="); + String header = helpMessagesService.getMessage(HelpMessage.HEADER); + if (!header.isEmpty()) { + lines.add(ChatColor.GOLD + header); + } CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription()); List labels = ImmutableList.copyOf(result.getLabels()); @@ -121,8 +120,7 @@ public class HelpProvider implements SettingsDependent { } @Override - public void reload(Settings settings) { - helpHeader = settings.getProperty(PluginSettings.HELP_HEADER); + public void reload() { // We don't know about the reloading order of the classes, i.e. we cannot assume that HelpMessagesService // has already been reloaded. So set the enabledSections flag to null and redefine it first time needed. enabledSections = null; diff --git a/src/main/java/fr/xephi/authme/command/help/HelpSection.java b/src/main/java/fr/xephi/authme/command/help/HelpSection.java index f879a553..23f6b1ee 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpSection.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpSection.java @@ -7,9 +7,9 @@ public enum HelpSection { COMMAND("command"), - SHORT_DESCRIPTION("description.short"), + SHORT_DESCRIPTION("description"), - DETAILED_DESCRIPTION("description.detailed"), + DETAILED_DESCRIPTION("detailedDescription"), ARGUMENTS("arguments"), diff --git a/src/main/java/fr/xephi/authme/message/MessageFileHandler.java b/src/main/java/fr/xephi/authme/message/MessageFileHandler.java index e2f14ff5..e870bdf3 100644 --- a/src/main/java/fr/xephi/authme/message/MessageFileHandler.java +++ b/src/main/java/fr/xephi/authme/message/MessageFileHandler.java @@ -26,7 +26,7 @@ public class MessageFileHandler { * @param file the file to use for messages * @param defaultFile the default file from the JAR to use if no message is found */ - MessageFileHandler(File file, String defaultFile) { + public MessageFileHandler(File file, String defaultFile) { this.filename = file.getName(); this.configuration = YamlConfiguration.loadConfiguration(file); this.defaultFile = defaultFile; diff --git a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java index b47ce8e1..3ef6259b 100644 --- a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java @@ -49,6 +49,7 @@ public class SettingsMigrationService extends PlainMigrationService { | migrateJoinLeaveMessages(resource) | migrateForceSpawnSettings(resource) | changeBooleanSettingToLogLevelProperty(resource) + | hasOldHelpHeaderProperty(resource) || hasDeprecatedProperties(resource); } @@ -154,6 +155,15 @@ public class SettingsMigrationService extends PlainMigrationService { return false; } + private static boolean hasOldHelpHeaderProperty(PropertyResource resource) { + if (resource.contains("settings.helpHeader")) { + ConsoleLogger.warning("Help header setting is now in messages/help_xx.yml, " + + "please check the file to set it again"); + return true; + } + return false; + } + /** * Checks for an old property path and moves it to a new path if present. * diff --git a/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java b/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java index 167e1655..bfb56533 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java @@ -9,10 +9,6 @@ import static com.github.authme.configme.properties.PropertyInitializer.newPrope public class PluginSettings implements SettingsHolder { - @Comment("The name shown in the help messages") - public static final Property HELP_HEADER = - newProperty("settings.helpHeader", "AuthMeReloaded"); - @Comment({ "Do you want to enable the session feature?", "If enabled, when a player authenticates successfully,", diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2dc4bb00..bed1d2c4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -41,8 +41,6 @@ DataSource: # Column for RealName mySQLRealName: realname settings: - # The name shown in the help messages. - helpHeader: AuthMeReloaded sessions: # Do you want to enable the session feature? # If enabled, when a player authenticates successfully, diff --git a/src/main/resources/messages/help_de.yml b/src/main/resources/messages/help_de.yml index 57e88a8a..46292e16 100644 --- a/src/main/resources/messages/help_de.yml +++ b/src/main/resources/messages/help_de.yml @@ -10,8 +10,8 @@ common: allowed: 'Allen erlaubt' section: command: 'Kommando' - description.short: 'Beschreibung' - description.detailed: 'Detaillierte Beschreibung' + description: 'Beschreibung' + detailedDescription: 'Detaillierte Beschreibung' arguments: 'Argumente' permissions: 'Rechte' alternatives: 'Alternativen' diff --git a/src/main/resources/messages/help_en.yml b/src/main/resources/messages/help_en.yml index 5a926c43..4a9fe5ec 100644 --- a/src/main/resources/messages/help_en.yml +++ b/src/main/resources/messages/help_en.yml @@ -1,4 +1,9 @@ +# Translation config for the AuthMe help, e.g. when /authme help or /authme help register is called + +# ------------------------------------------------------- +# List of texts used in the help section common: + header: '==========[ AuthMeReloaded HELP ]==========' optional: 'Optional' hasPermission: 'You have permission' noPermission: 'No permission' @@ -8,14 +13,25 @@ common: notAllowed: 'No permission' opOnly: 'OP''s only' allowed: 'Everyone allowed' + +# ------------------------------------------------------- +# Titles of the individual help sections +# Set the translation text to empty text to disable the section, e.g. to hide alternatives: +# alternatives: '' section: command: 'Command' - description.short: 'Short description' - description.detailed: 'Detailed description' + description: 'Short description' + detailedDescription: 'Detailed description' arguments: 'Arguments' permissions: 'Permissions' alternatives: 'Alternatives' children: 'Commands' + +# ------------------------------------------------------- +# You can translate the data for all commands using the below pattern. +# So for example to translate /authme reload, create a section "authme.reload", or "login" for /login +# If the command has arguments, you can use arg1 as below to translate the first argument, and so forth +# Translations don't need to be complete; any missing section will be taken from the default silently commands: authme.register: description: 'Register a player' diff --git a/src/test/java/fr/xephi/authme/command/help/HelpMessageAndHelpSectionConsistencyTest.java b/src/test/java/fr/xephi/authme/command/help/HelpMessageAndHelpSectionConsistencyTest.java new file mode 100644 index 00000000..c0841139 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/help/HelpMessageAndHelpSectionConsistencyTest.java @@ -0,0 +1,39 @@ +package fr.xephi.authme.command.help; + +import fr.xephi.authme.util.StringUtils; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +/** + * Test for enums {@link HelpMessage} and {@link HelpSection}. + */ +public class HelpMessageAndHelpSectionConsistencyTest { + + @Test + public void shouldHaveUniqueNonEmptyKeys() { + // given + Set keys = new HashSet<>(); + + // when / then + for (HelpMessage message : HelpMessage.values()) { + assertThat("Key for message '" + message + "' is empty", + StringUtils.isEmpty(message.getKey()), equalTo(false)); + if (!keys.add(message.getKey())) { + fail("Key for message '" + message + "' is already used elsewhere"); + } + } + for (HelpSection section : HelpSection.values()) { + assertThat("Key for section '" + section + "' is empty", + StringUtils.isEmpty(section.getKey()), equalTo(false)); + if (!keys.add(section.getKey())) { + fail("Key for section '" + section + "' is already used elsewhere"); + } + } + } +} diff --git a/src/test/java/fr/xephi/authme/command/help/HelpMessagesServiceTest.java b/src/test/java/fr/xephi/authme/command/help/HelpMessagesServiceTest.java new file mode 100644 index 00000000..39130534 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/help/HelpMessagesServiceTest.java @@ -0,0 +1,102 @@ +package fr.xephi.authme.command.help; + +import ch.jalu.injector.testing.BeforeInjecting; +import ch.jalu.injector.testing.DelayedInjectionRunner; +import ch.jalu.injector.testing.InjectDelayed; +import fr.xephi.authme.command.CommandDescription; +import fr.xephi.authme.command.TestCommandsUtil; +import fr.xephi.authme.message.MessageFileHandler; +import fr.xephi.authme.message.MessageFileHandlerProvider; +import fr.xephi.authme.permission.DefaultPermission; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +import java.util.Set; +import java.util.function.Function; + +import static fr.xephi.authme.TestHelper.getJarFile; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; + +/** + * Test for {@link HelpMessagesService}. + */ +@RunWith(DelayedInjectionRunner.class) +public class HelpMessagesServiceTest { + + private static final String TEST_FILE = "/fr/xephi/authme/command/help/help_test.yml"; + private static final Set COMMANDS = TestCommandsUtil.generateCommands(); + + @InjectDelayed + private HelpMessagesService helpMessagesService; + + @Mock + private MessageFileHandlerProvider messageFileHandlerProvider; + + @BeforeInjecting + public void initializeHandler() { + MessageFileHandler handler = new MessageFileHandler(getJarFile(TEST_FILE), "messages/messages_en.yml"); + given(messageFileHandlerProvider.initializeHandler(any(Function.class))).willReturn(handler); + } + + @Test + public void shouldReturnLocalizedCommand() { + // given + CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "authme", "register"); + + // when + CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command); + + // then + assertThat(localCommand.getDescription(), equalTo("Registration")); + assertThat(localCommand.getDetailedDescription(), equalTo("Registers the player")); + assertThat(localCommand.getExecutableCommand(), equalTo(command.getExecutableCommand())); + assertThat(localCommand.getPermission(), equalTo(command.getPermission())); + assertThat(localCommand.getArguments(), hasSize(2)); + assertThat(localCommand.getArguments().get(0).getName(), equalTo("password")); + assertThat(localCommand.getArguments().get(0).getDescription(), equalTo("The password")); + assertThat(localCommand.getArguments().get(1).getName(), equalTo("confirmation")); + assertThat(localCommand.getArguments().get(1).getDescription(), equalTo("The confirmation")); + } + + @Test + public void shouldReturnLocalizedCommandWithDefaults() { + // given + CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "authme", "login"); + + // when + CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command); + + // then + assertThat(localCommand.getDescription(), equalTo("Logging in")); + assertThat(localCommand.getDetailedDescription(), equalTo("'login' test command")); + assertThat(localCommand.getArguments(), hasSize(1)); + assertThat(localCommand.getArguments().get(0).getName(), equalTo("user password")); + assertThat(localCommand.getArguments().get(0).getDescription(), equalTo("'password' argument description")); + } + + @Test + public void shouldReturnSameCommandForNoLocalization() { + // given + CommandDescription command = TestCommandsUtil.getCommandWithLabel(COMMANDS, "email"); + + // when + CommandDescription localCommand = helpMessagesService.buildLocalizedDescription(command); + + // then + assertThat(localCommand, sameInstance(command)); + } + + @Test + public void shouldGetTranslationsForSectionAndMessage() { + // given / when / then + assertThat(helpMessagesService.getMessage(DefaultPermission.OP_ONLY), equalTo("only op")); + assertThat(helpMessagesService.getMessage(HelpMessage.RESULT), equalTo("res.")); + assertThat(helpMessagesService.getMessage(HelpSection.ARGUMENTS), equalTo("arg.")); + } +} diff --git a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java index 8786b6e7..5a0f06f2 100644 --- a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java +++ b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java @@ -10,8 +10,6 @@ import fr.xephi.authme.command.TestCommandsUtil; import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.DefaultPermission; import fr.xephi.authme.permission.PermissionsManager; -import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.settings.properties.PluginSettings; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.junit.BeforeClass; @@ -25,6 +23,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel; import static fr.xephi.authme.command.help.HelpProvider.ALL_OPTIONS; @@ -42,8 +41,10 @@ import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; /** @@ -52,7 +53,6 @@ import static org.mockito.Mockito.verify; @RunWith(DelayedInjectionRunner.class) public class HelpProviderTest { - private static final String HELP_HEADER = "Help"; private static Set commands; @InjectDelayed @@ -62,8 +62,6 @@ public class HelpProviderTest { @Mock private HelpMessagesService helpMessagesService; @Mock - private Settings settings; - @Mock private CommandSender sender; @BeforeClass @@ -73,7 +71,6 @@ public class HelpProviderTest { @BeforeInjecting public void setInitialSettings() { - given(settings.getProperty(PluginSettings.HELP_HEADER)).willReturn(HELP_HEADER); setDefaultHelpMessages(helpMessagesService); } @@ -89,11 +86,11 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(5)); - assertThat(lines.get(0), containsString(HELP_HEADER + " HELP")); - assertThat(removeColors(lines.get(1)), containsString("Command: /authme login ")); - assertThat(removeColors(lines.get(2)), containsString("Short description: login cmd")); - assertThat(removeColors(lines.get(3)), equalTo("Detailed description:")); - assertThat(removeColors(lines.get(4)), containsString("'login' test command")); + assertThat(lines.get(0), containsString("Header")); + assertThat(lines.get(1), containsString("Command: /authme login ")); + assertThat(lines.get(2), containsString("Short description: login cmd")); + assertThat(lines.get(3), equalTo("Detailed description:")); + assertThat(lines.get(4), containsString("'login' test command")); } @Test @@ -108,10 +105,10 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(4)); - assertThat(lines.get(0), containsString(HELP_HEADER + " HELP")); - assertThat(removeColors(lines.get(1)), equalTo("Arguments:")); - assertThat(removeColors(lines.get(2)), containsString("password: 'password' argument description")); - assertThat(removeColors(lines.get(3)), containsString("confirmation: 'confirmation' argument description")); + assertThat(lines.get(0), containsString("Header")); + assertThat(lines.get(1), equalTo("Arguments:")); + assertThat(lines.get(2), containsString("password: 'password' argument description")); + assertThat(lines.get(3), containsString("confirmation: 'confirmation' argument description")); } @Test @@ -126,7 +123,7 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(3)); - assertThat(removeColors(lines.get(2)), containsString("player: 'player' argument description (Optional)")); + assertThat(lines.get(2), containsString("player: 'player' argument description (Optional)")); } /** Verifies that the "Arguments:" line is not shown if the command has no arguments. */ @@ -159,11 +156,11 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(5)); - assertThat(removeColors(lines.get(1)), containsString("Permissions:")); - assertThat(removeColors(lines.get(2)), + assertThat(lines.get(1), containsString("Permissions:")); + assertThat(lines.get(2), containsString(AdminPermission.UNREGISTER.getNode() + " (Has permission)")); - assertThat(removeColors(lines.get(3)), containsString("Default: Op only (Has permission)")); - assertThat(removeColors(lines.get(4)), containsString("Result: Has permission")); + assertThat(lines.get(3), containsString("Default: Op only (Has permission)")); + assertThat(lines.get(4), containsString("Result: Has permission")); } @Test @@ -181,11 +178,11 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(5)); - assertThat(removeColors(lines.get(1)), containsString("Permissions:")); - assertThat(removeColors(lines.get(2)), + assertThat(lines.get(1), containsString("Permissions:")); + assertThat(lines.get(2), containsString(AdminPermission.UNREGISTER.getNode() + " (No permission)")); - assertThat(removeColors(lines.get(3)), containsString("Default: Op only (No permission)")); - assertThat(removeColors(lines.get(4)), containsString("Result: No permission")); + assertThat(lines.get(3), containsString("Default: Op only (No permission)")); + assertThat(lines.get(4), containsString("Result: No permission")); } @Test @@ -230,9 +227,9 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(4)); - assertThat(removeColors(lines.get(1)), containsString("Alternatives:")); - assertThat(removeColors(lines.get(2)), containsString("/authme register ")); - assertThat(removeColors(lines.get(3)), containsString("/authme r ")); + assertThat(lines.get(1), containsString("Alternatives:")); + assertThat(lines.get(2), containsString("/authme register ")); + assertThat(lines.get(3), containsString("/authme r ")); } @Test @@ -261,9 +258,9 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(4)); - assertThat(removeColors(lines.get(1)), containsString("Children:")); - assertThat(removeColors(lines.get(2)), containsString("/authme login: login cmd")); - assertThat(removeColors(lines.get(3)), containsString("/authme register: register cmd")); + assertThat(lines.get(1), containsString("Children:")); + assertThat(lines.get(2), containsString("/authme login: login cmd")); + assertThat(lines.get(3), containsString("/authme register: register cmd")); } @Test @@ -311,8 +308,8 @@ public class HelpProviderTest { // then List lines = getLines(sender); assertThat(lines, hasSize(2)); - assertThat(lines.get(0), containsString(HELP_HEADER + " HELP")); - assertThat(removeColors(lines.get(1)), containsString("Command: /authme register ")); + assertThat(lines.get(0), containsString("Header")); + assertThat(lines.get(1), containsString("Command: /authme register ")); } @Test @@ -341,6 +338,63 @@ public class HelpProviderTest { assertThat(result, contains("authme", "register")); } + @Test + public void shouldDisableSectionsWithEmptyTranslations() { + // given + given(helpMessagesService.getMessage(HelpSection.DETAILED_DESCRIPTION)).willReturn(""); + given(helpMessagesService.getMessage(HelpSection.ALTERNATIVES)).willReturn(""); + given(helpMessagesService.getMessage(HelpSection.PERMISSIONS)).willReturn(""); + + CommandDescription command = getCommandWithLabel(commands, "authme", "register"); + FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); + + // when + helpProvider.outputHelp(sender, result, ALL_OPTIONS); + + // then + List lines = getLines(sender); + assertThat(lines, hasSize(6)); + assertThat(lines.get(0), equalTo("Header")); + assertThat(lines.get(1), equalTo("Command: /authme register ")); + assertThat(lines.get(2), equalTo("Short description: register cmd")); + assertThat(lines.get(3), equalTo("Arguments:")); + assertThat(lines.get(4), containsString("'password' argument description")); + assertThat(lines.get(5), containsString("'confirmation' argument description")); + } + + @Test + public void shouldNotReturnAnythingForAllDisabledSections() { + // given + given(helpMessagesService.getMessage(HelpSection.COMMAND)).willReturn(""); + given(helpMessagesService.getMessage(HelpSection.ALTERNATIVES)).willReturn(""); + given(helpMessagesService.getMessage(HelpSection.PERMISSIONS)).willReturn(""); + + CommandDescription command = getCommandWithLabel(commands, "authme", "register"); + FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); + + // when + helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_ALTERNATIVES | SHOW_PERMISSIONS); + + // then + verify(sender, never()).sendMessage(anyString()); + } + + @Test + public void shouldSkipEmptyHeader() { + // given + given(helpMessagesService.getMessage(HelpMessage.HEADER)).willReturn(""); + CommandDescription command = getCommandWithLabel(commands, "authme", "register"); + FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); + + // when + helpProvider.outputHelp(sender, result, SHOW_COMMAND); + + // then + List lines = getLines(sender); + assertThat(lines, hasSize(1)); + assertThat(lines.get(0), equalTo("Command: /authme register ")); + } + /** * Generate an instance of {@link FoundCommandResult} with the given command and labels. All other fields aren't * retrieved by {@link HelpProvider} and so are initialized to default values for the tests. @@ -363,7 +417,7 @@ public class HelpProviderTest { private static List getLines(CommandSender sender) { ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); verify(sender, atLeastOnce()).sendMessage(captor.capture()); - return captor.getAllValues(); + return captor.getAllValues().stream().map(s -> removeColors(s)).collect(Collectors.toList()); } private static void setDefaultHelpMessages(HelpMessagesService helpMessagesService) { diff --git a/src/test/resources/fr/xephi/authme/command/help/help_test.yml b/src/test/resources/fr/xephi/authme/command/help/help_test.yml new file mode 100644 index 00000000..a7616cc9 --- /dev/null +++ b/src/test/resources/fr/xephi/authme/command/help/help_test.yml @@ -0,0 +1,22 @@ +common: + result: 'res.' + defaultPermissions: + opOnly: 'only op' + +section: + arguments: 'arg.' + +commands: + authme.register: + description: 'Registration' + detailedDescription: 'Registers the player' + arg1: + label: 'password' + description: 'The password' + arg2: + label: 'confirmation' + description: 'The confirmation' + authme.login: + description: 'Logging in' + arg1: + label: 'user password'