Browse Source

Fix referrers.

pull/671/head
Sebastian 5 years ago
parent
commit
1b1e14e026
  1. 2
      backend/src/Squidex.Domain.Apps.Entities/Assets/Commands/BulkUpdateAssets.cs
  2. 8
      backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetsBulkUpdateCommandMiddleware.cs
  3. 2
      backend/src/Squidex.Domain.Apps.Entities/Contents/Commands/BulkUpdateContents.cs
  4. 20
      backend/src/Squidex.Domain.Apps.Entities/Contents/DomainObject/ContentsBulkUpdateCommandMiddleware.cs
  5. 5
      backend/src/Squidex/Areas/Api/Controllers/Assets/Models/BulkUpdateAssetsDto.cs
  6. 2
      backend/src/Squidex/Areas/Api/Controllers/Contents/Models/BulkUpdateContentsDto.cs
  7. 162
      backend/tools/TestSuite/TestSuite.ApiTests/ContentReferencesTests.cs
  8. 4
      backend/tools/TestSuite/TestSuite.Shared/TestSuite.Shared.csproj

2
backend/src/Squidex.Domain.Apps.Entities/Assets/Commands/BulkUpdateAssets.cs

@ -13,6 +13,8 @@ namespace Squidex.Domain.Apps.Entities.Assets.Commands
{
public NamedId<DomainId> AppId { get; set; }
public bool CheckReferrers { get; set; }
public BulkUpdateJob[]? Jobs { get; set; }
}
}

8
backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetsBulkUpdateCommandMiddleware.cs

