Files
Umbraco-CMS/src/Umbraco.Tests/Cache/PublishedCache/PublishedMediaCacheTests.cs

414 lines
18 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Examine;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
2018-07-20 16:39:39 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Strings;
using Umbraco.Tests.TestHelpers;
2016-12-16 10:40:14 +01:00
using Umbraco.Tests.Testing;
2017-05-30 18:13:11 +02:00
using Current = Umbraco.Web.Composing.Current;
2017-06-20 12:12:29 +02:00
using Umbraco.Core.Models;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Tests.LegacyXmlPublishedCache;
2018-04-28 16:34:43 +02:00
using Umbraco.Tests.PublishedContent;
using Umbraco.Web;
namespace Umbraco.Tests.Cache.PublishedCache
{
2017-07-20 11:21:28 +02:00
[TestFixture]
2016-11-05 19:23:55 +01:00
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class PublishMediaCacheTests : BaseWebTest
2017-07-20 11:21:28 +02:00
{
2018-04-28 16:34:43 +02:00
private Dictionary<string, PublishedContentType> _mediaTypes;
private IUmbracoContextAccessor _umbracoContextAccessor;
2016-11-07 20:50:14 +01:00
protected override void Compose()
{
2016-11-07 20:50:14 +01:00
base.Compose();
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<UrlSegmentProviderCollectionBuilder>()
2016-11-08 17:38:05 +01:00
.Clear()
.Append<DefaultUrlSegmentProvider>();
_umbracoContextAccessor = Current.UmbracoContextAccessor;
}
2016-07-29 10:58:57 +02:00
2018-04-28 16:34:43 +02:00
protected override void Initialize()
{
base.Initialize();
var type = new AutoPublishedContentType(22, "myType", new PublishedPropertyType[] { });
var image = new AutoPublishedContentType(23, "Image", new PublishedPropertyType[] { });
var testMediaType = new AutoPublishedContentType(24, "TestMediaType", new PublishedPropertyType[] { });
_mediaTypes = new Dictionary<string, PublishedContentType>
{
{ type.Alias, type },
{ image.Alias, image },
{ testMediaType.Alias, testMediaType }
};
ContentTypesCache.GetPublishedContentTypeByAlias = alias => _mediaTypes[alias];
}
2017-07-20 11:21:28 +02:00
private IMediaType MakeNewMediaType(IUser user, string text, int parentId = -1)
{
var mt = new MediaType(parentId) { Name = text, Alias = text, Thumbnail = "icon-folder", Icon = "icon-folder" };
2017-06-20 12:12:29 +02:00
ServiceContext.MediaTypeService.Save(mt);
2017-07-20 11:21:28 +02:00
return mt;
}
2017-06-20 12:12:29 +02:00
2017-07-20 11:21:28 +02:00
private IMedia MakeNewMedia(string name, IMediaType mediaType, IUser user, int parentId)
{
var m = ServiceContext.MediaService.CreateMediaWithIdentity(name, parentId, mediaType.Alias);
return m;
}
2017-06-20 12:12:29 +02:00
//NOTE: This is "Without_Examine" too
[Test]
2017-07-20 11:21:28 +02:00
public void Get_Root_Docs()
{
var user = ServiceContext.UserService.GetUserById(0);
2017-06-20 12:12:29 +02:00
var mType = MakeNewMediaType(user, "TestMediaType");
2017-07-20 11:21:28 +02:00
var mRoot1 = MakeNewMedia("MediaRoot1", mType, user, -1);
var mRoot2 = MakeNewMedia("MediaRoot2", mType, user, -1);
var mChild1 = MakeNewMedia("Child1", mType, user, mRoot1.Id);
var mChild2 = MakeNewMedia("Child2", mType, user, mRoot2.Id);
2016-07-29 10:58:57 +02:00
2017-07-20 11:21:28 +02:00
var ctx = GetUmbracoContext("/test");
var cache = new PublishedMediaCache(new XmlStore((XmlDocument) null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new DictionaryAppCache(), ContentTypesCache, Factory.GetInstance<IEntityXmlSerializer>(), Factory.GetInstance<IUmbracoContextAccessor>());
2017-07-20 11:21:28 +02:00
var roots = cache.GetAtRoot();
Assert.AreEqual(2, roots.Count());
Assert.IsTrue(roots.Select(x => x.Id).ContainsAll(new[] {mRoot1.Id, mRoot2.Id}));
2017-07-20 11:21:28 +02:00
}
2017-07-20 11:21:28 +02:00
[Test]
public void Get_Item_Without_Examine()
{
var user = ServiceContext.UserService.GetUserById(0);
2017-06-20 12:12:29 +02:00
var mType = MakeNewMediaType(user, "TestMediaType");
2018-04-28 16:34:43 +02:00
_mediaTypes[mType.Alias] = new PublishedContentType(mType, null);
2017-07-20 11:21:28 +02:00
var mRoot = MakeNewMedia("MediaRoot", mType, user, -1);
var mChild1 = MakeNewMedia("Child1", mType, user, mRoot.Id);
//var publishedMedia = PublishedMediaTests.GetNode(mRoot.Id, GetUmbracoContext("/test", 1234));
var umbracoContext = GetUmbracoContext("/test");
var cache = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), Current.Services.MediaService, Current.Services.UserService, new DictionaryAppCache(), ContentTypesCache, Factory.GetInstance<IEntityXmlSerializer>(), Factory.GetInstance<IUmbracoContextAccessor>());
var publishedMedia = cache.GetById(mRoot.Id);
Assert.IsNotNull(publishedMedia);
2017-07-20 11:21:28 +02:00
Assert.AreEqual(mRoot.Id, publishedMedia.Id);
Assert.AreEqual(mRoot.CreateDate.ToString("dd/MM/yyyy HH:mm:ss"), publishedMedia.CreateDate.ToString("dd/MM/yyyy HH:mm:ss"));
Assert.AreEqual(mRoot.CreatorId, publishedMedia.CreatorId);
//Assert.AreEqual(mRoot.User.Name, publishedMedia.CreatorName);
2018-04-28 16:34:43 +02:00
Assert.AreEqual(mRoot.ContentType.Alias, publishedMedia.ContentType.Alias);
Assert.AreEqual(mRoot.ContentType.Id, publishedMedia.ContentType.Id);
2017-07-20 11:21:28 +02:00
Assert.AreEqual(mRoot.Level, publishedMedia.Level);
2019-04-16 16:25:10 +02:00
Assert.AreEqual(mRoot.Name, publishedMedia.Name());
2017-07-20 11:21:28 +02:00
Assert.AreEqual(mRoot.Path, publishedMedia.Path);
Assert.AreEqual(mRoot.SortOrder, publishedMedia.SortOrder);
2019-04-22 11:59:06 +02:00
Assert.IsNull(publishedMedia.Parent());
2017-07-20 11:21:28 +02:00
}
[TestCase("id")]
[TestCase("__NodeId")]
public void DictionaryDocument_Id_Keys(string key)
{
var dicDoc = GetDictionaryDocument(idKey: key);
DoAssert(dicDoc);
}
[TestCase("template")]
[TestCase("templateId")]
public void DictionaryDocument_Template_Keys(string key)
{
var dicDoc = GetDictionaryDocument(templateKey: key);
DoAssert(dicDoc);
}
[TestCase("nodeName")]
public void DictionaryDocument_NodeName_Keys(string key)
{
var dicDoc = GetDictionaryDocument(nodeNameKey: key);
DoAssert(dicDoc);
}
[TestCase("nodeTypeAlias")]
[TestCase("__NodeTypeAlias")]
public void DictionaryDocument_NodeTypeAlias_Keys(string key)
{
var dicDoc = GetDictionaryDocument(nodeTypeAliasKey: key);
DoAssert(dicDoc);
}
[TestCase("path")]
[TestCase("__Path")]
public void DictionaryDocument_Path_Keys(string key)
{
var dicDoc = GetDictionaryDocument(pathKey: key);
DoAssert(dicDoc);
}
2015-09-01 16:11:26 +02:00
[Test]
public void DictionaryDocument_Key()
{
var key = Guid.NewGuid();
var dicDoc = GetDictionaryDocument(keyVal: key);
DoAssert(dicDoc, keyVal: key);
}
2017-07-20 11:21:28 +02:00
[Test]
public void DictionaryDocument_Get_Children()
{
var child1 = GetDictionaryDocument(idVal: 222333);
var child2 = GetDictionaryDocument(idVal: 444555);
2017-07-20 11:21:28 +02:00
var dicDoc = GetDictionaryDocument(children: new List<IPublishedContent>()
{
child1, child2
});
2016-07-29 10:58:57 +02:00
2019-04-22 15:39:24 +02:00
Assert.AreEqual(2, dicDoc.Children().Count());
Assert.AreEqual(222333, dicDoc.Children().ElementAt(0).Id);
Assert.AreEqual(444555, dicDoc.Children().ElementAt(1).Id);
2017-07-20 11:21:28 +02:00
}
2016-07-29 10:58:57 +02:00
2016-04-28 22:12:55 +02:00
[Test]
public void Convert_From_Search_Result()
2017-07-20 11:21:28 +02:00
{
var ctx = GetUmbracoContext("/test");
2017-07-20 11:21:28 +02:00
var key = Guid.NewGuid();
var fields = new Dictionary<string, string>
{
{"__IndexType", "media"},
{"__NodeId", "1234"},
{"__NodeTypeAlias", Constants.Conventions.MediaTypes.Image},
{"__Path", "-1,1234"},
{"__nodeName", "Test"},
{"id", "1234"},
{"key", key.ToString()},
{"urlName", "/media/test.jpg"},
{"nodeType", "0"},
{"sortOrder", "0"},
{"level", "2"},
{"nodeName", "Test"},
{"nodeTypeAlias", Constants.Conventions.MediaTypes.Image},
{"parentID", "-1"},
{"path", "-1,1234"},
{"updateDate", DateTime.Parse("2012-07-16T10:34:09").Ticks.ToString()},
{"createDate", DateTime.Parse("2012-07-17T10:34:09").Ticks.ToString()},
{"creatorID", "0"},
{"creatorName", "Shannon"}
};
var result = new SearchResult("1234", 1, () => fields.ToDictionary(x => x.Key, x => new List<string> { x.Value }));
2018-04-28 16:34:43 +02:00
var store = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new DictionaryAppCache(), ContentTypesCache, Factory.GetInstance<IEntityXmlSerializer>(), Factory.GetInstance<IUmbracoContextAccessor>());
2017-07-20 11:21:28 +02:00
var doc = store.CreateFromCacheValues(store.ConvertFromSearchResult(result));
DoAssert(doc, 1234, key, null, 0, "/media/test.jpg", "Image", 23, "Shannon", "Shannon", 0, 0, "-1,1234", DateTime.Parse("2012-07-17T10:34:09"), DateTime.Parse("2012-07-16T10:34:09"), 2);
2019-04-22 11:59:06 +02:00
Assert.AreEqual(null, doc.Parent());
2017-07-20 11:21:28 +02:00
}
2016-04-28 22:12:55 +02:00
[Test]
public void Convert_From_XPath_Navigator()
2017-07-20 11:21:28 +02:00
{
var ctx = GetUmbracoContext("/test");
2015-09-01 16:11:26 +02:00
var key = Guid.NewGuid();
2017-07-20 11:21:28 +02:00
var xmlDoc = GetMediaXml();
2016-04-28 22:12:55 +02:00
((XmlElement)xmlDoc.DocumentElement.FirstChild).SetAttribute("key", key.ToString());
var navigator = xmlDoc.SelectSingleNode("/root/Image").CreateNavigator();
var cache = new PublishedMediaCache(new XmlStore((XmlDocument)null, null, null, null), ServiceContext.MediaService, ServiceContext.UserService, new DictionaryAppCache(), ContentTypesCache, Factory.GetInstance<IEntityXmlSerializer>(), Factory.GetInstance<IUmbracoContextAccessor>());
2017-07-20 11:21:28 +02:00
var doc = cache.CreateFromCacheValues(cache.ConvertFromXPathNavigator(navigator, true));
DoAssert(doc, 2000, key, null, 2, "image1", "Image", 23, "Shannon", "Shannon", 33, 33, "-1,2000", DateTime.Parse("2012-06-12T14:13:17"), DateTime.Parse("2012-07-20T18:50:43"), 1);
2019-04-22 11:59:06 +02:00
Assert.AreEqual(null, doc.Parent());
2019-04-22 15:39:24 +02:00
Assert.AreEqual(2, doc.Children().Count());
Assert.AreEqual(2001, doc.Children().ElementAt(0).Id);
Assert.AreEqual(2002, doc.Children().ElementAt(1).Id);
2017-07-20 11:21:28 +02:00
}
private XmlDocument GetMediaXml()
{
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
2016-07-29 10:58:57 +02:00
<!DOCTYPE root[
<!ELEMENT Home ANY>
<!ATTLIST Home id ID #REQUIRED>
<!ELEMENT CustomDocument ANY>
<!ATTLIST CustomDocument id ID #REQUIRED>
]>
<root id=""-1"">
2017-07-20 11:21:28 +02:00
<Image id=""2000"" parentID=""-1"" level=""1"" writerID=""33"" creatorID=""33"" nodeType=""2044"" template=""0"" sortOrder=""2"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Image1"" urlName=""image1"" writerName=""Shannon"" creatorName=""Shannon"" path=""-1,2000"" isDoc="""">
<file><![CDATA[/media/1234/image1.png]]></file>
<Image id=""2001"" parentID=""2000"" level=""2"" writerID=""33"" creatorID=""33"" nodeType=""2044"" template=""0"" sortOrder=""2"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Image1"" urlName=""image1"" writerName=""Shannon"" creatorName=""Shannon"" path=""-1,2000,2001"" isDoc="""">
<file><![CDATA[/media/1234/image1.png]]></file>
</Image>
<Image id=""2002"" parentID=""2000"" level=""2"" writerID=""33"" creatorID=""33"" nodeType=""2044"" template=""0"" sortOrder=""2"" createDate=""2012-06-12T14:13:17"" updateDate=""2012-07-20T18:50:43"" nodeName=""Image1"" urlName=""image1"" writerName=""Shannon"" creatorName=""Shannon"" path=""-1,2000,2002"" isDoc="""">
<file><![CDATA[/media/1234/image1.png]]></file>
</Image>
</Image>
</root>";
2017-07-20 11:21:28 +02:00
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
return xmlDoc;
}
2017-07-20 11:21:28 +02:00
private Dictionary<string, string> GetDictionary(
int id,
2015-09-01 16:11:26 +02:00
Guid key,
2017-07-20 11:21:28 +02:00
int parentId,
string idKey,
string templateKey,
string nodeNameKey,
string nodeTypeAliasKey,
string pathKey)
{
return new Dictionary<string, string>()
{
{idKey, id.ToString()},
2015-09-01 16:11:26 +02:00
{"key", key.ToString()},
2017-07-20 11:21:28 +02:00
{templateKey, "0"},
{"sortOrder", "44"},
{nodeNameKey, "Testing"},
{"urlName", "testing"},
{nodeTypeAliasKey, "myType"},
{"nodeType", "22"},
{"writerName", "Shannon"},
{"creatorName", "Shannon"},
{"writerID", "33"},
{"creatorID", "33"},
{pathKey, "1,2,3,4,5"},
{"createDate", "2012-01-02"},
{"updateDate", "2012-01-02"},
{"level", "3"},
{"parentID", parentId.ToString()}
};
}
2018-04-28 16:34:43 +02:00
private DictionaryPublishedContent GetDictionaryDocument(
2017-07-20 11:21:28 +02:00
string idKey = "id",
string templateKey = "template",
string nodeNameKey = "nodeName",
string nodeTypeAliasKey = "nodeTypeAlias",
string pathKey = "path",
int idVal = 1234,
2015-09-01 16:11:26 +02:00
Guid keyVal = default(Guid),
2017-07-20 11:21:28 +02:00
int parentIdVal = 321,
IEnumerable<IPublishedContent> children = null)
{
if (children == null)
children = new List<IPublishedContent>();
2018-04-28 16:34:43 +02:00
var dicDoc = new DictionaryPublishedContent(
//the dictionary
GetDictionary(idVal, keyVal, parentIdVal, idKey, templateKey, nodeNameKey, nodeTypeAliasKey, pathKey),
//callback to get the parent
2018-04-28 16:34:43 +02:00
d => new DictionaryPublishedContent(
// the dictionary
GetDictionary(parentIdVal, default(Guid), -1, idKey, templateKey, nodeNameKey, nodeTypeAliasKey, pathKey),
// callback to get the parent: there is no parent
a => null,
// callback to get the children: we're not going to test this so ignore
(dd, n) => new List<IPublishedContent>(),
// callback to get a property
(dd, a) => dd.Properties.FirstOrDefault(x => x.Alias.InvariantEquals(a)),
null, // cache provider
ContentTypesCache,
// no xpath
2015-06-18 15:36:04 +02:00
null,
// not from examine
false,
_umbracoContextAccessor),
//callback to get the children
(dd, n) => children,
// callback to get a property
(dd, a) => dd.Properties.FirstOrDefault(x => x.Alias.InvariantEquals(a)),
null, // cache provider
ContentTypesCache,
// no xpath
null,
// not from examine
false,
_umbracoContextAccessor);
2017-07-20 11:21:28 +02:00
return dicDoc;
}
2017-07-20 11:21:28 +02:00
private void DoAssert(
2018-04-28 16:34:43 +02:00
DictionaryPublishedContent dicDoc,
2017-07-20 11:21:28 +02:00
int idVal = 1234,
2015-09-01 16:11:26 +02:00
Guid keyVal = default(Guid),
int? templateIdVal = null,
2017-07-20 11:21:28 +02:00
int sortOrderVal = 44,
string urlNameVal = "testing",
string nodeTypeAliasVal = "myType",
int nodeTypeIdVal = 22,
string writerNameVal = "Shannon",
string creatorNameVal = "Shannon",
int writerIdVal = 33,
int creatorIdVal = 33,
string pathVal = "1,2,3,4,5",
DateTime? createDateVal = null,
DateTime? updateDateVal = null,
int levelVal = 3,
int parentIdVal = 321)
{
if (!createDateVal.HasValue)
createDateVal = DateTime.Parse("2012-01-02");
if (!updateDateVal.HasValue)
updateDateVal = DateTime.Parse("2012-01-02");
DoAssert((IPublishedContent)dicDoc, idVal, keyVal, templateIdVal, sortOrderVal, urlNameVal, nodeTypeAliasVal, nodeTypeIdVal, writerNameVal,
creatorNameVal, writerIdVal, creatorIdVal, pathVal, createDateVal, updateDateVal, levelVal);
//now validate the parentId that has been parsed, this doesn't exist on the IPublishedContent
Assert.AreEqual(parentIdVal, dicDoc.ParentId);
}
private void DoAssert(
IPublishedContent doc,
int idVal = 1234,
2015-09-01 16:11:26 +02:00
Guid keyVal = default(Guid),
int? templateIdVal = null,
2017-07-20 11:21:28 +02:00
int sortOrderVal = 44,
string urlNameVal = "testing",
string nodeTypeAliasVal = "myType",
int nodeTypeIdVal = 22,
string writerNameVal = "Shannon",
string creatorNameVal = "Shannon",
int writerIdVal = 33,
int creatorIdVal = 33,
string pathVal = "1,2,3,4,5",
DateTime? createDateVal = null,
DateTime? updateDateVal = null,
int levelVal = 3)
{
if (!createDateVal.HasValue)
createDateVal = DateTime.Parse("2012-01-02");
if (!updateDateVal.HasValue)
updateDateVal = DateTime.Parse("2012-01-02");
Assert.AreEqual(idVal, doc.Id);
Assert.AreEqual(keyVal, doc.Key);
2017-07-20 11:21:28 +02:00
Assert.AreEqual(templateIdVal, doc.TemplateId);
Assert.AreEqual(sortOrderVal, doc.SortOrder);
Assert.AreEqual(urlNameVal, doc.UrlSegment());
2018-04-28 16:34:43 +02:00
Assert.AreEqual(nodeTypeAliasVal, doc.ContentType.Alias);
Assert.AreEqual(nodeTypeIdVal, doc.ContentType.Id);
2017-07-20 11:21:28 +02:00
Assert.AreEqual(writerNameVal, doc.WriterName);
Assert.AreEqual(creatorNameVal, doc.CreatorName);
Assert.AreEqual(writerIdVal, doc.WriterId);
Assert.AreEqual(creatorIdVal, doc.CreatorId);
Assert.AreEqual(pathVal, doc.Path);
Assert.AreEqual(createDateVal.Value, doc.CreateDate);
Assert.AreEqual(updateDateVal.Value, doc.UpdateDate);
Assert.AreEqual(levelVal, doc.Level);
}
}
}