Skip to content

Commit

Permalink
Refactor breadcrumb code out of team controllers #626
Browse files Browse the repository at this point in the history
  • Loading branch information
sussexrick committed Dec 8, 2022
1 parent 0ebbabe commit de1415a
Show file tree
Hide file tree
Showing 169 changed files with 382 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using Stoolball.Clubs;
using Stoolball.Competitions;
using Stoolball.MatchLocations;
using Stoolball.Navigation;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Xunit;

namespace Stoolball.UnitTests.Statistics
namespace Stoolball.Web.UnitTests.Navigation
{
public class StatisticsBreadcrumbBuilderTests
{
Expand Down
62 changes: 62 additions & 0 deletions Stoolball.Web.UnitTests/Navigation/TeamBreadcrumbBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Stoolball.Clubs;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Xunit;

namespace Stoolball.Web.UnitTests.Navigation
{
public class TeamBreadcrumbBuilderTests
{
#nullable disable
[Fact]
public void Null_breadcrumbs_throws_ArgumentNullException()
{
var builder = new TeamBreadcrumbBuilder();

Assert.Throws<ArgumentNullException>(() => builder.BuildBreadcrumbs(null, new Team(), true));
}

[Fact]
public void Null_team_throws_ArgumentNullException()
{
var builder = new TeamBreadcrumbBuilder();

Assert.Throws<ArgumentNullException>(() => builder.BuildBreadcrumbs(new List<Breadcrumb>(), null, true));
}
#nullable enable

[Fact]
public void Club_adds_club_breadcrumb()
{
var builder = new TeamBreadcrumbBuilder();
var breadcrumbs = new List<Breadcrumb>();
var team = new Team { Club = new Club { ClubName = "Example club", ClubRoute = "/clubs/example-club" } };

builder.BuildBreadcrumbs(breadcrumbs, team, false);

Assert.Equal(2, breadcrumbs.Count);
Assert.Equal(Constants.Pages.Teams, breadcrumbs[0].Name);
Assert.Equal(Constants.Pages.TeamsUrl, breadcrumbs[0].Url?.ToString());
Assert.Equal(team.Club.ClubName, breadcrumbs[1].Name);
Assert.Equal(team.Club.ClubRoute, breadcrumbs[1].Url?.ToString());
}

[Fact]
public void IncludeTeam_adds_team_breadcrumb()
{
var builder = new TeamBreadcrumbBuilder();
var breadcrumbs = new List<Breadcrumb>();
var team = new Team { TeamName = "Example team", TeamRoute = "/teams/example-team" };

builder.BuildBreadcrumbs(breadcrumbs, team, true);

Assert.Equal(2, breadcrumbs.Count);
Assert.Equal(Constants.Pages.Teams, breadcrumbs[0].Name);
Assert.Equal(Constants.Pages.TeamsUrl, breadcrumbs[0].Url?.ToString());
Assert.Equal(team.TeamName, breadcrumbs[1].Name);
Assert.Equal(team.TeamRoute, breadcrumbs[1].Url?.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
using Stoolball.Clubs;
using Stoolball.Competitions;
using Stoolball.MatchLocations;
using Stoolball.Navigation;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Statistics;
using Stoolball.Web.Statistics.Models;
using Umbraco.Cms.Core.Web;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Stoolball.Data.Abstractions;
using Stoolball.Matches;
using Stoolball.Statistics;
using Stoolball.Web.Navigation;
using Stoolball.Web.Statistics;
using Stoolball.Web.Statistics.Models;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Threading.Tasks;
using Moq;
using Stoolball.Data.Abstractions;
using Stoolball.Navigation;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Statistics;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Stoolball.Data.Abstractions;
using Stoolball.Matches;
using Stoolball.Statistics;
using Stoolball.Web.Navigation;
using Stoolball.Web.Statistics;
using Stoolball.Web.Statistics.Models;
using Xunit;
Expand Down
12 changes: 10 additions & 2 deletions Stoolball.Web.UnitTests/Teams/DeleteTeamControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using Stoolball.Data.Abstractions;
using Stoolball.Security;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -17,6 +20,8 @@ public class DeleteTeamControllerTests : UmbracoBaseTest
private readonly Mock<ITeamDataSource> _teamDataSource = new();
private readonly Mock<IMatchListingDataSource> _matchListingDataSource = new();
private readonly Mock<IPlayerDataSource> _playerDataSource = new();
private readonly Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private DeleteTeamController CreateController()
{
Expand All @@ -27,7 +32,8 @@ private DeleteTeamController CreateController()
_teamDataSource.Object,
_matchListingDataSource.Object,
_playerDataSource.Object,
Mock.Of<IAuthorizationPolicy<Team>>())
_authorizationPolicy.Object,
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand All @@ -49,7 +55,9 @@ public async Task Route_not_matching_team_returns_404()
[Fact]
public async Task Route_matching_team_returns_DeleteTeamViewModel()
{
_teamDataSource.Setup(x => x.ReadTeamByRoute(It.IsAny<string>(), true)).ReturnsAsync(new Team { TeamId = Guid.NewGuid(), TeamRoute = "/teams/example" });
var team = new Team { TeamId = Guid.NewGuid(), TeamRoute = "/teams/example" };
_teamDataSource.Setup(x => x.ReadTeamByRoute(It.IsAny<string>(), true)).ReturnsAsync(team);
_playerDataSource.Setup(x => x.ReadPlayerIdentities(It.Is<PlayerFilter>(f => f.TeamIds.Count == 1 && f.TeamIds.Contains(team.TeamId.Value)))).Returns(Task.FromResult(new List<PlayerIdentity>()));

using (var controller = CreateController())
{
Expand Down
25 changes: 7 additions & 18 deletions Stoolball.Web.UnitTests/Teams/EditPlayersForTeamControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using Stoolball.Clubs;
using Stoolball.Data.Abstractions;
using Stoolball.Security;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -20,6 +20,7 @@ public class EditPlayersForTeamControllerTests : UmbracoBaseTest
private readonly Mock<ITeamDataSource> _teamDataSource = new();
private readonly Mock<IPlayerDataSource> _playerDataSource = new();
private readonly Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private EditPlayersForTeamController CreateController()
{
Expand All @@ -29,7 +30,8 @@ private EditPlayersForTeamController CreateController()
UmbracoContextAccessor.Object,
_teamDataSource.Object,
_authorizationPolicy.Object,
_playerDataSource.Object)
_playerDataSource.Object,
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand Down Expand Up @@ -124,22 +126,10 @@ public async Task Route_matching_team_sets_page_title()
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Route_matching_team_sets_breadcrumbs_including_club(bool hasClub)
[Fact]
public async Task Route_matching_team_sets_breadcrumbs()
{
var team = CreateTeam();

if (hasClub)
{
team.Club = new Club
{
ClubId = Guid.NewGuid(),
ClubName = "Example club",
ClubRoute = "/clubs/example-club"
};
}
SetupMocks(team);

using (var controller = CreateController())
Expand All @@ -148,8 +138,7 @@ public async Task Route_matching_team_sets_breadcrumbs_including_club(bool hasCl

var model = (TeamViewModel)((ViewResult)result).Model;

Assert.Equal(hasClub ? 5 : 4, model.Breadcrumbs.Count);
Assert.Equal(team.TeamName, model.Breadcrumbs[^1].Name);
_breadcrumbBuilder.Verify(x => x.BuildBreadcrumbs(model.Breadcrumbs, model.Team!, true), Times.Once());
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Stoolball.Web.UnitTests/Teams/EditTeamControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Stoolball.Data.Abstractions;
using Stoolball.Security;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -14,6 +15,8 @@ namespace Stoolball.Web.UnitTests.Teams
public class EditTeamControllerTests : UmbracoBaseTest
{
private readonly Mock<ITeamDataSource> _teamDataSource = new();
private Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private EditTeamController CreateController()
{
Expand All @@ -22,7 +25,8 @@ private EditTeamController CreateController()
CompositeViewEngine.Object,
UmbracoContextAccessor.Object,
_teamDataSource.Object,
Mock.Of<IAuthorizationPolicy<Team>>())
_authorizationPolicy.Object,
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Stoolball.Security;
using Stoolball.Teams;
using Stoolball.Web.Matches;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -23,6 +24,7 @@ public class MatchesForTeamControllerTests : UmbracoBaseTest
private readonly Mock<IMatchListingDataSource> _matchListingDataSource = new();
private readonly Mock<ICreateMatchSeasonSelector> _createMatchSeasonSelector = new();
private readonly Mock<IMatchFilterQueryStringParser> _matchFilterQueryStringParser = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private MatchesForTeamController CreateController()
{
Expand All @@ -38,7 +40,8 @@ private MatchesForTeamController CreateController()
Mock.Of<IAuthorizationPolicy<Team>>(),
_matchFilterQueryStringParser.Object,
Mock.Of<IMatchFilterHumanizer>(),
Mock.Of<IAddMatchMenuViewModelFactory>())
Mock.Of<IAddMatchMenuViewModelFactory>(),
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand Down
24 changes: 7 additions & 17 deletions Stoolball.Web.UnitTests/Teams/PlayersForTeamControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using Stoolball.Clubs;
using Stoolball.Data.Abstractions;
using Stoolball.Security;
using Stoolball.Statistics;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -20,6 +20,7 @@ public class PlayersForTeamControllerTests : UmbracoBaseTest
private readonly Mock<ITeamDataSource> _teamDataSource = new();
private readonly Mock<IPlayerDataSource> _playerDataSource = new();
private readonly Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private PlayersForTeamController CreateController()
{
Expand All @@ -29,7 +30,8 @@ private PlayersForTeamController CreateController()
UmbracoContextAccessor.Object,
_teamDataSource.Object,
_authorizationPolicy.Object,
_playerDataSource.Object)
_playerDataSource.Object,
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand Down Expand Up @@ -115,27 +117,15 @@ public async Task Route_matching_team_sets_page_title_and_description()
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Route_matching_team_sets_breadcrumbs_including_club(bool hasClub)
[Fact]
public async Task Route_matching_team_sets_breadcrumbs()
{
var team = new Team
{
TeamId = Guid.NewGuid(),
TeamName = "Example team",
TeamRoute = "/teams/example-team"
};

if (hasClub)
{
team.Club = new Club
{
ClubId = Guid.NewGuid(),
ClubName = "Example club",
ClubRoute = "/clubs/example-club"
};
}
SetupMocks(team);

using (var controller = CreateController())
Expand All @@ -144,7 +134,7 @@ public async Task Route_matching_team_sets_breadcrumbs_including_club(bool hasCl

var model = (TeamViewModel)((ViewResult)result).Model;

Assert.Equal(hasClub ? 4 : 3, model.Breadcrumbs.Count);
_breadcrumbBuilder.Verify(x => x.BuildBreadcrumbs(model.Breadcrumbs, model.Team!, false), Times.Once());
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Stoolball.Web.UnitTests/Teams/TeamActionsControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Stoolball.Data.Abstractions;
using Stoolball.Security;
using Stoolball.Teams;
using Stoolball.Web.Navigation;
using Stoolball.Web.Teams;
using Stoolball.Web.Teams.Models;
using Xunit;
Expand All @@ -14,6 +15,8 @@ namespace Stoolball.Web.UnitTests.Teams
public class TeamActionsControllerTests : UmbracoBaseTest
{
private readonly Mock<ITeamDataSource> _teamDataSource = new();
private readonly Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new();
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new();

private TeamActionsController CreateController()
{
Expand All @@ -22,7 +25,8 @@ private TeamActionsController CreateController()
CompositeViewEngine.Object,
UmbracoContextAccessor.Object,
_teamDataSource.Object,
Mock.Of<IAuthorizationPolicy<Team>>())
_authorizationPolicy.Object,
_breadcrumbBuilder.Object)
{
ControllerContext = ControllerContext
};
Expand Down
Loading

0 comments on commit de1415a

Please sign in to comment.