Skip to content

Commit

Permalink
Add SitePageVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
pschaeflein committed Feb 22, 2023
1 parent f8ae457 commit 3c336bd
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 9 deletions.
34 changes: 34 additions & 0 deletions src/Models/SitePageVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Text.Json.Serialization;
using Microsoft.Graph;

namespace Graph.Community
{
public class SitePageVersion : BaseItem
{
public new int Id { get; set; }

//public string CheckInComment { get; set; }
public DateTime Created { get; set; }
public bool IsCurrentVersion { get; set; }

// Refer to https://github.com/pnp/pnpcore/issues/581
public string Length { get; set; }

[JsonIgnore]
public long Size
{
get
{
if (long.TryParse(Length, out var size))
{
return size;
}
return 0;
}

}

public string VersionLabel { get; set; }
}
}
7 changes: 2 additions & 5 deletions src/Requests/SitePages/ISitePageCollectionRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using Microsoft.Graph;

namespace Graph.Community
{
public interface ISitePageCollectionRequestBuilder
public interface ISitePageCollectionRequestBuilder: IBaseRequestBuilder
{
/// <summary>
/// Builds the request.
Expand All @@ -21,6 +19,5 @@ public interface ISitePageCollectionRequestBuilder
ISitePageCollectionRequest Request(IEnumerable<Option> options);

ISitePageRequestBuilder this[string name] { get; }

}
}
2 changes: 2 additions & 0 deletions src/Requests/SitePages/ISitePageRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public interface ISitePageRequestBuilder : IBaseRequestBuilder
/// <param name="options">The query and header options for the request.</param>
/// <returns>The built request.</returns>
ISitePageRequest Request(IEnumerable<Option> options);

ISitePageVersionCollectionRequestBuilder Versions { get; }
}
}
9 changes: 9 additions & 0 deletions src/Requests/SitePages/ISitePageVersionCollectionPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Graph;

namespace Graph.Community
{
[InterfaceConverter(typeof(InterfaceConverter<SitePageVersionCollectionPage>))]
public interface ISitePageVersionCollectionPage : ICollectionPage<SitePageVersion>
{
}
}
12 changes: 12 additions & 0 deletions src/Requests/SitePages/ISitePageVersionCollectionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace Graph.Community
{
public interface ISitePageVersionCollectionRequest : IBaseRequest
{
Task<ISitePageVersionCollectionPage> GetAsync();
Task<ISitePageVersionCollectionPage> GetAsync(CancellationToken cancellationToken);
}
}
21 changes: 21 additions & 0 deletions src/Requests/SitePages/ISitePageVersionCollectionRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using Microsoft.Graph;

namespace Graph.Community
{
public interface ISitePageVersionCollectionRequestBuilder:IBaseRequestBuilder
{
/// <summary>
/// Builds the request.
/// </summary>
/// <returns>The built request.</returns>
ISitePageVersionCollectionRequest Request();

/// <summary>
/// Builds the request.
/// </summary>
/// <param name="options">The query and header options for the request.</param>
/// <returns>The built request.</returns>
ISitePageVersionCollectionRequest Request(IEnumerable<Option> options);
}
}
4 changes: 1 addition & 3 deletions src/Requests/SitePages/SitePageCollectionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;
Expand Down
1 change: 0 additions & 1 deletion src/Requests/SitePages/SitePageCollectionRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Microsoft.Graph;

namespace Graph.Community
Expand Down
8 changes: 8 additions & 0 deletions src/Requests/SitePages/SitePageRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,13 @@ public ISitePageRequest Request(IEnumerable<Option> options)
{
return new SitePageRequest(this.RequestUrl, this.Client, options);
}

public ISitePageVersionCollectionRequestBuilder Versions
{
get
{
return new Graph.Community.SitePageVersionCollectionRequestBuilder(this.AppendSegmentToRequestUrl("versions"), this.Client);
}
}
}
}
8 changes: 8 additions & 0 deletions src/Requests/SitePages/SitePageVersionCollectionPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.Graph;

