Browse Source

Merge branch 'master' of github.com:Squidex/squidex

# Conflicts:
#	backend/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppLanguages.cs
pull/479/head
Sebastian 6 years ago
parent
commit
a1946817fa
  1. 10
      backend/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppLanguages.cs
  2. 2
      backend/src/Squidex.Domain.Apps.Events/Apps/AppLanguageUpdated.cs
  3. 2
      backend/src/Squidex.Infrastructure/Commands/DomainObjectBase.cs
  4. 55
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppLanguagesTests.cs

10
backend/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppLanguages.cs

@ -73,11 +73,19 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
EnsureConfigExists(languages, language); EnsureConfigExists(languages, language);
if ((languages.IsMaster(language) || command.IsMaster) && command.IsOptional) if (languages.IsMaster(language) || command.IsMaster)
{
if (command.IsOptional)
{ {
e("Master language cannot be made optional.", nameof(command.IsMaster)); e("Master language cannot be made optional.", nameof(command.IsMaster));
} }
if (command.Fallback?.Count > 0)
{
e("Master language cannot have fallback languages.", nameof(command.Fallback));
}
}
if (command.Fallback == null) if (command.Fallback == null)
{ {
return; return;

2
backend/src/Squidex.Domain.Apps.Events/Apps/AppLanguageUpdated.cs

@ -20,6 +20,6 @@ namespace Squidex.Domain.Apps.Events.Apps
public bool IsMaster { get; set; } public bool IsMaster { get; set; }
public List<Language> Fallback { get; set; } public List<Language>? Fallback { get; set; }
} }
} }

2
backend/src/Squidex.Infrastructure/Commands/DomainObjectBase.cs

@ -149,7 +149,7 @@ namespace Squidex.Infrastructure.Commands
throw new DomainObjectVersionException(id.ToString(), GetType(), Version, command.ExpectedVersion); throw new DomainObjectVersionException(id.ToString(), GetType(), Version, command.ExpectedVersion);
} }
if (isUpdate == true && Version < 0) if (isUpdate && Version < 0)
{ {
throw new DomainObjectNotFoundException(id.ToString(), GetType()); throw new DomainObjectNotFoundException(id.ToString(), GetType());
} }

55
backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppLanguagesTests.cs

