2018-08-15 17:44:06 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2018-08-16 23:30:33 +10:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-08-15 17:44:06 +10:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Core.Composing;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Entities;
|
|
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Core.PropertyEditors;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
|
|
|
|
|
using Umbraco.Tests.TestHelpers.ControllerTesting;
|
|
|
|
|
|
using Umbraco.Tests.TestHelpers.Entities;
|
|
|
|
|
|
using Umbraco.Tests.Testing;
|
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
using Umbraco.Web.Editors;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache;
|
2018-10-29 17:27:33 +11:00
|
|
|
|
|
2018-08-15 17:44:06 +10:00
|
|
|
|
using Task = System.Threading.Tasks.Task;
|
2018-09-06 17:37:47 +10:00
|
|
|
|
using Umbraco.Core.Dictionary;
|
|
|
|
|
|
using Umbraco.Web.PropertyEditors;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using Umbraco.Web.WebApi;
|
|
|
|
|
|
using Umbraco.Web.Trees;
|
2018-09-06 20:33:32 +10:00
|
|
|
|
using System.Globalization;
|
2019-02-01 15:24:07 +11:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Persistence;
|
2018-10-29 17:27:33 +11:00
|
|
|
|
using Umbraco.Web.Actions;
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Web.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.None)]
|
|
|
|
|
|
public class ContentControllerTests : TestWithDatabaseBase
|
|
|
|
|
|
{
|
|
|
|
|
|
protected override void ComposeApplication(bool withApplication)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.ComposeApplication(withApplication);
|
|
|
|
|
|
|
|
|
|
|
|
//Replace with mockable services:
|
|
|
|
|
|
|
|
|
|
|
|
var userServiceMock = new Mock<IUserService>();
|
|
|
|
|
|
userServiceMock.Setup(service => service.GetUserById(It.IsAny<int>()))
|
|
|
|
|
|
.Returns((int id) => id == 1234 ? new User(1234, "Test", "test@test.com", "test@test.com", "", new List<IReadOnlyUserGroup>(), new int[0], new int[0]) : null);
|
2018-09-06 20:33:32 +10:00
|
|
|
|
userServiceMock.Setup(x => x.GetProfileById(It.IsAny<int>()))
|
|
|
|
|
|
.Returns((int id) => id == 1234 ? new User(1234, "Test", "test@test.com", "test@test.com", "", new List<IReadOnlyUserGroup>(), new int[0], new int[0]) : null);
|
2018-08-15 17:44:06 +10:00
|
|
|
|
userServiceMock.Setup(service => service.GetPermissionsForPath(It.IsAny<IUser>(), It.IsAny<string>()))
|
|
|
|
|
|
.Returns(new EntityPermissionSet(123, new EntityPermissionCollection(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
new EntityPermission(0, 123, new[]
|
|
|
|
|
|
{
|
2018-10-29 17:27:33 +11:00
|
|
|
|
ActionBrowse.ActionLetter.ToString(),
|
|
|
|
|
|
ActionUpdate.ActionLetter.ToString(),
|
|
|
|
|
|
ActionPublish.ActionLetter.ToString(),
|
|
|
|
|
|
ActionNew.ActionLetter.ToString()
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}),
|
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
|
|
var entityService = new Mock<IEntityService>();
|
|
|
|
|
|
entityService.Setup(x => x.GetAllPaths(UmbracoObjectTypes.Document, It.IsAny<int[]>()))
|
2018-09-06 20:33:32 +10:00
|
|
|
|
.Returns((UmbracoObjectTypes objType, int[] ids) => ids.Select(x => new TreeEntityPath { Path = $"-1,{x}", Id = x }).ToList());
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
var dataTypeService = new Mock<IDataTypeService>();
|
|
|
|
|
|
dataTypeService.Setup(service => service.GetDataType(It.IsAny<int>()))
|
2018-09-06 17:37:47 +10:00
|
|
|
|
.Returns(Mock.Of<IDataType>(type => type.Id == 9876 && type.Name == "text"));
|
|
|
|
|
|
dataTypeService.Setup(service => service.GetDataType(-87)) //the RTE
|
|
|
|
|
|
.Returns(Mock.Of<IDataType>(type => type.Id == -87 && type.Name == "Rich text" && type.Configuration == new RichTextConfiguration()));
|
2018-09-06 20:33:32 +10:00
|
|
|
|
|
|
|
|
|
|
var langService = new Mock<ILocalizationService>();
|
|
|
|
|
|
langService.Setup(x => x.GetAllLanguages()).Returns(new[] {
|
|
|
|
|
|
Mock.Of<ILanguage>(x => x.IsoCode == "en-US"),
|
|
|
|
|
|
Mock.Of<ILanguage>(x => x.IsoCode == "es-ES"),
|
|
|
|
|
|
Mock.Of<ILanguage>(x => x.IsoCode == "fr-FR")
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var textService = new Mock<ILocalizedTextService>();
|
2018-12-20 15:36:12 +01:00
|
|
|
|
textService.Setup(x => x.Localize(It.IsAny<string>(), It.IsAny<CultureInfo>(), It.IsAny<IDictionary<string, string>>())).Returns("text");
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
2018-11-29 10:35:16 +01:00
|
|
|
|
Composition.RegisterUnique(f => Mock.Of<IContentService>());
|
|
|
|
|
|
Composition.RegisterUnique(f => userServiceMock.Object);
|
|
|
|
|
|
Composition.RegisterUnique(f => entityService.Object);
|
|
|
|
|
|
Composition.RegisterUnique(f => dataTypeService.Object);
|
|
|
|
|
|
Composition.RegisterUnique(f => langService.Object);
|
|
|
|
|
|
Composition.RegisterUnique(f => textService.Object);
|
|
|
|
|
|
Composition.RegisterUnique(f => Mock.Of<ICultureDictionaryFactory>());
|
|
|
|
|
|
Composition.RegisterUnique(f => new UmbracoApiControllerTypeCollection(new[] { typeof(ContentTreeController) }));
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private MultipartFormDataContent GetMultiPartRequestContent(string json)
|
|
|
|
|
|
{
|
|
|
|
|
|
var multiPartBoundary = "----WebKitFormBoundary123456789";
|
|
|
|
|
|
return new MultipartFormDataContent(multiPartBoundary)
|
|
|
|
|
|
{
|
|
|
|
|
|
new StringContent(json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Headers =
|
|
|
|
|
|
{
|
|
|
|
|
|
ContentDisposition = new ContentDispositionHeaderValue("form-data")
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "contentItem"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-06 17:37:47 +10:00
|
|
|
|
private IContent GetMockedContent()
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = MockedContent.CreateSimpleContent(MockedContentTypes.CreateSimpleContentType());
|
|
|
|
|
|
content.Id = 123;
|
|
|
|
|
|
content.Path = "-1,123";
|
|
|
|
|
|
//ensure things have ids
|
|
|
|
|
|
var ids = 888;
|
|
|
|
|
|
foreach (var g in content.PropertyGroups)
|
|
|
|
|
|
{
|
|
|
|
|
|
g.Id = ids;
|
|
|
|
|
|
ids++;
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var p in content.PropertyTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
p.Id = ids;
|
|
|
|
|
|
ids++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return content;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private const string PublishJsonInvariant = @"{
|
2018-08-15 17:44:06 +10:00
|
|
|
|
""id"": 123,
|
|
|
|
|
|
""contentTypeAlias"": ""page"",
|
|
|
|
|
|
""parentId"": -1,
|
|
|
|
|
|
""action"": ""save"",
|
|
|
|
|
|
""variants"": [
|
|
|
|
|
|
{
|
2018-09-06 17:37:47 +10:00
|
|
|
|
""name"": ""asdf"",
|
2018-08-15 17:44:06 +10:00
|
|
|
|
""properties"": [
|
|
|
|
|
|
{
|
|
|
|
|
|
""id"": 1,
|
|
|
|
|
|
""alias"": ""title"",
|
|
|
|
|
|
""value"": ""asdf""
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
2018-09-06 17:37:47 +10:00
|
|
|
|
""culture"": null,
|
|
|
|
|
|
""save"": true,
|
|
|
|
|
|
""publish"": true
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}";
|
|
|
|
|
|
|
|
|
|
|
|
private const string PublishJsonVariant = @"{
|
2018-08-15 17:44:06 +10:00
|
|
|
|
""id"": 123,
|
|
|
|
|
|
""contentTypeAlias"": ""page"",
|
|
|
|
|
|
""parentId"": -1,
|
|
|
|
|
|
""action"": ""save"",
|
|
|
|
|
|
""variants"": [
|
|
|
|
|
|
{
|
2018-09-06 20:33:32 +10:00
|
|
|
|
""name"": ""asdf"",
|
2018-08-15 17:44:06 +10:00
|
|
|
|
""properties"": [
|
|
|
|
|
|
{
|
|
|
|
|
|
""id"": 1,
|
|
|
|
|
|
""alias"": ""title"",
|
|
|
|
|
|
""value"": ""asdf""
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
2018-09-06 20:33:32 +10:00
|
|
|
|
""culture"": ""en-US"",
|
|
|
|
|
|
""save"": true,
|
|
|
|
|
|
""publish"": true
|
2018-08-15 17:44:06 +10:00
|
|
|
|
},
|
|
|
|
|
|
{
|
2018-09-06 20:33:32 +10:00
|
|
|
|
""name"": ""asdf"",
|
2018-08-15 17:44:06 +10:00
|
|
|
|
""properties"": [
|
|
|
|
|
|
{
|
|
|
|
|
|
""id"": 1,
|
|
|
|
|
|
""alias"": ""title"",
|
|
|
|
|
|
""value"": ""asdf""
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
2018-09-06 20:33:32 +10:00
|
|
|
|
""culture"": ""fr-FR"",
|
|
|
|
|
|
""save"": true,
|
|
|
|
|
|
""publish"": true
|
2018-08-15 17:44:06 +10:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
""name"": ""asdf"",
|
|
|
|
|
|
""properties"": [
|
|
|
|
|
|
{
|
|
|
|
|
|
""id"": 1,
|
|
|
|
|
|
""alias"": ""title"",
|
|
|
|
|
|
""value"": ""asdf""
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
""culture"": ""es-ES"",
|
|
|
|
|
|
""save"": true,
|
|
|
|
|
|
""publish"": true
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns 404 if the content wasn't found based on the ID specified
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public async Task PostSave_Validate_Existing_Content()
|
|
|
|
|
|
{
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-08-15 17:44:06 +10:00
|
|
|
|
{
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
2018-09-06 17:37:47 +10:00
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => null); //do not find it
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
|
|
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-08-15 17:44:06 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
2018-09-06 17:37:47 +10:00
|
|
|
|
content: GetMultiPartRequestContent(PublishJsonInvariant),
|
2018-08-15 17:44:06 +10:00
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.NotFound, response.Item1.StatusCode);
|
2018-08-16 23:30:33 +10:00
|
|
|
|
Assert.AreEqual(")]}',\n{\"Message\":\"content was not found\"}", response.Item1.Content.ReadAsStringAsync().Result);
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
//var obj = JsonConvert.DeserializeObject<PagedResult<UserDisplay>>(response.Item2);
|
|
|
|
|
|
//Assert.AreEqual(0, obj.TotalItems);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-16 23:30:33 +10:00
|
|
|
|
[Test]
|
|
|
|
|
|
public async Task PostSave_Validate_At_Least_One_Variant_Flagged_For_Saving()
|
|
|
|
|
|
{
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-08-16 23:30:33 +10:00
|
|
|
|
{
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
2018-09-06 17:37:47 +10:00
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => GetMockedContent());
|
2018-08-16 23:30:33 +10:00
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-08-16 23:30:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-06 17:37:47 +10:00
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(PublishJsonInvariant);
|
2018-08-16 23:30:33 +10:00
|
|
|
|
//remove all save flaggs
|
2018-09-06 17:37:47 +10:00
|
|
|
|
((JArray)json["variants"])[0]["save"] = false;
|
2018-08-16 23:30:33 +10:00
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-08-16 23:30:33 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
|
|
|
|
|
content: GetMultiPartRequestContent(JsonConvert.SerializeObject(json)),
|
|
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.NotFound, response.Item1.StatusCode);
|
|
|
|
|
|
Assert.AreEqual(")]}',\n{\"Message\":\"No variants flagged for saving\"}", response.Item1.Content.ReadAsStringAsync().Result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-15 17:44:06 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns 404 if any of the posted properties dont actually exist
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2018-09-06 17:37:47 +10:00
|
|
|
|
[Test]
|
2018-08-15 17:44:06 +10:00
|
|
|
|
public async Task PostSave_Validate_Properties_Exist()
|
|
|
|
|
|
{
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-08-15 17:44:06 +10:00
|
|
|
|
{
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
2018-09-06 17:37:47 +10:00
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => GetMockedContent());
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-06 17:37:47 +10:00
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(PublishJsonInvariant);
|
|
|
|
|
|
//add a non-existent property to a variant being saved
|
|
|
|
|
|
var variantProps = (JArray)json["variants"].ElementAt(0)["properties"];
|
|
|
|
|
|
variantProps.Add(JObject.FromObject(new
|
|
|
|
|
|
{
|
|
|
|
|
|
id = 2,
|
|
|
|
|
|
alias = "doesntExist",
|
|
|
|
|
|
value = "hello"
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-08-15 17:44:06 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
2018-09-06 17:37:47 +10:00
|
|
|
|
content: GetMultiPartRequestContent(JsonConvert.SerializeObject(json)),
|
2018-08-15 17:44:06 +10:00
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.NotFound, response.Item1.StatusCode);
|
2018-09-06 17:37:47 +10:00
|
|
|
|
}
|
2018-09-06 20:33:32 +10:00
|
|
|
|
|
2018-09-06 17:37:47 +10:00
|
|
|
|
[Test]
|
|
|
|
|
|
public async Task PostSave_Simple_Invariant()
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = GetMockedContent();
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-08-15 17:44:06 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
2018-09-06 17:37:47 +10:00
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => content);
|
|
|
|
|
|
contentServiceMock.Setup(x => x.Save(It.IsAny<IContent>(), It.IsAny<int>(), It.IsAny<bool>()))
|
|
|
|
|
|
.Returns(new OperationResult(OperationResultType.Success, new Core.Events.EventMessages())); //success
|
2018-08-15 17:44:06 +10:00
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-08-15 17:44:06 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
2018-09-06 17:37:47 +10:00
|
|
|
|
content: GetMultiPartRequestContent(PublishJsonInvariant),
|
2018-08-15 17:44:06 +10:00
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
2018-09-06 17:37:47 +10:00
|
|
|
|
Assert.AreEqual(HttpStatusCode.OK, response.Item1.StatusCode);
|
|
|
|
|
|
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(response.Item2);
|
|
|
|
|
|
Assert.AreEqual(1, display.Variants.Count());
|
|
|
|
|
|
Assert.AreEqual(content.PropertyGroups.Count(), display.Variants.ElementAt(0).Tabs.Count());
|
|
|
|
|
|
Assert.AreEqual(content.PropertyTypes.Count(), display.Variants.ElementAt(0).Tabs.ElementAt(0).Properties.Count());
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-06 20:33:32 +10:00
|
|
|
|
[Test]
|
|
|
|
|
|
public async Task PostSave_Validate_Empty_Name()
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = GetMockedContent();
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-09-06 20:33:32 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
|
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => content);
|
|
|
|
|
|
contentServiceMock.Setup(x => x.Save(It.IsAny<IContent>(), It.IsAny<int>(), It.IsAny<bool>()))
|
|
|
|
|
|
.Returns(new OperationResult(OperationResultType.Success, new Core.Events.EventMessages())); //success
|
|
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-09-06 20:33:32 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//clear out the name
|
|
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(PublishJsonInvariant);
|
|
|
|
|
|
json["variants"].ElementAt(0)["name"] = null;
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-09-06 20:33:32 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
|
|
|
|
|
content: GetMultiPartRequestContent(JsonConvert.SerializeObject(json)),
|
|
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.BadRequest, response.Item1.StatusCode);
|
|
|
|
|
|
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(response.Item2);
|
|
|
|
|
|
Assert.AreEqual(1, display.Errors.Count());
|
|
|
|
|
|
Assert.IsTrue(display.Errors.ContainsKey("Variants[0].Name"));
|
|
|
|
|
|
//ModelState":{"Variants[0].Name":["Required"]}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public async Task PostSave_Validate_Variants_Empty_Name()
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = GetMockedContent();
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
ApiController CtrlFactory(HttpRequestMessage message, UmbracoContext umbracoContext, UmbracoHelper helper)
|
2018-09-06 20:33:32 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var contentServiceMock = Mock.Get(Current.Services.ContentService);
|
|
|
|
|
|
contentServiceMock.Setup(x => x.GetById(123)).Returns(() => content);
|
|
|
|
|
|
contentServiceMock.Setup(x => x.Save(It.IsAny<IContent>(), It.IsAny<int>(), It.IsAny<bool>()))
|
|
|
|
|
|
.Returns(new OperationResult(OperationResultType.Success, new Core.Events.EventMessages())); //success
|
|
|
|
|
|
|
|
|
|
|
|
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var controller = new ContentController(
|
|
|
|
|
|
propertyEditorCollection,
|
|
|
|
|
|
Factory.GetInstance<IGlobalSettings>(),
|
|
|
|
|
|
umbracoContext,
|
|
|
|
|
|
Factory.GetInstance<ISqlContext>(),
|
|
|
|
|
|
Factory.GetInstance<ServiceContext>(),
|
|
|
|
|
|
Factory.GetInstance<AppCaches>(),
|
|
|
|
|
|
Factory.GetInstance<IProfilingLogger>(),
|
|
|
|
|
|
Factory.GetInstance<IRuntimeState>(),
|
|
|
|
|
|
helper);
|
|
|
|
|
|
|
|
|
|
|
|
return controller;
|
2018-09-06 20:33:32 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//clear out one of the names
|
|
|
|
|
|
var json = JsonConvert.DeserializeObject<JObject>(PublishJsonVariant);
|
|
|
|
|
|
json["variants"].ElementAt(0)["name"] = null;
|
|
|
|
|
|
|
2019-02-01 15:24:07 +11:00
|
|
|
|
var runner = new TestRunner(CtrlFactory);
|
2018-09-06 20:33:32 +10:00
|
|
|
|
var response = await runner.Execute("Content", "PostSave", HttpMethod.Post,
|
|
|
|
|
|
content: GetMultiPartRequestContent(JsonConvert.SerializeObject(json)),
|
|
|
|
|
|
mediaTypeHeader: new MediaTypeWithQualityHeaderValue("multipart/form-data"),
|
|
|
|
|
|
assertOkResponse: false);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(HttpStatusCode.BadRequest, response.Item1.StatusCode);
|
|
|
|
|
|
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(response.Item2);
|
|
|
|
|
|
Assert.AreEqual(2, display.Errors.Count());
|
|
|
|
|
|
Assert.IsTrue(display.Errors.ContainsKey("Variants[0].Name"));
|
|
|
|
|
|
Assert.IsTrue(display.Errors.ContainsKey("_content_variant_en-US_"));
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
2018-09-06 20:33:32 +10:00
|
|
|
|
|
2019-01-27 01:17:32 -05:00
|
|
|
|
// TODO: There are SOOOOO many more tests we should write - a lot of them to do with validation
|
2018-09-06 20:33:32 +10:00
|
|
|
|
|
2018-08-15 17:44:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|