Skip to content

Added the last used content-type and entry-uid for the timeline support and added the test cases #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Version: 2.22.1
#### Date: June-13-2025

##### Fix:
- Fixed Timeline issue of Entry not getting updated

### Version: 2.22.0
#### Date: March-03-2025

Expand Down
242 changes: 242 additions & 0 deletions Contentstack.Core.Tests/ContentstackClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Contentstack.Core/ContentstackClientTest.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Contentstack.Core.Configuration;

namespace Contentstack.Core.Tests
{
public class ContentstackClientTest
{
private ContentstackClient CreateClient()
{
var options = new ContentstackOptions()
{
ApiKey = "api_key",
DeliveryToken = "delivery_token",
Environment = "environment",
Version = "1.2.3",
LivePreview = new LivePreviewConfig()
{
Enable = true,
Host = "https://preview.contentstack.com",
ReleaseId = "rid",
PreviewTimestamp = "ts",
PreviewToken = "pt"

}
};
return new ContentstackClient(options);
}

private string GetPrivateField(ContentstackClient client, string fieldName)
{
var field = typeof(ContentstackClient).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
return (string)field.GetValue(client);
}

[Fact]
public void SetEntryUid_SetsValue_WhenNonEmpty()
{
var client = CreateClient();
client.SetEntryUid("entry123");
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}

[Fact]
public void SetEntryUid_DoesNotSet_WhenEmpty()
{
var client = CreateClient();
client.SetEntryUid("entry123");
client.SetEntryUid("");
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}

[Fact]
public void SetEntryUid_DoesNotSet_WhenNull()
{
var client = CreateClient();
client.SetEntryUid("entry123");
client.SetEntryUid(null);
Assert.Equal("entry123", GetPrivateField(client, "currentEntryUid"));
}

[Fact]
public void ContentType_SetscurrentContenttypeUid_WhenNonEmpty()
{
var client = CreateClient();
client.ContentType("blog");
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}

[Fact]
public void ContentType_DoesNotSet_WhenEmpty()
{
var client = CreateClient();
client.ContentType("blog");
client.ContentType("");
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}

[Fact]
public void ContentType_DoesNotSet_WhenNull()
{
var client = CreateClient();
client.ContentType("blog");
client.ContentType(null);
Assert.Equal("blog", GetPrivateField(client, "currentContenttypeUid"));
}

[Fact]
public void ContentType_ReturnsContentTypeInstance()
{
var client = CreateClient();
var contentType = client.ContentType("blog");
Assert.NotNull(contentType);
Assert.Equal("blog", contentType.ContentTypeId);
}

[Fact]
public void GlobalField_ReturnsGlobalFieldInstance()
{
var client = CreateClient();
var globalField = client.GlobalField("author");
Assert.NotNull(globalField);
Assert.Equal("author", globalField.GlobalFieldId);
}

[Fact]
public void Asset_ReturnsAssetInstance()
{
var client = CreateClient();
var asset = client.Asset("asset_uid");
Assert.NotNull(asset);
Assert.Equal("asset_uid", asset.Uid);
}

[Fact]
public void AssetLibrary_ReturnsAssetLibraryInstance()
{
var client = CreateClient();
var assetLibrary = client.AssetLibrary();
Assert.NotNull(assetLibrary);
}

[Fact]
public void Taxonomies_ReturnsTaxonomyInstance()
{
var client = CreateClient();
var taxonomy = client.Taxonomies();
Assert.NotNull(taxonomy);
}

[Fact]
public void GetVersion_ReturnsVersion()
{
var client = CreateClient();
var t = client.GetVersion();
Assert.Equal("1.2.3", client.GetVersion());
}

[Fact]
public void GetApplicationKey_ReturnsApiKey()
{
var client = CreateClient();
Assert.Equal("api_key", client.GetApplicationKey());
}

[Fact]
public void GetAccessToken_ReturnsDeliveryToken()
{
var client = CreateClient();
Assert.Equal("delivery_token", client.GetAccessToken());
}

[Fact]
public void GetLivePreviewConfig_ReturnsConfig()
{
var client = CreateClient();
Assert.NotNull(client.GetLivePreviewConfig());
}

[Fact]
public void RemoveHeader_RemovesHeader()
{
var client = CreateClient();
client.SetHeader("custom", "value");
client.RemoveHeader("custom");
var localHeaders = typeof(ContentstackClient)
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(client) as Dictionary<string, object>;
Assert.False(localHeaders.ContainsKey("custom"));
}

[Fact]
public void SetHeader_AddsHeader()
{
var client = CreateClient();
client.SetHeader("custom", "value");
var localHeaders = typeof(ContentstackClient)
.GetField("_LocalHeaders", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(client) as Dictionary<string, object>;
Assert.True(localHeaders.ContainsKey("custom"));
Assert.Equal("value", localHeaders["custom"]);
}

[Fact]
public async Task LivePreviewQueryAsync_SetsLivePreviewConfigFields()
{
var client = CreateClient();
client.ContentType("ctuid");
client.ContentType("ctuid").Entry("euid");
// Mock GetLivePreviewData to avoid actual HTTP call
var method = typeof(ContentstackClient).GetMethod("GetLivePreviewData", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(client, null); // Just to ensure method exists

var query = new Dictionary<string, string>
{
{ "live_preview", "hash" },
{ "release_id", "rid" },
{ "preview_timestamp", "ts" }
};

// Patch GetLivePreviewData to return a dummy JObject

// Since GetLivePreviewData is private and does a real HTTP call,
// we only test the config fields are set correctly before the call
await client.LivePreviewQueryAsync(query);
var v = client.GetLivePreviewConfig();
Assert.Equal("ctuid", GetPrivateField(client, "currentContenttypeUid"));
Assert.Equal("euid", GetPrivateField(client, "currentEntryUid"));
Assert.Equal(true, v.Enable );
Assert.Equal("rid", v.ReleaseId);
Assert.Equal("ts", v.PreviewTimestamp);
Assert.Equal("pt", v.PreviewToken);
}

// For SyncRecursive, SyncPaginationToken, SyncToken, you should mock HTTP calls.
// Here we just check that the methods exist and can be called (will throw if not configured).
[Fact]
public async Task SyncRecursive_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncRecursive());
}

[Fact]
public async Task SyncPaginationToken_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncPaginationToken("pagetoken"));
}

