Skip to content

Commit

Permalink
Deletes all associated files when a record is deleted DynamicsValue/f…
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 20, 2024
1 parent e51de98 commit df0aa00
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ public void DeleteFile(string fileId)
//Set file attribute to null
var entityReference = file.Target;
var table = _db.GetTable(entityReference.LogicalName);
var entity = table.GetById(entityReference.Id);
entity[file.AttributeName] = null;
table.Replace(entity);
if (table.Contains(entityReference.Id))
{
var entity = table.GetById(entityReference.Id);
entity[file.AttributeName] = null;
table.Replace(entity);
}
}

public List<IFileAttachment> GetFilesForTarget(EntityReference target)
Expand Down
20 changes: 18 additions & 2 deletions src/FakeXrmEasy.Core/XrmFakedContext.Crud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public void UpdateEntity(Entity e)
}
}

DeleteAssociatedFiles(e);
DeleteAssociatedFilesAfterUpdate(e);
}

/// <summary>
/// Deletes any associated files to an entity that has their file attributes as null
/// </summary>
/// <param name="e"></param>
private void DeleteAssociatedFiles(Entity e)
private void DeleteAssociatedFilesAfterUpdate(Entity e)
{
var associatedFiles = FileDb.GetFilesForTarget(e.ToEntityReference());

Expand All @@ -199,6 +199,20 @@ private void DeleteAssociatedFiles(Entity e)
}
}

/// <summary>
/// Deletes any associated files after a Delete message
/// </summary>
/// <param name="er">The entity reference that was deleted</param>
private void DeleteAssociatedFiles(EntityReference er)
{
var associatedFiles = FileDb.GetFilesForTarget(er);

foreach (var associatedFile in associatedFiles)
{
FileDb.DeleteFile(associatedFile.Id);
}
}

/// <summary>
/// Returns an entity record by logical name and primary key
/// </summary>
Expand Down Expand Up @@ -338,6 +352,8 @@ public void DeleteEntity(EntityReference er)
// Entity found => remove entity
var table = Db.GetTable(er.LogicalName);
table.Remove(er.Id);

DeleteAssociatedFiles(er);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#if FAKE_XRM_EASY_9
using System;
using System.Collections.Generic;
using System.Linq;
using DataverseEntities;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Download;
using FakeXrmEasy.Extensions;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

Expand All @@ -16,9 +20,11 @@ public class DeleteFileTests: FakeXrmEasyTestsBase

private readonly InMemoryDb _db;
private readonly InMemoryFileDb _fileDb;
private readonly Entity _entity;
private readonly FileAttachment _file;
private readonly Entity _entity, _entity2;
private readonly FileAttachment _file1, _file2;

private readonly EntityMetadata _entityMetadata;

public DeleteFileTests()
{
_db = (_context as XrmFakedContext).Db;
Expand All @@ -29,7 +35,12 @@ public DeleteFileTests()
Id = Guid.NewGuid(),
};

_file = new FileAttachment()
_entity2 = new Entity(dv_test.EntityLogicalName)
{
Id = Guid.NewGuid(),
};

_file1 = new FileAttachment()
{
Id = Guid.NewGuid().ToString(),
MimeType = "application/pdf",
Expand All @@ -38,8 +49,34 @@ public DeleteFileTests()
AttributeName = FILE_ATTRIBUTE_NAME,
Content = new byte[] { 1, 2, 3, 4 }
};

_file2 = new FileAttachment()
{
Id = Guid.NewGuid().ToString(),
MimeType = "application/pdf",
FileName = "TestFile2.pdf",
Target = _entity2.ToEntityReference(),
AttributeName = FILE_ATTRIBUTE_NAME,
Content = new byte[] { 1, 2, 3, 4 }
};

_entity[FILE_ATTRIBUTE_NAME] = _file.Id;
_entity[FILE_ATTRIBUTE_NAME] = _file1.Id;
_entity2[FILE_ATTRIBUTE_NAME] = _file2.Id;

var fileAttributeMetadata = new FileAttributeMetadata()
{
LogicalName = "dv_file"
};
fileAttributeMetadata.MaxSizeInKB = 1; //1 KB

_entityMetadata = new EntityMetadata()
{
LogicalName = dv_test.EntityLogicalName
};
_entityMetadata.SetAttributeCollection(new List<AttributeMetadata>()
{
fileAttributeMetadata
});
}

[Fact]
Expand All @@ -57,28 +94,46 @@ public void Should_throw_exception_when_a_file_doesnt_exists()
[Fact]
public void Should_delete_an_existing_file()
{
_db.AddEntityRecord(_entity);
_fileDb.AddFile(_file);
_context.InitializeMetadata(_entityMetadata);
_context.Initialize(_entity);
_context.InitializeFiles(new [] {_file1 });

_fileDb.DeleteFile(_file.Id);
_fileDb.DeleteFile(_file1.Id);

Assert.Empty(_fileDb.GetAllFiles());

var entityAfter = _db.GetTable(_entity.LogicalName).GetById(_entity.Id);
Assert.Null(entityAfter[_file.AttributeName]);
Assert.Null(entityAfter[_file1.AttributeName]);
}

/*

[Fact]
public void Should_delete_file_when_the_associated_record_is_deleted()
{
_context.InitializeMetadata(_entityMetadata);
_context.Initialize(_entity);
_fileDb.AddFile(_file);
_context.InitializeFiles(new [] {_file1 });

_service.Delete(_entity.LogicalName, _entity.Id);

Assert.Empty(_fileDb.GetAllFiles());
}
*/

[Fact]
public void Should_not_delete_file_that_is_not_associated_with_the_deleted_record()
{
_context.InitializeMetadata(_entityMetadata);
_context.Initialize(new [] {_entity, _entity2 });
_context.InitializeFiles(new [] { _file1, _file2 });

_service.Delete(_entity.LogicalName, _entity.Id);

Assert.Single(_fileDb.GetAllFiles());

var existingFile = _fileDb.GetAllFiles().FirstOrDefault();
Assert.Equal(_file2.Id, existingFile.Id);
}

}
}
}
#endif

0 comments on commit df0aa00

Please sign in to comment.