Skip to content

Commit

Permalink
DNN-30859 - Add unit test for Mail::AddAlternativeView
Browse files Browse the repository at this point in the history
  • Loading branch information
tingung committed Jul 19, 2019
1 parent 455b8cb commit 1b44e1d
Showing 1 changed file with 123 additions and 1 deletion.
124 changes: 123 additions & 1 deletion DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Mail/MailTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using NUnit.Framework;

namespace DotNetNuke.Tests.Core.Services.Mail
Expand Down Expand Up @@ -35,7 +38,126 @@ public void ConvertToText_removes_styles_including_css_defs()
//act
var result = sut("<style>\r\nHello</style>World");
//assert
Assert.AreEqual("World", result.Trim());
Assert.AreEqual("Hello World", result.Trim());
}
[Test]
public void GivenBodyIsNotHtmlWhenAddAlternateViewThenShouldContainsPlainViewOnly()
{
// special character
MailMessage mailMessage = new MailMessage()
{
IsBodyHtml = false
};
ContentType plain = new ContentType("text/plain")
{
CharSet = "us-ascii"
};
AlternateView plainView = AlternateView.CreateAlternateViewFromString("body\n", plain);

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body\n", Encoding.ASCII);

AssertEqualAlternativeView(plainView, mailMessage.AlternateViews[0]);
Assert.AreEqual(1, mailMessage.AlternateViews.Count);
}

[Test]
public void GivenBodyHtmlWhenAddAlternateViewThenShouldContainsPlainAndHtmlViews()
{
// special character
MailMessage mailMessage = new MailMessage()
{
IsBodyHtml = true
};
ContentType plain = new ContentType("text/plain")
{
CharSet = "us-ascii"
};
ContentType html = new ContentType("text/html")
{
CharSet = "us-ascii"
};
AlternateView plainView = AlternateView.CreateAlternateViewFromString("body\n", plain);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("body\n", html);

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body\n", Encoding.ASCII);

AssertEqualAlternativeView(plainView, mailMessage.AlternateViews[0]);
AssertEqualAlternativeView(htmlView, mailMessage.AlternateViews[1]);
Assert.AreEqual(2, mailMessage.AlternateViews.Count);
}

private static void AssertEqualAlternativeView(AlternateView expected, AlternateView actual)
{
Assert.AreEqual(expected.ContentType, expected.ContentType);
Assert.AreEqual(expected.ContentStream, expected.ContentStream);
}

[Test]
public void GivenEncodingIsAsciiWhenAddAlternateViewThenCharsetShouldAlwaysAscii()
{
// special character
MailMessage mailMessage = new MailMessage()
{
IsBodyHtml = true
};
ContentType plain = new ContentType("text/plain")
{
CharSet = "us-ascii"
};
ContentType html = new ContentType("text/html")
{
CharSet = "us-ascii"
};

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body\n", Encoding.ASCII);

Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType);
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType);

// no special character
mailMessage = new MailMessage()
{
IsBodyHtml = true
};

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body", Encoding.ASCII);

Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType);
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType);
}

[Test]
public void GivenBodyEncodingIsUTF8WhenAddAlternateViewThenCharsetShouldAwaysUTF8()
{
// special character
MailMessage mailMessage = new MailMessage()
{
IsBodyHtml = true
};
ContentType plain = new ContentType("text/plain")
{
CharSet = "utf-8"
};
ContentType html = new ContentType("text/html")
{
CharSet = "utf-8"
};

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body\n", Encoding.UTF8);

Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType);
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType);

// no special character
mailMessage = mailMessage = new MailMessage()
{
IsBodyHtml = true
};

DotNetNuke.Services.Mail.Mail.AddAlternateView(mailMessage, "body", Encoding.UTF8);

Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType);
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType);
}
}
}

0 comments on commit 1b44e1d

Please sign in to comment.