From 432ed4620c8bb0e0ec1e7c0a04848735538fadad Mon Sep 17 00:00:00 2001
From: ljacqu <ljacqu@users.noreply.github.com>
Date: Sat, 13 Feb 2016 12:35:33 +0100
Subject: [PATCH] Fix AuthMe thinking config needs a migration - Old,
 deprecated property now exists again -> stop checking for its presence as a
 reason for migration - Create test for SettingsMigrationService to detect
 such issues in the future

---
 .../settings/SettingsMigrationService.java    |  7 +-
 .../SettingsMigrationServiceTest.java         | 66 +++++++++++++++++++
 2 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java

diff --git a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java
index a8347684b..dc4480277 100644
--- a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java
+++ b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java
@@ -49,11 +49,9 @@ public final class SettingsMigrationService {
 
         // Note ljacqu 20160211: Concatenating migration methods with | instead of the usual ||
         // ensures that all migrations will be performed
-        changes = changes
+        return changes
             | performMailTextToFileMigration(configuration, pluginFolder)
             | migrateJoinLeaveMessages(configuration);
-
-        return changes;
     }
 
     @VisibleForTesting
@@ -69,8 +67,7 @@ public final class SettingsMigrationService {
     private static boolean hasDeprecatedProperties(FileConfiguration configuration) {
         String[] deprecatedProperties = {
             "Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications",
-            "Passpartu", "Performances", "settings.delayJoinMessage", "settings.restrictions.enablePasswordVerifier",
-            "Xenoforo.predefinedSalt"};
+            "Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt"};
         for (String deprecatedPath : deprecatedProperties) {
             if (configuration.contains(deprecatedPath)) {
                 return true;
diff --git a/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java b/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java
new file mode 100644
index 000000000..cc89a463a
--- /dev/null
+++ b/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java
@@ -0,0 +1,66 @@
+package fr.xephi.authme.settings;
+
+import com.google.common.io.Files;
+import fr.xephi.authme.TestHelper;
+import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
+import fr.xephi.authme.settings.propertymap.PropertyMap;
+import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.hamcrest.Matchers.arrayWithSize;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeThat;
+
+/**
+ * Test for {@link SettingsMigrationService}.
+ */
+public class SettingsMigrationServiceTest {
+
+    @Rule
+    public TemporaryFolder testFolderHandler = new TemporaryFolder();
+
+    private File testFolder;
+    private File configTestFile;
+
+    /**
+     * Ensure that AuthMe regards the JAR's own config.yml as complete.
+     * If something legitimately needs migrating, a test from {@link ConfigFileConsistencyTest} should fail.
+     * If none fails in that class, it means something is wrong with the migration service
+     * as it wants to perform a migration on our up-to-date config.yml.
+     */
+    @Test
+    public void shouldNotRewriteJarConfig() throws IOException {
+        // given
+        copyConfigToTestFolder();
+        FileConfiguration configuration = YamlConfiguration.loadConfiguration(configTestFile);
+        PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
+        assumeThat(testFolder.listFiles(), arrayWithSize(1));
+
+        // when
+        boolean result = SettingsMigrationService.checkAndMigrate(configuration, propertyMap, testFolder);
+
+        // then
+        assertThat(result, equalTo(false));
+        assertThat(testFolder.listFiles(), arrayWithSize(1));
+    }
+
+    private void copyConfigToTestFolder() throws IOException {
+        testFolder = testFolderHandler.newFolder("migrationtest");
+
+        final File testConfig = testFolderHandler.newFile("migrationtest/config.yml");
+        final File realConfig = TestHelper.getJarFile("/config.yml");
+
+        Files.copy(realConfig, testConfig);
+        if (!testConfig.exists()) {
+            throw new IOException("Could not copy project's config.yml to test folder");
+        }
+        configTestFile = testConfig;
+    }
+}