[Fact]
public async Task SyncToken_ThrowsOrReturns()
{
var client = CreateClient();
await Assert.ThrowsAnyAsync<Exception>(() => client.SyncToken("synctoken"));
}
}
}
2 changes: 1 addition & 1 deletion Contentstack.Core/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal string getBaseUrl (LivePreviewConfig livePreviewConfig, string contentT
{
if (livePreviewConfig != null
&& livePreviewConfig.Enable
&& livePreviewConfig.LivePreview != "init"
&& livePreviewConfig.LivePreview != "init" && !string.IsNullOrEmpty(livePreviewConfig.LivePreview)
&& livePreviewConfig.ContentTypeUID == contentTypeUID)
{
return getLivePreviewUrl(livePreviewConfig);
Expand Down
53 changes: 42 additions & 11 deletions Contentstack.Core/ContentstackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ private string _Url
}
}
private Dictionary<string, object> _StackHeaders = new Dictionary<string, object>();

// This is used to store the last content type UID for live preview
private string currentContenttypeUid = null;
private string currentEntryUid = null;
public List<IContentstackPlugin> Plugins { get; set; } = new List<IContentstackPlugin>();
/// <summary>
/// Initializes a instance of the <see cref="ContentstackClient"/> class.
Expand Down Expand Up @@ -349,14 +353,14 @@ private async Task<JObject> GetLivePreviewData()
foreach (var header in headers)
{
if (this.LivePreviewConfig.Enable == true
&& header.Key == "access_token")
&& header.Key == "access_token" && !string.IsNullOrEmpty(LivePreviewConfig.LivePreview))
{
continue;
}
headerAll.Add(header.Key, (String)header.Value);
}
}
mainJson.Add("live_preview", this.LivePreviewConfig.LivePreview ?? "init");
mainJson.Add("live_preview", string.IsNullOrEmpty(this.LivePreviewConfig.LivePreview) ? "init" : this.LivePreviewConfig.LivePreview);

