public void Items_IndexMatchDb_ReturnsAllItems([Content] DbItem[] contentItems, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem)
    {
      //arrange
      var results = GetResults(contentItems);

      InitIndexes(index, searchProvider, results);
      var renderingModel = new QueryableDatasourceRenderingModel() { Rendering = new Rendering() { DataSource = "notEmpty" } };

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(contentItems.Length);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);

    }
    public void Items_NotLatestItemVersionExists_ReturnsOnlyLatestItems([Content] DbItem[] contentItems, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem)
    {
      //arrange
      var results = GetResults(contentItems).ToArray();
      results.First().IsLatestVersion = false;

      InitIndexes(index, searchProvider, results.AsQueryable());
      var renderingModel = new QueryableDatasourceRenderingModel() { Rendering = new Rendering() { DataSource = "notEmpty" } };

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(contentItems.Length-1);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);

    }
    public void Initialize_TemplateResolved_DatasourceTemplateShouldBeSet([Content] DbTemplate templateItem, [ResolvePipeline("getRenderingDatasource")] FakeDatasourceResolverPipeline processor, [Content] Item renderingItem)
    {
      //arrange
      processor.Item = renderingItem.Database.GetItem(templateItem.ID);
      var rendering = new Rendering
      {
        DataSource = "ds",
        RenderingItem = new RenderingItem(renderingItem)
        
      };
      ContextService.Get().Push(new PageContext());
      PageContext.Current.Item = renderingItem;
      var renderingModel = new QueryableDatasourceRenderingModel();
      //act
      renderingModel.Initialize(rendering);


      //assert
      renderingModel.DatasourceTemplate.Should().NotBeNull();
      renderingModel.DatasourceTemplate.ID.Should().Be(templateItem.ID);
    }
    public void Items_DifferentItemLanguageExists_ReturnsOnlyContextLanguage([Content] DbItem[] contentItems, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem, IRenderingPropertiesRepository renderingPropertiesRepository)
    {
      //arrange
      var results = GetResults(contentItems).ToArray();
      results.First().Language = "noncontext";
      renderingPropertiesRepository.Get<QueryableDatasourceRenderingSettings>()
        .Returns(new QueryableDatasourceRenderingSettings
        {
          SearchResultsLimit = 10
        });

      InitIndexes(index, searchProvider, results.AsQueryable());
      var renderingModel = new QueryableDatasourceRenderingModel(renderingPropertiesRepository)
      {
        DatasourceString = "notEmpty"
      };

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(contentItems.Length - 1);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);
    }
    public void Items_EmptyDatasource_ReturnsEmptyCollection([ResolvePipeline("getRenderingDatasource")] EmptyPipeline processor, List<DbItem> indexedItems,SearchProvider searchProvider, ISearchIndex index, string indexName, [Content] Item renderingItem)
    {
      //arrange

      InitIndexes(index, searchProvider, new List<SearchResultItem>().AsQueryable());
      var renderingModel = new QueryableDatasourceRenderingModel() { Rendering = new Rendering()};

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(0);
      index.CreateSearchContext().DidNotReceiveWithAnyArgs();
 
    }
    public void Items_IndexHaveNonexistentItems_ReturnsExistentItems([Content] DbItem[] contentItems, DbItem brokenItem, List<DbItem> indexedItems, ISearchIndex index, string indexName, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem)
    {
      //arrange
      indexedItems.AddRange(contentItems);
      indexedItems.Add(brokenItem);

      var results = GetResults(indexedItems);

      InitIndexes(index, searchProvider, results);
      var renderingModel = new QueryableDatasourceRenderingModel() { Rendering = new Rendering() { DataSource = "notEmpty" } };

      //act
      var items = renderingModel.Items.ToArray();


      //assert
      items.Count().Should().Be(contentItems.Length);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);
    }
    public void Items_ItemTemplateSet_FiltersByTemplateId(Db db, [Content] DbTemplate templateItem,  [Content] DbItem[] contentItems, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, string indexName, [Content] Item renderingItem)
    {
      //arrange
      var dbItem = new DbItem("templated", ID.NewID, templateItem.ID);
      db.Add(dbItem);
      var dbItems = contentItems.ToList();
      dbItems.Add(dbItem);
      var results = GetResults(dbItems);

      InitIndexes(index, searchProvider,  results);

      
      var renderingModel = new QueryableDatasourceRenderingModel() {Rendering = new Rendering() {DataSource = "notEmpty"} };
      renderingModel.DatasourceTemplate = db.GetItem(templateItem.ID);

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(1);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);
    }
    public void DatasourceString_IdAsDatasource_IDSetAsLocationRoot([ResolvePipeline("getRenderingDatasource")] EmptyPipeline processor, [Content] Item renderingItem)
    {
      //arrange
      ContextService.Get().Push(new PageContext());
      PageContext.Current.Item = renderingItem;
      var renderingModel = new QueryableDatasourceRenderingModel();
      var dataSource = ID.NewID.ToString();

      //act
      renderingModel.Initialize(new Rendering
      {
        DataSource = dataSource,
        RenderingItem = new RenderingItem(renderingItem)
      });

      //assert
      renderingModel.DatasourceString.Should().Be("+location:" + dataSource);
    }
    public void Items_IndexEmpty_ReturnsEmptyCollection([ResolvePipeline("getRenderingDatasource")] EmptyPipeline processor, List<DbItem> indexedItems, ISearchIndex index, string indexName, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem, IRenderingPropertiesRepository renderingPropertiesRepository)
    {
      //arrange
      InitIndexes(index, searchProvider, new List<SearchResultItem>().AsQueryable());
      renderingPropertiesRepository.Get<QueryableDatasourceRenderingSettings>()
        .Returns(new QueryableDatasourceRenderingSettings
        {
          SearchResultsLimit = 10
        });

      var renderingModel = new QueryableDatasourceRenderingModel(renderingPropertiesRepository)
      {
        DatasourceString = "notEmpty"
      };

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(0);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);
    }
    public void Items_StandardValuesExists_IgnoresItemsUnderTemplates(Db db, ISearchIndex index,
      [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem, IRenderingPropertiesRepository renderingPropertiesRepository)
    {
      //arrange
      var templateID = ID.NewID;
      db.Add(new DbTemplate("Sample", templateID));
      var stdValues = db.GetItem("/sitecore/templates/Sample").Add(Sitecore.Constants.StandardValuesItemName, new TemplateID(templateID));

      renderingPropertiesRepository.Get<QueryableDatasourceRenderingSettings>()
        .Returns(new QueryableDatasourceRenderingSettings
        {
          SearchResultsLimit = 10
        });

      var results = GetResults(new List<DbItem>
      {
        new DbItem(Sitecore.Constants.StandardValuesItemName, stdValues.ID)
      });

      InitIndexes(index, searchProvider, results);
      var renderingModel = new QueryableDatasourceRenderingModel(renderingPropertiesRepository)
      {
        DatasourceString = "notEmpty"
      };

      //act
      var items = renderingModel.Items.ToArray();


      //assert
      items.Count().Should().Be(0);
    }
    public void Items_StandardValuesExistsInContentTree_IgnoresStandartValueByName(Db db, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, IRenderingPropertiesRepository renderingPropertiesRepository)
    {
      //arrange
      var id = ID.NewID;
      var dbItem = new DbItem(Sitecore.Constants.StandardValuesItemName, id);
      db.Add(dbItem);

      renderingPropertiesRepository.Get<QueryableDatasourceRenderingSettings>().Returns(new QueryableDatasourceRenderingSettings
      {
        SearchResultsLimit = 10
      });

      var results = GetResults(new List<DbItem>
      {
        dbItem
      });

      InitIndexes(index, searchProvider, results);
      var renderingModel = new QueryableDatasourceRenderingModel(renderingPropertiesRepository)
      {
        DatasourceString = "notEmpty"
      };

      //act
      var items = renderingModel.Items.ToArray();


      //assert
      items.Count().Should().Be(0);
    }
    public void Items_IndexMatchDb_ReturnsAllItems([Content] DbItem[] contentItems, ISearchIndex index, [ReplaceSearchProvider] SearchProvider searchProvider, [Content] Item renderingItem, IRenderingPropertiesRepository renderingPropertiesRepository)
    {
      //arrange
      var results = GetResults(contentItems);
      renderingPropertiesRepository.Get<QueryableDatasourceRenderingSettings>()
        .Returns(new QueryableDatasourceRenderingSettings
        {
          SearchResultsLimit = 10
        });

      InitIndexes(index, searchProvider, results);
      var renderingModel = new QueryableDatasourceRenderingModel(renderingPropertiesRepository)
      {
        DatasourceString = "notEmpty"
      };

      //act
      var items = renderingModel.Items;

      //assert
      items.Count().Should().Be(contentItems.Length);
      index.CreateSearchContext().ReceivedWithAnyArgs(1);
    }