Add() public method

Adds the specified page to the search index.
An error occured with the lucene.net IndexWriter while adding the page to the index.
public Add ( PageViewModel model ) : void
model Roadkill.Core.Mvc.ViewModels.PageViewModel The page to add.
return void
Example #1
0
		public void Search_With_No_Field_Returns_Results()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "title content", "tag1", "title content1");
			PageViewModel page2 = CreatePage(2, "admin", "title content", "tag1", "title content2");

			searchService.Add(page1);
			searchService.Add(page2);

			// Act
			List<SearchResultViewModel> results = searchService.Search("title content").ToList();

			// Assert
			Assert.That(results.Count, Is.EqualTo(2)); // Lucene will ignore the 1 and 2 in the content
		}
        /// <summary>
        /// Adds the page to the database.
        /// </summary>
        /// <param name="model">The summary details for the page.</param>
        /// <returns>A <see cref="PageViewModel"/> for the newly added page.</returns>
        /// <exception cref="DatabaseException">An databaseerror occurred while saving.</exception>
        /// <exception cref="SearchException">An error occurred adding the page to the search index.</exception>
        public PageViewModel AddPage(PageViewModel model)
        {
            try
            {
                string currentUser   = _context.CurrentUsername;
                var    currentUserId = _context.CurrentUserId;

                Page page = new Page();
                page.Title                      = model.Title;
                page.Tags                       = model.CommaDelimitedTags();
                page.CreatedBy                  = AppendIpForDemoSite(currentUser);
                page.CreatedOn                  = DateTime.UtcNow;
                page.ModifiedOn                 = DateTime.UtcNow;
                page.ModifiedBy                 = AppendIpForDemoSite(currentUser);
                page.ProjectStart               = model.ProjectStart;
                page.ProjectEnd                 = model.ProjectEnd;
                page.ProjectEstimatedTime       = model.ProjectEstimatedTime;
                page.ProjectLanguage            = model.ProjectLanguage;
                page.ProjectStatus              = model.ProjectStatus;
                page.ProjectAgileLifeCyclePhase = model.ProjectAgileLifeCyclePhase;
                page.Department                 = model.Department;
                page.FundingBoundary            = model.FundingBoundary;
                page.orgID                      = model.orgID;

                // Double check, incase the HTML form was faked.
                if (_context.IsAdmin)
                {
                    page.IsLocked = model.IsLocked;
                }


                PageContent pageContent = Repository.AddNewPage(page, model.Content, AppendIpForDemoSite(currentUser), DateTime.UtcNow, model.ProjectStart, model.ProjectEnd, model.ProjectEstimatedTime, model.ProjectStatus, Phase2Params.FromModel(model), model.ProjectLanguage, model.orgID);

                _listCache.RemoveAll();
                _pageViewModelCache.RemoveAll(); // completely clear the cache to update any reciprocal links.

                // Update the lucene index
                PageViewModel savedModel = new PageViewModel(pageContent, _markupConverter);
                try
                {
                    _searchService.Add(savedModel);
                }
                catch (SearchException)
                {
                    // TODO: log
                }

                Repository.SetContributeAutoApprovedInProject(savedModel.Id, currentUserId, savedModel.orgID);
                return(savedModel);
            }
            catch (DatabaseException e)
            {
                throw new DatabaseException(e, "An error occurred while adding page '{0}' to the database", model.Title);
            }
        }
        public void CreatedBy_Only_Searchable_Using_Field_Syntax()
        {
            // Arrange
            SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
            searchService.CreateIndex();

            PageViewModel page1 = CreatePage(1, "admin", "random name2", "homepage, tag1", "title content 11");
            PageViewModel page2 = CreatePage(2, "admin", "random name2", "tag1, tag", "title content 2");

            searchService.Add(page1);
            searchService.Add(page2);

            // Act
            List<SearchResultViewModel> results = searchService.Search("admin").ToList();
            List<SearchResultViewModel> createdByResults = searchService.Search("createdby: admin").ToList();

            // Assert
            Assert.That(results.Count, Is.EqualTo(0), "admin title count");
            Assert.That(createdByResults.Count, Is.EqualTo(2), "createdby count");
        }
