Skip to content
This repository has been archived by the owner on Oct 18, 2018. It is now read-only.

Commit

Permalink
Refactorings. Yay! The static evil that was ObjectProvider.Instance()…
Browse files Browse the repository at this point in the history
… is dead!
  • Loading branch information
haacked committed Jun 4, 2011
1 parent 1685db0 commit b8ac067
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 238 deletions.
12 changes: 0 additions & 12 deletions src/Subtext.Framework/Providers/ObjectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using Subtext.Extensibility.Interfaces;
using Subtext.Framework.Components;
using Subtext.Framework.Configuration;
using Subtext.Framework.Data;

namespace Subtext.Framework.Providers
{
Expand All @@ -32,17 +31,6 @@ namespace Subtext.Framework.Providers
/// </summary>
public abstract class ObjectProvider : ProviderBase
{
private static readonly DatabaseObjectProvider _objectProvider = new DatabaseObjectProvider();

/// <summary>
/// Returns the currently configured ObjectProvider.
/// </summary>
/// <returns></returns>
public static ObjectProvider Instance()
{
return _objectProvider;
}

public abstract void ClearBlogContent(int blogId);

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Subtext.Framework/Services/Security/SecurityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Subtext.Framework.Providers;
using Subtext.Framework.Text;
using Subtext.Framework.Web;
using Subtext.Framework.Data;

namespace Subtext.Framework.Security
{
Expand Down Expand Up @@ -176,7 +177,7 @@ public static bool Authenticate(string claimedIdentifier, bool persist)
/// <returns></returns>
public static bool AuthenticateHostAdmin(this HostInfo host, string username, string password, bool persist)
{
var repository = ObjectProvider.Instance();
var repository = new DatabaseObjectProvider();
if (!String.Equals(username, host.HostUserName, StringComparison.InvariantCultureIgnoreCase))
{
return false;
Expand Down Expand Up @@ -432,10 +433,11 @@ public static string ResetPassword()
/// <param name="password">Supplied Password</param>
public static void UpdatePassword(string password)
{
var repository = new DatabaseObjectProvider();
Blog info = Config.CurrentBlog;
info.Password = Config.CurrentBlog.IsPasswordHashed ? HashPassword(password) : password;
//Save new password.
ObjectProvider.Instance().UpdateConfigData(info);
repository.UpdateConfigData(info);
}

public static void UpdateHostAdminPassword(this ObjectProvider repository, HostInfo hostInfo, string password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Subtext.Framework.Components;
using Subtext.Framework.Configuration;
using Subtext.Framework.Data;
using Subtext.Framework.Providers;
using Subtext.Framework.Services;
using Subtext.Framework.Web.HttpModules;

Expand Down Expand Up @@ -145,27 +144,28 @@ private static void CreateDeletedComments(int count, Entry entry)
[RollBack2]
public void CreateFeedbackSetsBlogStatsCorrectly()
{
var repository = new DatabaseObjectProvider();
Entry entry = SetupBlogForCommentsAndCreateEntry();
Blog info = Config.CurrentBlog;

Assert.AreEqual(0, info.CommentCount);
Assert.AreEqual(0, info.PingTrackCount);

info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder); // pull back the updated info from the datastore.
info = repository.GetBlog(info.Host, info.Subfolder); // pull back the updated info from the datastore.
Assert.AreEqual(0, info.CommentCount);
Assert.AreEqual(0, info.PingTrackCount);

CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.Approved);
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.PingTrack, FeedbackStatusFlag.Approved);

info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder);
info = repository.GetBlog(info.Host, info.Subfolder);
Assert.AreEqual(1, info.CommentCount, "Blog CommentCount should be 1");
Assert.AreEqual(1, info.PingTrackCount, "Blog Ping/Trackback count should be 1");

CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.Approved);
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.PingTrack, FeedbackStatusFlag.Approved);

info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder);
info = repository.GetBlog(info.Host, info.Subfolder);
Assert.AreEqual(2, info.CommentCount, "Blog CommentCount should be 2");
Assert.AreEqual(2, info.PingTrackCount, "Blog Ping/Trackback count should be 2");
}
Expand All @@ -174,6 +174,7 @@ public void CreateFeedbackSetsBlogStatsCorrectly()
[RollBack2]
public void CreateEntryDoesNotResetBlogStats()
{
var repository = new DatabaseObjectProvider();
Entry entry = SetupBlogForCommentsAndCreateEntry();
Blog info = Config.CurrentBlog;

Expand All @@ -182,7 +183,7 @@ public void CreateEntryDoesNotResetBlogStats()

Entry entry2 = UnitTestHelper.CreateEntryInstanceForSyndication("johnny b goode", "foo-bar", "zaa zaa zoo.");
UnitTestHelper.Create(entry2);
info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder); // pull back the updated info from the datastore
info = repository.GetBlog(info.Host, info.Subfolder); // pull back the updated info from the datastore

