public void put_should_update_page() { // Arrange DateTime version1Date = DateTime.Today.AddDays(-1); // stops the getlatestcontent acting up when add+update are the same time Page page = new Page(); page.Title = "Hello world"; page.Tags = "tag1, tag2"; page.CreatedOn = version1Date; page.ModifiedOn = version1Date; PageContent pageContent = _pageRepositoryMock.AddNewPage(page, "Some content1", "editor", version1Date); PageViewModel model = new PageViewModel(pageContent.Page); model.Title = "New title"; model.Content = "Some content2"; model.ModifiedOn = DateTime.UtcNow; // Act _pagesController.Put(model); // Assert Assert.That(_pageService.AllPages().Count(), Is.EqualTo(1)); PageViewModel actualPage = _pageService.GetById(1, true); Assert.That(actualPage.Title, Is.EqualTo("New title")); Assert.That(actualPage.Content, Is.EqualTo("Some content2")); }
public void AllPages_Should_Add_To_Cache_When_Cache_Is_Empty(bool loadPageContent) { // Arrange string cacheKey = (loadPageContent) ? (CacheKeys.AllPagesWithContent()) : (CacheKeys.AllPages()); SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Title = "1" }, "text", "admin", DateTime.UtcNow); pageRepository.AddNewPage(new Page() { Title = "2" }, "text", "admin", DateTime.UtcNow); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(null, listCache, settingsRepository, pageRepository); // Act pageService.AllPages(loadPageContent); // Assert Assert.That(listCache.CacheItems.Count, Is.EqualTo(1)); Assert.That(listCache.CacheItems.FirstOrDefault().Key, Is.EqualTo(cacheKey)); }
public void DeletePage_Should_Remove_Correct_Page() { // Arrange PageViewModel page1 = AddToStubbedRepository(1, "admin", "Homepage", "homepage;"); PageViewModel page2 = AddToStubbedRepository(2, "admin", "page 2", "page2;"); PageViewModel page3 = AddToStubbedRepository(3, "admin", "page 3", "page3;"); PageViewModel page4 = AddToStubbedRepository(4, "admin", "page 4", "animals;"); PageViewModel page5 = AddToStubbedRepository(5, "admin", "page 5", "animals;"); // Act _pageService.DeletePage(page1.Id); _pageService.DeletePage(page2.Id); List <PageViewModel> models = _pageService.AllPages().ToList(); // Assert Assert.That(models.Count, Is.EqualTo(3), "Page count"); Assert.That(models.FirstOrDefault(p => p.Title == "Homepage"), Is.Null); Assert.That(models.FirstOrDefault(p => p.Title == "page 2"), Is.Null); }
public override ActionResult GetResult(SpecialPagesController controller) { RouteValueDictionary routeValueDictionary = new RouteValueDictionary(); List <PageViewModel> pages = PageService.AllPages().ToList(); if (pages.Count == 0) { routeValueDictionary.Add("controller", "Home"); routeValueDictionary.Add("action", "Index"); } else { int randomIndex = _random.Next(0, pages.Count - 1); PageViewModel randomPage = pages[randomIndex]; routeValueDictionary.Add("controller", "Wiki"); routeValueDictionary.Add("action", "Index"); routeValueDictionary.Add("id", randomPage.Id); } return(new RedirectToRouteResult(routeValueDictionary)); }
public void AllPages_Should_Load_From_Cache(bool loadPageContent) { string cacheKey = (loadPageContent) ? (CacheKeys.AllPagesWithContent()) : (CacheKeys.AllPages()); // Arrange RepositoryMock repository = new RepositoryMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(null, listCache, repository); PageViewModel expectedModel = CreatePageViewModel(); AddListCacheItem(listCache, cacheKey, new List <PageViewModel>() { expectedModel }); // Act IEnumerable <PageViewModel> actualList = pageService.AllPages(loadPageContent); // Assert Assert.That(actualList, Contains.Item(expectedModel)); }
public virtual void ExportAsWikiFiles(string filename) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename"); } IEnumerable <PageViewModel> pages = _pageService.AllPages(); char[] invalidChars = Path.GetInvalidFileNameChars(); if (!Directory.Exists(ExportFolder)) { Directory.CreateDirectory(ExportFolder); } string zipFullPath = Path.Combine(ExportFolder, filename); if (File.Exists(zipFullPath)) { File.Delete(zipFullPath); } using (ZipFile zip = new ZipFile(zipFullPath)) { int index = 0; List <string> filenames = new List <string>(); foreach (PageViewModel summary in pages.OrderBy(p => p.Title)) { // Double check for blank titles, as the API can add // pages with blanks titles even though the UI doesn't allow it. if (string.IsNullOrEmpty(summary.Title)) { summary.Title = "(No title -" + summary.Id + ")"; } string filePath = summary.Title; // Ensure the filename is unique as its title based. // Simply replace invalid path characters with a '-' foreach (char item in invalidChars) { filePath = filePath.Replace(item, '-'); } if (filenames.Contains(filePath)) { filePath += (++index) + ""; } else { index = 0; } filenames.Add(filePath); filePath = Path.Combine(ExportFolder, filePath); filePath += ".wiki"; string content = "Tags:" + summary.SpaceDelimitedTags() + "\r\n" + summary.Content; Console.WriteLine(filePath); File.WriteAllText(filePath, content); if (!zip.ContainsEntry(filePath)) { zip.AddFile(filePath, ""); } } zip.Save(); } }