forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add back alternate views for core mail provider
This functionality was inexplicably removed in b3e066f dnnsoftware#4187 Includes fixes originally from dnnsoftware#2900 (1b44e1d) Fixes dnnsoftware#2899
- Loading branch information
Showing
2 changed files
with
182 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 161 additions & 45 deletions
206
DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Mail/MailTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,161 @@ | ||
// | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
// | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace DotNetNuke.Tests.Core.Services.Mail | ||
{ | ||
[TestFixture] | ||
public class MailTests | ||
{ | ||
[Test] | ||
public void ConvertToText_returns_the_input_for_simple_strings() | ||
{ | ||
//arrange | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
//act | ||
var result = sut("Hello World"); | ||
//assert | ||
Assert.AreEqual("Hello World", result); | ||
} | ||
[Test] | ||
public void ConvertToText_removes_tags() | ||
{ | ||
//arrange | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
//act | ||
var result = sut("<div class=\"x\"><p>Hello World</p></div>"); | ||
//assert | ||
Assert.AreEqual("Hello World", result.Trim()); | ||
} | ||
|
||
[Test] | ||
public void ConvertToText_removes_styles_including_css_defs() | ||
{ | ||
//arrange | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
//act | ||
var result = sut("<style>\r\nHello</style>World"); | ||
//assert | ||
Assert.AreEqual("World", result.Trim()); | ||
} | ||
} | ||
} | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information | ||
namespace DotNetNuke.Tests.Core.Services.Mail | ||
{ | ||
using System; | ||
using System.Net.Mail; | ||
using System.Net.Mime; | ||
using System.Text; | ||
|
||
using DotNetNuke.Services.Mail; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class MailTests | ||
{ | ||
[Test] | ||
public void ConvertToText_returns_the_input_for_simple_strings() | ||
{ | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
var result = sut("Hello World"); | ||
Assert.AreEqual("Hello World", result); | ||
} | ||
|
||
[Test] | ||
public void ConvertToText_removes_tags() | ||
{ | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
var result = sut("<div class=\"x\"><p>Hello World</p></div>"); | ||
Assert.AreEqual("Hello World", result.Trim()); | ||
} | ||
|
||
[Test] | ||
public void ConvertToText_removes_styles_including_css_defs() | ||
{ | ||
Func<string, string> sut = DotNetNuke.Services.Mail.Mail.ConvertToText; | ||
var result = sut("<style>\r\nHello</style>World"); | ||
Assert.AreEqual("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); | ||
|
||
CoreMailProvider.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); | ||
|
||
CoreMailProvider.AddAlternateView(mailMessage, "body\n", Encoding.ASCII); | ||
|
||
AssertEqualAlternativeView(plainView, mailMessage.AlternateViews[0]); | ||
AssertEqualAlternativeView(htmlView, mailMessage.AlternateViews[1]); | ||
Assert.AreEqual(2, mailMessage.AlternateViews.Count); | ||
} | ||
|
||
[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" | ||
}; | ||
|
||
CoreMailProvider.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 | ||
}; | ||
|
||
CoreMailProvider.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" | ||
}; | ||
|
||
CoreMailProvider.AddAlternateView(mailMessage, "body\n", Encoding.UTF8); | ||
|
||
Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType); | ||
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType); | ||
|
||
// no special character | ||
mailMessage = new MailMessage() | ||
{ | ||
IsBodyHtml = true | ||
}; | ||
|
||
CoreMailProvider.AddAlternateView(mailMessage, "body", Encoding.UTF8); | ||
|
||
Assert.AreEqual(plain, mailMessage.AlternateViews[0].ContentType); | ||
Assert.AreEqual(html, mailMessage.AlternateViews[1].ContentType); | ||
} | ||
|
||
private static void AssertEqualAlternativeView(AlternateView expected, AlternateView actual) | ||
{ | ||
Assert.AreEqual(expected.ContentType, actual.ContentType); | ||
Assert.AreEqual(expected.ContentStream, actual.ContentStream); | ||
} | ||
} | ||
} |