/src/Framework/Tests/Web/PathDataTests.cs

https://github.com/lundbeck/n2cms · C# · 328 lines · 270 code · 58 blank · 0 comment · 0 complexity · b5d3d26eb203d12a28f64de9fd89abe5 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using N2.Web;
  7. using Shouldly;
  8. using N2.Persistence;
  9. using N2.Details;
  10. namespace N2.Tests.Web
  11. {
  12. [TestFixture]
  13. public class PathDataTests
  14. {
  15. private ContentPersister persister;
  16. private Items.PageItem page;
  17. private Items.DataItem item;
  18. [SetUp]
  19. public void SetUp()
  20. {
  21. persister = TestSupport.SetupFakePersister();
  22. persister.Save(page = new Items.PageItem { ID = 1 });
  23. persister.Save(item = new Items.DataItem { ID = 2 });
  24. }
  25. [Test]
  26. public void SetItem()
  27. {
  28. var path = new PathData(page);
  29. path.CurrentItem.ID.ShouldBe(1);
  30. path.ID.ShouldBe(1);
  31. }
  32. [Test]
  33. public void SetItem_ShouldBeFallback_OfPage()
  34. {
  35. var path = new PathData(page);
  36. path.CurrentPage.ID.ShouldBe(1);
  37. }
  38. [Test]
  39. public void SetItem_ToNull_ShouldGiveNullItem()
  40. {
  41. var path = new PathData(null);
  42. path.CurrentItem.ShouldBe(null);
  43. }
  44. [Test]
  45. public void Item_and_Page_MayDiffer()
  46. {
  47. var path = new PathData();
  48. path.CurrentItem = item;
  49. path.CurrentPage = page;
  50. path.CurrentPage.ShouldNotBe(item);
  51. path.CurrentItem.ShouldNotBe(page);
  52. }
  53. [Test]
  54. public void Item_MayBe_null()
  55. {
  56. var path = new PathData();
  57. path.CurrentItem = item;
  58. path.CurrentItem = null;
  59. path.CurrentItem.ShouldBe(null);
  60. path.ID.ShouldBe(0);
  61. }
  62. [Test]
  63. public void Page_MayBe_null()
  64. {
  65. var path = new PathData();
  66. path.CurrentPage = page;
  67. path.CurrentPage = null;
  68. path.CurrentPage.ShouldBe(null);
  69. path.PageID.ShouldBe(0);
  70. }
  71. [Test]
  72. public void Detach_removes_reference_to_item_but_leaves_id()
  73. {
  74. var path = new PathData { CurrentItem = page };
  75. path = path.Detach();
  76. path.CurrentItem.ShouldBe(null);
  77. path.ID.ShouldBe(1);
  78. }
  79. [Test]
  80. public void Detach_removes_reference_to_page_but_leaves_id()
  81. {
  82. var path = new PathData { CurrentPage = page };
  83. path = path.Detach();
  84. path.CurrentPage.ShouldBe(null);
  85. path.PageID.ShouldBe(1);
  86. }
  87. [Test]
  88. public void Detach_removes_reference_to_StopItem_but_leaves_id()
  89. {
  90. var path = new PathData { StopItem = page };
  91. path = path.Detach();
  92. path.StopItem.ShouldBe(null);
  93. path.StopID.ShouldBe(1);
  94. }
  95. [Test]
  96. public void Detach_creates_cloned_object()
  97. {
  98. var path = new PathData();
  99. var detached = path.Detach();
  100. detached.ShouldNotBeSameAs(path);
  101. }
  102. [Test]
  103. public void Attach_creates_cloned_object()
  104. {
  105. var path = new PathData { Action = "hej", CurrentItem = item, Ignore = true, IsCacheable = true, IsPubliclyAvailable = true, IsRewritable = true, Path = "/x", StopItem = page, TemplateUrl = "/hello.aspx" };
  106. var detached = path.Detach();
  107. var reattached = detached.Attach(persister);
  108. reattached.ShouldNotBeSameAs(detached);
  109. }
  110. [Test]
  111. public void Detached_path_values_doesnt_mutate_original()
  112. {
  113. var path = new PathData { Action = "hej", CurrentItem = item, Ignore = true, IsCacheable = true, IsPubliclyAvailable = true, IsRewritable = true, Path = "/x", StopItem = page, TemplateUrl = "/hello.aspx" };
  114. var detached = path.Detach();
  115. detached.Action = "hejdå";
  116. detached.CurrentItem = page;
  117. detached.Ignore = false;
  118. detached.IsCacheable = false;
  119. detached.IsPubliclyAvailable = false;
  120. detached.IsRewritable = false;
  121. detached.Path = "/y";
  122. detached.StopItem = item;
  123. detached.TemplateUrl = "/world.aspx";
  124. detached.QueryParameters["Hello"] = "world";
  125. path.Action.ShouldNotBe("hejdå");
  126. path.CurrentItem.ShouldNotBe(page);
  127. path.Ignore.ShouldNotBe(false);
  128. path.IsCacheable.ShouldNotBe(false);
  129. path.IsPubliclyAvailable.ShouldNotBe(false);
  130. path.IsRewritable.ShouldNotBe(false);
  131. path.Path.ShouldNotBe("/y");
  132. path.StopItem.ShouldNotBe(item);
  133. path.TemplateUrl.ShouldNotBe("/world.aspx");
  134. path.QueryParameters.ContainsKey("Hello").ShouldBe(false);
  135. }
  136. [Test]
  137. public void Attached_path_values_doesnt_mutate_original()
  138. {
  139. var path = new PathData { Action = "hej", CurrentItem = item, Ignore = true, IsCacheable = true, IsPubliclyAvailable = true, IsRewritable = true, Path = "/x", StopItem = page, TemplateUrl = "/hello.aspx" };
  140. var detached = path.Detach();
  141. var reattached = detached.Attach(persister);
  142. detached.Action = "hejdå";
  143. detached.CurrentItem = page;
  144. detached.Ignore = false;
  145. detached.IsCacheable = false;
  146. detached.IsPubliclyAvailable = false;
  147. detached.IsRewritable = false;
  148. detached.Path = "/y";
  149. detached.StopItem = item;
  150. detached.TemplateUrl = "/world.aspx";
  151. detached.QueryParameters["Hello"] = "world";
  152. reattached.Action.ShouldNotBe("hejdå");
  153. reattached.CurrentItem.ShouldNotBe(page);
  154. reattached.Ignore.ShouldNotBe(false);
  155. reattached.IsCacheable.ShouldNotBe(false);
  156. reattached.IsPubliclyAvailable.ShouldNotBe(false);
  157. reattached.IsRewritable.ShouldNotBe(false);
  158. reattached.Path.ShouldNotBe("/y");
  159. reattached.StopItem.ShouldNotBe(item);
  160. reattached.TemplateUrl.ShouldNotBe("/world.aspx");
  161. reattached.QueryParameters.ContainsKey("Hello").ShouldBe(false);
  162. }
  163. [Test]
  164. public void Attach_uses_persister_to_load_item()
  165. {
  166. var path = new PathData { CurrentItem = page };
  167. path = path.Detach();
  168. path.CurrentItem.ShouldBe(null);
  169. var loadedPath = path.Attach(persister);
  170. loadedPath.CurrentItem.ID.ShouldBe(1);
  171. }
  172. [Test]
  173. public void Attach_uses_persister_to_load_page()
  174. {
  175. var path = new PathData { CurrentPage = page };
  176. path = path.Detach();
  177. path.CurrentPage.ShouldBe(null);
  178. var loadedPath = path.Attach(persister);
  179. loadedPath.CurrentPage.ID.ShouldBe(1);
  180. }
  181. [Test]
  182. public void Attach_uses_persister_to_load_stop()
  183. {
  184. var path = new PathData { StopItem = page };
  185. path = path.Detach();
  186. path.StopItem.ShouldBe(null);
  187. var loadedPath = path.Attach(persister);
  188. loadedPath.StopItem.ID.ShouldBe(1);
  189. }
  190. [Test]
  191. public void PubliclyAvailable_is_determined_by_current()
  192. {
  193. var path = new PathData { CurrentItem = item, CurrentPage = page };
  194. path.IsPubliclyAvailable.ShouldBe(true);
  195. }
  196. [Test]
  197. public void PubliclyAvailable_is_determined_by_current_nonpublic_item()
  198. {
  199. var path = new PathData { CurrentItem = new Items.DataItem { AlteredPermissions = N2.Security.Permission.Read }, CurrentPage = page };
  200. path.IsPubliclyAvailable.ShouldBe(false);
  201. }
  202. [Test]
  203. public void PubliclyAvailable_is_determined_by_current_nonpublic_page()
  204. {
  205. var path = new PathData { CurrentItem = item, CurrentPage = new Items.PageItem { AlteredPermissions = N2.Security.Permission.Read } };
  206. path.IsPubliclyAvailable.ShouldBe(false);
  207. }
  208. [TestCase(ContentState.Deleted, false)]
  209. [TestCase(ContentState.Draft, false)]
  210. [TestCase(ContentState.New, true)]
  211. [TestCase(ContentState.None, true)]
  212. [TestCase(ContentState.Published, true)]
  213. [TestCase(ContentState.Unpublished, false)]
  214. [TestCase(ContentState.Waiting, false)]
  215. public void PubliclyAvailable_is_determined_by_current_nonpublished_item(ContentState state, bool expectedAvailability)
  216. {
  217. var path = new PathData { CurrentItem = new Items.DataItem { State = state }, CurrentPage = page };
  218. path.IsPubliclyAvailable.ShouldBe(expectedAvailability);
  219. }
  220. [TestCase(ContentState.Deleted, false)]
  221. [TestCase(ContentState.Draft, false)]
  222. [TestCase(ContentState.New, true)]
  223. [TestCase(ContentState.None, true)]
  224. [TestCase(ContentState.Published, true)]
  225. [TestCase(ContentState.Unpublished, false)]
  226. [TestCase(ContentState.Waiting, false)]
  227. public void PubliclyAvailable_is_determined_by_current_nonpublished_page(ContentState state, bool expectedAvailability)
  228. {
  229. var path = new PathData { CurrentItem = item, CurrentPage = new Items.PageItem { State = state } };
  230. path.IsPubliclyAvailable.ShouldBe(expectedAvailability);
  231. }
  232. [Test]
  233. public void Cloned_path_data_has_same_values()
  234. {
  235. var path = new PathData(page, item) { Action = "hello", Argument = "world", Ignore = true, IsCacheable = false, IsPubliclyAvailable = true, IsRewritable = false, TemplateUrl = "asdf" };
  236. var clone = path.Clone();
  237. path.Action.ShouldBe(clone.Action);
  238. path.Argument.ShouldBe(clone.Argument);
  239. path.CurrentItem.ShouldBe(clone.CurrentItem);
  240. path.CurrentPage.ShouldBe(clone.CurrentPage);
  241. path.ID.ShouldBe(clone.ID);
  242. path.Ignore.ShouldBe(clone.Ignore);
  243. path.IsCacheable.ShouldBe(clone.IsCacheable);
  244. path.IsPubliclyAvailable.ShouldBe(clone.IsPubliclyAvailable);
  245. path.IsRewritable.ShouldBe(clone.IsRewritable);
  246. path.PageID.ShouldBe(clone.PageID);
  247. path.Path.ShouldBe(clone.Path);
  248. path.QueryParameters.Count.ShouldBe(clone.QueryParameters.Count);
  249. path.StopID.ShouldBe(clone.StopID);
  250. path.StopItem.ShouldBe(clone.StopItem);
  251. path.TemplateUrl.ShouldBe(clone.TemplateUrl);
  252. }
  253. [Test]
  254. public void Cloned_path_data_should_not_be_same_item()
  255. {
  256. var path = new PathData(page, item);
  257. var clone = path.Clone();
  258. clone.ShouldNotBeSameAs(path);
  259. clone.QueryParameters.ShouldNotBeSameAs(path.QueryParameters);
  260. }
  261. [Test]
  262. public void ToString_should_maintina_page_and_item_info()
  263. {
  264. var path = new PathData(page, item);
  265. var reparsed = PathData.Parse(path.ToString(), persister);
  266. reparsed.CurrentItem.ShouldBe(path.CurrentItem);
  267. reparsed.CurrentPage.ShouldBe(path.CurrentPage);
  268. }
  269. }
  270. }