public void Should_Replace_Known_Tokens_When_Logged_In_As_Editor() { // Arrange string menuMarkup = "* %categories%\r\n\r\n%allpages%\r\n%mainpage%\r\n%newpage%\r\n%managefiles%\r\n%sitesettings%\r\n"; string expectedHtml = "<ul><li><a href=\"/pages/alltags\">Categories</a></li></ul>" + "<a href=\"/pages/allpages\">All pages</a>" + "<a href=\"/\">Main Page</a>" + "<a href=\"/pages/new\">New page</a><a href=\"/filemanager\">Manage files</a>"; RepositoryMock repository = new RepositoryMock(); repository.SiteSettings = new SiteSettings(); repository.SiteSettings.MarkupType = "Markdown"; repository.SiteSettings.MenuMarkup = menuMarkup; UserContextStub userContext = new UserContextStub(); userContext.IsAdmin = false; userContext.IsLoggedIn = true; ApplicationSettings applicationSettings = new ApplicationSettings(); applicationSettings.Installed = true; CacheMock cache = new CacheMock(); SiteCache siteCache = new SiteCache(applicationSettings, cache); MarkupConverter converter = new MarkupConverter(applicationSettings, repository, _pluginFactory); MenuParser parser = new MenuParser(converter, repository, siteCache, userContext); // Act string actualHtml = parser.GetMenu(); // Assert Assert.That(actualHtml, Is.EqualTo(expectedHtml), actualHtml); }
public void addloggedinmenu_should_cache_html() { // Arrange CacheMock cache = new CacheMock(); SiteCache siteCache = new SiteCache(cache); // Act siteCache.AddLoggedInMenu("some html"); // Assert Assert.That(cache.Count(), Is.EqualTo(1)); IEnumerable<string> keys = cache.Select(x => x.Key); Assert.That(keys, Contains.Item(CacheKeys.LoggedInMenuKey())); }
public void Should_Add_Item() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems = new List<string>() { "1", "2" }; ListCache listCache = new ListCache(settings, cache); // Act listCache.Add("all.tags", tagCacheItems); // Assert Assert.That(cache.CacheItems.Count, Is.EqualTo(1)); }
public void AddMenu_Should_Cache_Html() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings(); SiteCache siteCache = new SiteCache(settings, cache); // Act siteCache.AddMenu("some html"); // Assert Assert.That(cache.Count(), Is.EqualTo(1)); IEnumerable<string> keys = cache.Select(x => x.Key); Assert.That(keys, Contains.Item(CacheKeys.MenuKey())); }
public void getmenu_should_return_correct_html() { // Arrange string expectedHtml = "some html"; CacheMock cache = new CacheMock(); SiteCache siteCache = new SiteCache(cache); siteCache.AddMenu(expectedHtml); // Act string actualHtml = siteCache.GetMenu(); // Assert Assert.That(actualHtml, Is.EqualTo(expectedHtml)); }
public void GetAdminMenu_Should_Return_Correct_Html() { // Arrange string expectedHtml = "some html"; CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings(); SiteCache siteCache = new SiteCache(settings, cache); siteCache.AddAdminMenu(expectedHtml); // Act string actualHtml = siteCache.GetAdminMenu(); // Assert Assert.That(actualHtml, Is.EqualTo(expectedHtml)); }
public void removeall_should_remove_pageviewmodelcache_keys_only() { // Arrange CacheMock cache = new CacheMock(); cache.Add("site.blah", "xyz", new CacheItemPolicy()); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = false }; PageViewModelCache pageCache = new PageViewModelCache(settings, cache); pageCache.Add(1, 1, new PageViewModel()); // Act pageCache.RemoveAll(); // Assert Assert.That(cache.Count(), Is.EqualTo(1)); }
public void Should_Get_Item() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems = new List<string>() { "1", "2" }; AddToCache(cache, "all.tags", tagCacheItems); ListCache listCache = new ListCache(settings, cache); // Act var tags = listCache.Get<string>("all.tags"); // Assert Assert.That(tags, Is.EqualTo(tagCacheItems)); }
public void Should_Remove_Item() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems = new List<string>() { "1", "2" }; AddToCache(cache, "all.tags", tagCacheItems); ListCache listCache = new ListCache(settings, cache); // Act listCache.Remove("all.tags"); // Assert var tags = cache.CacheItems.FirstOrDefault(); Assert.That(tags, Is.Null); }
public void GetById_Should_Load_From_Cache_When_PageSummary_Exists_In_Cache() { // Arrange RepositoryMock repository = new RepositoryMock(); CacheMock pageModelCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, null, repository); PageViewModel expectedModel = CreatePageViewModel(); string cacheKey = CacheKeys.PageViewModelKey(1, PageViewModelCache.LATEST_VERSION_NUMBER); pageModelCache.Add(cacheKey, expectedModel, new CacheItemPolicy()); // Act PageViewModel actualModel = pageService.GetById(1); // Assert Assert.That(actualModel.Id, Is.EqualTo(expectedModel.Id)); Assert.That(actualModel.VersionNumber, Is.EqualTo(expectedModel.VersionNumber)); Assert.That(actualModel.Title, Is.EqualTo(expectedModel.Title)); }
public void Should_GetAllKeys() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems1 = new List<string>() { "1", "2" }; List<string> tagCacheItems2 = new List<string>() { "a", "b" }; ListCache listCache = new ListCache(settings, cache); // Act listCache.Add("all.tags1", tagCacheItems1); listCache.Add("all.tags2", tagCacheItems2); // Assert List<string> keys = listCache.GetAllKeys().ToList(); Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags1"))); Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags2"))); }
public void AddPage_Should_Clear_List_And_PageSummary_Caches() { // Arrange RepositoryMock repository = new RepositoryMock(); CacheMock pageModelCache = new CacheMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, listCache, repository); PageViewModel expectedModel = CreatePageViewModel(); AddPageCacheItem(pageModelCache, "key", expectedModel); AddListCacheItem(listCache, "key", new List<string>() { "tag1", "tag2" }); // Act pageService.AddPage(new PageViewModel() { Title = "totoro" }); // Assert Assert.That(pageModelCache.CacheItems.Count, Is.EqualTo(0)); Assert.That(listCache.CacheItems.Count, Is.EqualTo(0)); }
public void AllPagesCreatedBy_Should_Add_To_Cache_When_Cache_Is_Empty() { // Arrange string adminCacheKey = CacheKeys.AllPagesCreatedByKey("admin"); RepositoryMock repository = new RepositoryMock(); repository.AddNewPage(new Page() { Title = "1" }, "text", "admin", DateTime.UtcNow); repository.AddNewPage(new Page() { Title = "2" }, "text", "admin", DateTime.UtcNow); repository.AddNewPage(new Page() { Title = "3" }, "text", "editor", DateTime.UtcNow); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(null, listCache, repository); // Act pageService.AllPagesCreatedBy("admin"); // Assert Assert.That(listCache.CacheItems.Count, Is.EqualTo(1)); Assert.That(listCache.CacheItems.FirstOrDefault().Key, Is.EqualTo(adminCacheKey)); }
public void getbyid_should_load_from_cache_when_pagesummary_exists_in_cache() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); CacheMock pageModelCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, null, settingsRepository, pageRepository); PageViewModel expectedModel = CreatePageViewModel(); string cacheKey = CacheKeys.PageViewModelKey(1, PageViewModelCache.LATEST_VERSION_NUMBER); pageModelCache.Add(cacheKey, expectedModel, new CacheItemPolicy()); // Act PageViewModel actualModel = pageService.GetById(1); // Assert Assert.That(actualModel.Id, Is.EqualTo(expectedModel.Id)); Assert.That(actualModel.VersionNumber, Is.EqualTo(expectedModel.VersionNumber)); Assert.That(actualModel.Title, Is.EqualTo(expectedModel.Title)); }
public void Setup() { _pluginFactory = new PluginFactoryMock(); _pageRepository = new PageRepositoryMock(); _settingsRepository = new SettingsRepositoryMock(); _settingsRepository.SiteSettings = new SiteSettings(); _settingsRepository.SiteSettings.MarkupType = "Markdown"; _userContext = new UserContextStub(); _applicationSettings = new ApplicationSettings(); _applicationSettings.Installed = true; _cache = new CacheMock(); _siteCache = new SiteCache(_cache); _converter = new MarkupConverter(_applicationSettings, _settingsRepository, _pageRepository, _pluginFactory); _menuParser = new MenuParser(_converter, _settingsRepository, _siteCache, _userContext); }
public void RemoveAll_Should_Remove_ListCache_Keys_Only() { // Arrange CacheMock cache = new CacheMock(); cache.Add("site.blah", "xyz", new CacheItemPolicy()); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems1 = new List<string>() { "1", "2" }; List<string> tagCacheItems2 = new List<string>() { "1", "2" }; AddToCache(cache, "all.tags1", tagCacheItems1); AddToCache(cache, "all.tags2", tagCacheItems2); ListCache listCache = new ListCache(settings, cache); // Act listCache.RemoveAll(); // Assert Assert.That(cache.Count(), Is.EqualTo(1)); }
public void GetById_Should_Add_To_Cache_When_PageSummary_Does_Not_Exist_In_Cache() { // Arrange RepositoryMock repository = new RepositoryMock(); CacheMock pageModelCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, null, repository); PageViewModel expectedModel = CreatePageViewModel(); expectedModel = pageService.AddPage(expectedModel); // get it back to update the version no. // Act pageService.GetById(1); // Assert CacheItem cacheItem = pageModelCache.CacheItems.First(); string cacheKey = CacheKeys.PageViewModelKey(1, PageViewModelCache.LATEST_VERSION_NUMBER); Assert.That(cacheItem.Key, Is.EqualTo(cacheKey)); PageViewModel actualModel = (PageViewModel) cacheItem.Value; Assert.That(actualModel.Id, Is.EqualTo(expectedModel.Id)); Assert.That(actualModel.VersionNumber, Is.EqualTo(expectedModel.VersionNumber)); Assert.That(actualModel.Title, Is.EqualTo(expectedModel.Title)); }
public void Should_Cache_Menu_Html_For_Admin_And_Editor_And_Guest_User() { // Arrange string menuMarkup = "My menu %newpage% %sitesettings%"; RepositoryMock repository = new RepositoryMock(); repository.SiteSettings = new SiteSettings(); repository.SiteSettings.MarkupType = "Markdown"; repository.SiteSettings.MenuMarkup = menuMarkup; UserContextStub userContext = new UserContextStub(); ApplicationSettings applicationSettings = new ApplicationSettings(); applicationSettings.Installed = true; CacheMock cache = new CacheMock(); SiteCache siteCache = new SiteCache(applicationSettings, cache); MarkupConverter converter = new MarkupConverter(applicationSettings, repository, _pluginFactory); MenuParser parser = new MenuParser(converter, repository, siteCache, userContext); // Act userContext.IsLoggedIn = false; userContext.IsAdmin = false; parser.GetMenu(); userContext.IsLoggedIn = true; userContext.IsAdmin = false; parser.GetMenu(); userContext.IsLoggedIn = true; userContext.IsAdmin = true; parser.GetMenu(); // Assert Assert.That(cache.CacheItems.Count, Is.EqualTo(3)); }
public void getbyid_should_add_to_cache_when_pagesummary_does_not_exist_in_cache() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); CacheMock pageModelCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, null, settingsRepository, pageRepository); PageViewModel expectedModel = CreatePageViewModel(); expectedModel = pageService.AddPage(expectedModel); // get it back to update the version no. // Act pageService.GetById(1); // Assert CacheItem cacheItem = pageModelCache.CacheItems.First(); string cacheKey = CacheKeys.PageViewModelKey(1, PageViewModelCache.LATEST_VERSION_NUMBER); Assert.That(cacheItem.Key, Is.EqualTo(cacheKey)); PageViewModel actualModel = (PageViewModel)cacheItem.Value; Assert.That(actualModel.Id, Is.EqualTo(expectedModel.Id)); Assert.That(actualModel.VersionNumber, Is.EqualTo(expectedModel.VersionNumber)); Assert.That(actualModel.Title, Is.EqualTo(expectedModel.Title)); }
public void addpage_should_clear_list_and_pagesummary_caches() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); CacheMock pageModelCache = new CacheMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(pageModelCache, listCache, settingsRepository, pageRepository); PageViewModel expectedModel = CreatePageViewModel(); AddPageCacheItem(pageModelCache, "key", expectedModel); AddListCacheItem(listCache, "key", new List<string>() { "tag1", "tag2" }); // Act pageService.AddPage(new PageViewModel() { Title = "totoro" }); // Assert Assert.That(pageModelCache.CacheItems.Count, Is.EqualTo(0)); Assert.That(listCache.CacheItems.Count, Is.EqualTo(0)); }
private void AddListCacheItem(CacheMock cache, string key, object value) { cache.Add(CacheKeys.ListCacheKey(key), value, new CacheItemPolicy()); }
private void AddPageCacheItem(CacheMock cache, string key, object value) { cache.Add(CacheKeys.PAGEVIEWMODEL_CACHE_PREFIX + key, value, new CacheItemPolicy()); }
public void renametag_should_clear_listcache() { // Arrange string tag1CacheKey = CacheKeys.PagesByTagKey("tag1"); SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Tags = "homepage, tag1" }, "text1", "admin", DateTime.UtcNow); CacheMock listCache = new CacheMock(); PageViewModel homepageModel = CreatePageViewModel(); PageViewModel page1Model = CreatePageViewModel(); AddListCacheItem(listCache, tag1CacheKey, new List<PageViewModel>() { homepageModel, page1Model }); PageService pageService = CreatePageService(null, listCache, settingsRepository, pageRepository); // Act pageService.RenameTag("tag1", "some.other.tag"); // calls UpdatePage, which clears the cache // Assert Assert.That(listCache.CacheItems.Count, Is.EqualTo(0)); }
public void updatepage_should_remove_homepage_from_cache_when_homepage_is_updated() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Tags = "homepage" }, "text", "admin", DateTime.UtcNow); CacheMock pageCache = new CacheMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(pageCache, listCache, settingsRepository, pageRepository); PageViewModel homepageModel = CreatePageViewModel(); homepageModel.RawTags = "homepage"; pageCache.Add(CacheKeys.HomepageKey(), homepageModel, new CacheItemPolicy()); // Act pageService.UpdatePage(homepageModel); // Assert Assert.That(pageCache.CacheItems.Count, Is.EqualTo(0)); }
public void updatepage_should_clear_list_cache_and_pagesummary_cache() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Tags = "homepage" }, "text", "admin", DateTime.UtcNow); pageRepository.AddNewPage(new Page() { Tags = "tag2" }, "text", "admin", DateTime.UtcNow); CacheMock pageCache = new CacheMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(pageCache, listCache, settingsRepository, pageRepository); PageViewModel homepageModel = CreatePageViewModel(); homepageModel.Id = 1; PageViewModel page2Model = CreatePageViewModel(); page2Model.Id = 2; AddPageCacheItem(pageCache, CacheKeys.HomepageKey(), homepageModel); pageCache.Add(CacheKeys.PageViewModelKey(2, 0), page2Model, new CacheItemPolicy()); AddListCacheItem(listCache, CacheKeys.AllTags(), new List<string>() { "tag1", "tag2" }); // Act pageService.UpdatePage(page2Model); // Assert Assert.That(pageCache.CacheItems.Count, Is.EqualTo(1)); Assert.That(listCache.CacheItems.Count, Is.EqualTo(0)); }
public void findbytag_should_add_to_cache_when_cache_is_empty() { // Arrange string cacheKey = CacheKeys.PagesByTagKey("tag1"); SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Title = "1", Tags = "tag1" }, "text", "admin", DateTime.UtcNow); pageRepository.AddNewPage(new Page() { Title = "2", Tags = "tag2" }, "text", "admin", DateTime.UtcNow); pageRepository.AddNewPage(new Page() { Title = "2", Tags = "tag3" }, "text", "admin", DateTime.UtcNow); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(null, listCache, settingsRepository, pageRepository); // Act pageService.FindByTag("tag1"); // Assert Assert.That(listCache.CacheItems.Count, Is.EqualTo(1)); Assert.That(listCache.CacheItems.FirstOrDefault().Key, Is.EqualTo(cacheKey)); }
public void findbytag_should_load_from_cache() { string tag1CacheKey = CacheKeys.PagesByTagKey("tag1"); string tag2CacheKey = CacheKeys.PagesByTagKey("tag2"); // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(null, listCache, settingsRepository, pageRepository); PageViewModel tag1Model = CreatePageViewModel(); tag1Model.RawTags = "tag1"; PageViewModel tag2Model = CreatePageViewModel(); tag2Model.RawTags = "tag2"; listCache.Add(tag1CacheKey, new List<PageViewModel>() { tag1Model }, new CacheItemPolicy()); listCache.Add(tag2CacheKey, new List<PageViewModel>() { tag2Model }, new CacheItemPolicy()); // Act IEnumerable<PageViewModel> actualList = pageService.FindByTag("tag1"); // Assert Assert.That(actualList, Contains.Item(tag1Model)); Assert.That(actualList, Is.Not.Contains(tag2Model)); }
public void findhomepage_should_add_to_cache_when_cache_is_empty() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page() { Title = "1", Tags = "homepage" }, "text", "admin", DateTime.UtcNow); CacheMock pageCache = new CacheMock(); PageService pageService = CreatePageService(pageCache, null, settingsRepository, pageRepository); // Act pageService.FindHomePage(); // Assert Assert.That(pageCache.CacheItems.Count, Is.EqualTo(1)); Assert.That(pageCache.CacheItems.FirstOrDefault().Key, Is.EqualTo(CacheKeys.HomepageKey())); }
public void findhomepage_should_load_from_cache() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); CacheMock modelCache = new CacheMock(); PageService pageService = CreatePageService(modelCache, null, settingsRepository, pageRepository); PageViewModel expectedModel = CreatePageViewModel(); expectedModel.RawTags = "homepage"; modelCache.Add(CacheKeys.HomepageKey(), expectedModel, new CacheItemPolicy()); // Act PageViewModel actualModel = pageService.FindHomePage(); // Assert Assert.That(actualModel.Id, Is.EqualTo(expectedModel.Id)); Assert.That(actualModel.VersionNumber, Is.EqualTo(expectedModel.VersionNumber)); Assert.That(actualModel.Title, Is.EqualTo(expectedModel.Title)); }
public void deletepage_should_clear_list_and_pagesummary_caches() { // Arrange SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock(); PageRepositoryMock pageRepository = new PageRepositoryMock(); pageRepository.AddNewPage(new Page(), "text", "admin", DateTime.UtcNow); CacheMock pageCache = new CacheMock(); CacheMock listCache = new CacheMock(); PageService pageService = CreatePageService(pageCache, listCache, settingsRepository, pageRepository); PageViewModel expectedModel = CreatePageViewModel(); AddPageCacheItem(pageCache, "key", expectedModel); AddListCacheItem(listCache, "key", new List<string>() { "tag1", "tag2" }); // Act pageService.DeletePage(1); // Assert Assert.That(pageCache.CacheItems.Count, Is.EqualTo(0)); Assert.That(listCache.CacheItems.Count, Is.EqualTo(0)); }