Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Core/Variants/ContentVariantAllowedActionTests.cs
Nikolaj Geisle 93da4371e3 V13: Rework attempt pattern to use userkey (#13964)
* Add default super user key to migrations

* Start refactoring all interfaces signatures with ids

* Refactor datatype service to use userKey pattern instead

* Refactor ContentEditingService to use userkeys

* Refactor services to userKey

* Refactor more services to use userkey instead of id

* Refactor RelationService to use userKeys

* Refactor template service to use keys instead of ids

* Refactor fileservice to use keys instead of ids

* Refactor LocalizationService to use keys instead of ids

* Refactor PackagingService to use keys instead of ids

* Refactor TemplateController to use current user keys

* Refactor DataTypeContainerService.cs

* Refactor DataTypeService to use keys instead of ids

* Fix up tests

* Fix up media editing service to use userkey instead of ID

* Update service ctor to avoid ambigious ctors

* refactor DataTypeService

* Refactor DataTypeService to not have a default value for parentKey

* Apply suggestions from code review

Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>

* Update comment

* Add suppression file

* Add backoffice CompatibilitySuppressions

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
Co-authored-by: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com>
2023-03-21 12:41:20 +01:00

88 lines
3.2 KiB
C#

using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Actions;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Integration.TestServerTest;
using Language = Umbraco.Cms.Core.Models.Language;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Extensions;
[TestFixture]
public class ContentVariantAllowedActionTests : UmbracoTestServerTestBase
{
private const string UsIso = "en-US";
private const string DkIso = "da-DK";
private ILanguageService LanguageService => GetRequiredService<ILanguageService>();
private IUserService UserService => GetRequiredService<IUserService>();
private IUmbracoMapper UmbracoMapper => GetRequiredService<IUmbracoMapper>();
[SetUp]
public async Task SetUpTestDate()
{
var dk = new Language(DkIso, "Danish");
await LanguageService.CreateAsync(dk, Constants.Security.SuperUserKey);
}
[Test]
public async Task CanCheckIfUserHasAccessToLanguage()
{
// setup user groups
var user = UserBuilder.CreateUser();
UserService.Save(user);
var userGroup = UserGroupBuilder.CreateUserGroup();
var languageId = (await LanguageService.GetAsync(DkIso))?.Id;
userGroup.AddAllowedLanguage(languageId!.Value);
UserService.Save(userGroup, new []{ user.Id});
var currentUser = UserService.GetUserById(user.Id);
var result = CreateContent(currentUser);
var danishVariant = result.Variants.FirstOrDefault(x => x.Language!.IsoCode is DkIso);
var usVariant = result.Variants.FirstOrDefault(x => x.Language!.IsoCode is UsIso);
// Right now we duplicate allowedActions if you have access, this should be changed
// when we implement granular permissions for languages
Assert.AreEqual(danishVariant!.AllowedActions, result.AllowedActions);
Assert.AreEqual(usVariant!.AllowedActions, new [] { ActionBrowse.ActionLetter.ToString() });
}
private ContentItemDisplay CreateContent(IUser user)
{
var contentTypeService = GetRequiredService<IContentTypeService>();
var contentType = new ContentTypeBuilder().WithContentVariation(ContentVariation.Culture).Build();
contentTypeService.Save(contentType);
var rootNode = new ContentBuilder()
.WithoutIdentity()
.WithContentType(contentType)
.WithCultureName(UsIso, "Root")
.WithCultureName(DkIso, "Rod")
.Build();
var contentService = GetRequiredService<IContentService>();
contentService.SaveAndPublish(rootNode);
ContentItemDisplay? display = UmbracoMapper.Map<ContentItemDisplay>(rootNode, context =>
{
context.Items["CurrentUser"] = user;
});
if (display is not null)
{
display.AllowPreview = display.AllowPreview && rootNode?.Trashed == false &&
rootNode.ContentType.IsElement == false;
}
return display;
}
}