More unit tests.

Fixes: 27339, 27338, 27336, 27334, 24871

[TFS Changeset #66527]
This commit is contained in:
Shandem
2010-06-03 14:14:55 +00:00
parent a6cdbf1a55
commit 2733e8e504
11 changed files with 1029 additions and 226 deletions

View File

@@ -0,0 +1,362 @@
using umbraco.cms.businesslogic.datatype;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.editorControls.textfield;
using umbraco.cms.businesslogic.web;
using System.Linq;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.propertytype;
using umbraco.editorControls;
using System.Linq;
namespace umbraco.Test
{
/// <summary>
///This is a test class for DataTypeDefinitionTest and is intended
///to contain all DataTypeDefinitionTest Unit Tests
///</summary>
[TestClass()]
public class DataTypeDefinitionTest
{
/// <summary>
///A test for MakeNew
///</summary>
[TestMethod()]
public void DataTypeDefinition_Make_New()
{
//create data tyep definition
var dtd = DataTypeDefinition.MakeNew(m_User, "TEST" + Guid.NewGuid().ToString("N"));
Assert.IsTrue(dtd.Id > 0);
Assert.IsInstanceOfType(dtd, typeof(DataTypeDefinition));
//now delete it
dtd.delete();
Assert.IsFalse(DataTypeDefinition.IsNode(dtd.Id));
}
/// <summary>
/// Create a data type definition, add some prevalues to it then delete it
///</summary>
[TestMethod()]
public void DataTypeDefinition_Assign_Data_Type_With_PreValues()
{
//System.Diagnostics.Debugger.Launch();
//create datatype definition, assign data type
var dtd = DataTypeDefinition.MakeNew(m_User, "TEST" + Guid.NewGuid().ToString("N"));
Assert.IsTrue(dtd.Id > 0);
Assert.IsInstanceOfType(dtd, typeof(DataTypeDefinition));
IDataType dt = new TextFieldDataType();
dt.DataTypeDefinitionId = dtd.Id; //need to set the data types data type definition id
dtd.DataType = dt; //set our data type definition's data type to the text field... i know this is all too weird.
Assert.AreEqual(dt.Id, dtd.DataType.Id);
Assert.IsInstanceOfType(dtd.DataType, dt.GetType());
Assert.AreEqual(dtd.Id, dt.DataTypeDefinitionId);
//create the prevalues
((DefaultPrevalueEditor)dt.PrevalueEditor).Prevalue = "TEST" + Guid.NewGuid().ToString("N");
dt.PrevalueEditor.Save();
//verify that the prevalue is there
Assert.AreEqual<int>(1, PreValues.GetPreValues(dtd.Id).Count);
//now remove it
dtd.delete();
Assert.IsFalse(DataTypeDefinition.IsNode(dtd.Id));
}
/// <summary>
/// Create a new definition, assign a data type to it,
/// create a doc type and assign this new data type to it,
/// then create a document from the doc type and set the value for the property.
///
/// Once the data is all setup, we'll delete the data type definition which should:
/// 1. Remove all property type values associated with the property type of the dtd
/// 2. Remove all property types from document types associated with the dtd
/// 3. Remove the dtd
///
/// then we'll clean up the rest of the data.
/// </summary>
[TestMethod()]
public void DataTypeDefinition_Assign_Data_Type_To_Doc_Type_Then_Create_Doc_And_Set_Value()
{
//create datatype definition, assign data type
var dtd = DataTypeDefinition.MakeNew(m_User, "TEST" + Guid.NewGuid().ToString("N"));
Assert.IsTrue(dtd.Id > 0);
Assert.IsInstanceOfType(dtd, typeof(DataTypeDefinition));
IDataType dt = new TextFieldDataType();
dtd.DataType = dt;
Assert.AreEqual(dt.Id, dtd.DataType.Id);
Assert.IsInstanceOfType(dtd.DataType, dt.GetType());
//create new doc type
var docType = DocumentType.MakeNew(m_User, "TEST" + Guid.NewGuid().ToString("N"));
//create the property with this new data type definition
var alias = "TEST" + Guid.NewGuid().ToString("N");
docType.AddPropertyType(dtd, alias, alias);
Assert.AreEqual<int>(1, docType.PropertyTypes.Count());
//create a new doc with the new doc type
var doc = Document.MakeNew("TEST" + Guid.NewGuid().ToString("N"), docType, m_User, -1);
//set the value of the property
var prop = doc.getProperty(alias);
var propType = prop.PropertyType;
Assert.IsNotNull(prop);
var val = "TEST" + Guid.NewGuid().ToString("N");
prop.Value = val;
Assert.AreEqual(val, prop.Value);
//ok, now that all of the data is setup, we'll just delete the data type definition.
dtd.delete();
//make sure the property value is gone, check with sql
Assert.AreEqual<int>(0, Application.SqlHelper.ExecuteScalar<int>(
"SELECT COUNT(id) FROM cmsPropertyData WHERE propertytypeid=@propTypeId",
Application.SqlHelper.CreateParameter("@propTypeId", propType.Id)));
//make sure the property type is gone
var hasError = false;
try
{
var confirmPropType = new PropertyType(propType.Id);
}
catch (ArgumentException)
{
hasError = true;
}
Assert.IsTrue(hasError);
//make sure the dtd is gone
Assert.IsFalse(DataTypeDefinition.IsNode(dtd.Id));
//now cleanup the rest
doc.delete(true);
Assert.IsFalse(Document.IsNode(doc.Id));
docType.delete();
Assert.IsFalse(DocumentType.IsNode(docType.Id));
}
#region Private methods/members
private User m_User = new User(0);
#endregion
#region Tests to write
///// <summary>
/////A test for DataTypeDefinition Constructor
/////</summary>
//[TestMethod()]
//public void DataTypeDefinitionConstructorTest()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id);
// Assert.Inconclusive("TODO: Implement code to verify target");
//}
///// <summary>
/////A test for DataTypeDefinition Constructor
/////</summary>
//[TestMethod()]
//public void DataTypeDefinitionConstructorTest1()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id);
// Assert.Inconclusive("TODO: Implement code to verify target");
//}
///// <summary>
/////A test for Delete
/////</summary>
//[TestMethod()]
//public void DeleteTest()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id); // TODO: Initialize to an appropriate value
// target.Delete();
// Assert.Inconclusive("A method that does not return a value cannot be verified.");
//}
///// <summary>
/////A test for GetAll
/////</summary>
//[TestMethod()]
//public void GetAllTest()
//{
// DataTypeDefinition[] expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition[] actual;
// actual = DataTypeDefinition.GetAll();
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetByDataTypeId
/////</summary>
//[TestMethod()]
//public void GetByDataTypeIdTest()
//{
// Guid DataTypeId = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// actual = DataTypeDefinition.GetByDataTypeId(DataTypeId);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetDataTypeDefinition
/////</summary>
//[TestMethod()]
//public void GetDataTypeDefinitionTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// actual = DataTypeDefinition.GetDataTypeDefinition(id);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetDataTypeDefinition
/////</summary>
//[TestMethod()]
//public void GetDataTypeDefinitionTest1()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// actual = DataTypeDefinition.GetDataTypeDefinition(id);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Import
/////</summary>
//[TestMethod()]
//public void ImportTest()
//{
// XmlNode xmlData = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// actual = DataTypeDefinition.Import(xmlData);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for IsDefaultData
/////</summary>
//[TestMethod()]
//public void IsDefaultDataTest()
//{
// object Data = null; // TODO: Initialize to an appropriate value
// bool expected = false; // TODO: Initialize to an appropriate value
// bool actual;
// actual = DataTypeDefinition.IsDefaultData(Data);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for MakeNew
/////</summary>
//[TestMethod()]
//public void MakeNewTest1()
//{
// User u = null; // TODO: Initialize to an appropriate value
// string Text = string.Empty; // TODO: Initialize to an appropriate value
// Guid UniqueId = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// actual = DataTypeDefinition.MakeNew(u, Text, UniqueId);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Save
/////</summary>
//[TestMethod()]
//public void SaveTest()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id); // TODO: Initialize to an appropriate value
// target.Save();
// Assert.Inconclusive("A method that does not return a value cannot be verified.");
//}
///// <summary>
/////A test for ToXml
/////</summary>
//[TestMethod()]
//public void ToXmlTest()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id); // TODO: Initialize to an appropriate value
// XmlDocument xd = null; // TODO: Initialize to an appropriate value
// XmlElement expected = null; // TODO: Initialize to an appropriate value
// XmlElement actual;
// actual = target.ToXml(xd);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for delete
/////</summary>
//[TestMethod()]
//public void deleteTest()
//{
// Guid id = new Guid(); // TODO: Initialize to an appropriate value
// DataTypeDefinition target = new DataTypeDefinition(id); // TODO: Initialize to an appropriate value
// target.delete();
// Assert.Inconclusive("A method that does not return a value cannot be verified.");
//}
#endregion
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
}
}

View File

@@ -0,0 +1,344 @@
using umbraco.cms.businesslogic.propertytype;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using umbraco.interfaces;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic;
using System.Linq;
using umbraco.cms.businesslogic.web;
namespace umbraco.Test
{
/// <summary>
///This is a test class for PropertyTypeTest and is intended
///to contain all PropertyTypeTest Unit Tests
///</summary>
[TestClass()]
public class PropertyTypeTest
{
/// <summary>
///A test for MakeNew
///</summary>
[TestMethod()]
public void PropertyType_Make_New()
{
var allDataTypes = DataTypeDefinition.GetAll().ToList(); //get all definitions
var contentTypes = DocumentType.GetAllAsList();
var name = "TEST" + Guid.NewGuid().ToString("N");
var pt = PropertyType.MakeNew(allDataTypes.First(), contentTypes.First(), name, name);
Assert.IsTrue(pt.Id > 0);
Assert.IsInstanceOfType(pt, typeof(PropertyType));
pt.delete();
//make sure it's gone
Assert.IsFalse(PropertyType.GetAll().Select(x => x.Id).Contains(pt.Id));
}
#region Tests to write
///// <summary>
/////A test for PropertyType Constructor
/////</summary>
//[TestMethod()]
//public void PropertyTypeConstructorTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id);
// Assert.Inconclusive("TODO: Implement code to verify target");
//}
///// <summary>
/////A test for GetAll
/////</summary>
//[TestMethod()]
//public void GetAllTest()
//{
// PropertyType[] expected = null; // TODO: Initialize to an appropriate value
// PropertyType[] actual;
// actual = PropertyType.GetAll();
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetEditControl
/////</summary>
//[TestMethod()]
//public void GetEditControlTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// object Value = null; // TODO: Initialize to an appropriate value
// bool IsPostBack = false; // TODO: Initialize to an appropriate value
// IDataType expected = null; // TODO: Initialize to an appropriate value
// IDataType actual;
// actual = target.GetEditControl(Value, IsPostBack);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetPropertyType
/////</summary>
//[TestMethod()]
//public void GetPropertyTypeTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType expected = null; // TODO: Initialize to an appropriate value
// PropertyType actual;
// actual = PropertyType.GetPropertyType(id);
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetRawDescription
/////</summary>
//[TestMethod()]
//public void GetRawDescriptionTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// actual = target.GetRawDescription();
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for GetRawName
/////</summary>
//[TestMethod()]
//public void GetRawNameTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// actual = target.GetRawName();
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Save
/////</summary>
//[TestMethod()]
//public void SaveTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// target.Save();
// Assert.Inconclusive("A method that does not return a value cannot be verified.");
//}
///// <summary>
/////A test for delete
/////</summary>
//[TestMethod()]
//public void deleteTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// target.delete();
// Assert.Inconclusive("A method that does not return a value cannot be verified.");
//}
///// <summary>
/////A test for Alias
/////</summary>
//[TestMethod()]
//public void AliasTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// target.Alias = expected;
// actual = target.Alias;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for ContentTypeId
/////</summary>
//[TestMethod()]
//public void ContentTypeIdTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// int actual;
// actual = target.ContentTypeId;
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for DataTypeDefinition
/////</summary>
//[TestMethod()]
//public void DataTypeDefinitionTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// DataTypeDefinition expected = null; // TODO: Initialize to an appropriate value
// DataTypeDefinition actual;
// target.DataTypeDefinition = expected;
// actual = target.DataTypeDefinition;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Description
/////</summary>
//[TestMethod()]
//public void DescriptionTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// target.Description = expected;
// actual = target.Description;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Id
/////</summary>
//[TestMethod()]
//public void IdTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// int actual;
// actual = target.Id;
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Mandatory
/////</summary>
//[TestMethod()]
//public void MandatoryTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// bool expected = false; // TODO: Initialize to an appropriate value
// bool actual;
// target.Mandatory = expected;
// actual = target.Mandatory;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for Name
/////</summary>
//[TestMethod()]
//public void NameTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// target.Name = expected;
// actual = target.Name;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for SortOrder
/////</summary>
//[TestMethod()]
//public void SortOrderTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// int expected = 0; // TODO: Initialize to an appropriate value
// int actual;
// target.SortOrder = expected;
// actual = target.SortOrder;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for TabId
/////</summary>
//[TestMethod()]
//public void TabIdTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// int expected = 0; // TODO: Initialize to an appropriate value
// int actual;
// target.TabId = expected;
// actual = target.TabId;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
///// <summary>
/////A test for ValidationRegExp
/////</summary>
//[TestMethod()]
//public void ValidationRegExpTest()
//{
// int id = 0; // TODO: Initialize to an appropriate value
// PropertyType target = new PropertyType(id); // TODO: Initialize to an appropriate value
// string expected = string.Empty; // TODO: Initialize to an appropriate value
// string actual;
// target.ValidationRegExp = expected;
// actual = target.ValidationRegExp;
// Assert.AreEqual(expected, actual);
// Assert.Inconclusive("Verify the correctness of this test method.");
//}
#endregion
#region Initialize and cleanup
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
}
}

View File

@@ -136,6 +136,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DataTypeDefinitionTest.cs" />
<Compile Include="DictionaryTest.cs" />
<Compile Include="DocumentTest.cs" />
<Compile Include="DocumentTypeTest.cs" />
@@ -145,6 +146,7 @@
<Compile Include="MemberTest.cs" />
<Compile Include="MemberTypeTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyTypeTest.cs" />
<Compile Include="UserTest.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -566,12 +566,25 @@ namespace umbraco.cms.businesslogic
IDataType uploadField = new Factory().GetNewObject(new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));
foreach (Property p in GenericProperties)
{
if (p.PropertyType.DataTypeDefinition.DataType.Id == uploadField.Id &&
p.Value.ToString() != "" &&
File.Exists(IOHelper.MapPath(p.Value.ToString())))
{
var isUploadField = false;
try
{
if (p.PropertyType.DataTypeDefinition.DataType.Id == uploadField.Id
&& p.Value.ToString() != ""
&& File.Exists(IOHelper.MapPath(p.Value.ToString())))
{
isUploadField = true;
}
}
catch (ArgumentException)
{
//the data type definition may not exist anymore at this point because another thread may
//have deleted it.
isUploadField = false;
}
if (isUploadField)
{
var fi = new FileInfo(IOHelper.MapPath(p.Value.ToString()));
fi.Directory.GetFiles().ToList().ForEach(x =>
@@ -580,7 +593,6 @@ namespace umbraco.cms.businesslogic
});
fi.Directory.Delete(true);
}
}
}

