Skip to content

Commit 6e2db71

Browse files
committed
Added the last used content-type and entry-uid for the timeline support and added the test cases
1 parent 126a01e commit 6e2db71

3 files changed

Lines changed: 277 additions & 4 deletions

File tree

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
// Contentstack.Core/ContentstackClientTest.cs
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using Contentstack.Core.Configuration;
8+
9+
namespace Contentstack.Core.Tests
10+
{
11+
public class ContentstackClientTest
12+
{
13+
private ContentstackClient CreateClient()
14+
{
15+
var options = new ContentstackOptions()
16+
{
17+
ApiKey = "api_key",
18+
DeliveryToken = "delivery_token",
19+
Environment = "environment",
20+
Version = "1.2.3",
21+
LivePreview = new LivePreviewConfig()
22+
{
23+
Enable = true,
24+
Host = "https://preview.contentstack.com",
25+
ReleaseId = "rid",
26+
PreviewTimestamp = "ts",
27+
PreviewToken = "pt"
28+
29+
}
30+
};
31+
return new ContentstackClient(options);
32+
}
33+
34+
private string GetPrivateField(ContentstackClient client, string fieldName)
35+
{
36+
var field = typeof(ContentstackClient).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
37+
return (string)field.GetValue(client);
38+
}
39+
40+
[Fact]
41+
public void SetEntryUid_SetsValue_WhenNonEmpty()
42+
{
43+
var client = CreateClient();
44+
client.SetEntryUid("entry123");
45+
Assert.Equal("entry123", GetPrivateField(client, "lastEntryUid"));
46+
}
47+
48+
[Fact]
49+
public void SetEntryUid_DoesNotSet_WhenEmpty()
50+
{
51+
var client = CreateClient();
52+
client.SetEntryUid("entry123");
53+
client.SetEntryUid("");
54+
Assert.Equal("entry123", GetPrivateField(client, "lastEntryUid"));
55+
}
56+
57+
[Fact]
58+
public void SetEntryUid_DoesNotSet_WhenNull()
59+
{
60+
var client = CreateClient();
61+
client.SetEntryUid("entry123");
62+
client.SetEntryUid(null);
63+
Assert.Equal("entry123", GetPrivateField(client, "lastEntryUid"));
64+
}
65+
66+
[Fact]
67+
public void ContentType_SetsLastContentTypeUid_WhenNonEmpty()
68+
{
69+
var client = CreateClient();
70+
client.ContentType("blog");
71+
Assert.Equal("blog", GetPrivateField(client, "lastContentTypeUid"));
72+
}
73+
74+
[Fact]
75+
public void ContentType_DoesNotSet_WhenEmpty()
76+
{
77+
var client = CreateClient();
78+
client.ContentType("blog");
79+
client.ContentType("");
80+
Assert.Equal("blog", GetPrivateField(client, "lastContentTypeUid"));
81+
}
82+
83+
[Fact]
84+
public void ContentType_DoesNotSet_WhenNull()
85+
{
86+
var client = CreateClient();
87+
client.ContentType("blog");
88+
client.ContentType(null);
89+
Assert.Equal("blog", GetPrivateField(client, "lastContentTypeUid"));
90+
}
91+
92+
[Fact]
93+
public void ContentType_ReturnsContentTypeInstance()
94+
{
95+
var client = CreateClient();
96+
var contentType = client.ContentType("blog");
97+
Assert.NotNull(contentType);
98+
Assert.Equal("blog", contentType.ContentTypeId);
99+
}
100+
101+
[Fact]
102+
public void GlobalField_ReturnsGlobalFieldInstance()
103+
{
104+
var client = CreateClient();
105+
var globalField = client.GlobalField("author");
106+
Assert.NotNull(globalField);
107+
Assert.Equal("author", globalField.GlobalFieldId);
108+
}
109+
110+
[Fact]
111+
public void Asset_ReturnsAssetInstance()
112+
{
113+
var client = CreateClient();
114+
var asset = client.Asset("asset_uid");
115+
Assert.NotNull(asset);
116+
Assert.Equal("asset_uid", asset.Uid);
117+
}
118+
119+
[Fact]
120+
public void AssetLibrary_ReturnsAssetLibraryInstance()
121+
{
122+
var client = CreateClient();
123+
var assetLibrary = client.AssetLibrary();
124+
Assert.NotNull(assetLibrary);
125+
}
126+
127+
[Fact]
128+
public void Taxonomies_ReturnsTaxonomyInstance()
129+
{
130+
var client = CreateClient();
131+
var taxonomy = client.Taxonomies();
132+
Assert.NotNull(taxonomy);
133+
}
134+
135+
[Fact]
136+
public void GetVersion_ReturnsVersion()
137+
{
138+
var client = CreateClient();
139+
var t = client.GetVersion();
140+
Assert.Equal("1.2.3", client.GetVersion());
141+
}
142+
143+
[Fact]
144+
public void GetApplicationKey_ReturnsApiKey()
145+
{
146+
var client = CreateClient();
147+
Assert.Equal("api_key", client.GetApplicationKey());
148+
}
149+
150+
[Fact]
151+
public void GetAccessToken_ReturnsDeliveryToken()
152+
{
153+
var client = CreateClient();
154+
Assert.Equal("delivery_token", client.GetAccessToken());
155+
}
156+
157+
[Fact]
158+
public void GetLivePreviewConfig_ReturnsConfig()
159+
{
160+
var client = CreateClient();
161+
Assert.NotNull(client.GetLivePreviewConfig());
162+
}
163+
164+
[Fact]
165+
public void RemoveHeader_RemovesHeader()
166+
{
167+
var client = CreateClient();
168+
client.SetHeader("custom", "value");
169+
client.RemoveHeader("custom");
170+
var localHeaders = typeof(ContentstackClient)
171+
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
172+
.GetValue(client) as Dictionary<string, object>;
173+
Assert.False(localHeaders.ContainsKey("custom"));
174+
}
175+
176+
[Fact]
177+
public void SetHeader_AddsHeader()
178+
{
179+
var client = CreateClient();
180+
client.SetHeader("custom", "value");
181+
var localHeaders = typeof(ContentstackClient)
182+
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
183+
.GetValue(client) as Dictionary<string, object>;
184+
Assert.True(localHeaders.ContainsKey("custom"));
185+
Assert.Equal("value", localHeaders["custom"]);
186+
}
187+
188+
[Fact]
189+
public async Task LivePreviewQueryAsync_SetsLivePreviewConfigFields()
190+
{
191+
var client = CreateClient();
192+
client.ContentType("ctuid");
193+
client.ContentType("ctuid").Entry("euid");
194+
// Mock GetLivePreviewData to avoid actual HTTP call
195+
var method = typeof(ContentstackClient).GetMethod("GetLivePreviewData", BindingFlags.NonPublic | BindingFlags.Instance);
196+
method.Invoke(client, null); // Just to ensure method exists
197+
198+
var query = new Dictionary<string, string>
199+
{
200+
{ "live_preview", "hash" },
201+
{ "release_id", "rid" },
202+
{ "preview_timestamp", "ts" }
203+
};
204+
205+
// Patch GetLivePreviewData to return a dummy JObject
206+
207+
// Since GetLivePreviewData is private and does a real HTTP call,
208+
// we only test the config fields are set correctly before the call
209+
await Assert.ThrowsAnyAsync<Exception>(() => client.LivePreviewQueryAsync(query));
210+
var v = client.GetLivePreviewConfig();
211+
Assert.Equal("ctuid", GetPrivateField(client, "lastContentTypeUid"));
212+
Assert.Equal("euid", GetPrivateField(client, "lastEntryUid"));
213+
Assert.Equal(true, v.Enable );
214+
Assert.Equal("rid", v.ReleaseId);
215+
Assert.Equal("ts", v.PreviewTimestamp);
216+
Assert.Equal("pt", v.PreviewToken);
217+
}
218+
219+
// For SyncRecursive, SyncPaginationToken, SyncToken, you should mock HTTP calls.
220+
// Here we just check that the methods exist and can be called (will throw if not configured).
221+
[Fact]
222+
public async Task SyncRecursive_ThrowsOrReturns()
223+
{
224+
var client = CreateClient();
225+
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncRecursive());
226+
}
227+
228+
[Fact]
229+
public async Task SyncPaginationToken_ThrowsOrReturns()
230+
{
231+
var client = CreateClient();
232+
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncPaginationToken("pagetoken"));
233+
}
234+
235+
[Fact]
236+
public async Task SyncToken_ThrowsOrReturns()
237+
{
238+
var client = CreateClient();
239+
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncToken("synctoken"));
240+
}
241+
}
242+
}