@ -13,20 +13,18 @@ using Squidex.Infrastructure;
using Squidex.Infrastructure.Validation; using Squidex.Infrastructure.Validation;
using Xunit; using Xunit;
#pragma warning disable SA1310 // Field names must not contain underscore
namespace Squidex.Domain.Apps.Entities.Apps.Guards namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
public class GuardAppLanguagesTests public class GuardAppLanguagesTests
{ {
private readonly LanguagesConfig languages_0 = LanguagesConfig.English; private readonly LanguagesConfig languages = LanguagesConfig.English.Set(Language.DE);
[Fact] [Fact]
public void CanAddLanguage_should_throw_exception_if_language_is_null() public void CanAddLanguage_should_throw_exception_if_language_is_null()
{ {
var command = new AddLanguage(); var command = new AddLanguage();
ValidationAssert.Throws(() => GuardAppLanguages.CanAdd(languages_0, command), ValidationAssert.Throws(() => GuardAppLanguages.CanAdd(languages, command),
new ValidationError("Language code is required.", "Language")); new ValidationError("Language code is required.", "Language"));
} }
@ -35,16 +33,16 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new AddLanguage { Language = Language.EN }; var command = new AddLanguage { Language = Language.EN };
ValidationAssert.Throws(() => GuardAppLanguages.CanAdd(languages_0, command), ValidationAssert.Throws(() => GuardAppLanguages.CanAdd(languages, command),
new ValidationError("Language has already been added.")); new ValidationError("Language has already been added."));
} }
[Fact] [Fact]
public void CanAddLanguage_should_not_throw_exception_if_language_valid() public void CanAddLanguage_should_not_throw_exception_if_language_valid()
{ {
var command = new AddLanguage { Language = Language.DE }; var command = new AddLanguage { Language = Language.IT };
GuardAppLanguages.CanAdd(languages_0, command); GuardAppLanguages.CanAdd(languages, command);
} }
[Fact] [Fact]
@ -52,16 +50,16 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new RemoveLanguage(); var command = new RemoveLanguage();
ValidationAssert.Throws(() => GuardAppLanguages.CanRemove(languages_0, command), ValidationAssert.Throws(() => GuardAppLanguages.CanRemove(languages, command),
new ValidationError("Language code is required.", "Language")); new ValidationError("Language code is required.", "Language"));
} }
[Fact] [Fact]
public void CanRemoveLanguage_should_throw_exception_if_language_not_found() public void CanRemoveLanguage_should_throw_exception_if_language_not_found()
{ {
var command = new RemoveLanguage { Language = Language.DE }; var command = new RemoveLanguage { Language = Language.IT };
Assert.Throws<DomainObjectNotFoundException>(() => GuardAppLanguages.CanRemove(languages_0, command)); Assert.Throws<DomainObjectNotFoundException>(() => GuardAppLanguages.CanRemove(languages, command));
} }
[Fact] [Fact]
@ -69,7 +67,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new RemoveLanguage { Language = Language.EN }; var command = new RemoveLanguage { Language = Language.EN };
ValidationAssert.Throws(() => GuardAppLanguages.CanRemove(languages_0, command), ValidationAssert.Throws(() => GuardAppLanguages.CanRemove(languages, command),
new ValidationError("Master language cannot be removed.")); new ValidationError("Master language cannot be removed."));
} }
@ -78,9 +76,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new RemoveLanguage { Language = Language.DE }; var command = new RemoveLanguage { Language = Language.DE };
var languages_1 = languages_0.Set(Language.DE); GuardAppLanguages.CanRemove(languages, command);
GuardAppLanguages.CanRemove(languages_1, command);
} }
[Fact] [Fact]
@ -88,9 +84,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new UpdateLanguage(); var command = new UpdateLanguage();
var languages_1 = languages_0.Set(Language.DE); ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages, command),
ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages_1, command),
new ValidationError("Language code is required.", "Language")); new ValidationError("Language code is required.", "Language"));
} }
@ -99,20 +93,25 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new UpdateLanguage { Language = Language.EN, IsOptional = true }; var command = new UpdateLanguage { Language = Language.EN, IsOptional = true };
var languages_1 = languages_0.Set(Language.DE); ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages, command),
ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages_1, command),
new ValidationError("Master language cannot be made optional.", "IsMaster")); new ValidationError("Master language cannot be made optional.", "IsMaster"));
} }
[Fact] [Fact]
public void CanUpdateLanguage_should_throw_exception_if_language_has_invalid_fallback() public void CanUpdateLanguage_should_throw_exception_if_fallback_language_defined_and_master()
{ {
var command = new UpdateLanguage { Language = Language.EN, Fallback = new List<Language> { Language.IT } }; var command = new UpdateLanguage { Language = Language.EN, Fallback = new List<Language> { Language.DE } };
var languages_1 = languages_0.Set(Language.DE); ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages, command),
new ValidationError("Master language cannot have fallback languages.", "Fallback"));
}
ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages_1, command), [Fact]
public void CanUpdateLanguage_should_throw_exception_if_language_has_invalid_fallback()
{
var command = new UpdateLanguage { Language = Language.DE, Fallback = new List<Language> { Language.IT } };
ValidationAssert.Throws(() => GuardAppLanguages.CanUpdate(languages, command),
new ValidationError("App does not have fallback language 'Italian'.", "Fallback")); new ValidationError("App does not have fallback language 'Italian'.", "Fallback"));
} }
@ -121,9 +120,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new UpdateLanguage { Language = Language.IT }; var command = new UpdateLanguage { Language = Language.IT };
var languages_1 = languages_0.Set(Language.DE); Assert.Throws<DomainObjectNotFoundException>(() => GuardAppLanguages.CanUpdate(languages, command));
Assert.Throws<DomainObjectNotFoundException>(() => GuardAppLanguages.CanUpdate(languages_1, command));
} }
[Fact] [Fact]
@ -131,9 +128,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
{ {
var command = new UpdateLanguage { Language = Language.DE, Fallback = new List<Language> { Language.EN } }; var command = new UpdateLanguage { Language = Language.DE, Fallback = new List<Language> { Language.EN } };
var languages_1 = languages_0.Set(Language.DE); GuardAppLanguages.CanUpdate(languages, command);
GuardAppLanguages.CanUpdate(languages_1, command);
} }
} }
} }

Loading…
Cancel
Save