Skip to content

Commit

Permalink
Fix tests and add new tests to cover new config option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 8, 2021
1 parent 1e01506 commit 2a0e361
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
25 changes: 13 additions & 12 deletions src/main/java/world/bentobox/islandfly/FlyToggleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* This command allows to enable and disable fly mode.
*/
public class FlyToggleCommand extends CompositeCommand {
private Settings settings;


private Settings settings;

/**
* Default constructor
* @param parent Instance of CompositeCommand
Expand Down Expand Up @@ -53,21 +53,22 @@ public boolean canExecute(User user, String label, List<String> args) {

// Enable fly if island is a spawn and user has permission for it
if (island.isSpawn() && user.hasPermission(this.getPermissionPrefix() + "island.flyspawn")) {
return true;
return true;
}

if (!island.isAllowed(user, IslandFlyAddon.ISLAND_FLY_PROTECTION) && !user.hasPermission(this.getPermissionPrefix() + "island.flybypass")) {

user.sendMessage("islandfly.command.not-allowed-fly");
return false;
}


if ( !this.settings.isAllowCommandOutsideProtectionRange() && !island.getProtectionBoundingBox().contains(user.getLocation().toVector())) {

user.sendMessage("islandfly.outside-protection-range");
return false;



if (!this.settings.isAllowCommandOutsideProtectionRange()
&& !island.getProtectionBoundingBox().contains(user.getLocation().toVector())) {

user.sendMessage("islandfly.outside-protection-range");
return false;

}


Expand Down
69 changes: 55 additions & 14 deletions src/test/java/world/bentobox/islandfly/FlyToggleCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
Expand All @@ -38,6 +40,7 @@
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.islandfly.config.Settings;

/**
* @author tastybento
Expand All @@ -46,7 +49,7 @@
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
public class FlyToggleCommandTest {

@Mock
private CompositeCommand ic;
private UUID uuid;
Expand All @@ -65,12 +68,15 @@ public class FlyToggleCommandTest {
private @Nullable Location location;
@Mock
private Island island;


private Settings settings;
@Mock
private BoundingBox box;


/**
* @throws java.lang.Exception
*/

@Before
public void setUp() throws Exception {
// Set up plugin
Expand Down Expand Up @@ -102,16 +108,26 @@ public void setUp() throws Exception {
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true);
when(user.getLocation()).thenReturn(location);

// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);

// Island Manager
when(plugin.getIslands()).thenReturn(im);
Optional<Island> opIsland = Optional.of(island);
when(im.getIslandAt(any())).thenReturn(opIsland);


// Settings
settings = new Settings();
when(addon.getSettings()).thenReturn(settings);

// Island
when(island.getProtectionBoundingBox()).thenReturn(box);
when(location.toVector()).thenReturn(new Vector(0,60,0));
// Locations are always inside the box for now
when(box.contains(any(Vector.class))).thenReturn(true);

ftc = new FlyToggleCommand(ic, addon);
}

Expand Down Expand Up @@ -148,7 +164,7 @@ public void testCanExecuteWrongWorld() {
when(Util.getWorld(any())).thenReturn(mock(World.class));
assertFalse(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user).sendMessage(eq("islandfly.wrong-world"));

}
/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
Expand All @@ -157,7 +173,7 @@ public void testCanExecuteWrongWorld() {
public void testCanExecuteNoIsland() {
when(im.getIslandAt(any())).thenReturn(Optional.empty());
assertFalse(ftc.canExecute(user, "fly", Collections.emptyList()));

}
/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
Expand All @@ -166,9 +182,9 @@ public void testCanExecuteNoIsland() {
public void testCanExecuteSpawn() {
when(island.isSpawn()).thenReturn(true);
when(user.hasPermission(eq("bskyblock.island.flyspawn"))).thenReturn(true);
assertTrue(ftc.canExecute(user, "fly", Collections.emptyList()));
assertTrue(ftc.canExecute(user, "fly", Collections.emptyList()));
}

/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
Expand All @@ -177,7 +193,7 @@ public void testCanExecuteNotAllowedFlagNoPermission() {
when(island.isAllowed(eq(user), any())).thenReturn(false);
when(user.hasPermission(anyString())).thenReturn(false);
assertFalse(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user).sendMessage(eq("islandfly.command.not-allowed-fly"));
verify(user).sendMessage(eq("islandfly.command.not-allowed-fly"));
}
/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
Expand All @@ -187,7 +203,7 @@ public void testCanExecuteNoFlagAllowedPermission() {
when(island.isAllowed(eq(user), any())).thenReturn(false);
when(user.hasPermission(anyString())).thenReturn(true);
assertTrue(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user, never()).sendMessage(eq("islandfly.command.not-allowed-fly"));
verify(user, never()).sendMessage(anyString());
}
/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
Expand All @@ -197,7 +213,32 @@ public void testCanExecuteFlagAllowed() {
when(island.isAllowed(eq(user), any())).thenReturn(true);
when(user.hasPermission(anyString())).thenReturn(false);
assertTrue(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user, never()).sendMessage(eq("islandfly.command.not-allowed-fly"));
verify(user, never()).sendMessage(anyString());
}

/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOutsideProtectionRange() {
when(island.isAllowed(eq(user), any())).thenReturn(true);
when(user.hasPermission(anyString())).thenReturn(false);
when(box.contains(any(Vector.class))).thenReturn(false);
assertFalse(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user).sendMessage(eq("islandfly.outside-protection-range"));
}

/**
* Test method for {@link world.bentobox.islandfly.FlyToggleCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteOutsideProtectionRangeCommandAllowed() {
settings.setAllowCommandOutsideProtectionRange(true);
when(island.isAllowed(eq(user), any())).thenReturn(true);
when(user.hasPermission(anyString())).thenReturn(false);
when(box.contains(any(Vector.class))).thenReturn(false);
assertTrue(ftc.canExecute(user, "fly", Collections.emptyList()));
verify(user, never()).sendMessage(anyString());
}

/**
Expand Down

0 comments on commit 2a0e361

Please sign in to comment.