public void GetEntriesByTagIncludesEnclosure()
        {
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);


            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

            var tags = new List <string>(new[] { "Tag1", "Tag2" });

            new DatabaseObjectProvider().SetEntryTagList(entryZero.Id, tags);
            new DatabaseObjectProvider().SetEntryTagList(entryOne.Id, tags);


            ICollection <Entry> entries = ObjectProvider.Instance().GetEntriesByTag(3, "Tag1");

            //Test outcome
            Assert.AreEqual(2, entries.Count, "Should have retrieved two entries.");

            Assert.AreEqual(entries.First().Id, entryOne.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(1).Id, entryZero.Id, "Ordering is off.");

            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(1).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(1).Enclosure);
        }
예제 #2
0
        public void CanUpdateEnclosure(string title, string url, string mimetype, long size, bool addToFeed,
                                       bool showWithPost)
        {
            UnitTestHelper.SetupBlog(string.Empty);
            Entry e = UnitTestHelper.CreateEntryInstanceForSyndication("Simone Chiaretta", "Post for testing Enclosures",
                                                                       "Listen to my great podcast");
            int       entryId = UnitTestHelper.Create(e);
            Enclosure enc     = UnitTestHelper.BuildEnclosure(title, url, mimetype, entryId, size, addToFeed, showWithPost);

            Enclosures.Create(enc);

            string randomStr = UnitTestHelper.GenerateUniqueString().Left(20);

            enc.Url = url + randomStr;

            if (!string.IsNullOrEmpty(title))
            {
                enc.Title = title + randomStr;
            }

            enc.MimeType = mimetype + randomStr;

            int randomSize = new Random().Next(10, 100);

            enc.Size = size + randomSize;

            Assert.IsTrue(Enclosures.Update(enc), "Should have updated the Enclosure");

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

            UnitTestHelper.AssertEnclosures(enc, newEntry.Enclosure);
        }
        public void RssWriterProducesValidFeedWithEnclosureFromDatabase()
        {
            string hostName = UnitTestHelper.GenerateUniqueString() + ".com";

            Config.CreateBlog("Test", "username", "password", hostName, string.Empty);

            UnitTestHelper.SetHttpContextWithBlogRequest(hostName, "");
            BlogRequest.Current.Blog = Config.GetBlog(hostName, string.Empty);
            Config.CurrentBlog.Email = "*****@*****.**";
            Config.CurrentBlog.RFC3229DeltaEncodingEnabled = false;

            Entry entry = UnitTestHelper.CreateEntryInstanceForSyndication("Author", "testtitle", "testbody", null,
                                                                           NullValue.NullDateTime);

            entry.DateCreated    = DateTime.ParseExact("2008/01/23", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            entry.DateSyndicated = entry.DateCreated;
            int entryId = UnitTestHelper.Create(entry); //persist to db.

            string enclosureUrl      = "http://perseus.franklins.net/hanselminutes_0107.mp3";
            string enclosureMimeType = "audio/mp3";
            long   enclosureSize     = 26707573;

            Enclosure enc =
                UnitTestHelper.BuildEnclosure("<Digital Photography Explained (for Geeks) with Aaron Hockley/>",
                                              enclosureUrl, enclosureMimeType, entryId, enclosureSize, true, true);

            Enclosures.Create(enc);

            var    subtextContext = new Mock <ISubtextContext>();
            string rssOutput      = null;

            subtextContext.FakeSyndicationContext(Config.CurrentBlog, "/", s => rssOutput = s);
            subtextContext.Setup(c => c.Repository).Returns(ObjectProvider.Instance());
            Mock <UrlHelper> urlHelper = Mock.Get(subtextContext.Object.UrlHelper);

            urlHelper.Setup(u => u.BlogUrl()).Returns("/");
            urlHelper.Setup(u => u.EntryUrl(It.IsAny <Entry>())).Returns("/archive/2008/01/23/testtitle.aspx");

            XmlNodeList itemNodes = GetRssHandlerItemNodes(subtextContext.Object, ref rssOutput);

            Assert.AreEqual(1, itemNodes.Count, "expected one item nodes.");

            string urlFormat   = "http://{0}/archive/2008/01/23/{1}.aspx";
            string expectedUrl = string.Format(urlFormat, hostName, "testtitle");

            Assert.AreEqual("testtitle", itemNodes[0].SelectSingleNode("title").InnerText,
                            "Not what we expected for the title.");
            Assert.AreEqual(expectedUrl, itemNodes[0].SelectSingleNode("link").InnerText,
                            "Not what we expected for the link.");
            Assert.AreEqual(expectedUrl, itemNodes[0].SelectSingleNode("guid").InnerText,
                            "Not what we expected for the guid.");
            Assert.AreEqual(enclosureUrl, itemNodes[0].SelectSingleNode("enclosure/@url").InnerText,
                            "Not what we expected for the enclosure url.");
            Assert.AreEqual(enclosureMimeType, itemNodes[0].SelectSingleNode("enclosure/@type").InnerText,
                            "Not what we expected for the enclosure mimetype.");
            Assert.AreEqual(enclosureSize.ToString(), itemNodes[0].SelectSingleNode("enclosure/@length").InnerText,
                            "Not what we expected for the enclosure size.");
            Assert.AreEqual(expectedUrl + "#feedback", itemNodes[0].SelectSingleNode("comments").InnerText,
                            "Not what we expected for the link.");
        }
        public void GetPostsByCategoryIDReturnsDaysWithEnclosure()
        {
            //Create Category
            int blogId     = Config.CurrentBlog.Id;
            int categoryId = UnitTestHelper.CreateCategory(blogId, "Test Category");

            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(100);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            Thread.Sleep(100);
            Entry entryThree = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            entryThree.DateCreated = DateTime.Now.AddDays(1);

            //Associate Category
            entryZero.Categories.Add("Test Category");
            entryOne.Categories.Add("Test Category");
            entryThree.Categories.Add("Test Category");


            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);
            UnitTestHelper.Create(entryThree);

            //Add Enclosure
            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

            //Get EntryDay
            ICollection <EntryDay> entryList = ObjectProvider.Instance().GetBlogPostsByCategoryGroupedByDay(10, categoryId).ToList();

            var days = new EntryDay[2];

            entryList.CopyTo(days, 0);

            //Test outcome
            Assert.AreEqual(2, entryList.Count, "Expected to find two days.");

            EntryDay entries = days[1];

            Assert.AreEqual(2, entries.Count, "Expected to find two entries.");

            Assert.AreEqual(entries.First().Id, entryOne.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(1).Id, entryZero.Id, "Ordering is off.");

            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(1).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(1).Enclosure);
        }
        public void GetRecentPostsIncludesEnclosure()
        {
            int blogId = Config.CurrentBlog.Id;

            for (int i = 0; i < 10; i++)
            {
                UnitTestHelper.CreateCategory(blogId, "cat" + i);
            }

            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(100);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            //Associate categories.
            for (int i = 0; i < 5; i++)
            {
                entryZero.Categories.Add("cat" + (i + 1));
                entryOne.Categories.Add("cat" + i);
            }
            entryTwo.Categories.Add("cat8");

            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);

            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

            //Get Entries
            ICollection <Entry> entries = ObjectProvider.Instance().GetEntries(3, PostType.BlogPost, PostConfig.IsActive, true);

            //Test outcome
            Assert.AreEqual(3, entries.Count, "Expected to find three entries.");

            Assert.AreEqual(entries.First().Id, entryTwo.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(1).Id, entryOne.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(2).Id, entryZero.Id, "Ordering is off.");

            Assert.AreEqual(1, entries.First().Categories.Count);
            Assert.AreEqual(5, entries.ElementAt(1).Categories.Count);
            Assert.AreEqual(5, entries.ElementAt(2).Categories.Count);

            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNull(entries.ElementAt(1).Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(2).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(2).Enclosure);
        }
        public void Create_WithInvalidEntry_ThrowsArgumentException()
        {
            // arrange
            var enclosure = new Enclosure {
                EntryId = 0
            };

            // act, assert
            Assert.IsFalse(enclosure.IsValid);
            UnitTestHelper.AssertThrows <ArgumentException>(() => Enclosures.Create(enclosure));
        }
        public void GetPostsByMonthReturnsDaysWithEnclosure()
        {
            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(100);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            Thread.Sleep(100);
            Entry entryThree = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            entryThree.DateCreated = DateTime.Now.AddDays(1);

            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);
            UnitTestHelper.Create(entryThree);

            //Add Enclosure
            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

            //Get EntryDay
            //ICollection<EntryDay> entryList = Entries.GetPostsByMonth(DateTime.Now.Month, DateTime.Now.Year);

            //EntryDay[] days = new EntryDay[2];
            //entryList.CopyTo(days, 0);

            ////Test outcome
            //Assert.AreEqual(2, entryList.Count, "Expected to find two days.");

            //EntryDay entries = days[1];
            //Assert.AreEqual(3, entries.Count, "Expected to find three entries.");

            //Assert.AreEqual(entries.First().Id, entryTwo.Id, "Ordering is off.");
            //Assert.AreEqual(entries.ElementAt(1).Id, entryOne.Id, "Ordering is off.");
            //Assert.AreEqual(entries.ElementAt(2).Id, entryZero.Id, "Ordering is off.");

            //Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            //Assert.IsNull(entries.ElementAt(1).Enclosure, "Entry should not have enclosure.");
            //Assert.IsNotNull(entries.ElementAt(2).Enclosure, "Entry should have enclosure.");
            //UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(2).Enclosure);
        }
        public void GetBlogPostsReturnsDaysWithEnclosure()
        {
            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(500);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(500);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            entryTwo.IsActive = false;
            Thread.Sleep(500);
            Entry entryThree = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-three", "body-three");

            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);
            UnitTestHelper.Create(entryThree);

            Assert.IsTrue(entryZero.DateCreated < entryOne.DateCreated);
            Assert.IsTrue(entryOne.DateCreated < entryTwo.DateCreated);
            Assert.IsTrue(entryTwo.DateCreated < entryThree.DateCreated);

            //Add Enclosure
            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

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

            Collection <Entry> entries = entryList.First();

            //Test outcome
            Assert.AreEqual(1, entryList.Count, "Expected to find one entry day.");

            Assert.AreEqual(3, entries.Count, "Expected to find three entries.");

            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNull(entries.ElementAt(1).Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(2).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(2).Enclosure);
        }
        public void CanInsertEnclosure(string title, string url, string mimetype, long size, bool addToFeed,
                                       bool showWithPost, string errMsg)
        {
            UnitTestHelper.SetupBlog();
            Entry e = UnitTestHelper.CreateEntryInstanceForSyndication("Simone Chiaretta", "Post for testing Enclosures",
                                                                       "Listen to my great podcast");
            int       entryId = UnitTestHelper.Create(e);
            Enclosure enc     = UnitTestHelper.BuildEnclosure(title, url, mimetype, entryId, size, addToFeed, showWithPost);

            Enclosures.Create(enc);

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

            Assert.IsNotNull(newEntry.Enclosure, errMsg);

            UnitTestHelper.AssertEnclosures(enc, newEntry.Enclosure);
        }
        public void GetPostsByDayRangeIncludesEnclosure()
        {
            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(100);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");


            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);

            //Add Enclosure
            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);


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


            //Test outcome
            Assert.AreEqual(3, entries.Count, "Expected to find three entries.");

            Assert.AreEqual(entries.First().Id, entryTwo.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(1).Id, entryOne.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(2).Id, entryZero.Id, "Ordering is off.");


            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNull(entries.ElementAt(1).Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(2).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(2).Enclosure);
        }
        public void GetSingleDayReturnsDayWithEnclosure()
        {
            //Create some entries.
            Entry entryZero = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-zero", "body-zero");

            Thread.Sleep(100);
            Entry entryOne = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-one", "body-one");

            Thread.Sleep(100);
            Entry entryTwo = UnitTestHelper.CreateEntryInstanceForSyndication("me", "title-two", "body-two");

            //Persist entries.
            UnitTestHelper.Create(entryZero);
            UnitTestHelper.Create(entryOne);
            UnitTestHelper.Create(entryTwo);

            //Add Enclosure
            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryZero.Id, 12345678, true, true);

            Enclosures.Create(enc);

            //Get EntryDay
            EntryDay entries = ObjectProvider.Instance().GetEntryDay(DateTime.Now);

            //Test outcome
            Assert.AreEqual(3, entries.Count, "Expected to find three entries.");

            Assert.AreEqual(entries.First().Id, entryTwo.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(1).Id, entryOne.Id, "Ordering is off.");
            Assert.AreEqual(entries.ElementAt(2).Id, entryZero.Id, "Ordering is off.");


            Assert.IsNull(entries.First().Enclosure, "Entry should not have enclosure.");
            Assert.IsNull(entries.ElementAt(1).Enclosure, "Entry should not have enclosure.");
            Assert.IsNotNull(entries.ElementAt(2).Enclosure, "Entry should have enclosure.");
            UnitTestHelper.AssertEnclosures(enc, entries.ElementAt(2).Enclosure);
        }
