public void Edit_Post_Test_InvalidModelState() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Id = 1, Name = "attr mmm" } }; controller.ModelState.AddModelError("Name", "Required"); controller.Edit(model); mockRepo.Verify(db => db.SaveChanges(), Times.Never); Assert.Equal("Test name", mockRepo.Object.ContentTypes.First().Name); Assert.Equal("Attr1", mockRepo.Object.ContentTypes.First().Attrs.First().Name); }
public void AddDictionaryAttributeTest_DictionaryNotFound() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.ContentAttrs, GetAttributeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, Name = "Test name", AddedOrEditedAttr = new ContentAttributeModel { Id = null, //it means that's a new attribute Name = "Sex", AttributeType = AttrType.Dictionary, DictionaryId = 11 } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); Assert.IsType <NotFoundObjectResult>(result); var r = (NotFoundObjectResult)result; Assert.Equal($"Dictionary with id = { model.AddedOrEditedAttr.DictionaryId } not found", r.Value); }
public void Edit_Post_Test() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel { Id = 1, Name = "attr mmm", Required = true, AttributeType = AttrType.Image } }; var result = controller.Edit(model); var viewResult = Assert.IsType <ViewResult>(result); mockRepo.Verify(db => db.SaveChanges(), Times.Once()); Assert.Equal("mmm", mockRepo.Object.ContentTypes.First().Name); Assert.Equal("attr mmm", mockRepo.Object.ContentTypes.First().Attrs.First().Name); var resultModel = Assert.IsAssignableFrom <EditContentTypeModel>( viewResult.ViewData.Model); Assert.Equal("mmm", resultModel.Name); Assert.Equal("attr mmm", resultModel.Attrs.First().Name); Assert.True(resultModel.Attrs.First().Required); Assert.Equal(AttrType.Image, resultModel.Attrs.First().AttributeType); }
public void Edit_Post_Test_InvalidContentTypeId() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 11, }; var result = controller.Edit(model); Assert.IsType <NotFoundResult>(result); }
public static EditContentTypeModel GetEditContentTypeModel(EntityType entityType) { var result = new EditContentTypeModel { Id = entityType.Id, Name = entityType.Name, Attrs = entityType.Attrs.Select(a => new ContentAttributeModel { Id = a.Id, Name = a.Name, AttributeType = a.AttrType, Required = a.Required, DictionaryId = a.DictionaryId }).ToList() }; return(result); }
public void Edit_Post_Test_AddAttribute() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Name = "attr mmm", Required = true, AttributeType = AttrType.Image } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(controller.ModelState); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); var viewResult = Assert.IsType <ViewResult>(result); var resultModel = Assert.IsAssignableFrom <EditContentTypeModel>( viewResult.ViewData.Model); mockRepo.Verify(db => db.SaveChanges(), Times.Once); Assert.Empty(controller.ModelState); Assert.Equal(2, mockRepo.Object.ContentTypes.First().Attrs.Count); Assert.Equal(2, resultModel.Attrs.Count); var attrs = mockRepo.Object.ContentTypes.First().Attrs.Where(a => a.Name == "attr mmm"); Assert.Single(attrs); Assert.True(attrs.First().Required); Assert.Equal(AttrType.Image, attrs.First().AttrType); }
public void Edit_Post_Test_InvalidAttrId() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Name = "mmm", Id = 1, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Id = 11, Name = "attr mmm" } }; Assert.Throws <InvalidOperationException>(() => controller.Edit(model)); mockRepo.Verify(db => db.SaveChanges(), Times.Never); }
public void Edit_Post_Test_AddAttribute_ContentTypeNotFound() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 11, AddedOrEditedAttr = new ContentAttributeModel//todo: not exist { Name = "attr mmm" } }; controller.ModelState.AddModelError("Name", "Required"); var result = controller.Edit(model); Assert.IsType <NotFoundResult>(result); }
public void AddDictionaryAttributeTest() { var mockRepo = new Mock <DevCmsDb>(); mockRepo.SetupDbSetMock(db => db.ContentTypes, GetContentTypeList()); mockRepo.SetupDbSetMock(db => db.ContentAttrs, GetAttributeList()); mockRepo.SetupDbSetMock(db => db.Dictionaries, GetDictionaries()); var controller = new ContentTypeController(mockRepo.Object); var model = new EditContentTypeModel { Id = 1, Name = "Test name", AddedOrEditedAttr = new ContentAttributeModel { Id = null, //it means that's a new attribute Name = "Sex", AttributeType = AttrType.Dictionary, DictionaryId = 1 } }; controller.ModelState.AddModelError("Name", "Required"); Assert.Single(mockRepo.Object.ContentTypes.First().Attrs); var result = controller.Edit(model); Assert.Equal(2, mockRepo.Object.ContentTypes.First().Attrs.Count); Assert.IsType <ViewResult>(result); mockRepo.Verify(db => db.SaveChanges(), Times.Once()); var attr = mockRepo.Object.ContentTypes.First().Attrs.Last(); Assert.Equal("Sex", attr.Name); Assert.Equal(AttrType.Dictionary, attr.AttrType); Assert.Equal(mockRepo.Object.Dictionaries.First().Id, attr.DictionaryId); }
public IActionResult Edit(EditContentTypeModel model) { // this mess exists because I wanted to have the only one endpoint to edit/create the attributes // and to edit the name of the content type // I can avoid it by using some RequireId attribute or by creating some another action like // CreateAttr /EditAttr, but I decided to use this approach if (IsAdditionNewAttribute(model)) { ModelState["Name"].Errors.Clear(); var contentType = _db.ContentTypes .Include(ct => ct.Attrs) .FirstOrDefault(t => t.Id == model.Id); if (contentType == null) { return(NotFound()); } if (model.AddedOrEditedAttr.AttributeType == AttrType.Dictionary) { if (!_db.Dictionaries.Any(d => d.Id == model.AddedOrEditedAttr.DictionaryId)) { return(NotFound($"Dictionary with id = { model.AddedOrEditedAttr.DictionaryId } not found")); } } contentType.Attrs.Add(new Attribute { Name = model.AddedOrEditedAttr.Name, AttrType = model.AddedOrEditedAttr.AttributeType, ContentTypeId = contentType.Id, Required = model.AddedOrEditedAttr.Required, DictionaryId = model.AddedOrEditedAttr.DictionaryId }); _db.SaveChanges(); ModelState.Clear(); model = DtoHelper.GetEditContentTypeModel(contentType); } else if (ModelState.IsValid) { var contentType = _db.ContentTypes .Include(ct => ct.Attrs) .FirstOrDefault(t => t.Id == model.Id); if (contentType == null) { return(NotFound()); } contentType.Name = model.Name; if (model.AddedOrEditedAttr != null && model.AddedOrEditedAttr.Id.HasValue) { var editedAttr = contentType.Attrs.First(at => at.Id == model.AddedOrEditedAttr.Id); editedAttr.Name = model.AddedOrEditedAttr.Name; editedAttr.AttrType = model.AddedOrEditedAttr.AttributeType; editedAttr.Required = model.AddedOrEditedAttr.Required; } _db.SaveChanges(); ModelState.Clear(); model = DtoHelper.GetEditContentTypeModel(contentType); } model.Dictionaries = _db.Dictionaries.Select(d => new SelectListItem { Value = d.Id.ToString(), Text = d.Name }).ToList(); return(View(model)); }
private bool IsAdditionNewAttribute(EditContentTypeModel model) { var result = model.AddedOrEditedAttr != null && !model.AddedOrEditedAttr.Id.HasValue; return(result); }