Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beyond Retail - updates for Blog migration to v11 + bugfixes + reimplementing missing bits from v7 package #30

Open
wants to merge 15 commits into
base: v10/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions Gibe.Umbraco.Blog.Tests/BlogServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Examine;
using Examine;
using Gibe.Pager.Interfaces;
using Gibe.Umbraco.Blog.Filters;
using Gibe.Umbraco.Blog.Models;
using NUnit.Framework;
using Moq;
using Gibe.Umbraco.Blog.Repositories;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Gibe.Umbraco.Blog.Tests
Expand All @@ -16,23 +15,22 @@ namespace Gibe.Umbraco.Blog.Tests
public class BlogServiceTests
{
private Mock<IPagerService> _pagerService;
private Mock<IBlogContentRepository> _blogContentRepository;
private Mock<IBlogPostMapper<BlogModel>> _blogPostMapper;

[SetUp]
public void SetUp()
{
_pagerService = new Mock<IPagerService>();
_blogContentRepository = new Mock<IBlogContentRepository>();

_blogContentRepository.Setup(r => r.BlogContent(It.IsAny<int>()))
.Returns((int id) => Content(id, "blogPost"));
_blogPostMapper = new Mock<IBlogPostMapper<BlogModel>>();
_blogPostMapper.Setup(x => x.ToBlogPosts(It.IsAny<IEnumerable<ISearchResult>>(), It.IsAny<IPublishedValueFallback>()))
.Returns((IEnumerable<ISearchResult> results, IPublishedValueFallback fb) => results.Select(x => new BlogModel()));
}

[Test]
public void GetRelatedPosts_Uses_Correct_Filters()
{
var blogSearch = new FakeBlogSearch(GetSearchResults());
var blogService = new BlogService<BlogModel>(_pagerService.Object, blogSearch, _blogContentRepository.Object);
var blogService = new BlogService<BlogModel>(_pagerService.Object, blogSearch, _blogPostMapper.Object);
var testPost = new BlogModel
{
Tags = new List<string>
Expand Down
4 changes: 4 additions & 0 deletions Gibe.Umbraco.Blog.Tests/Gibe.Umbraco.Blog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand All @@ -17,6 +18,9 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
82 changes: 82 additions & 0 deletions Gibe.Umbraco.Blog.Tests/UnpagedBlogServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;
using Examine;
using Gibe.Umbraco.Blog;
using Gibe.Umbraco.Blog.Tests;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Gibe.UmbracoBlog.Tests
{
[TestFixture]
public class UnpagedBlogServiceTests
{
private MockRepository _repository;

[SetUp]
public void Setup()
{
_repository = new MockRepository(MockBehavior.Strict);
}

private IBlogPostMapper<BlogModel> BlogPostMapper(int searchResultsCount)
{
var blogPostMapper = _repository.Create<IBlogPostMapper<BlogModel>>();

var mappedSearchResults = new List<BlogModel>();
for (var i = 0; i < searchResultsCount; i++)
{
mappedSearchResults.Add(new BlogModel { Id = i });
}

blogPostMapper.Setup(m => m.ToBlogPosts(It.IsAny<IEnumerable<ISearchResult>>(), It.IsAny<NoopPublishedValueFallback>())).Returns(mappedSearchResults);

return blogPostMapper.Object;
}

private IBlogSearch BlogSearch(ISearchResults searchResults)
{
return new FakeBlogSearch(searchResults);
}

private ISearchResults SearchResults(int searchResultsCount)
{
var searchResults = new List<SearchResult>();
for (var i = 0; i < searchResultsCount; i++)
{
searchResults.Add(SearchResult(i));
}
return new FakeSearchResults(searchResults);
}

private SearchResult SearchResult(int id)
{
return new SearchResult(id.ToString(), 0, () => new Dictionary<string, List<string>>());
}

private IUnpagedBlogService<BlogModel> Service(IBlogSearch blogSearch, IBlogPostMapper<BlogModel> blogPostMapper)
=> new UnpagedBlogService<BlogModel>(blogSearch, blogPostMapper);

[TestCase(1, 5, false)]
[TestCase(2, 7, false)]
[TestCase(1, 10, true)]
[TestCase(4, 8, true)]
[TestCase(2, 3, false)]
public void GetPosts_Returns_Expected_Range_Of_BlogPosts_And_Expected_Total(int startPost, int postCount, bool isLastPage)
{
var totalBlogPostsCount = 10;
var searchResults = SearchResults(totalBlogPostsCount);

var blogSearch = BlogSearch(searchResults);

var service = Service(blogSearch, BlogPostMapper(postCount));

var results = service.GetPosts(startPost, postCount);

Assert.That(results.BlogPosts.Count(), Is.EqualTo(postCount));
Assert.That(results.TotalItemsCount, Is.EqualTo(totalBlogPostsCount));
Assert.That(results.IsLastPage, Is.EqualTo(isLastPage));
}
}
}
43 changes: 43 additions & 0 deletions Gibe.Umbraco.Blog/BlogPostMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Examine;
using Gibe.Umbraco.Blog.Models;
using Gibe.Umbraco.Blog.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Gibe.Umbraco.Blog
{
public class BlogPostMapper<T> : IBlogPostMapper<T> where T : class, IBlogPostModel
{
private readonly IBlogContentRepository _blogContentRepository;

public BlogPostMapper(IBlogContentRepository blogContentRepository)
{
_blogContentRepository = blogContentRepository;
}

public T ToBlogPost(IPublishedContent content, IPublishedValueFallback fallback)
{
return Activator.Activate<T>(content, fallback);
}

public IEnumerable<T> ToBlogPosts(IEnumerable<IPublishedContent> content, IPublishedValueFallback fallback)
{
return content.Select(b => ToBlogPost(b, fallback));
}

public IEnumerable<T> ToBlogPosts(IEnumerable<ISearchResult> searchResults, IPublishedValueFallback fallback)
{
return ToBlogPosts(searchResults.Select(r => GetContent(r.Id)), fallback);
}

public IPublishedContent GetContent(string id)
{
var integerId = Convert.ToInt32(id);

return _blogContentRepository.BlogContent(integerId);
}
}

}
3 changes: 1 addition & 2 deletions Gibe.Umbraco.Blog/BlogSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ private ISearchResults SearchForBlogPosts(IOrdering operation, int? skip = null,
{
if (skip.HasValue)
{
var options = new QueryOptions(skip.Value, take);
return operation.Execute(options);
return operation.Execute(QueryOptions.SkipTake(skip.Value, take));
}
return operation.Execute();
}
Expand Down
20 changes: 15 additions & 5 deletions Gibe.Umbraco.Blog/BlogSections.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using Examine;
using Gibe.Umbraco.Blog.Models;
using Gibe.Umbraco.Blog.Wrappers;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;

namespace Gibe.Umbraco.Blog
{
public class BlogSections<T> : IBlogSections<T> where T : class
{
private const string BlogSectionDocType = ""; // TODO
private readonly ISearchIndex _searchIndex;
private readonly IBlogSettings _blogSettings;
private readonly IPublishedContentQuery _publishedContentQuery;
private readonly IPublishedValueFallback _publishedValueFallback;

public BlogSections(ISearchIndex searchIndex)
public BlogSections(ISearchIndex searchIndex,
IBlogSettings blogSettings,
IPublishedContentQuery publishedContentQuery,
IPublishedValueFallback publishedValueFallback)
{
_searchIndex = searchIndex;
_blogSettings = blogSettings;
_publishedContentQuery = publishedContentQuery;
_publishedValueFallback = publishedValueFallback;
}

public IEnumerable<T> All()
{
var results = SearchForBlogSections();
return Enumerable.Empty<T>();
//return results.Select(r => Activator.Activate<T>(_umbracoWrapper.TypedContent(r.Id)));
return results.Select(r => Activator.Activate<T>(_publishedContentQuery.Content(r.Id), _publishedValueFallback));
}

private ISearchResults SearchForBlogSections()
{
var query = _searchIndex.CreateSearchQuery()
.NodeTypeAlias(BlogSectionDocType);
.NodeTypeAlias(_blogSettings.BlogSectionDocumentTypeAlias);

return query.Execute();
}
Expand Down
48 changes: 13 additions & 35 deletions Gibe.Umbraco.Blog/BlogService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Examine;
using Gibe.Pager.Interfaces;
using Gibe.Pager.Interfaces;
using Gibe.Pager.Models;
using Gibe.Umbraco.Blog.Filters;
using Gibe.Umbraco.Blog.Models;
using Gibe.Umbraco.Blog.Repositories;
using Gibe.Umbraco.Blog.Sort;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Gibe.Umbraco.Blog
Expand All @@ -16,13 +13,16 @@ public class BlogService<T> : IBlogService<T> where T : class, IBlogPostModel
{
private readonly IPagerService _pagerService;
private readonly IBlogSearch _blogSearch;
private readonly IBlogContentRepository _blogContentRepository;
private readonly IBlogPostMapper<T> _blogPostMapper;

public BlogService(IPagerService pagerService, IBlogSearch blogSearch, IBlogContentRepository blogContentRepository)
public BlogService(
IPagerService pagerService,
IBlogSearch blogSearch,
IBlogPostMapper<T> blogPostMapper)
{
_pagerService = pagerService;
_blogSearch = blogSearch;
_blogContentRepository = blogContentRepository;
_blogPostMapper = blogPostMapper;
}

public PageQueryResultModel<T> GetPosts(int itemsPerPage, int currentPage)
Expand All @@ -43,41 +43,26 @@ public PageQueryResultModel<T> GetPosts(IEnumerable<IBlogPostFilter> filters, in
public PageQueryResultModel<T> GetPosts(IEnumerable<IBlogPostFilter> filters, int itemsPerPage, int currentPage, ISort sort)
{
var posts = _blogSearch.Search(filters, sort, (currentPage - 1) * itemsPerPage, itemsPerPage);
return PageQueryResultModel(itemsPerPage, currentPage, ToBlogPosts(posts, new NoopPublishedValueFallback()), posts.TotalItemCount);
return PageQueryResultModel(itemsPerPage, currentPage, _blogPostMapper.ToBlogPosts(posts, new NoopPublishedValueFallback()), posts.TotalItemCount);
}

private PageQueryResultModel<T> PageQueryResultModel(int itemsPerPage, int currentPage, IEnumerable<T> posts, long totalPostCount)
{
return _pagerService.GetPageQueryResultModel(posts, itemsPerPage, currentPage, (int)totalPostCount);
}

private T ToBlogPost(IPublishedContent content, IPublishedValueFallback fallback)
{
return Activator.Activate<T>(content, fallback);
}

private IEnumerable<T> ToBlogPosts(IEnumerable<IPublishedContent> content, IPublishedValueFallback fallback)
{
return content.Select(b => ToBlogPost(b, fallback));
}

private IEnumerable<T> ToBlogPosts(IEnumerable<ISearchResult> searchResults, IPublishedValueFallback fallback)
{
return ToBlogPosts(searchResults.Select(r => GetContent(r.Id)), fallback);
}

public T GetNextPost(T current, IEnumerable<IBlogPostFilter> filters, ISort sort)
{
var results = _blogSearch.Search(filters, sort);
var post = results.TakeWhile(r => r.Id != current.Id.ToString()).LastOrDefault();
return post != null ? ToBlogPost(GetContent(post.Id), new NoopPublishedValueFallback()) : null;
return post != null ? _blogPostMapper.ToBlogPost(_blogPostMapper.GetContent(post.Id), new NoopPublishedValueFallback()) : null;
}

public T GetPreviousPost(T current, IEnumerable<IBlogPostFilter> filters, ISort sort)
{
var results = _blogSearch.Search(filters, sort);
var post = results.SkipWhile(r => r.Id != current.Id.ToString()).Skip(1).FirstOrDefault();
return post != null ? ToBlogPost(GetContent(post.Id), new NoopPublishedValueFallback()) : null;
return post != null ? _blogPostMapper.ToBlogPost(_blogPostMapper.GetContent(post.Id), new NoopPublishedValueFallback()) : null;
}

public IEnumerable<T> GetRelatedPosts(T post, int count)
Expand All @@ -89,14 +74,7 @@ public IEnumerable<T> GetRelatedPosts(IEnumerable<string> tags, int postId, int
{
var filter = new AtLeastOneMatchingTagFilter(tags);
var results = _blogSearch.Search(filter, new RelevanceSort()).Where(r => r.Id != postId.ToString());
return ToBlogPosts(results.Take(count), new NoopPublishedValueFallback());
}

private IPublishedContent GetContent(string id)
{
var integerId = Convert.ToInt32(id);

return _blogContentRepository.BlogContent(integerId);
return _blogPostMapper.ToBlogPosts(results.Take(count), new NoopPublishedValueFallback());
}
}
}
Loading