Example #4
0
		public void Search_By_Title()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "the title", "tag1", "title content");
			PageViewModel page2 = CreatePage(2, "admin", "random name1", "tag1", "title content");
			PageViewModel page3 = CreatePage(3, "admin", "random name2", "tag1", "title content");
			PageViewModel page4 = CreatePage(4, "admin", "random name3", "tag1", "title content");

			searchService.Add(page1);
			searchService.Add(page2);
			searchService.Add(page3);
			searchService.Add(page4);

			// Act
			List<SearchResultViewModel> results = searchService.Search("title:\"the title\"").ToList();

			// Assert
			Assert.That(results.Count, Is.EqualTo(1));
		}
        public void Cyrillic_Content_Should_Be_Stored_And_Retrieved_Correctly()
        {
            // Arrange
            SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
            searchService.CreateIndex();

            PageViewModel page1 = CreatePage(1, "admin", "ОШИБКА: неверная последовательность байт для кодировки", "tag1",
                "БД сервера событий была перенесена из PostgreSQL 8.3 на PostgreSQL 9.1.4. Сервер, развернутый на Windows платформе, не мог с ней работать, т.к. установщик PostgreSQL 9.1.4 создает шаблон базы с использованием кодировки UTF8 и, сответственно, новая БД не могла быть создана с требуемой");

            searchService.Add(page1);

            // Act
            List<SearchResultViewModel> results = searchService.Search("ОШИБКА").ToList();

            // Assert
            Assert.That(results[0].ContentSummary, Contains.Substring("БД сервера событий была перенесена из"));
        }
Example #6
0
        /// <summary>
        /// Adds the page to the database.
        /// </summary>
        /// <param name="model">The summary details for the page.</param>
        /// <returns>A <see cref="PageViewModel"/> for the newly added page.</returns>
        /// <exception cref="DatabaseException">An databaseerror occurred while saving.</exception>
        /// <exception cref="SearchException">An error occurred adding the page to the search index.</exception>
        public PageViewModel AddPage(PageViewModel model)
        {
            try
            {
                string currentUser = _context.CurrentUsername;

                Page page = new Page();
                page.Title      = model.Title;
                page.Tags       = model.CommaDelimitedTags();
                page.CreatedBy  = AppendIpForDemoSite(currentUser);
                page.CreatedOn  = DateTime.UtcNow;
                page.ModifiedOn = DateTime.UtcNow;
                page.ModifiedBy = AppendIpForDemoSite(currentUser);

                // Double check, incase the HTML form was faked.
                if (_context.IsAdmin)
                {
                    page.IsLocked = model.IsLocked;
                }

                PageContent pageContent = Repository.AddNewPage(page, model.Content, AppendIpForDemoSite(currentUser), DateTime.UtcNow);

                _listCache.RemoveAll();
                _pageViewModelCache.RemoveAll();                 // completely clear the cache to update any reciprocal links.

                // Update the lucene index
                PageViewModel savedModel = new PageViewModel(pageContent, _markupConverter);
                try
                {
                    _searchService.Add(savedModel);
                }
                catch (SearchException)
                {
                    // TODO: log
                }

                return(savedModel);
            }
            catch (DatabaseException e)
            {
                throw new DatabaseException(e, "An error occurred while adding page '{0}' to the database", model.Title);
            }
        }
        public void Delete_Should_Remove_Page_From_Index()
        {
            // Arrange
            SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
            searchService.CreateIndex();

            PageViewModel page1 = CreatePage(1, "admin", "homepage title", "homepage1, tag1", "title content");
            PageViewModel page2 = CreatePage(2, "admin", "random name2", "tag1", "random name 2");

            searchService.Add(page1);
            searchService.Add(page2);

            // Act
            searchService.Delete(page1);
            List<SearchResultViewModel> results = searchService.Search("homepage title").ToList();

            // Assert
            Assert.That(results.Count, Is.EqualTo(0), "homepage title still appears after deletion");
        }
Example #8
0
		public void Search_By_TagsField_Returns_Multiple_Results()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "random name1", "homepage1, tag1", "title content");
			PageViewModel page2 = CreatePage(2, "admin", "random name2", "tag1, tag", "title content");
			PageViewModel page3 = CreatePage(3, "admin", "random name3", "tag3, tag", "title content");
			PageViewModel page4 = CreatePage(4, "admin", "random name4", "tag4, tag", "title content");

			searchService.Add(page1);
			searchService.Add(page2);
			searchService.Add(page3);
			searchService.Add(page4);

			// Act
			List<SearchResultViewModel> results = searchService.Search("tags:\"tag1\"").ToList();

			// Assert
			Assert.That(results.Count, Is.EqualTo(2));
		}
