Skip to content
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

Adds capabilities for Content Type Hub #881

Merged
merged 7 commits into from
Jun 15, 2022
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
359 changes: 359 additions & 0 deletions src/sdk/PnP.Core.Test/SharePoint/ContentTypeHubTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
using PnP.Core.Test.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PnP.Core.Test.SharePoint
{
[TestClass]
public class ContentTypeHubTests
{
private const string docSetId = "0x0120D520002E5AAE78D8DB1947903FC99D0979E4A5";

[ClassInitialize]
public static void TestFixtureSetup(TestContext context)
{
// Configure mocking default for all tests in this class, unless override by a specific test
//TestCommon.Instance.Mocking = false;
}

[TestMethod]
public async Task GetContentTypesFromHubAsyncTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
await context.ContentTypeHub.LoadAsync(y => y.ContentTypes);

Assert.IsNotNull(context.ContentTypeHub.ContentTypes);
}
}

[TestMethod]
public async Task AddContentTypeToHubAsyncTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5966F", "AddContentTypeToHubAsyncTest", "TESTING", "TESTING");

// Test the created object
Assert.IsNotNull(newContentType);
Assert.AreEqual("0x0100302EF0D1F1DB4C4EBF58251BCCF5966F", newContentType.StringId);
Assert.AreEqual("AddContentTypeToHubAsyncTest", newContentType.Name);
Assert.AreEqual("TESTING", newContentType.Description);
Assert.AreEqual("TESTING", newContentType.Group);

await context.ContentTypeHub.LoadAsync(y => y.ContentTypes);

var matchingCt = context.ContentTypeHub.ContentTypes.AsRequested().FirstOrDefault(y => y.Id == newContentType.StringId);

Assert.IsNotNull(matchingCt);

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task PublishContentTypeAsyncTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5959D", "IsContentTypePublished", "TESTING", "TESTING");

var isPublished = await newContentType.IsPublishedAsync();

Assert.IsFalse(isPublished);

await newContentType.PublishAsync();

isPublished = await newContentType.IsPublishedAsync();

Assert.IsTrue(isPublished);

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task UnpublishContentTypeAsyncTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5968D", "TEST ADD Unpublish", "TESTING", "TESTING");

// First we have to publish the content type

await newContentType.PublishAsync();

var isPublished = await newContentType.IsPublishedAsync();

Assert.IsTrue(isPublished);

await newContentType.UnpublishAsync();

isPublished = await newContentType.IsPublishedAsync();

Assert.IsFalse(isPublished);

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task CheckIfContentTypeIsPublishedAsyncTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5968A", "TEST ADD IsPublished", "TESTING", "TESTING");

var isPublished = await newContentType.IsPublishedAsync();

Assert.IsFalse(isPublished);

await newContentType.PublishAsync();

isPublished = await newContentType.IsPublishedAsync();

Assert.IsTrue(isPublished);

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task CreateContentTypeAndAddFieldTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5961A", "CreateContentTypeC", "TESTING", "TESTING");

IField newField = await context.ContentTypeHub.Fields.AddBooleanAsync("TestField", new FieldBooleanOptions {});

await newContentType.AddFieldAsync(newField);
await newContentType.LoadAsync(y => y.Fields);

// Check if field is created on ct
var matchingField = newContentType.Fields.AsRequested().FirstOrDefault(y => y.Id == newField.Id);

Assert.IsNotNull(matchingField);

// Clean up
await newContentType.DeleteAsync();
await newField.DeleteAsync();
}
}

[TestMethod]
public async Task UnpublishContentTypeAsyncExceptionTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.ContentTypeHub.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5968D", "TEST ADD Unpublish", "TESTING", "TESTING");

await Assert.ThrowsExceptionAsync<Exception>(async () =>
{
await newContentType.UnpublishAsync();
});

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task PublishContentTypeAsyncTargetExceptionTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType newContentType = await context.Web.ContentTypes.AddAsync("0x0100302EF0D1F1DB4C4EBF58251BCCF5968D", "TEST ADD Publish", "TESTING", "TESTING");