Assert.AreEqual(1, info.CommentCount, "Blog CommentCount should be 1");
Assert.AreEqual(1, info.PingTrackCount, "Blog Ping/Trackback count should be 1");
Expand All @@ -192,21 +193,22 @@ public void CreateEntryDoesNotResetBlogStats()
[RollBack2]
public void DeleteEntrySetsBlogStats()
{
Entry entry = SetupBlogForCommentsAndCreateEntry();
var repository = new DatabaseObjectProvider();
Entry entry = SetupBlogForCommentsAndCreateEntry(repository);
Blog info = Config.CurrentBlog;

CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.Approved);
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.PingTrack, FeedbackStatusFlag.Approved);

info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder);
Assert.AreEqual(1, info.CommentCount, "Blog CommentCount should be 1");
Assert.AreEqual(1, info.PingTrackCount, "Blog Ping/Trackback count should be 1");
Blog blog = repository.GetBlog(info.Host, info.Subfolder);
Assert.AreEqual(1, blog.CommentCount, "Blog CommentCount should be 1");
Assert.AreEqual(1, blog.PingTrackCount, "Blog Ping/Trackback count should be 1");

ObjectProvider.Instance().DeleteEntry(entry.Id);
info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(info.Host, info.Subfolder);
repository.DeleteEntry(entry.Id);
blog = repository.GetBlog(info.Host, info.Subfolder);

Assert.AreEqual(0, info.CommentCount, "Blog CommentCount should be 0");
Assert.AreEqual(0, info.PingTrackCount, "Blog Ping/Trackback count should be 0");
Assert.AreEqual(0, blog.CommentCount, "Blog CommentCount should be 0");
Assert.AreEqual(0, blog.PingTrackCount, "Blog Ping/Trackback count should be 0");
}

[Test]
Expand Down Expand Up @@ -323,7 +325,7 @@ public void OnlyApprovedItemsContributeToEntryFeedbackCount()
public void CanGetItemsFlaggedAsSpam()
{
Entry entry = SetupBlogForCommentsAndCreateEntry();

var repository = new DatabaseObjectProvider();
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.FalsePositive);
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.Approved);
CreateAndUpdateFeedbackWithExactStatus(entry, FeedbackType.Comment, FeedbackStatusFlag.ConfirmedSpam);
Expand All @@ -334,7 +336,7 @@ public void CanGetItemsFlaggedAsSpam()
FeedbackStatusFlag.NeedsModeration);