namespace Graph.Community
{
public class SitePageVersionCollectionPage: CollectionPage<SitePageVersion>, ISitePageVersionCollectionPage
{
}
}
39 changes: 39 additions & 0 deletions src/Requests/SitePages/SitePageVersionCollectionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;

namespace Graph.Community
{
public class SitePageVersionCollectionRequest : BaseSharePointAPIRequest, ISitePageVersionCollectionRequest
{
public SitePageVersionCollectionRequest(
string requestUrl,
IBaseClient client,
IEnumerable<Option> options)
: base("SitePageVersionCollection", requestUrl, client, options)
{
this.Headers.Add(new HeaderOption(SharePointAPIRequestConstants.Headers.AcceptHeaderName, SharePointAPIRequestConstants.Headers.AcceptHeaderValue));
this.Headers.Add(new HeaderOption(SharePointAPIRequestConstants.Headers.ODataVersionHeaderName, SharePointAPIRequestConstants.Headers.ODataVersionHeaderValue));
}

public async Task<ISitePageVersionCollectionPage> GetAsync()
{
return await this.GetAsync(CancellationToken.None);
}

public async Task<ISitePageVersionCollectionPage> GetAsync(CancellationToken cancellationToken)
{
this.ContentType = "application/json";
var response = await this.SendAsync<SharePointAPICollectionResponse<ISitePageVersionCollectionPage>>(null, cancellationToken).ConfigureAwait(false);

if (response?.Value?.CurrentPage != null)
{
return response.Value;
}

return null;
}

}
}
29 changes: 29 additions & 0 deletions src/Requests/SitePages/SitePageVersionCollectionRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Microsoft.Graph;

namespace Graph.Community
{
public class SitePageVersionCollectionRequestBuilder: BaseRequestBuilder, ISitePageVersionCollectionRequestBuilder
{
private readonly IEnumerable<Option> options;

public SitePageVersionCollectionRequestBuilder(
string requestUrl,
IBaseClient client,
IEnumerable<Option> options = null)
: base(requestUrl, client)
{
this.options = options;
}

public ISitePageVersionCollectionRequest Request()
{
return this.Request(options);
}

public ISitePageVersionCollectionRequest Request(IEnumerable<Option> options)
{
return new SitePageVersionCollectionRequest(this.RequestUrl, this.Client, options);
}
}
}
5 changes: 5 additions & 0 deletions test/Graph.Community.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="Mocks\GetSitePageVersionsResponse.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Mocks\ApplySiteDesignResponse.json" />
<EmbeddedResource Include="Mocks\CreateSiteDesignResponse.json" />
Expand All @@ -24,6 +28,7 @@
<EmbeddedResource Include="Mocks\GetSiteGroupsResponse.json" />
<EmbeddedResource Include="Mocks\GetSitePageResponse.json" />
<EmbeddedResource Include="Mocks\GetSitePagesResponse.json" />
<EmbeddedResource Include="Mocks\GetSitePageVersionsResponse.json" />
<EmbeddedResource Include="Mocks\GetSiteScriptMetadataResponse.json" />
<EmbeddedResource Include="Mocks\GetSiteScriptsResponse.json" />
<EmbeddedResource Include="Mocks\GetSiteUserResponse.json" />
Expand Down
44 changes: 44 additions & 0 deletions test/Mocks/GetSitePageVersionsResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"odata.metadata": "https://mock.sharepoint.com/sites/mockSite/_api/$metadata#SP.ApiData.FileVersions",
"value": [
{
"odata.type": "SP.FileVersion",
"odata.id": "https://mock.sharepoint.com/sites/mockSite/_api/SP.FileVersionc75ac36c-9b5b-4645-afac-ac14d63cb005",
"odata.editLink": "SP.FileVersionc75ac36c-9b5b-4645-afac-ac14d63cb005",
"CheckInComment": "",
"Created": "2023-02-14T10:56:13Z",
"ID": 512,
"IsCurrentVersion": false,
"Length": "26340",
"Size": 26340,
"Url": "_vti_history/512/SitePages/home.aspx.IgnoreExt?IgnoreExt=1",
"VersionLabel": "1.0"
},
{
"odata.type": "SP.FileVersion",
"odata.id": "https://mock.sharepoint.com/sites/mockSite/_api/SP.FileVersion53bee446-0c9a-4cda-ad3e-c81b5a53a980",
"odata.editLink": "SP.FileVersion53bee446-0c9a-4cda-ad3e-c81b5a53a980",
"CheckInComment": "",
"Created": "2023-02-20T21:33:06Z",
"ID": 1024,
"IsCurrentVersion": true,
"Length": "27887",
"Size": 27887,
"Url": "_vti_history/1024/SitePages/home.aspx.IgnoreExt?IgnoreExt=1",
"VersionLabel": "2.0"
},
{
"odata.type": "SP.FileVersion",
"odata.id": "https://mock.sharepoint.com/sites/mockSite/_api/SP.FileVersion744871d5-1b26-41a5-bd18-9923986e9fed",
"odata.editLink": "SP.FileVersion744871d5-1b26-41a5-bd18-9923986e9fed",
"CheckInComment": "",
"Created": "2023-02-22T21:28:29Z",
"ID": 1025,
"IsCurrentVersion": false,
"Length": "27744",
"Size": 27744,
"Url": "_vti_history/1025/SitePages/home.aspx.IgnoreExt?IgnoreExt=1",
"VersionLabel": "2.1"
}
]
}
68 changes: 68 additions & 0 deletions test/SitePageRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,73 @@ public async Task GetByName_ReturnsCorrectResponse()
// Props checked in SitePageFileInfoConverterTests
}
}

