diff --git a/umbraco.Test/RelationTest.cs b/umbraco.Test/RelationTest.cs index ded7497aaf..f2614ae388 100644 --- a/umbraco.Test/RelationTest.cs +++ b/umbraco.Test/RelationTest.cs @@ -3,6 +3,9 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using umbraco.cms.businesslogic; +using umbraco.cms.businesslogic.web; +using System.Linq; +using umbraco.BusinessLogic; namespace umbraco.Test { @@ -16,19 +19,61 @@ namespace umbraco.Test public class RelationTest { - ///// - ///// Creates 2 documents and relates them - ///// - //[TestMethod()] - //public void Relation_Make_New() - //{ - - - - // actual = Relation.MakeNew(ParentId, ChildId, RelType, Comment); - // Assert.AreEqual(expected, actual); - // Assert.Inconclusive("Verify the correctness of this test method."); - //} + /// + /// Creates 2 documents and relates them, then deletes the relation + /// + [TestMethod()] + public void Relation_Make_New() + { + var dt = DocumentType.GetAllAsList().First(); + var parent = Document.MakeNew(Guid.NewGuid().ToString("N"), dt, m_User, -1); + var child = Document.MakeNew(Guid.NewGuid().ToString("N"), dt, m_User, -1); + var rt = RelationType.GetAll().First(); + + //make the relation + var r = Relation.MakeNew(parent.Id, child.Id, rt, Guid.NewGuid().ToString("N")); + Assert.IsTrue(r.Id > 0); + Assert.IsInstanceOfType(r, typeof(Relation)); + + //delete the relation + r.Delete(); + //make sure it's gone by looking up both parent & children + Assert.AreEqual(0, Relation.GetRelations(parent.Id).Count()); + Assert.AreEqual(0, Relation.GetRelations(child.Id).Count()); + + //now remove the documents + child.delete(true); + parent.delete(true); + } + + /// + /// Creates 2 documents, relates them, then deletes the parent doc and ensure that the relation is gone + /// + [TestMethod()] + public void Relation_Relate_Docs_And_Delete_Parent() + { + var dt = DocumentType.GetAllAsList().First(); + var parent = Document.MakeNew(Guid.NewGuid().ToString("N"), dt, m_User, -1); + var child = Document.MakeNew(Guid.NewGuid().ToString("N"), dt, m_User, -1); + var rt = RelationType.GetAll().First(); + + //make the relation + var r = Relation.MakeNew(parent.Id, child.Id, rt, Guid.NewGuid().ToString("N")); + Assert.IsTrue(r.Id > 0); + Assert.IsInstanceOfType(r, typeof(Relation)); + + //deletes the parent + parent.delete(true); + + //make sure it's gone by looking up both parent & children + Assert.AreEqual(0, Relation.GetRelations(parent.Id).Count()); + Assert.AreEqual(0, Relation.GetRelations(child.Id).Count()); + + //now remove the documents + child.delete(true); + } + + private User m_User = new User(0); #region Tests to write diff --git a/umbraco/cms/businesslogic/relation/RelationType.cs b/umbraco/cms/businesslogic/relation/RelationType.cs index c3259e8c61..c331235672 100644 --- a/umbraco/cms/businesslogic/relation/RelationType.cs +++ b/umbraco/cms/businesslogic/relation/RelationType.cs @@ -4,6 +4,7 @@ using System.Web; using umbraco.BusinessLogic; using umbraco.DataLayer; +using System.Collections.Generic; namespace umbraco.cms.businesslogic.relation { @@ -32,20 +33,24 @@ namespace umbraco.cms.businesslogic.relation #region Constructors - public RelationType(int Id) + /// + /// Internal constructor to create a new relation type + /// + internal RelationType() { } + + public RelationType(int id) { using (IRecordsReader dr = SqlHelper.ExecuteReader( - "select id, dual, name, alias from umbracoRelationType where id = @id", SqlHelper.CreateParameter("@id", Id))) + "select id, dual, name, alias from umbracoRelationType where id = @id", SqlHelper.CreateParameter("@id", id))) { - if(dr.Read()) - { - this._id = dr.GetInt("id"); - this._dual = dr.GetBoolean("dual"); - //this._parentObjectType = dr.GetGuid("parentObjectType"); - //this._childObjectType = dr.GetGuid("childObjectType"); - this._name = dr.GetString("name"); - this._alias = dr.GetString("alias"); - } + if (dr.Read()) + { + PopulateFromReader(dr); + } + else + { + throw new ArgumentException("Not RelationType found for id " + id.ToString()); + } } } @@ -96,6 +101,16 @@ namespace umbraco.cms.businesslogic.relation #endregion + private void PopulateFromReader(IRecordsReader dr) + { + this._id = dr.GetInt("id"); + this._dual = dr.GetBoolean("dual"); + //this._parentObjectType = dr.GetGuid("parentObjectType"); + //this._childObjectType = dr.GetGuid("childObjectType"); + this._name = dr.GetString("name"); + this._alias = dr.GetString("alias"); + } + #region Methods /// @@ -105,6 +120,27 @@ namespace umbraco.cms.businesslogic.relation { } + /// + /// Return all relation types + /// + /// + public static IEnumerable GetAll() + { + var relationTypes = new List(); + + using (IRecordsReader dr = SqlHelper.ExecuteReader("select id, dual, name, alias from umbracoRelationType")) + { + while (dr.Read()) + { + var rt = new RelationType(); + rt.PopulateFromReader(dr); + relationTypes.Add(rt); + } + } + + return relationTypes; + } + public static RelationType GetById(int id) { try