Skip to content

Commit 2d9a274

Browse files
authored
Merge pull request #2164 from TechnologyEnhancedLearning/Develop/Fixes/TD-2408-AddedCheckForDuplicateCentreInEditScreen
TD-2408 Added a condition for duplicate centre in edit centre details and added a corresponding web test.
2 parents 2aee373 + 398c547 commit 2d9a274

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/SuperAdmin/CentresControllerTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public void Cleanup()
6262
public void EditCentreDetails_updates_centre_and_redirects_with_successful_save()
6363
{
6464
// Given
65+
IEnumerable<(int, string)> centresList = new List<(int, string)> { (374, "##HEE Demo Centre1##") };
66+
A.CallTo(() => centresDataService.GetAllCentres(false)).Returns(centresList);
6567
var model = new EditCentreDetailsSuperAdminViewModel
6668
{
6769
CentreId = 374,
@@ -91,6 +93,46 @@ public void EditCentreDetails_updates_centre_and_redirects_with_successful_save(
9193
.MustHaveHappenedOnceExactly();
9294
}
9395

96+
[Test]
97+
public void EditCentreDetails_results_DuplicateCentre_error()
98+
{
99+
// Given
100+
IEnumerable<(int, string)> centresList = new List<(int, string)> { (374, "##HEE Demo Centre##") };
101+
A.CallTo(() => centresDataService.GetAllCentres(false)).Returns(centresList);
102+
var model = new EditCentreDetailsSuperAdminViewModel
103+
{
104+
CentreId = 374,
105+
CentreName = "##HEE Demo Centre##",
106+
CentreTypeId = 1,
107+
CentreType = "NHS Organisation",
108+
RegionName = "National",
109+
CentreEmail = "[email protected]",
110+
IpPrefix = "12.33.4",
111+
ShowOnMap = true,
112+
RegionId = 13
113+
};
114+
115+
// When
116+
var result = controller.EditCentreDetails(model);
117+
// Then
118+
result.Should().BeViewResult();
119+
controller.ModelState.IsValid.Should().BeFalse();
120+
var centreNameErrors = controller.ModelState["CentreName"].Errors;
121+
centreNameErrors.Should().NotBeEmpty();
122+
centreNameErrors.Should().Contain(error => error.ErrorMessage ==
123+
"The centre name you have entered already exists, please enter a different centre name");
124+
125+
A.CallTo(() => centresDataService.UpdateCentreDetailsForSuperAdmin(
126+
model.CentreId,
127+
model.CentreName,
128+
model.CentreTypeId,
129+
model.RegionId,
130+
model.CentreEmail,
131+
model.IpPrefix,
132+
model.ShowOnMap))
133+
.MustNotHaveHappened();
134+
}
135+
94136
[Test]
95137
public void AddCentre_adds_centre_and_redirects_with_successful_save()
96138
{

DigitalLearningSolutions.Web/Controllers/SuperAdmin/Centres/CentresController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public IActionResult EditCentreDetails(int centreId = 0)
227227
[Route("SuperAdmin/Centres/{centreId=0:int}/EditCentreDetails")]
228228
public IActionResult EditCentreDetails(EditCentreDetailsSuperAdminViewModel model)
229229
{
230+
bool isCentreNamePresent = centresDataService.GetAllCentres().Any(center => center.Item2 == model.CentreName);
231+
if (isCentreNamePresent)
232+
{
233+
ModelState.AddModelError("CentreName", CommonValidationErrorMessages.CentreNameAlreadyExist);
234+
}
235+
230236
if (!ModelState.IsValid)
231237
{
232238
var regions = regionDataService.GetRegionsAlphabetical().ToList();

0 commit comments

Comments
 (0)