await Assert.ThrowsExceptionAsync<InvalidOperationException>(async () =>
{
await newContentType.PublishAsync();
});

await newContentType.DeleteAsync();
}
}

#region Document Sets

// Ensure the document set site collection feature is enabled before running test tests live

[TestMethod]
public async Task AddContentTypeAsDocumentSet()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
await context.ContentTypeHub.LoadAsync(y => y.Fields, y => y.ContentTypes);

var categoriesField = context.ContentTypeHub.Fields.AsRequested().FirstOrDefault(y => y.InternalName == "Categories");
var managersField = context.ContentTypeHub.Fields.AsRequested().FirstOrDefault(y => y.InternalName == "ManagersName");
var documentCt = context.ContentTypeHub.ContentTypes.AsRequested().FirstOrDefault(y => y.Name == "Document");
var formCt = context.ContentTypeHub.ContentTypes.AsRequested().FirstOrDefault(y => y.Name == "Form");

var documentSetOptions = new DocumentSetOptions
{
AllowedContentTypes = new List<IContentType>
{
documentCt,
formCt
},
ShouldPrefixNameToFile = true,
PropagateWelcomePageChanges = true,
SharedColumns = new List<IField>
{
managersField,
categoriesField
},
WelcomePageColumns = new List<IField>
{
managersField,
categoriesField
}
};

IDocumentSet newDocumentSet = await context.ContentTypeHub.ContentTypes.AddDocumentSetAsync(docSetId, "Document set name", "TESTING", "TESTING", documentSetOptions);
IContentType newContentType = newDocumentSet.Parent as IContentType;
// Test the created object
Assert.IsNotNull(newContentType);
Assert.IsNotNull(newDocumentSet);

Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.AllowedContentTypes.Count, documentSetOptions.AllowedContentTypes.Count);
Assert.AreEqual(newDocumentSet.ShouldPrefixNameToFile, documentSetOptions.ShouldPrefixNameToFile);

await newContentType.DeleteAsync();
}
}

[TestMethod]
public async Task UpdateContentTypeAsDocumentSet()
{
TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var list = await context.Web.Lists.GetByTitle("Documents").GetAsync();
var rootFolder = await list.RootFolder.GetAsync();

(_, _, string documentUrl) = await TestAssets.CreateTestDocumentAsync(parentFolder: rootFolder);

var categoriesField = await context.Web.Fields.FirstAsync(y => y.InternalName == "Categories").ConfigureAwait(false);
var managersField = await context.Web.Fields.FirstAsync(y => y.InternalName == "ManagersName").ConfigureAwait(false);
var documentCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Document").ConfigureAwait(false);

var file = await context.Web.GetFileByServerRelativeUrlAsync(documentUrl);

var documentSetOptions = new DocumentSetOptions
{
SharedColumns = new List<IField>
{
categoriesField
},
WelcomePageColumns = new List<IField>
{
categoriesField
},
DefaultContents = new List<DocumentSetContentOptions>
{
new DocumentSetContentOptions
{
FileName = "Test.docx",
FolderName = "FolderName",
File = file,
ContentTypeId = documentCt.StringId
}
}
};

IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync(docSetId, "Document Set Name", "TESTING", "TESTING", documentSetOptions);

Assert.IsNotNull(newDocumentSet);
Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.DefaultContents.Count, documentSetOptions.DefaultContents.Count);

var documentSetOptionsUpdate = new DocumentSetOptions
{
SharedColumns = new List<IField>
{
managersField
},
WelcomePageColumns = new List<IField>
{
managersField
},
DefaultContents = new List<DocumentSetContentOptions>
{
new DocumentSetContentOptions
{
FileName = "Test2.docx",
FolderName = "FolderName2",
File = file,
ContentTypeId = documentCt.StringId
}
}
};
newDocumentSet = await newDocumentSet.UpdateAsync(documentSetOptionsUpdate);
IContentType newContentType = newDocumentSet.Parent as IContentType;

