Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions MailMerge.Tests/FunctionalSpecs/DomainExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using TestBase;

namespace MailMerge.Tests.FunctionalSpecs
Expand Down Expand Up @@ -34,5 +36,32 @@ public static (string asText, string asXml) AsWordDocumentMainPartTextAndXml(thi
return (asText, asXml);

}

public static (string allHeaderText, string allHeaderXml) GetCombinedHeadersTextAndXml(this Stream documentStream)
{
var sbHeaderText = new StringBuilder();
var sbHeaderXml = new StringBuilder();

if (documentStream.CanSeek)
{
documentStream.Position = 0;
}

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(documentStream, isEditable: false))
{
if (wordDoc.MainDocumentPart != null)
{
foreach (HeaderPart headerPart in wordDoc.MainDocumentPart.HeaderParts)
{
if (headerPart.Header != null)
{
sbHeaderText.Append(headerPart.Header.InnerText);
sbHeaderXml.Append(headerPart.Header.OuterXml);
}
}
}
}
return (sbHeaderText.ToString(), sbHeaderXml.ToString());
}
}
}
19 changes: 16 additions & 3 deletions MailMerge.Tests/FunctionalSpecs/GivenFldSimpleMergeField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class GivenFldSimpleMergeField
{
const string TestDocDir = "TestDocuments";
const string DocWithSimpleMergeFieldDocx = "DocWithSimpleMergeField.docx";
const string DocWithFieldInHeaderDocx = "DocWithFieldInHeader.docx";
MailMerger sut;

static Dictionary<string, string> SingleLine = new Dictionary<string, string>
Expand All @@ -26,9 +27,17 @@ public class GivenFldSimpleMergeField
{
{"SimpleMergeField","SimpleMergeField Was\nReplaced\nwith multiple lines\ndelimited by newlines/"}
};


static readonly Dictionary<string, string> HeaderAndBodyFields = new Dictionary<string, string>
{
{ "FieldInHeader", "Yes, you can!" }, // Expected in header
{ "FirstName", "A replaced first name field" }, // Expected in body
{ "LastName", "A replaced last name field" } // Expected in body
};

[TestCase(DocWithSimpleMergeFieldDocx, nameof(SingleLine))]
[TestCase(DocWithSimpleMergeFieldDocx, nameof(MultiLine))]
[TestCase(DocWithFieldInHeaderDocx, nameof(HeaderAndBodyFields))]
public void Returns_TheDocumentWithMergeFieldsReplaced(string source, string sourceFieldsSource)
{
source = Path.Combine(TestDocDir, source);
Expand All @@ -45,6 +54,9 @@ public void Returns_TheDocumentWithMergeFieldsReplaced(string source, string sou

var (outputText, outputXml) = output.AsWordDocumentMainPartTextAndXml();

var (headerPartText, headerXml) = output.GetCombinedHeadersTextAndXml();

var combinedDocumentText = outputText + " " + headerPartText;
using (var outFile = new FileInfo(source.Replace(".", $" {sourceFieldsSource} Output.")).OpenWrite())
{
output.Position = 0;
Expand All @@ -53,15 +65,16 @@ public void Returns_TheDocumentWithMergeFieldsReplaced(string source, string sou

if (exceptions.InnerExceptions.Any()) { throw exceptions; }

var combinedOutputXml = outputXml + " " + headerXml;

sourceFields
.Values
.Cast<string>()
.ShouldHaveBeenSplitOnNewLinesAndEachLineInserted(outputXml);
.ShouldHaveBeenSplitOnNewLinesAndEachLineInserted(combinedOutputXml);

sourceFields
.Keys
.ShouldAll(k => outputText.ShouldNotContain("«" + k + "»"));
.ShouldAll(k => combinedDocumentText.ShouldNotContain("«" + k + "»"));

}
finally{ output?.Dispose(); }
Expand Down
Loading