View File

@@ -110,19 +110,13 @@ namespace umbraco.cms.businesslogic
/// <returns></returns>
public static ContentType GetContentType(int id)
{
if (HttpRuntime.Cache[string.Format("UmbracoContentType{0}", id.ToString())] == null)
{
lock (m_Locker)
return Cache.GetCacheItem<ContentType>(string.Format("UmbracoContentType{0}", id.ToString()),
m_Locker,
TimeSpan.FromMinutes(30),
delegate
{
//double check
if (HttpRuntime.Cache[string.Format("UmbracoContentType{0}", id.ToString())] == null)
{
ContentType ct = new ContentType(id);
HttpRuntime.Cache.Insert(string.Format("UmbracoContentType{0}", id.ToString()), ct);
}
}
}
return (ContentType)HttpRuntime.Cache[string.Format("UmbracoContentType{0}", id.ToString())];
return new ContentType(id);
});
}
/// <summary>
@@ -756,20 +750,8 @@ namespace umbraco.cms.businesslogic
/// <param name="Id">The id.</param>
protected void FlushFromCache(int Id)
{
if (HttpRuntime.Cache[string.Format("UmbracoContentType{0}", Id.ToString())] != null)
{
lock (m_Locker)
{
//double check
if (HttpRuntime.Cache[string.Format("UmbracoContentType{0}", Id.ToString())] != null)
{
HttpRuntime.Cache.Remove(string.Format("UmbracoContentType{0}", Id.ToString()));
}
}
}
string cacheKey = GetPropertiesCacheKey();
Cache.ClearCacheItem(cacheKey);
Cache.ClearCacheItem(string.Format("UmbracoContentType{0}", Id.ToString()));
Cache.ClearCacheItem(GetPropertiesCacheKey());
ClearVirtualTabs();
}

