Skip to content

Commit

Permalink
Deletes a file when an update to the file attribute is set to null Dy…
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 10, 2024
1 parent 7be6fb0 commit fe3db51
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Core.FileStorage.Db
{
Expand All @@ -7,5 +8,6 @@ internal interface IInMemoryFileDbInternal
List<FileAttachment> GetAllFiles();
void AddFile(FileAttachment fileAttachment);
void DeleteFile(string fileId);
List<FileAttachment> GetFilesForTarget(EntityReference target);
}
}
9 changes: 9 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public void DeleteFile(string fileId)
entity[file.AttributeName] = null;
table.Replace(entity);
}

public List<FileAttachment> GetFilesForTarget(EntityReference target)
{
return _files.Values.Where
(f =>
f.Target.LogicalName.Equals(target.LogicalName) &&
f.Target.Id.Equals(target.Id))
.ToList();
}
#endregion

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions src/FakeXrmEasy.Core/XrmFakedContext.Crud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,31 @@ public void UpdateEntity(Entity e)
_service.Execute(request);
}
}

DeleteAssociatedFiles(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)
{
var associatedFiles = FileDb.GetFilesForTarget(e.ToEntityReference());

foreach (var updatedAttribute in e.Attributes.Keys)
{
if (e[updatedAttribute] == null)
{
var associatedFile = associatedFiles.FirstOrDefault(f => f.AttributeName.Equals(updatedAttribute));
if (associatedFile != null)
{
FileDb.DeleteFile(associatedFile.Id);
}
}
}
}

/// <summary>
/// Returns an entity record by logical name and primary key
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FakeXrmEasy.Core.Tests.FileStorage.Db
{
public class InMemoryFileDbInternalTests
{

}
}
60 changes: 60 additions & 0 deletions tests/FakeXrmEasy.Core.Tests/FileStorage/Db/UpdateFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using DataverseEntities;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db;
using Microsoft.Xrm.Sdk;
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
{
public class UpdateFileTests: FakeXrmEasyTestsBase
{
private const string FILE_ATTRIBUTE_NAME = "dv_file";

private readonly InMemoryDb _db;
private readonly InMemoryFileDb _fileDb;
private readonly Entity _entity;
private readonly FileAttachment _file;

public UpdateFileTests()
{
_db = (_context as XrmFakedContext).Db;
_fileDb = (_context as XrmFakedContext).FileDb;

_entity = new Entity(dv_test.EntityLogicalName)
{
Id = Guid.NewGuid(),
};

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

_entity[FILE_ATTRIBUTE_NAME] = _file.Id;
}

[Fact]
public void Should_remove_file_when_updating_file_attribute_to_null()
{
_fileDb.AddFile(_file);
_context.Initialize(_entity);

var entityToUpdate = new Entity(_entity.LogicalName)
{
Id = _entity.Id,
[FILE_ATTRIBUTE_NAME] = null
};

_service.Update(entityToUpdate);

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

0 comments on commit fe3db51

Please sign in to comment.