//We expect 2 of the four.
IPagedCollection<FeedbackItem> feedback = ObjectProvider.Instance().GetPagedFeedback(0, 10,
IPagedCollection<FeedbackItem> feedback = repository.GetPagedFeedback(0, 10,
FeedbackStatusFlag.
FlaggedAsSpam,
FeedbackStatusFlag.
Expand Down Expand Up @@ -373,7 +375,7 @@ static FeedbackItem CreateAndUpdateFeedbackWithExactStatus(Entry entry, Feedback
var subtextContext = new Mock<ISubtextContext>();
subtextContext.Setup(c => c.Cache).Returns(new TestCache());
subtextContext.SetupBlog(Config.CurrentBlog);
subtextContext.SetupRepository(ObjectProvider.Instance());
subtextContext.SetupRepository(repository);
subtextContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
subtextContext.Setup(c => c.HttpContext).Returns(new HttpContextWrapper(HttpContext.Current));

Expand All @@ -387,17 +389,18 @@ static FeedbackItem CreateAndUpdateFeedbackWithExactStatus(Entry entry, Feedback
return repository.Get(id);
}

Entry SetupBlogForCommentsAndCreateEntry()
Entry SetupBlogForCommentsAndCreateEntry(DatabaseObjectProvider repository = null)
{
new global::Subtext.Framework.Data.DatabaseObjectProvider().CreateBlog(string.Empty, "username", "password", _hostName, string.Empty);
Blog info = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(_hostName, string.Empty);
repository = repository ?? new DatabaseObjectProvider();
repository.CreateBlog(string.Empty, "username", "password", _hostName, string.Empty);
Blog info = repository.GetBlog(_hostName, string.Empty);
BlogRequest.Current.Blog = info;
info.Email = "test@example.com";
info.Title = "You've been haacked";
info.CommentsEnabled = true;
info.ModerationEnabled = false;

ObjectProvider.Instance().UpdateConfigData(info);
repository.UpdateConfigData(info);

Entry entry = UnitTestHelper.CreateEntryInstanceForSyndication("blah", "blah", "blah");
UnitTestHelper.Create(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using MbUnit.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Data;
using Subtext.Framework.Providers;

namespace UnitTests.Subtext.Framework.Components.EnclosureTests
{
Expand Down Expand Up @@ -77,11 +76,12 @@ public void Create_WithInvalidEntry_ThrowsArgumentException()
public void EntryWithNoEnclosureHasNullAsEnclosure()
{
UnitTestHelper.SetupBlog();
var repository = new DatabaseObjectProvider();
Entry e = UnitTestHelper.CreateEntryInstanceForSyndication("Simone Chiaretta", "Post for testing Enclosures",
"Listen to my great podcast");
int entryId = UnitTestHelper.Create(e);

Entry newEntry = ObjectProvider.Instance().GetEntry(entryId, true, false);
Entry newEntry = repository.GetEntry(entryId, true, false);

Assert.IsNull(newEntry.Enclosure, "enclosure must be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void GetPostsByDayRangeIncludesEnclosure()

//Get Entries
var beginningOfMonth = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1);
ICollection<Entry> entries = ObjectProvider.Instance().GetPostsByDayRange(beginningOfMonth, beginningOfMonth.AddMonths(1),
ICollection<Entry> entries = repository.GetPostsByDayRange(beginningOfMonth, beginningOfMonth.AddMonths(1),
PostType.BlogPost, true);


Expand Down Expand Up @@ -248,7 +248,7 @@ public void GetPostCollectionByMonthIncludesEnclosure()
repository.Create(enc);

//Get Entries
ICollection<Entry> entries = ObjectProvider.Instance().GetPostsByMonth(DateTime.UtcNow.Month, DateTime.UtcNow.Year);
ICollection<Entry> entries = repository.GetPostsByMonth(DateTime.UtcNow.Month, DateTime.UtcNow.Year);

//Test outcome
Assert.AreEqual(3, entries.Count, "Expected to find three entries.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
using Subtext.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Configuration;
using Subtext.Framework.Providers;
using Subtext.Framework.Web.HttpModules;
using Subtext.Framework.Data;
using Subtext.Framework.Web.HttpModules;

namespace UnitTests.Subtext.Framework.Components.EntryTests
{
Expand Down Expand Up @@ -88,6 +87,7 @@ public void GetSingleDayReturnsDayWithEnclosure()
public void GetBlogPostsReturnsAllPostsIfPostConfigNoneSpecified()
{
// Create four entries.
var repository = new DatabaseObjectProvider();
Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");
entryZero.IsActive = entryZero.IncludeInMainSyndication = true;
Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");
Expand All @@ -114,7 +114,7 @@ public void GetBlogPostsReturnsAllPostsIfPostConfigNoneSpecified()
Assert.IsTrue(entryThree.DatePublishedUtc > DateTime.UtcNow);

//Get EntryDay
ICollection<EntryDay> entryList = ObjectProvider.Instance().GetBlogPostsForHomePage(10, PostConfig.None).ToList();
ICollection<EntryDay> entryList = repository.GetBlogPostsForHomePage(10, PostConfig.None).ToList();

//Test outcome
Assert.AreEqual(3, entryList.Count);
Expand All @@ -138,6 +138,7 @@ public void GetBlogPostsReturnsAllPostsIfPostConfigNoneSpecified()
public void GetBlogPostsReturnsActiveOnlyAndNoneInFuture()
{
//Create some entries.
var repository = new DatabaseObjectProvider();
Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");
entryZero.IsActive = entryZero.IncludeInMainSyndication = true;
Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");
Expand All @@ -162,7 +163,7 @@ public void GetBlogPostsReturnsActiveOnlyAndNoneInFuture()
Assert.IsTrue(entryThree.DatePublishedUtc > DateTime.UtcNow);

//Get EntryDay
ICollection<EntryDay> entryList = ObjectProvider.Instance().GetBlogPostsForHomePage(10, PostConfig.IsActive).ToList();
ICollection<EntryDay> entryList = repository.GetBlogPostsForHomePage(10, PostConfig.IsActive).ToList();

//Test outcome
Assert.AreEqual(2, entryList.Count);
Expand Down Expand Up @@ -247,7 +248,7 @@ public void GetHomePageEntriesReturnsDaysWithEnclosure()
repository.Create(enc);

//Get EntryDay
ICollection<EntryDay> entryList = ObjectProvider.Instance().GetBlogPostsForHomePage(10, PostConfig.DisplayOnHomepage | PostConfig.IsActive).ToList();
ICollection<EntryDay> entryList = repository.GetBlogPostsForHomePage(10, PostConfig.DisplayOnHomepage | PostConfig.IsActive).ToList();

var days = new EntryDay[2];
entryList.CopyTo(days, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void TagDoesNotRetrieveDraftEntry()
UnitTestHelper.Create(entry);
var tags = new List<string>(new[] { "Tag1", "Tag2" });
new DatabaseObjectProvider().SetEntryTagList(entry.Id, tags);
ICollection<Entry> entries = ObjectProvider.Instance().GetEntriesByTag(1, "Tag1");
ICollection<Entry> entries = repository.GetEntriesByTag(1, "Tag1");
Assert.AreEqual(0, entries.Count, "Should not retrieve draft entry.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Subtext.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Configuration;
using Subtext.Framework.Providers;
using Subtext.Framework.Data;
using Subtext.Framework.Web.HttpModules;

namespace UnitTests.Subtext.Framework.Components.EntryTests
Expand All @@ -21,16 +21,17 @@ public class EntryUpdateTests
[RollBack2]
public void CanDeleteEntry()
{
new global::Subtext.Framework.Data.DatabaseObjectProvider().CreateBlog("", "username", "password", _hostName, string.Empty);
BlogRequest.Current.Blog = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(_hostName, string.Empty);
var repository = new DatabaseObjectProvider();
repository.CreateBlog("", "username", "password", _hostName, string.Empty);
BlogRequest.Current.Blog = repository.GetBlog(_hostName, string.Empty);

Entry entry = UnitTestHelper.CreateEntryInstanceForSyndication("Haacked", "Title Test", "Body Rocking");
UnitTestHelper.Create(entry);

Entry savedEntry = UnitTestHelper.GetEntry(entry.Id, PostConfig.None, false);
Assert.IsNotNull(savedEntry);

ObjectProvider.Instance().DeleteEntry(entry.Id);
repository.DeleteEntry(entry.Id);

savedEntry = UnitTestHelper.GetEntry(entry.Id, PostConfig.None, false);
Assert.IsNull(savedEntry, "Entry should now be null.");
Expand All @@ -44,8 +45,9 @@ public void CanDeleteEntry()
public void SettingDatePublishedUtcToNullRemovesItemFromSyndication()
{
//arrange
new global::Subtext.Framework.Data.DatabaseObjectProvider().CreateBlog("", "username", "password", _hostName, string.Empty);
BlogRequest.Current.Blog = new global::Subtext.Framework.Data.DatabaseObjectProvider().GetBlog(_hostName, string.Empty);
var repository = new DatabaseObjectProvider();
repository.CreateBlog("", "username", "password", _hostName, string.Empty);
BlogRequest.Current.Blog = repository.GetBlog(_hostName, string.Empty);

Entry entry = UnitTestHelper.CreateEntryInstanceForSyndication("Haacked", "Title Test", "Body Rocking");
UnitTestHelper.Create(entry);
Expand All @@ -65,7 +67,7 @@ public void SettingDatePublishedUtcToNullRemovesItemFromSyndication()
//save it
var subtextContext = new Mock<ISubtextContext>();
subtextContext.Setup(c => c.Blog).Returns(Config.CurrentBlog);
subtextContext.Setup(c => c.Repository).Returns(new global::Subtext.Framework.Data.DatabaseObjectProvider());
subtextContext.Setup(c => c.Repository).Returns(repository);
UnitTestHelper.Update(entry, subtextContext.Object);
Entry savedEntry = UnitTestHelper.GetEntry(entry.Id, PostConfig.None, false);

Expand Down
Loading

0 comments on commit b8ac067

Please sign in to comment.