using umbraco.cms.businesslogic.media;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using umbraco.BusinessLogic;
using System.Linq;
using umbraco.editorControls.textfield;
using umbraco.editorControls.label;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.property;
namespace umbraco.Test
{
///
/// This will test the Media data layer.
/// These test assume the following criteria, if this criteria is not met, these tests will fail:
/// - There is a Label data type assigned to one of your Media types.
///
[TestClass()]
public class MediaTest
{
///
///A test for making a new media and deleting it which actuall first moves it to the recycle bin
///and then deletes it.
///
[TestMethod()]
public void Media_Make_New()
{
//System.Diagnostics.Debugger.Break();
Assert.IsInstanceOfType(m_NewRootMedia, typeof(Media));
}
[TestMethod()]
public void Media_Update_Data()
{
//System.Diagnostics.Debugger.Break();
//set the value of a text property
var m = CreateNewUnderRoot(m_ExistingMediaType);
var p = GetLabelProperty(m_ExistingMediaType, m);
p.Value = "HELLO!";
Assert.AreEqual("HELLO!", m.getProperty(p.PropertyType).Value);
//completely delete
m.delete(true);
Assert.IsFalse(Media.IsNode(m.Id));
}
[TestMethod()]
public void Media_Delete_Heirarchy_Permanently()
{
var mediaList = new List();
var total = 20;
var mt = new MediaType(GetExistingMediaTypeId());
//allow the doc type to be created underneath itself
mt.AllowedChildContentTypeIDs = new int[] { mt.Id };
mt.Save();
//create 20 content nodes underneath each other, this will test deleting with heirarchy as well
var lastParentId = -1;
for (var i = 0; i < total; i++)
{
var newDoc = Media.MakeNew(i.ToString() + Guid.NewGuid().ToString("N"), mt, m_User, lastParentId);
mediaList.Add(newDoc);
Assert.IsTrue(mediaList[mediaList.Count - 1].Id > 0);
lastParentId = newDoc.Id;
}
//now delete all of them permanently, since they are nested, we only need to delete one
mediaList.First().delete(true);
//make sure they are all gone
foreach (var d in mediaList)
{
Assert.IsFalse(Media.IsNode(d.Id));
}
}
[TestMethod]
public void Media_Move()
{
//first need to document type that allows other types of document types to exist underneath it
MediaType parent = null;
MediaType child = null;
var ids = MediaType.getAllUniqueNodeIdsFromObjectType(MediaType._objectType);
foreach (var id in ids)
{
var dt = new MediaType(id);
var allowed = dt.AllowedChildContentTypeIDs.ToList();
if (allowed.Count() > 0)
{
parent = dt;
child = new MediaType(allowed[0]);
break;
}
}
if (parent == null || child == null)
{
throw new NotImplementedException("The umbraco install doesn't have document types that support a heirarchy");
}
//now that we have a parent and a child, we need to create some documents
var node1 = Media.MakeNew("FromCopy" + Guid.NewGuid().ToString("N"), parent, m_User, -1);
Assert.IsTrue(node1.Id > 0);
var node2 = Media.MakeNew("ToCopy" + Guid.NewGuid().ToString("N"), parent, m_User, -1);
Assert.IsTrue(node2.Id > 0);
//we now have 2 nodes in the root of the same type, we'll create a child node under node1 and move it to node2
var childNode = Media.MakeNew("ChildCopy" + Guid.NewGuid().ToString("N"), child, m_User, node2.Id);
Assert.IsTrue(childNode.Id > 0);
childNode.Move(node2.Id);
Assert.AreEqual(node2.Id, childNode.Parent.Id);
RecycleAndDelete(childNode);
RecycleAndDelete(node2);
RecycleAndDelete(node1);
}
[TestMethod()]
public void Media_Delete_All_Docs_By_Document_Type()
{
//System.Diagnostics.Debugger.Break();
//create a new media type
string name = "TEST-" + Guid.NewGuid().ToString("N");
var mt = MediaType.MakeNew(m_User, name);
//test the media type
Assert.AreEqual(DateTime.Now.Date, mt.CreateDateTime.Date);
Assert.IsTrue(mt.Id > 0);
//allow itself to be created under itself
mt.AllowedChildContentTypeIDs = new int[] { mt.Id };
//create a tab
mt.AddVirtualTab("TEST");
//test the tab
var tabs = mt.getVirtualTabs.ToList();
Assert.AreEqual(1, tabs.Count);
//create a property
var allDataTypes = DataTypeDefinition.GetAll().ToList(); //get all definitions
mt.AddPropertyType(allDataTypes[0], "testProperty", "Test Property"); //add a property type of the first type found in the list
//test the prop
var prop = mt.getPropertyType("testProperty");
Assert.AreEqual("Test Property", prop.Name);
//create 1st node
var node1 = Media.MakeNew("TEST-" + Guid.NewGuid().ToString("N"), mt, m_User, -1);
Assert.IsTrue(node1.Id > 0);
//create 2nd node underneath node 1
var node2 = Media.MakeNew("TEST-" + Guid.NewGuid().ToString("N"), mt, m_User, node1.Id);
Assert.IsTrue(node2.Id > 0);
Assert.AreEqual(node1.Id, node2.Parent.Id);
//create 3rd node underneath node 2
var node3 = Media.MakeNew("TEST-" + Guid.NewGuid().ToString("N"), mt, m_User, node2.Id);
Assert.IsTrue(node3.Id > 0);
Assert.AreEqual(node2.Id, node3.Parent.Id);
Media.DeleteFromType(mt);
Assert.IsFalse(Media.IsNode(node1.Id));
Assert.IsFalse(Media.IsNode(node2.Id));
Assert.IsFalse(Media.IsNode(node3.Id));
//now remove the document type created
mt.delete();
Assert.IsFalse(MediaType.IsNode(mt.Id));
}
[TestMethod]
public void Media_Empty_Recycle_Bin()
{
//System.Diagnostics.Debugger.Break();
var mediaList = new List();
var total = 20;
var mt = m_ExistingMediaType;
//allow the doc type to be created underneath itself
mt.AllowedChildContentTypeIDs = new int[] { mt.Id };
mt.Save();
//create 20 media nodes underneath each other, this will test deleting with heirarchy as well
var lastParentId = -1;
for (var i = 0; i < total; i++)
{
var newMedia = Media.MakeNew("R-" + i.ToString() + Guid.NewGuid().ToString("N"), mt, m_User, lastParentId);
mediaList.Add(newMedia);
Assert.IsTrue(mediaList[mediaList.Count - 1].Id > 0);
Assert.AreEqual(lastParentId, newMedia.ParentId);
lastParentId = newMedia.Id;
}
//now delete all of them, since they are nested, we only need to delete one
mediaList.First().delete();
//a callback action for each item removed from the recycle bin
var totalDeleted = 0;
var bin = new RecycleBin(RecycleBin.RecycleBinType.Media);
var totalTrashedItems = bin.GetDescendants().Cast