public void Index_Delete_Index_Item_Ensure_Heirarchy_Removed()
        {
            var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false);

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir))
                    using (indexer.ProcessNonAsync())
                    {
                        var searcher = indexer.GetSearcher();

                        //create the whole thing
                        rebuilder.Populate(indexer);

                        //now delete a node that has children

                        indexer.DeleteFromIndex(1140.ToString());
                        //this node had children: 1141 & 1142, let's ensure they are also removed

                        var results = searcher.CreateQuery().Id(1141).Execute();
                        Assert.AreEqual(0, results.Count());

                        results = searcher.CreateQuery().Id(1142).Execute();
                        Assert.AreEqual(0, results.Count());
                    }
        }
        public void Index_Reindex_Content()
        {
            var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false);

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir,
                                                                        validator: new ContentValueSetValidator(false)))
                    using (indexer.ProcessNonAsync())
                    {
                        var searcher = indexer.GetSearcher();

                        //create the whole thing
                        rebuilder.Populate(indexer);

                        var result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute();
                        Assert.AreEqual(21, result.TotalItemCount);

                        //delete all content
                        foreach (var r in result)
                        {
                            indexer.DeleteFromIndex(r.Id);
                        }


                        //ensure it's all gone
                        result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute();
                        Assert.AreEqual(0, result.TotalItemCount);

                        //call our indexing methods
                        rebuilder.Populate(indexer);

                        result = searcher.CreateQuery().Field(LuceneIndex.CategoryFieldName, IndexTypes.Content).Execute();
                        Assert.AreEqual(21, result.TotalItemCount);
                    }
        }
        public void Index_Protected_Content_Not_Indexed()
        {
            var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false);


            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir))
                    using (indexer.ProcessNonAsync())
                        using (var searcher = ((LuceneSearcher)indexer.GetSearcher()).GetLuceneSearcher())
                        {
                            //create the whole thing
                            rebuilder.Populate(indexer);


                            var protectedQuery = new BooleanQuery();
                            protectedQuery.Add(
                                new BooleanClause(
                                    new TermQuery(new Term(LuceneIndex.CategoryFieldName, IndexTypes.Content)),
                                    Occur.MUST));

                            protectedQuery.Add(
                                new BooleanClause(
                                    new TermQuery(new Term(LuceneIndex.ItemIdFieldName, ExamineDemoDataContentService.ProtectedNode.ToString())),
                                    Occur.MUST));

                            var collector = TopScoreDocCollector.Create(100, true);

                            searcher.Search(protectedQuery, collector);

                            Assert.AreEqual(0, collector.TotalHits, "Protected node should not be indexed");
                        }
        }
        public void Events_Ignoring_Node()
        {
            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir,
                                                                        //make parent id 999 so all are ignored
                                                                        validator: new ContentValueSetValidator(false, 999)))
                    using (indexer.ProcessNonAsync())
                    {
                        var searcher = indexer.GetSearcher();

                        var contentService = new ExamineDemoDataContentService();
                        //get a node from the data repo
                        var node = contentService.GetPublishedContentByXPath("//*[string-length(@id)>0 and number(@id)>0]")
                                   .Root
                                   .Elements()
                                   .First();

                        var valueSet = node.ConvertToValueSet(IndexTypes.Content);
                        indexer.IndexItems(new[] { valueSet });

                        var found = searcher.CreateQuery().Id((string)node.Attribute("id")).Execute();

                        Assert.AreEqual(0, found.TotalItemCount);
                    }
        }
        public void Index_Move_Media_From_Non_Indexable_To_Indexable_ParentID()
        {
            // create a validator with
            // publishedValuesOnly false
            // parentId 1116 (only content under that parent will be indexed)
            var validator = new ContentValueSetValidator(false, 1116);

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir, validator: validator))
                    using (indexer.ProcessNonAsync())
                    {
                        var searcher = indexer.GetSearcher();

                        //get a node from the data repo (this one exists underneath 2222)
                        var node = _mediaService.GetLatestMediaByXpath("//*[string-length(@id)>0 and number(@id)>0]")
                                   .Root.Elements()
                                   .First(x => (int)x.Attribute("id") == 2112);

                        var currPath = (string)node.Attribute("path"); //should be : -1,1111,2222,2112
                        Assert.AreEqual("-1,1111,2222,2112", currPath);

                        //ensure it's indexed
                        indexer.IndexItem(node.ConvertToValueSet(IndexTypes.Media));

                        //it will not exist because it exists under 2222
                        var results = searcher.CreateQuery().Id(2112).Execute();
                        Assert.AreEqual(0, results.Count());

                        //now mimic moving 2112 to 1116
                        //node.SetAttributeValue("path", currPath.Replace("2222", "1116"));
                        node.SetAttributeValue("path", "-1,1116,2112");
                        node.SetAttributeValue("parentID", "1116");

                        //now reindex the node, this should first delete it and then WILL add it because of the parent id constraint
                        indexer.IndexItems(new[] { node.ConvertToValueSet(IndexTypes.Media) });

                        //now ensure it exists
                        results = searcher.CreateQuery().Id(2112).Execute();
                        Assert.AreEqual(1, results.Count());
                    }
        }
        public void Rebuild_Index()
        {
            var contentRebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockContentService(), ScopeProvider.SqlContext, false);
            var mediaRebuilder   = IndexInitializer.GetMediaIndexRebuilder(Factory.GetInstance <PropertyEditorCollection>(), IndexInitializer.GetMockMediaService());

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir,
                                                                        validator: new ContentValueSetValidator(false)))
                    using (indexer.ProcessNonAsync())
                    {
                        var searcher = indexer.GetSearcher();

                        //create the whole thing
                        contentRebuilder.Populate(indexer);
                        mediaRebuilder.Populate(indexer);

                        var result = searcher.CreateQuery().All().Execute();

                        Assert.AreEqual(29, result.TotalItemCount);
                    }
        }
        public void Index_Property_Data_With_Value_Indexer()
        {
            var contentValueSetBuilder = IndexInitializer.GetContentValueSetBuilder(Factory.GetInstance <PropertyEditorCollection>(), false);

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir,
                                                                        validator: new ContentValueSetValidator(false)))
                    using (indexer.ProcessNonAsync())
                    {
                        indexer.CreateIndex();

                        var contentType = MockedContentTypes.CreateBasicContentType();
                        contentType.AddPropertyType(new PropertyType("test", ValueStorageType.Ntext)
                        {
                            Alias = "grid",
                            Name  = "Grid",
                            PropertyEditorAlias = Core.Constants.PropertyEditors.Aliases.Grid
                        });
                        var content = MockedContent.CreateBasicContent(contentType);
                        content.Id   = 555;
                        content.Path = "-1,555";
                        var gridVal = new GridValue
                        {
                            Name     = "n1",
                            Sections = new List <GridValue.GridSection>
                            {
                                new GridValue.GridSection
                                {
                                    Grid = "g1",
                                    Rows = new List <GridValue.GridRow>
                                    {
                                        new GridValue.GridRow
                                        {
                                            Id    = Guid.NewGuid(),
                                            Name  = "row1",
                                            Areas = new List <GridValue.GridArea>
                                            {
                                                new GridValue.GridArea
                                                {
                                                    Grid     = "g2",
                                                    Controls = new List <GridValue.GridControl>
                                                    {
                                                        new GridValue.GridControl
                                                        {
                                                            Editor = new GridValue.GridEditor
                                                            {
                                                                Alias = "editor1",
                                                                View  = "view1"
                                                            },
                                                            Value = "value1"
                                                        },
                                                        new GridValue.GridControl
                                                        {
                                                            Editor = new GridValue.GridEditor
                                                            {
                                                                Alias = "editor1",
                                                                View  = "view1"
                                                            },
                                                            Value = "value2"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        };

                        var json = JsonConvert.SerializeObject(gridVal);
                        content.Properties["grid"].SetValue(json);

                        var valueSet = contentValueSetBuilder.GetValueSets(content);
                        indexer.IndexItems(valueSet);

                        var searcher = indexer.GetSearcher();

                        var results = searcher.CreateQuery().Id(555).Execute();
                        Assert.AreEqual(1, results.TotalItemCount);

                        var result = results.First();
                        Assert.IsTrue(result.Values.ContainsKey("grid.row1"));
                        Assert.AreEqual("value1", result.AllValues["grid.row1"][0]);
                        Assert.AreEqual("value2", result.AllValues["grid.row1"][1]);
                        Assert.IsTrue(result.Values.ContainsKey("grid"));
                        Assert.AreEqual("value1 value2 ", result["grid"]);
                        Assert.IsTrue(result.Values.ContainsKey($"{UmbracoExamineIndex.RawFieldPrefix}grid"));
                        Assert.AreEqual(json, result[$"{UmbracoExamineIndex.RawFieldPrefix}grid"]);
                    }
        }
        public void Test_Sort_Order_Sorting()
        {
            long totalRecs;
            var  demoData = new ExamineDemoDataContentService(TestFiles.umbraco_sort);
            var  allRecs  = demoData.GetLatestContentByXPath("//*[@isDoc]")
                            .Root
                            .Elements()
                            .Select(x => Mock.Of <IContent>(
                                        m =>
                                        m.Id == (int)x.Attribute("id") &&
                                        m.ParentId == (int)x.Attribute("parentID") &&
                                        m.Level == (int)x.Attribute("level") &&
                                        m.CreatorId == 0 &&
                                        m.SortOrder == (int)x.Attribute("sortOrder") &&
                                        m.CreateDate == (DateTime)x.Attribute("createDate") &&
                                        m.UpdateDate == (DateTime)x.Attribute("updateDate") &&
                                        m.Name == (string)x.Attribute("nodeName") &&
                                        m.GetCultureName(It.IsAny <string>()) == (string)x.Attribute("nodeName") &&
                                        m.Path == (string)x.Attribute("path") &&
                                        m.Properties == new PropertyCollection() &&
                                        m.Published == true &&
                                        m.ContentType == Mock.Of <ISimpleContentType>(mt =>
                                                                                      mt.Icon == "test" &&
                                                                                      mt.Alias == x.Name.LocalName &&
                                                                                      mt.Id == (int)x.Attribute("nodeType"))))
                            .ToArray();
            var contentService = Mock.Of <IContentService>(
                x => x.GetPagedDescendants(
                    It.IsAny <int>(), It.IsAny <long>(), It.IsAny <int>(), out totalRecs, It.IsAny <IQuery <IContent> >(), It.IsAny <Ordering>())
                ==
                allRecs);

            var propertyEditors = Factory.GetInstance <PropertyEditorCollection>();
            var rebuilder       = IndexInitializer.GetContentIndexRebuilder(propertyEditors, contentService, ScopeProvider.SqlContext, true);

            using (var luceneDir = new RandomIdRamDirectory())
                using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, luceneDir))
                    using (indexer.ProcessNonAsync())
                    {
                        indexer.CreateIndex();
                        rebuilder.Populate(indexer);

                        var searcher = indexer.GetSearcher();

                        var numberSortedCriteria = searcher.CreateQuery()
                                                   .ParentId(1148)
                                                   .OrderBy(new SortableField("sortOrder", SortType.Int));
                        var numberSortedResult = numberSortedCriteria.Execute();

                        var stringSortedCriteria = searcher.CreateQuery()
                                                   .ParentId(1148)
                                                   .OrderBy(new SortableField("sortOrder"));//will default to string
                        var stringSortedResult = stringSortedCriteria.Execute();

                        Assert.AreEqual(12, numberSortedResult.TotalItemCount);
                        Assert.AreEqual(12, stringSortedResult.TotalItemCount);

                        Assert.IsTrue(IsSortedByNumber(numberSortedResult));
                        Assert.IsFalse(IsSortedByNumber(stringSortedResult));
                    }
        }