@ -32,7 +32,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.DomainObject
private sealed record BulkTask(
ICommandBus Bus,
int JobIndex,
BulkUpdateJob Job,
BulkUpdateJob CommandJob,
BulkUpdateAssets Command,
ConcurrentBag<BulkUpdateResultItem> Results
)
@ -136,7 +136,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.DomainObject
private BulkTaskCommand? CreateCommand(BulkTask task)
{
var id = task.Job.Id;
var id = task.CommandJob.Id;
try
{
@ -161,7 +161,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.DomainObject
private AssetCommand CreateCommandCore(BulkTask task)
{
var job = task.Job;
var job = task.CommandJob;
switch (job.Type)
{
@ -197,7 +197,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.DomainObject
private void EnrichAndCheckPermission<T>(BulkTask task, T command, string permissionId) where T : AssetCommand
{
SimpleMapper.Map(task.Command, command);
SimpleMapper.Map(task.Job, command);
SimpleMapper.Map(task.CommandJob, command);
if (!contextProvider.Context.Allows(permissionId))
{

2
backend/src/Squidex.Domain.Apps.Entities/Contents/Commands/BulkUpdateContents.cs

@ -23,6 +23,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.Commands
public bool DoNotScript { get; set; }
public bool CheckReferrers { get; set; }
public bool OptimizeValidation { get; set; }
public BulkUpdateJob[]? Jobs { get; set; }

20
backend/src/Squidex.Domain.Apps.Entities/Contents/DomainObject/ContentsBulkUpdateCommandMiddleware.cs

@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.DomainObject
ICommandBus Bus,
string Schema,
int JobIndex,
BulkUpdateJob Job,
BulkUpdateJob CommandJob,
BulkUpdateContents Command,
ConcurrentBag<BulkUpdateResultItem> Results
)
@ -190,7 +190,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.DomainObject
private async Task<ContentCommand> CreateCommandAsync(BulkTask task)
{
var job = task.Job;
var job = task.CommandJob;
switch (job.Type)
{
@ -258,9 +258,9 @@ namespace Squidex.Domain.Apps.Entities.Contents.DomainObject
private async Task EnrichAndCheckPermissionAsync<T>(BulkTask task, T command, string permissionId) where T : ContentCommand
{
SimpleMapper.Map(task.Command, command);
SimpleMapper.Map(task.Job, command);
SimpleMapper.Map(task.CommandJob, command);
if (!string.IsNullOrWhiteSpace(task.Job.Schema))
if (!string.IsNullOrWhiteSpace(task.CommandJob.Schema))
{
var schema = await contentQuery.GetSchemaOrThrowAsync(contextProvider.Context, task.Schema);
@ -277,20 +277,20 @@ namespace Squidex.Domain.Apps.Entities.Contents.DomainObject
private async Task<DomainId[]> FindIdAsync(BulkTask task)
{
var id = task.Job.Id;
var id = task.CommandJob.Id;
if (id != null)
{
return new[] { id.Value };
}
if (task.Job.Query != null)
if (task.CommandJob.Query != null)
{
task.Job.Query.Take = task.Job.ExpectedCount;
task.CommandJob.Query.Take = task.CommandJob.ExpectedCount;
var existing = await contentQuery.QueryAsync(contextProvider.Context, task.Schema, Q.Empty.WithJsonQuery(task.Job.Query));
var existing = await contentQuery.QueryAsync(contextProvider.Context, task.Schema, Q.Empty.WithJsonQuery(task.CommandJob.Query));
if (existing.Total > task.Job.ExpectedCount)
if (existing.Total > task.CommandJob.ExpectedCount)
{
throw new DomainException(T.Get("contents.bulkInsertQueryNotUnique"));
}
@ -298,7 +298,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.DomainObject
return existing.Select(x => x.Id).ToArray();
}
if (task.Job.Type == BulkUpdateContentType.Create || task.Job.Type == BulkUpdateContentType.Upsert)
if (task.CommandJob.Type == BulkUpdateContentType.Create || task.CommandJob.Type == BulkUpdateContentType.Upsert)
{
return new[] { DomainId.NewGuid() };
}

5
backend/src/Squidex/Areas/Api/Controllers/Assets/Models/BulkUpdateAssetsDto.cs

@ -20,6 +20,11 @@ namespace Squidex.Areas.Api.Controllers.Assets.Models
[LocalizedRequired]
public BulkUpdateAssetsJobDto[]? Jobs { get; set; }
/// <summary>
/// True to check referrers of deleted assets.
/// </summary>
public bool CheckReferrers { get; set; }
public BulkUpdateAssets ToCommand()
{
var result = SimpleMapper.Map(this, new BulkUpdateAssets());

2
backend/src/Squidex/Areas/Api/Controllers/Contents/Models/BulkUpdateContentsDto.cs

@ -46,7 +46,7 @@ namespace Squidex.Areas.Api.Controllers.Contents.Models
public bool DoNotValidateWorkflow { get; set; }
/// <summary>
/// True to check referrers of this content.
/// True to check referrers of deleted contents.
/// </summary>
public bool CheckReferrers { get; set; }

162
backend/tools/TestSuite/TestSuite.ApiTests/ContentReferencesTests.cs

@ -5,7 +5,9 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Threading.Tasks;
using Squidex.ClientLibrary;
using TestSuite.Fixtures;
using TestSuite.Model;
using Xunit;
@ -36,9 +38,7 @@ namespace TestSuite.ApiTests
// STEP 2: Create a content with a reference.
var dataB = new TestEntityWithReferencesData { References = new[] { contentA_1.Id } };
var contentB_1 = await _.Contents.CreateAsync(dataB);
await _.Contents.ChangeStatusAsync(contentB_1.Id, "Published");
var contentB_1 = await _.Contents.CreateAsync(dataB, true);
// STEP 3: Query new item
@ -56,5 +56,161 @@ namespace TestSuite.ApiTests
Assert.Equal(new string[] { contentA_1.Id }, contentB_3.Data.References);
}
[Fact]
public async Task Should_not_delete_when_referenced()
{
// STEP 1: Create a referenced content.
var dataA = new TestEntityWithReferencesData();
var contentA_1 = await _.Contents.CreateAsync(dataA, true);
// STEP 2: Create a content with a reference.
var dataB = new TestEntityWithReferencesData { References = new[] { contentA_1.Id } };
var contentB_1 = await _.Contents.CreateAsync(dataB, true);
// STEP 3: Try to delete with referrer check.
await Assert.ThrowsAsync<SquidexException>(() => _.Contents.DeleteAsync(contentA_1.Id, checkReferrers: true));
// STEP 4: Delete without referrer check
await _.Contents.DeleteAsync(contentA_1.Id, checkReferrers: false);
}
[Fact]
public async Task Should_not_unpublish_when_referenced()
{
// STEP 1: Create a published referenced content.
var dataA = new TestEntityWithReferencesData();
var contentA_1 = await _.Contents.CreateAsync(dataA, true);
// STEP 2: Create a content with a reference.
var dataB = new TestEntityWithReferencesData { References = new[] { contentA_1.Id } };
var contentB_1 = await _.Contents.CreateAsync(dataB, true);
// STEP 3: Try to delete with referrer check.
await Assert.ThrowsAsync<SquidexException>(() => _.Contents.ChangeStatusAsync(contentA_1.Id, new ChangeStatus
{
Status = "Draft",
CheckReferrers = true
}));
// STEP 4: Delete without referrer check
await _.Contents.ChangeStatusAsync(contentA_1.Id, new ChangeStatus
{
Status = "Draft",
CheckReferrers = false
});
}
[Fact]
public async Task Should_not_delete_with_bulk_when_referenced()
{
// STEP 1: Create a referenced content.
var dataA = new TestEntityWithReferencesData();
var contentA_1 = await _.Contents.CreateAsync(dataA, true);
// STEP 2: Create a content with a reference.
var dataB = new TestEntityWithReferencesData { References = new[] { contentA_1.Id } };
await _.Contents.CreateAsync(dataB, true);
// STEP 3: Try to delete with referrer check.
var result1 = await _.Contents.BulkUpdateAsync(new BulkUpdate
{
Jobs = new List<BulkUpdateJob>
{
new BulkUpdateJob
{
Id = contentA_1.Id,
Type = BulkUpdateType.Delete,
Status = "Draft"
}
},
CheckReferrers = true
});
Assert.NotNull(result1[0].Error);
// STEP 4: Delete without referrer check
var result2 = await _.Contents.BulkUpdateAsync(new BulkUpdate
{
Jobs = new List<BulkUpdateJob>
{
new BulkUpdateJob
{
Id = contentA_1.Id,
Type = BulkUpdateType.Delete,
Status = "Draft"
}
},
CheckReferrers = false
});
Assert.Null(result2[0].Error);
}
[Fact]
public async Task Should_not_unpublish_with_bulk_when_referenced()
{
// STEP 1: Create a published referenced content.
var dataA = new TestEntityWithReferencesData();
var contentA_1 = await _.Contents.CreateAsync(dataA, true);
// STEP 2: Create a published content with a reference.
var dataB = new TestEntityWithReferencesData { References = new[] { contentA_1.Id } };
await _.Contents.CreateAsync(dataB, true);
// STEP 3: Try to delete with referrer check.
var result1 = await _.Contents.BulkUpdateAsync(new BulkUpdate
{
Jobs = new List<BulkUpdateJob>
{
new BulkUpdateJob
{
Id = contentA_1.Id,
Type = BulkUpdateType.ChangeStatus,
Status = "Draft"
}
},
CheckReferrers = true
});
Assert.NotNull(result1[0].Error);
// STEP 4: Delete without referrer check
var result2 = await _.Contents.BulkUpdateAsync(new BulkUpdate
{
Jobs = new List<BulkUpdateJob>
{
new BulkUpdateJob
{
Id = contentA_1.Id,
Type = BulkUpdateType.ChangeStatus,
Status = "Draft"
}
},
CheckReferrers = false
});
Assert.Null(result2[0].Error);
}
}
}

4
backend/tools/TestSuite/TestSuite.Shared/TestSuite.Shared.csproj

@ -5,13 +5,13 @@
<RootNamespace>TestSuite</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fody" Version="6.3.0">
<PackageReference Include="Fody" Version="6.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Lazy.Fody" Version="1.9.0" PrivateAssets="all" />
<PackageReference Include="RefactoringEssentials" Version="5.6.0" PrivateAssets="all" />
<PackageReference Include="Squidex.ClientLibrary" Version="6.16.0" />
<PackageReference Include="Squidex.ClientLibrary" Version="6.19.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

Loading…
Cancel
Save