Example #9
0
		public void GetContentSummary_Should_Remove_Html_From_Summary()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "A page title", "tag1", "**some bold** \n\n=my header=");
			searchService.Add(page1);

			// Act
			List<SearchResultViewModel> results = searchService.Search("my header").ToList();

			// Assert
			Assert.That(results[0].ContentSummary, Is.Not.StringContaining("<b>some bold</b>"));
			Assert.That(results[0].ContentSummary, Is.Not.StringContaining("<h1>my header</h1>"));

		}
Example #10
0
		public void GetContentSummary_Should_Only_Contain_First_150_Characters_For_Summary()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "A page title", "tag1", "Lorizzle ipsizzle dolor sit amizzle, (pre character 150 boundary) rizzle adipiscing tellivizzle. Nullizzle sapizzle velizzle, yo mamma volutpat, suscipizzle bow wow wow, gravida vizzle, (post 150 character boundary) shizznit. Pellentesque da bomb tortizzle. Hizzle erizzle. Its fo rizzle izzle sheezy dapibizzle mofo tempizzle tempizzle. Maurizzle away nibh izzle turpis. Phat izzle hizzle. Pellentesque eleifend rhoncus rizzle. Da bomb things dang platea dictumst. Fo shizzle my nizzle dapibizzle. Shiz tellus owned, pretizzle eu, mattizzle ac, bow wow wow its fo rizzle, nunc. Shiz suscipit. Integizzle own yo' we gonna chung sed go to hizzle.");
			searchService.Add(page1);

			// Act
			List<SearchResultViewModel> results = searchService.Search("rizzle").ToList();

			// Assert
			Assert.That(results[0].ContentSummary, Contains.Substring("(pre character 150 boundary)"));
			Assert.That(results[0].ContentSummary, Is.Not.StringContaining("(post character 150 boundary)"));

		}
Example #11
0
		public void Update_Should_Show_In_Index_Search()
		{
			// Arrange
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "homepage title", "homepage1, tag1", "title content");
			PageViewModel page2 = CreatePage(2, "admin", "random name2", "tag1", "random name 2");

			searchService.Add(page1);
			searchService.Add(page2);

			// Act
			page1.Title = "A new hope";
			searchService.Update(page1);

			Thread thread = new Thread(delegate()
			{
				// Perform the test in a new thread, so that the add + delete commit is picked up
				// which is periodically done by Lucene.
				List<SearchResultViewModel> oldResults = searchService.Search("homepage title").ToList();
				List<SearchResultViewModel> newResults = searchService.Search("A new hope").ToList();

				// Assert
				Assert.That(oldResults.Count, Is.EqualTo(0), "old results");
				Assert.That(newResults.Count, Is.EqualTo(1), "new results");
			});
			thread.Start();
		}
Example #12
0
		public void Search_By_CreatedOnField_Returns_Results()
		{
			// Arrange
			string todaysDate = DateTime.Today.ToShortDateString(); // (SearchService stores dates, not times)
			SearchService searchService = new SearchService(_config, _repository, _pluginFactory);
			searchService.CreateIndex();

			PageViewModel page1 = CreatePage(1, "admin", "random name2", "homepage, tag1", "title content", DateTime.Today);
			PageViewModel page2 = CreatePage(2, "admin", "random name2", "tag1, tag", "title content", DateTime.Today);
			PageViewModel page3 = CreatePage(3, "admin", "random name3", "tag1, tag", "title content", DateTime.Today.AddDays(1));
			PageViewModel page4 = CreatePage(4, "admin", "random name4", "tag1, tag", "title content", DateTime.Today.AddDays(2));

			searchService.Add(page1);
			searchService.Add(page2);
			searchService.Add(page3);
			searchService.Add(page4);

			// Act
			List<SearchResultViewModel> createdOnResults = searchService.Search("createdon:" +todaysDate).ToList();

			// Assert
			Assert.That(createdOnResults.Count, Is.EqualTo(2), "createdon count");
		}