Skip to content

Commit 13e4fe9

Browse files
committed
Remove relationships when calling TopicRepositoryBase.Delete()
Previously, when a topic was deleted, relationships from the topic and its descendants were not deleted. This potentially introduces problems as any target relationships outside of the deleted topic graph would maintain `IncomingRelationships` back to the deleted topics. That means that those topics would persist in memory. It also means that any views displaying content from `IncomingRelationships` may provide links to resources that no longer exist. To mitigate this, when `Delete()` is called, it now loops through all topics in the topic graph to be deleted, and removes all relationships from them. And, thanks to a new bug fix which ensures reciprocal `IncomingRelationships` are deleted when `RemoveTopic()` is called (f8b80ec), this fully addresses this issue. In addition, a unit test has been introduced to validate this functionality.
1 parent f8b80ec commit 13e4fe9

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

OnTopic.Tests/TopicRepositoryBaseTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,29 @@ public void Delete_NestedTopics_Succeeds() {
146146

147147
}
148148

149+
/*==========================================================================================================================
150+
| TEST: DELETE: RELATIONSHIPS: DELETE RELATIONSHIPS
151+
\-------------------------------------------------------------------------------------------------------------------------*/
152+
/// <summary>
153+
/// Deletes a topic with outgoing relationships. Deletes those relationships from that topic's <see cref=
154+
/// "Topic.IncomingRelationships"/> collection.
155+
/// </summary>
156+
[TestMethod]
157+
public void Delete_Relationships_DeleteRelationships() {
158+
159+
var root = TopicFactory.Create("Root", "Page");
160+
var topic = TopicFactory.Create("Topic", "Page", root);
161+
var child = TopicFactory.Create("Child", "Page", topic);
162+
var related = TopicFactory.Create("Related", "Page", root);
163+
164+
child.Relationships.SetTopic("Related", related);
165+
166+
_topicRepository.Delete(topic, true);
167+
168+
Assert.AreEqual<int>(0, related.IncomingRelationships.GetTopics("Related").Count);
169+
170+
}
171+
149172
/*==========================================================================================================================
150173
| TEST: GET ATTRIBUTES: ANY ATTRIBUTES: RETURNS ALL ATTRIBUTES
151174
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,21 @@ public virtual void Delete([ValidatedNotNull]Topic topic, bool isRecursive) {
491491
topic.Parent.Children.Remove(topic.Key);
492492
}
493493

494+
/*------------------------------------------------------------------------------------------------------------------------
495+
| Remove relationships
496+
\-----------------------------------------------------------------------------------------------------------------------*/
497+
var descendantTopics = topic.FindAll(t => true);
498+
499+
foreach (var descendantTopic in descendantTopics) {
500+
foreach (var relationship in descendantTopic.Relationships) {
501+
foreach (var relatedTopic in relationship.ToArray()) {
502+
if (!descendantTopics.Contains(relatedTopic)) {
503+
descendantTopic.Relationships.RemoveTopic(relationship.Name, relatedTopic);
504+
}
505+
}
506+
}
507+
}
508+
494509
/*------------------------------------------------------------------------------------------------------------------------
495510
| If content type, remove from cache
496511
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)