예제 #12
0
        public void CanDeleteEnclosure()
        {
            Blog blog = UnitTestHelper.CreateBlogAndSetupContext();

            Entry e = UnitTestHelper.CreateEntryInstanceForSyndication("Simone Chiaretta", "Post for testing Enclosures",
                                                                       "Listen to my great podcast");
            int entryId = UnitTestHelper.Create(e);

            Enclosure enc = UnitTestHelper.BuildEnclosure("Nothing to see here.", "httP://blablabla.com", "audio/mp3",
                                                          entryId, 12345678, true, true);

            Enclosures.Create(enc);

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

            Assert.IsNotNull(newEntry.Enclosure, "Did not create enclosure.");

            Enclosures.Delete(enc.Id);

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

            Assert.IsNull(newEntry1.Enclosure, "Did not delete enclosure.");
        }
 public void Create_WithNullEnclosure_ThrowsArgumentNullException()
 {
     UnitTestHelper.AssertThrowsArgumentNullException(() => Enclosures.Create(null));
 }
예제 #14
0
        private void UpdatePost()
        {
            DateTime postDate = NullValue.NullDateTime;

            vCustomPostDate.IsValid = string.IsNullOrEmpty(txtPostDate.Text) || DateTime.TryParse(txtPostDate.Text, out postDate);

            EnableEnclosureValidation(EnclosureEnabled());

            if (Page.IsValid)
            {
                string successMessage = Constants.RES_SUCCESSNEW;

                try
                {
                    Entry entry;
                    if (PostId == null)
                    {
                        ValidateEntryTypeIsNotNone(EntryType);
                        entry = new Entry(EntryType);
                    }
                    else
                    {
                        entry = GetEntryForEditing(PostId.Value);
                        if (entry.PostType != EntryType)
                        {
                            EntryType = entry.PostType;
                        }
                    }

                    entry.Title  = txbTitle.Text;
                    entry.Body   = richTextEditor.Xhtml;
                    entry.Author = Config.CurrentBlog.Author;
                    entry.Email  = Config.CurrentBlog.Email;
                    entry.BlogId = Config.CurrentBlog.Id;

                    //Enclosure
                    int enclosureId = 0;
                    if (entry.Enclosure != null)
                    {
                        enclosureId = entry.Enclosure.Id;
                    }

                    if (EnclosureEnabled())
                    {
                        if (entry.Enclosure == null)
                        {
                            entry.Enclosure = new Enclosure();
                        }
                        Enclosure enc = entry.Enclosure;

                        enc.Title    = txbEnclosureTitle.Text;
                        enc.Url      = txbEnclosureUrl.Text;
                        enc.MimeType = ddlMimeType.SelectedValue.Equals("other") ? txbEnclosureOtherMimetype.Text : ddlMimeType.SelectedValue;
                        long size;
                        Int64.TryParse(txbEnclosureSize.Text, out size);
                        enc.Size         = size;
                        enc.AddToFeed    = Boolean.Parse(ddlAddToFeed.SelectedValue);
                        enc.ShowWithPost = Boolean.Parse(ddlDisplayOnPost.SelectedValue);
                    }
                    else
                    {
                        entry.Enclosure = null;
                    }

                    // Advanced options
                    entry.IsActive                 = ckbPublished.Checked;
                    entry.AllowComments            = chkComments.Checked;
                    entry.CommentingClosed         = chkCommentsClosed.Checked;
                    entry.DisplayOnHomePage        = chkDisplayHomePage.Checked;
                    entry.IncludeInMainSyndication = chkMainSyndication.Checked;
                    entry.SyndicateDescriptionOnly = chkSyndicateDescriptionOnly.Checked;
                    entry.IsAggregated             = chkIsAggregated.Checked;
                    entry.EntryName                = txbEntryName.Text.NullIfEmpty();
                    entry.Description              = txbExcerpt.Text.NullIfEmpty();
                    entry.Categories.Clear();
                    ReplaceSelectedCategoryNames(entry.Categories);

                    if (!NullValue.IsNull(postDate))
                    {
                        entry.DateSyndicated = postDate;
                    }

                    if (PostId != null)
                    {
                        successMessage     = Constants.RES_SUCCESSEDIT;
                        entry.DateModified = Config.CurrentBlog.TimeZone.Now;
                        entry.Id           = PostId.Value;

                        var entryPublisher = SubtextContext.ServiceLocator.GetService <IEntryPublisher>();
                        entryPublisher.Publish(entry);

                        if (entry.Enclosure == null && enclosureId != 0)
                        {
                            Enclosures.Delete(enclosureId);
                        }
                        else if (entry.Enclosure != null && entry.Enclosure.Id != 0)
                        {
                            Enclosures.Update(entry.Enclosure);
                        }
                        else if (entry.Enclosure != null && entry.Enclosure.Id == 0)
                        {
                            entry.Enclosure.EntryId = entry.Id;
                            Enclosures.Create(entry.Enclosure);
                        }

                        UpdateCategories();
                    }
                    else
                    {
                        var entryPublisher = SubtextContext.ServiceLocator.GetService <IEntryPublisher>();
                        _postId = entryPublisher.Publish(entry);
                        NotificationServices.Run(entry, Blog, Url);

                        if (entry.Enclosure != null)
                        {
                            entry.Enclosure.EntryId = PostId.Value;
                            Enclosures.Create(entry.Enclosure);
                        }

                        UpdateCategories();
                        AddCommunityCredits(entry);
                    }
                }
                catch (Exception ex)
                {
                    Messages.ShowError(String.Format(Constants.RES_EXCEPTION,
                                                     Constants.RES_FAILUREEDIT, ex.Message));
                    successMessage = string.Empty;
                }

                //Prepared success messages were reset in the catch block because of some error on posting the content
                if (!String.IsNullOrEmpty(successMessage))
                {
                    ReturnToOrigin(successMessage);
                }
            }
        }
        public void GetPage_ReturnsPostWithhCorrectEntrUrl()
        {
            //arrange
            string hostname = UnitTestHelper.GenerateUniqueString();

            Config.CreateBlog("", "username", "password", hostname, "");
            UnitTestHelper.SetHttpContextWithBlogRequest(hostname, "");
            BlogRequest.Current.Blog = Config.GetBlog(hostname, "");
            Config.CurrentBlog.AllowServiceAccess = true;

            var urlHelper = new Mock <UrlHelper>();

            urlHelper.Setup(u => u.EntryUrl(It.IsAny <Entry>())).Returns("/entry/whatever");
            var subtextContext = new Mock <ISubtextContext>();

            subtextContext.Setup(c => c.Blog).Returns(Config.CurrentBlog);
            //TODO: FIX!!!
            subtextContext.Setup(c => c.Repository).Returns(ObjectProvider.Instance());
            subtextContext.Setup(c => c.ServiceLocator).Returns(new Mock <IServiceLocator>().Object);
            subtextContext.Setup(c => c.UrlHelper).Returns(urlHelper.Object);

            var    api           = new MetaWeblog(subtextContext.Object);
            string category1Name = UnitTestHelper.GenerateUniqueString();
            string category2Name = UnitTestHelper.GenerateUniqueString();

            UnitTestHelper.CreateCategory(Config.CurrentBlog.Id, category1Name);
            UnitTestHelper.CreateCategory(Config.CurrentBlog.Id, category2Name);

            var entry = new Entry(PostType.Story);

            entry.Title    = "Title 1";
            entry.Body     = "Blah";
            entry.IsActive = true;
            entry.IncludeInMainSyndication = true;
            entry.DateCreated          =
                entry.DateSyndicated   =
                    entry.DateModified = DateTime.ParseExact("1975/01/23", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            entry.Categories.Add(category1Name);
            int    entryId           = UnitTestHelper.Create(entry);
            string enclosureUrl      = "http://perseus.franklins.net/hanselminutes_0107.mp3";
            string enclosureMimeType = "audio/mp3";
            long   enclosureSize     = 26707573;

            FrameworkEnclosure enc =
                UnitTestHelper.BuildEnclosure("<Digital Photography Explained (for Geeks) with Aaron Hockley/>",
                                              enclosureUrl, enclosureMimeType, entryId, enclosureSize, true, true);

            Enclosures.Create(enc);

            //act
            Post post = api.getPage(Config.CurrentBlog.Id.ToString(), entryId.ToString(), "username", "password");

            //assert
            Assert.AreEqual(1, post.categories.Length);
            Assert.AreEqual("http://" + hostname + "/entry/whatever", post.link);
            Assert.AreEqual("http://" + hostname + "/entry/whatever", post.permalink);
            Assert.AreEqual(category1Name, post.categories[0]);
            Assert.AreEqual(enclosureUrl, post.enclosure.Value.url);
            Assert.AreEqual(enclosureMimeType, post.enclosure.Value.type);
            Assert.AreEqual(enclosureSize, post.enclosure.Value.length);
        }
        public void GetRecentPosts_ReturnsRecentPosts()
        {
            string hostname = UnitTestHelper.GenerateUniqueString();

            Config.CreateBlog("", "username", "password", hostname, "");
            UnitTestHelper.SetHttpContextWithBlogRequest(hostname, "");
            Blog blog = Config.GetBlog(hostname, "");

            BlogRequest.Current.Blog = blog;
            blog.AllowServiceAccess  = true;

            var urlHelper = new Mock <UrlHelper>();

            urlHelper.Setup(u => u.EntryUrl(It.IsAny <Entry>())).Returns("/entry/whatever");
            var subtextContext = new Mock <ISubtextContext>();

            subtextContext.Setup(c => c.Blog).Returns(Config.CurrentBlog);
            //TODO: FIX!!!
            subtextContext.Setup(c => c.Repository).Returns(ObjectProvider.Instance());
            subtextContext.SetupBlog(blog);
            subtextContext.Setup(c => c.UrlHelper).Returns(urlHelper.Object);
            subtextContext.Setup(c => c.ServiceLocator).Returns(new Mock <IServiceLocator>().Object);

            var api = new MetaWeblog(subtextContext.Object);

            Post[] posts = api.getRecentPosts(Config.CurrentBlog.Id.ToString(), "username", "password", 10);
            Assert.AreEqual(0, posts.Length);

            string category1Name = UnitTestHelper.GenerateUniqueString();
            string category2Name = UnitTestHelper.GenerateUniqueString();

            UnitTestHelper.CreateCategory(Config.CurrentBlog.Id, category1Name);
            UnitTestHelper.CreateCategory(Config.CurrentBlog.Id, category2Name);

            var entry = new Entry(PostType.BlogPost);

            entry.Title    = "Title 1";
            entry.Body     = "Blah";
            entry.IsActive = true;
            entry.IncludeInMainSyndication = true;
            entry.DateCreated          =
                entry.DateSyndicated   =
                    entry.DateModified = DateTime.ParseExact("1975/01/23", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            entry.Categories.Add(category1Name);
            UnitTestHelper.Create(entry);

            entry = new Entry(PostType.BlogPost);
            entry.IncludeInMainSyndication = true;
            entry.Title                = "Title 2";
            entry.Body                 = "Blah1";
            entry.IsActive             = true;
            entry.DateCreated          =
                entry.DateSyndicated   =
                    entry.DateModified = DateTime.ParseExact("1976/05/25", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            entry.Categories.Add(category1Name);
            entry.Categories.Add(category2Name);
            UnitTestHelper.Create(entry);

            entry       = new Entry(PostType.BlogPost);
            entry.Title = "Title 3";
            entry.IncludeInMainSyndication = true;
            entry.Body                 = "Blah2";
            entry.IsActive             = true;
            entry.DateCreated          =
                entry.DateSyndicated   =
                    entry.DateModified = DateTime.ParseExact("1979/09/16", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            UnitTestHelper.Create(entry);

            entry       = new Entry(PostType.BlogPost);
            entry.Title = "Title 4";
            entry.IncludeInMainSyndication = true;
            entry.Body                 = "Blah3";
            entry.IsActive             = true;
            entry.DateCreated          =
                entry.DateSyndicated   =
                    entry.DateModified = DateTime.ParseExact("2006/01/01", "yyyy/MM/dd", CultureInfo.InvariantCulture);
            entry.Categories.Add(category2Name);
            int entryId = UnitTestHelper.Create(entry);

            string enclosureUrl      = "http://perseus.franklins.net/hanselminutes_0107.mp3";
            string enclosureMimeType = "audio/mp3";
            long   enclosureSize     = 26707573;

            FrameworkEnclosure enc =
                UnitTestHelper.BuildEnclosure("<Digital Photography Explained (for Geeks) with Aaron Hockley/>",
                                              enclosureUrl, enclosureMimeType, entryId, enclosureSize, true, true);

            Enclosures.Create(enc);

            posts = api.getRecentPosts(Config.CurrentBlog.Id.ToString(), "username", "password", 10);
            Assert.AreEqual(4, posts.Length, "Expected 4 posts");
            Assert.AreEqual(1, posts[3].categories.Length, "Expected our categories to be there.");
            Assert.AreEqual(2, posts[2].categories.Length, "Expected our categories to be there.");
            Assert.IsNotNull(posts[1].categories, "Expected our categories to be there.");
            Assert.AreEqual(1, posts[0].categories.Length, "Expected our categories to be there.");
            Assert.AreEqual(category1Name, posts[3].categories[0], "The category returned by the MetaBlogApi is wrong.");
            Assert.AreEqual(category2Name, posts[0].categories[0], "The category returned by the MetaBlogApi is wrong.");

            Assert.AreEqual(enclosureUrl, posts[0].enclosure.Value.url, "Not what we expected for the enclosure url.");
            Assert.AreEqual(enclosureMimeType, posts[0].enclosure.Value.type,
                            "Not what we expected for the enclosure mimetype.");
            Assert.AreEqual(enclosureSize, posts[0].enclosure.Value.length,
                            "Not what we expected for the enclosure size.");
        }