[Fact]
public void GetVersions_GeneratesCorrectRequestUriAndHeaders()
{
// ARRANGE
var mockPagename = "champions.aspx";
var expectedUri = new Uri($"{mockWebUrl}/_api/web/getfilebyserverrelativeurl('/sites/mockSite/SitePages/{mockPagename}')/versions");

using HttpResponseMessage response = new HttpResponseMessage();
using GraphServiceTestClient testClient = GraphServiceTestClient.Create(response);

// ACT
var request = testClient.GraphServiceClient
.SharePointAPI(mockWebUrl)
.SitePages[mockPagename]
.Versions
.Request()
.GetHttpRequestMessage();

// ASSERT

Assert.Equal(expectedUri, request.RequestUri);
Assert.Equal(SharePointAPIRequestConstants.Headers.AcceptHeaderValue, request.Headers.Accept.ToString());
Assert.True(request.Headers.Contains(SharePointAPIRequestConstants.Headers.ODataVersionHeaderName), $"Header does not contain {SharePointAPIRequestConstants.Headers.ODataVersionHeaderName} header");
Assert.Equal(SharePointAPIRequestConstants.Headers.ODataVersionHeaderValue, string.Join(',', request.Headers.GetValues(SharePointAPIRequestConstants.Headers.ODataVersionHeaderName)));

}

[Fact]
public async Task GetVersions_ReturnsCorrectResponse()
{
// ARRANGE
var mockPagename = "champions.aspx";

var responseContent = ResourceManager.GetHttpResponseContent("GetSitePageVersionsResponse.json");
var responseMessage = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(responseContent),
};

using (responseMessage)
using (GraphServiceTestClient gsc = GraphServiceTestClient.Create(responseMessage))
{
// ACT
var response = await gsc.GraphServiceClient
.SharePointAPI(mockWebUrl)
.SitePages[mockPagename]
.Versions
.Request()
.GetAsync();


var actual = response.CurrentPage;

//// ASSERT
Assert.IsAssignableFrom<IList<SitePageVersion>>(actual);
Assert.Equal(3, actual.Count);

var testVersion = actual[1];
Assert.Equal(new DateTime(2023, 2, 20, 21, 33, 6), testVersion.Created);
Assert.True(testVersion.IsCurrentVersion);
Assert.Equal(27887, testVersion.Size);
Assert.Equal("2.0", testVersion.VersionLabel);


}
}
}
}

0 comments on commit 3c336bd

Please sign in to comment.