View File

@@ -5,6 +5,7 @@ using System.Collections;
using umbraco.DataLayer;
using System.Xml;
using umbraco.interfaces;
using umbraco.cms.businesslogic.propertytype;
namespace umbraco.cms.businesslogic.datatype
{
@@ -66,6 +67,16 @@ namespace umbraco.cms.businesslogic.datatype
#region Public methods
public override void delete()
{
//first clear the prevalues
PreValues.DeleteByDataTypeDefinition(this.Id);
//next clear out the property types
var propTypes = PropertyType.GetByDataTypeDefinition(this.Id);
foreach (var p in propTypes)
{
p.delete();
}
//delete the cmsDataType role, then the umbracoNode
SqlHelper.ExecuteNonQuery("delete from cmsDataType where nodeId=@nodeId", SqlHelper.CreateParameter("@nodeId", this.Id));
base.delete();
@@ -295,14 +306,16 @@ namespace umbraco.cms.businesslogic.datatype
{
base.setupNode();
IRecordsReader dr = SqlHelper.ExecuteReader("select dbType, controlId from cmsDataType where nodeId = '" + this.Id.ToString() + "'");
if (dr.Read())
using (IRecordsReader dr = SqlHelper.ExecuteReader("select dbType, controlId from cmsDataType where nodeId = '" + this.Id.ToString() + "'"))
{
_controlId = dr.GetGuid("controlId");
if (dr.Read())
{
_controlId = dr.GetGuid("controlId");
}
else
throw new ArgumentException("No dataType with id = " + this.Id.ToString() + " found");
}
else
throw new ArgumentException("No dataType with id = " + this.Id.ToString() + " found");
dr.Close();
}
#endregion

View File

@@ -16,6 +16,8 @@ namespace umbraco.cms.businesslogic.datatype
// referenced datatype
private cms.businesslogic.datatype.BaseDataType _datatype;
//WHY IS THIS HERE... IT IS NEVER SET!?
private BaseDataType _datatypeOld;
private bool _isEnsured = false;
@@ -130,17 +132,11 @@ namespace umbraco.cms.businesslogic.datatype
else
throw new ArgumentException("Datatype is not initialized");
bool hasPrevalue = (SqlHelper.ExecuteScalar<int>("select count(id) from cmsDataTypePreValues where dataTypeNodeId = " + defId) > 0);
IParameter[] SqlParams = new IParameter[]
{
SqlHelper.CreateParameter("@value", _textbox.Text),
SqlHelper.CreateParameter("@dtdefid", defId)
};
bool hasPrevalue = PreValues.CountOfPreValues(defId) > 0;
if (!hasPrevalue)
{
SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')",
SqlParams);
PreValue.MakeNew(defId, _textbox.Text);
}
_isEnsured = true;
}
@@ -195,6 +191,7 @@ namespace umbraco.cms.businesslogic.datatype
}
[Obsolete("Use the PreValues class for data access instead")]
public static string GetPrevalueFromId(int Id)
{
return SqlHelper.ExecuteScalar<string>("Select [value] from cmsDataTypePreValues where id = @id",

View File

@@ -0,0 +1,210 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Runtime.CompilerServices;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
namespace umbraco.cms.businesslogic.datatype
{
/// <summary>
/// A simple class for storing predefined values on a datatype.
/// A prevalue contains a value, a unique key and sort order.
/// </summary>
public class PreValue
{
private static ISqlHelper SqlHelper
{
get { return Application.SqlHelper; }
}
#region Contructors
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
public PreValue()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="Id">The id.</param>
/// <param name="SortOrder">The sort order.</param>
/// <param name="Value">The value.</param>
public PreValue(int Id, int SortOrder, string Value)
{
_id = Id;
_sortOrder = SortOrder;
_value = Value;
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="Id">The id.</param>
public PreValue(int Id)
{
_id = Id;
initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="DataTypeId">The data type id.</param>
/// <param name="Value">The value.</param>
public PreValue(int DataTypeId, string Value)
{
object id = SqlHelper.ExecuteScalar<object>(
"Select id from cmsDataTypePreValues where [Value] = @value and DataTypeNodeId = @dataTypeId",
SqlHelper.CreateParameter("@dataTypeId", DataTypeId),
SqlHelper.CreateParameter("@value", Value));
if (id != null)
_id = int.Parse(id.ToString());
initialize();
}
#endregion
/// <summary>
/// Create a new pre value with a value
/// </summary>
/// <param name="dataTypeDefId"></param>
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static PreValue MakeNew(int dataTypeDefId, string value)
{
SqlHelper.ExecuteNonQuery(
"insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')",
SqlHelper.CreateParameter("@dtdefid", dataTypeDefId),
SqlHelper.CreateParameter("@value", value));
var id = SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM cmsDataTypePreValues");
return new PreValue(id);
}
#region Private members
private int _dataTypeId;
private int? _id;
private string _value;
private int _sortOrder;
#endregion
#region Public properties
/// <summary>
/// Gets or sets the data type id.
/// </summary>
/// <value>The data type id.</value>
public int DataTypeId
{
get { return _dataTypeId; }
set { _dataTypeId = value; }
}
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public int Id
{
get { return _id.Value; }
set { _id = value; }
}
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public string Value
{
get { return _value; }
set { _value = value; }
}
/// <summary>
/// Gets or sets the sort order.
/// </summary>
/// <value>The sort order.</value>
public int SortOrder
{
get { return _sortOrder; }
set { _sortOrder = value; }
}
#endregion
#region Public methods
/// <summary>
/// Deletes a prevalue item
/// </summary>
public void Delete()
{
if (_id == null)
{
throw new ArgumentNullException("Id");
}
SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where id = @id",
SqlHelper.CreateParameter("@id", this.Id));
}
/// <summary>
/// Saves this instance.
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Save()
{
// Check for new
if (Id == 0)
{
// Update sortOrder
object tempSortOrder = SqlHelper.ExecuteScalar<object>("select max(sortorder) from cmsDataTypePreValues where datatypenodeid = @dataTypeId", SqlHelper.CreateParameter("@dataTypeId", DataTypeId));
int _sortOrder = 0;
if (tempSortOrder != null && int.TryParse(tempSortOrder.ToString(), out _sortOrder))
SortOrder = _sortOrder + 1;
else
SortOrder = 1;
IParameter[] SqlParams = new IParameter[] {
SqlHelper.CreateParameter("@value",Value),
SqlHelper.CreateParameter("@dtdefid",DataTypeId)};
// The method is synchronized
SqlHelper.ExecuteNonQuery("INSERT INTO cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) VALUES (@dtdefid,@value,0,'')", SqlParams);
_id = SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM cmsDataTypePreValues");
}
SqlHelper.ExecuteNonQuery(
"update cmsDataTypePreValues set sortorder = @sortOrder, [value] = @value where id = @id",
SqlHelper.CreateParameter("@sortOrder", SortOrder),
SqlHelper.CreateParameter("@value", Value),
SqlHelper.CreateParameter("@id", Id));
}
#endregion
#region Private methods
/// <summary>
/// Initializes this instance.
/// </summary>
private void initialize()
{
IRecordsReader dr = SqlHelper.ExecuteReader(
"Select id, sortorder, [value] from cmsDataTypePreValues where id = @id order by sortorder",
SqlHelper.CreateParameter("@id", Id));
if (dr.Read())
{
_sortOrder = dr.GetInt("sortorder");
_value = dr.GetString("value");
}
dr.Close();
}
#endregion
}
}

View File

@@ -43,166 +43,29 @@ namespace umbraco.cms.businesslogic.datatype
return retval;
}
/// <summary>
/// Removes all prevalues with the specified data type definition id
/// </summary>
/// <param name="dataTypeDefId"></param>
public static void DeleteByDataTypeDefinition(int dataTypeDefId)
{
SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid",
SqlHelper.CreateParameter("@dtdefid", dataTypeDefId));
}
/// <summary>
/// Returns the number of prevalues for data type definition
/// </summary>
/// <param name="dataTypeDefId"></param>
/// <returns></returns>
public static int CountOfPreValues(int dataTypeDefId)
{
return SqlHelper.ExecuteScalar<int>(
"select count(id) from cmsDataTypePreValues where dataTypeNodeId = @dataTypeId",
SqlHelper.CreateParameter("@dataTypeId", dataTypeDefId));
}
}
/// <summary>
/// A simple class for storing predefined values on a datatype.
/// A prevalue contains a value, a unique key and sort order.
/// </summary>
public class PreValue
{
private static ISqlHelper SqlHelper
{
get { return Application.SqlHelper; }
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
public PreValue()
{
}
/// <summary>
/// Saves this instance.
/// </summary>
[MethodImpl(MethodImplOptions.Synchronized)]
public void Save()
{
// Check for new
if (Id == 0)
{
// Update sortOrder
object tempSortOrder = SqlHelper.ExecuteScalar<object>("select max(sortorder) from cmsDataTypePreValues where datatypenodeid = @dataTypeId", SqlHelper.CreateParameter("@dataTypeId", DataTypeId));
int _sortOrder = 0;
if (tempSortOrder!=null && int.TryParse(tempSortOrder.ToString(), out _sortOrder))
SortOrder = _sortOrder + 1;
else
SortOrder = 1;
IParameter[] SqlParams = new IParameter[] {
SqlHelper.CreateParameter("@value",Value),
SqlHelper.CreateParameter("@dtdefid",DataTypeId)};
// The method is synchronized
SqlHelper.ExecuteNonQuery("INSERT INTO cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) VALUES (@dtdefid,@value,0,'')", SqlParams);
_id = SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM cmsDataTypePreValues");
}
SqlHelper.ExecuteNonQuery(
"update cmsDataTypePreValues set sortorder = @sortOrder, [value] = @value where id = @id",
SqlHelper.CreateParameter("@sortOrder", SortOrder),
SqlHelper.CreateParameter("@value", Value),
SqlHelper.CreateParameter("@id", Id));
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="Id">The id.</param>
/// <param name="SortOrder">The sort order.</param>
/// <param name="Value">The value.</param>
public PreValue(int Id, int SortOrder, string Value)
{
_id = Id;
_sortOrder = SortOrder;
_value = Value;
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="Id">The id.</param>
public PreValue(int Id)
{
_id = Id;
initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="PreValue"/> class.
/// </summary>
/// <param name="DataTypeId">The data type id.</param>
/// <param name="Value">The value.</param>
public PreValue(int DataTypeId, string Value)
{
object id = SqlHelper.ExecuteScalar<object>(
"Select id from cmsDataTypePreValues where [Value] = @value and DataTypeNodeId = @dataTypeId",
SqlHelper.CreateParameter("@dataTypeId", DataTypeId),
SqlHelper.CreateParameter("@value", Value));
if (id != null)
_id = int.Parse(id.ToString());
initialize();
}
/// <summary>
/// Initializes this instance.
/// </summary>
private void initialize()
{
IRecordsReader dr = SqlHelper.ExecuteReader(
"Select id, sortorder, [value] from cmsDataTypePreValues where id = @id order by sortorder",
SqlHelper.CreateParameter("@id", _id));
if (dr.Read())
{
_sortOrder = dr.GetInt("sortorder");
_value = dr.GetString("value");
}
dr.Close();
}
private int _dataTypeId;
/// <summary>
/// Gets or sets the data type id.
/// </summary>
/// <value>The data type id.</value>
public int DataTypeId
{
get { return _dataTypeId; }
set { _dataTypeId = value; }
}
private int _id;
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _value;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public string Value
{
get { return _value; }
set { _value = value; }
}
private int _sortOrder;
/// <summary>
/// Gets or sets the sort order.
/// </summary>
/// <value>The sort order.</value>
public int SortOrder
{
get { return _sortOrder; }
set { _sortOrder = value; }
}
}
}

View File

@@ -4,7 +4,7 @@ using System.Data;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Web.UI;
using System.Linq;
using umbraco.cms.businesslogic.cache;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.language;
@@ -250,29 +250,19 @@ namespace umbraco.cms.businesslogic.propertytype
PropertyType pt;
try
{
// The method is synchronized
// The method is synchronized, but we'll still look it up with an additional parameter (alias)
SqlHelper.ExecuteNonQuery("INSERT INTO cmsPropertyType (DataTypeId, ContentTypeId, alias, name) VALUES (@DataTypeId, @ContentTypeId, @alias, @name)",
SqlHelper.CreateParameter("@DataTypeId", dt.Id),
SqlHelper.CreateParameter("@ContentTypeId", ct.Id),
SqlHelper.CreateParameter("@alias", helpers.Casing.SafeAliasWithForcingCheck(Alias)),
SqlHelper.CreateParameter("@name", Name));
pt = new PropertyType(SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM cmsPropertyType"));
pt = new PropertyType(SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM cmsPropertyType WHERE alias=@alias",
SqlHelper.CreateParameter("@alias", Alias)));
}
finally
{
// Clear cached items
System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache;
if (c != null)
{
System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator();
while (cacheEnumerator.MoveNext())
{
if (cacheEnumerator.Key is string && ((string)cacheEnumerator.Key).StartsWith(UmbracoPropertyTypeCacheKey))
{
Cache.ClearCacheItem((string)cacheEnumerator.Key);
}
}
}
Cache.ClearCacheByKeySearch(UmbracoPropertyTypeCacheKey);
}
return pt;
@@ -289,11 +279,34 @@ namespace umbraco.cms.businesslogic.propertytype
PropertyType pt = GetPropertyType(dr.GetInt("id"));
if(pt != null)
result.Add(pt);
}
return result.ToArray();
}
}
return result.ToArray();
}
/// <summary>
/// Returns all property types based on the data type definition
/// </summary>
/// <param name="dataTypeDefId"></param>
/// <returns></returns>
public static IEnumerable<PropertyType> GetByDataTypeDefinition(int dataTypeDefId)
{
List<PropertyType> result = new List<PropertyType>();
using (IRecordsReader dr =
SqlHelper.ExecuteReader(
"select id, Name from cmsPropertyType where dataTypeId=@dataTypeId order by Name",
SqlHelper.CreateParameter("@dataTypeId", dataTypeDefId)))
{
while (dr.Read())
{
PropertyType pt = GetPropertyType(dr.GetInt("id"));
if (pt != null)
result.Add(pt);
}
}
return result.ToList();
}
public void delete()
{
// flush cache
@@ -301,9 +314,13 @@ namespace umbraco.cms.businesslogic.propertytype
// Delete all properties of propertytype
var objs = Content.getContentOfContentType(new ContentType(_contenttypeid));
foreach(Content c in objs)
foreach(Content c in objs.ToList())
{
c.getProperty(this).delete();
var prop = c.getProperty(this);
if (prop != null)
{
prop.delete();
}
}
// Delete PropertyType ..
SqlHelper.ExecuteNonQuery("Delete from cmsPropertyType where id = " + this.Id);

View File

@@ -191,6 +191,7 @@
<Compile Include="businesslogic\CMSPreviewNode.cs" />
<Compile Include="businesslogic\datatype\ClientDependencyAttribute.cs" />
<Compile Include="businesslogic\datatype\DBTypes.cs" />
<Compile Include="businesslogic\datatype\PreValue.cs" />
<Compile Include="businesslogic\ISaveHandlerContents.cs" />
<Compile Include="businesslogic\Packager\FileResources\PackageFiles.Designer.cs">
<AutoGen>True</AutoGen>