if (!string.IsNullOrEmpty(this.LivePreviewConfig.ManagementToken))
{
Expand Down Expand Up @@ -384,7 +388,8 @@ private async Task<JObject> GetLivePreviewData()
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
//string branch = this.Config.Branch ? this.Config.Branch : "main";
var outputResult = await RequestHandler.ProcessRequest(String.Format("{0}/content_types/{1}/entries/{2}", this.Config.getLivePreviewUrl(this.LivePreviewConfig), this.LivePreviewConfig.ContentTypeUID, this.LivePreviewConfig.EntryUID), headerAll, mainJson, Branch: this.Config.Branch, isLivePreview: true, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
string URL = String.Format("{0}/content_types/{1}/entries/{2}", this.Config.getBaseUrl(this.LivePreviewConfig, this.LivePreviewConfig.ContentTypeUID), this.LivePreviewConfig.ContentTypeUID, this.LivePreviewConfig.EntryUID);
var outputResult = await RequestHandler.ProcessRequest(URL, headerAll, mainJson, Branch: this.Config.Branch, isLivePreview: true, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
return (JObject)data["entry"];
}
Expand All @@ -394,6 +399,13 @@ private async Task<JObject> GetLivePreviewData()
}
}

public void SetEntryUid(string entryUid)
{
if (!string.IsNullOrEmpty(entryUid))
{
this.currentEntryUid = entryUid;
}
}
/// <summary>
/// Represents a ContentType. Creates ContenntType Instance.
/// </summary>
Expand All @@ -407,6 +419,10 @@ private async Task<JObject> GetLivePreviewData()
/// </example>
public ContentType ContentType(String contentTypeName)
{
if (!string.IsNullOrEmpty(contentTypeName))
{
this.currentContenttypeUid = contentTypeName;
}
ContentType contentType = new ContentType(contentTypeName);
contentType.SetStackInstance(this);

Expand Down Expand Up @@ -488,7 +504,7 @@ public Taxonomy Taxonomies()
/// </example>
public string GetVersion()
{
return Version;
return this.Config.Version;
}

/// <summary>
Expand Down Expand Up @@ -595,27 +611,39 @@ public void SetHeader(string key, string value)
public async Task LivePreviewQueryAsync(Dictionary<string, string> query)
{
this.LivePreviewConfig.LivePreview = null;
this.LivePreviewConfig.ContentTypeUID = null;
this.LivePreviewConfig.EntryUID = null;

this.LivePreviewConfig.PreviewTimestamp = null;
this.LivePreviewConfig.ReleaseId = null;
if (query.Keys.Contains("content_type_uid"))
{
string contentTypeUID = null;
query.TryGetValue("content_type_uid", out contentTypeUID);
this.LivePreviewConfig.ContentTypeUID = contentTypeUID;
}
else if (!string.IsNullOrEmpty(this.currentContenttypeUid))
{
this.LivePreviewConfig.ContentTypeUID = this.currentContenttypeUid;
}


if (query.Keys.Contains("entry_uid"))
{
string contentTypeUID = null;
query.TryGetValue("entry_uid", out contentTypeUID);
this.LivePreviewConfig.EntryUID = contentTypeUID;
string entryUID = null;
query.TryGetValue("entry_uid", out entryUID);
this.LivePreviewConfig.EntryUID = entryUID;
}
else if (!string.IsNullOrEmpty(this.currentEntryUid))
{
this.LivePreviewConfig.EntryUID = this.currentEntryUid;
}


if (query.Keys.Contains("live_preview"))
{
string hash = null;
query.TryGetValue("live_preview", out hash);
this.LivePreviewConfig.LivePreview = hash;
}

if (query.Keys.Contains("release_id"))
{
string ReleaseId = null;
Expand All @@ -628,7 +656,10 @@ public async Task LivePreviewQueryAsync(Dictionary<string, string> query)
query.TryGetValue("preview_timestamp", out PreviewTimestamp);
this.LivePreviewConfig.PreviewTimestamp = PreviewTimestamp;
}
this.LivePreviewConfig.PreviewResponse = await GetLivePreviewData();
//if (!string.IsNullOrEmpty(this.LivePreviewConfig.LivePreview))
//{
// this.LivePreviewConfig.PreviewResponse = await GetLivePreviewData();
//}
}

/// <summary>
Expand Down
Loading
Loading