Assert.IsNotNull(newDocumentSet);
Assert.AreEqual(newDocumentSet.SharedColumns.Count, documentSetOptionsUpdate.SharedColumns.Count + documentSetOptions.SharedColumns.Count);
Assert.AreEqual(newDocumentSet.WelcomePageColumns.Count, documentSetOptionsUpdate.WelcomePageColumns.Count + documentSetOptions.WelcomePageColumns.Count);
Assert.AreEqual(newDocumentSet.DefaultContents.Count, documentSetOptionsUpdate.DefaultContents.Count + documentSetOptions.DefaultContents.Count);

await newContentType.DeleteAsync();
await file.DeleteAsync();
}
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public async Task AddContentTypeAsDocumentSetExceptionAsyncTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync("0x0101mlalalala", "Document set name");
}
}

[TestMethod]
[ExpectedException(typeof(ClientException))]
public async Task GetContentTypeAsDocumentSetExceptionTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IContentType contentType = (from ct in context.Web.ContentTypes
where ct.StringId == "0x0101"
select ct)
.QueryProperties(ct => ct.StringId, ct => ct.Id)
.FirstOrDefault();
var documentSet = contentType.AsDocumentSet();
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"6f9747a0-6011-4000-4c6b-adcf880df962","SPClientServiceRequestDuration":"10","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u0022ceb5fd80-a434-4a62-a60b-b272f10ba1df\u0022,\u0022Url\u0022:\u0022https://mathijsdev2.sharepoint.com/sites/pnpcoresdktestgroup\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"6f9747a0-1024-4000-7a67-c3e146205724","SPClientServiceRequestDuration":"11","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u00220ad55b5d-6a79-467b-ad21-d4bef7948a79\u0022,\u0022Id\u0022:\u00220f9b8f4f-0e8e-4630-bb0a-501442db9b64\u0022}"}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"6f9747a0-808a-4000-4c6b-a2308844a81e","SPClientServiceRequestDuration":"399","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"[\r{\r\u0022SchemaVersion\u0022:\u002215.0.0.0\u0022,\u0022LibraryVersion\u0022:\u002216.0.22601.12003\u0022,\u0022ErrorInfo\u0022:null,\u0022TraceCorrelationId\u0022:\u00226f9747a0-808a-4000-4c6b-a2308844a81e\u0022\r},5,{\r\u0022IsNull\u0022:false\r},6,{\r\u0022_ObjectIdentity_\u0022:\u00226f9747a0-808a-4000-4c6b-a2308844a81e|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:21e4fbca-acce-40e9-be2c-1359cac6c5d9:web:d89551d4-d05a-4247-a401-b3e509878eca:contenttype:0x0120D520002E5AAE78D8DB1947903FC99D0979E4A5\u0022\r}\r]"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"Cache-Control":"no-store, no-cache","Transfer-Encoding":"chunked","Vary":"Accept-Encoding","Strict-Transport-Security":"max-age=31536000","request-id":"c657870e-3ca6-41ae-9fe6-f17f4e86bcb7","client-request-id":"c657870e-3ca6-41ae-9fe6-f17f4e86bcb7","x-ms-ags-diagnostic":"{\u0022ServerInfo\u0022:{\u0022DataCenter\u0022:\u0022North Europe\u0022,\u0022Slice\u0022:\u0022E\u0022,\u0022Ring\u0022:\u00224\u0022,\u0022ScaleUnit\u0022:\u0022002\u0022,\u0022RoleInstance\u0022:\u0022DU2PEPF00010811\u0022}}","OData-Version":"4.0","Date":"Wed, 15 Jun 2022 12:15:20 GMT"},"Response":"{\u0022@odata.context\u0022:\u0022https://graph.microsoft.com/v1.0/$metadata#sites(id)/$entity\u0022,\u0022id\u0022:\u0022mathijsdev2.sharepoint.com,21e4fbca-acce-40e9-be2c-1359cac6c5d9,d89551d4-d05a-4247-a401-b3e509878eca\u0022}"}
Loading