Contentstack.Core/ContentstackClient.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ private string _Url
5656
}
5757
}
5858
private Dictionary<string, object> _StackHeaders = new Dictionary<string, object>();
59+
60+
// This is used to store the last content type UID for live preview
61+
private string lastContentTypeUid = null;
62+
private string lastEntryUid = null;
5963
public List<IContentstackPlugin> Plugins { get; set; } = new List<IContentstackPlugin>();
6064
/// <summary>
6165
/// Initializes a instance of the <see cref="ContentstackClient"/> class.
@@ -394,6 +398,13 @@ private async Task<JObject> GetLivePreviewData()
394398
}
395399
}
396400

401+
public void SetEntryUid(string entryUid)
402+
{
403+
if (!string.IsNullOrEmpty(entryUid))
404+
{
405+
lastEntryUid = entryUid;
406+
}
407+
}
397408
/// <summary>
398409
/// Represents a ContentType. Creates ContenntType Instance.
399410
/// </summary>
@@ -407,6 +418,10 @@ private async Task<JObject> GetLivePreviewData()
407418
/// </example>
408419
public ContentType ContentType(String contentTypeName)
409420
{
421+
if (!string.IsNullOrEmpty(contentTypeName))
422+
{
423+
lastContentTypeUid = contentTypeName;
424+
}
410425
ContentType contentType = new ContentType(contentTypeName);
411426
contentType.SetStackInstance(this);
412427

@@ -488,7 +503,7 @@ public Taxonomy Taxonomies()
488503
/// </example>
489504
public string GetVersion()
490505
{
491-
return Version;
506+
return this.Config.Version;
492507
}
493508

494509
/// <summary>
@@ -604,12 +619,24 @@ public async Task LivePreviewQueryAsync(Dictionary<string, string> query)
604619
query.TryGetValue("content_type_uid", out contentTypeUID);
605620
this.LivePreviewConfig.ContentTypeUID = contentTypeUID;
606621
}
622+
else if (lastContentTypeUid != null)
623+
{
624+
this.LivePreviewConfig.ContentTypeUID = lastContentTypeUid;
625+
}
626+
627+
607628
if (query.Keys.Contains("entry_uid"))
608629
{
609-
string contentTypeUID = null;
610-
query.TryGetValue("entry_uid", out contentTypeUID);
611-
this.LivePreviewConfig.EntryUID = contentTypeUID;
630+
string entryUID = null;
631+
query.TryGetValue("entry_uid", out entryUID);
632+
this.LivePreviewConfig.EntryUID = entryUID;
612633
}
634+
else if (lastEntryUid != null)
635+
{
636+
this.LivePreviewConfig.EntryUID = lastEntryUid;
637+
}
638+
639+
613640
if (query.Keys.Contains("live_preview"))
614641
{
615642
string hash = null;

Contentstack.Core/Models/ContentType.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ public void RemoveHeader(string key)
236236
/// </example>
237237
public Entry Entry(String entryUid)
238238
{
239+
if(!String.IsNullOrEmpty(entryUid))
240+
{
241+
this.StackInstance.SetEntryUid(entryUid);
242+
}
239243
Entry entry = new Entry(ContentTypeId);
240244
entry._FormHeaders = GetHeader(_Headers);
241245
entry.SetContentTypeInstance(this);

0 commit comments

Comments
 (0)