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

SPSite: Fix: SPSite Constructur returns $null when run as Ressource on SPSE #1443

Merged
merged 6 commits into from
Jan 23, 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
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
"cwd": "${file}",
"createTemporaryIntegratedConsole": true
},
{
"type": "PowerShell",
"request": "launch",
"name": "Run current unit test (SPSE)",
"script": "${file}",
"args": [
"${workspaceRoot}/tests/Unit/Stubs/SharePoint/16.0.14326.20450/SharePointServer.psm1"
],
"cwd": "${file}",
"createTemporaryIntegratedConsole": true
},
{
"type": "PowerShell",
"request": "launch",
Expand Down Expand Up @@ -67,6 +78,17 @@
],
"createTemporaryIntegratedConsole": true
},
{
"type": "PowerShell",
"request": "launch",
"name": "Get current unit test code overage (SPSE)",
"script": "${workspaceRoot}/.vscode/GetTestCoverage.ps1",
"args": [
"${file}",
"${workspaceRoot}/tests/Unit/Stubs/SharePoint/16.0.14326.20450/SharePointServer.psm1"
],
"createTemporaryIntegratedConsole": true
},
{
"name": "Debug current file",
"type": "PowerShell",
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SPShellAdmins
- Fixed that the Member comparison was not case insensitive.
- SPSite
- The Get Method failed to get an existing Site Collection on SharePoint Server
Subscription Edition


- SPSearchCrawlerImpactRule
- Ressource threw an error if the Crawler Impact Rule did not exist when
Expand Down
24 changes: 18 additions & 6 deletions SharePointDsc/DSCResources/MSFT_SPSite/MSFT_SPSite.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,29 @@ function Get-TargetResource
$params = $args[0]
$site = $null

try
$productVersion = Get-SPDscInstalledProductVersion
if ($productVersion.FileMajorPart -eq 16 `
-and $productVersion.FileBuildPart -gt 13000)
{
$centralAdminWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$centralAdminSite = Get-SPSite -Identity $centralAdminWebApp.Url -ErrorAction Stop

$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)
# On SharePoint Subscription Edition the Microsoft.SharePoint.SPSite Constructor never gets a Site Collection when run by the LCM.
# Fixes 1442: https://github.com/dsccommunity/SharePointDsc/issues/1442
$site = Get-SPSite -Identity $params.Url -ErrorAction SilentlyContinue
}
catch [System.Exception]
else
{
try
{
$centralAdminWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$centralAdminSite = Get-SPSite -Identity $centralAdminWebApp.Url -ErrorAction Stop

$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)
}
catch [System.Exception]
{
}
}


if ($null -eq $site)
{
Write-Verbose "Site Collection not found"
Expand Down
52 changes: 38 additions & 14 deletions tests/Unit/SharePointDsc/SharePointDsc.SPSite.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Invoke-TestSetup

$script:testEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceFullName `
-DscResourceName $script:DSCResourceFullName `
-ResourceType 'Mof' `
-TestType 'Unit'
}
Expand Down Expand Up @@ -184,19 +184,42 @@ try
$ArgumentList[1] -eq "CentralAdminSystemAccountUserToken"
}

$global:SPDscGetSPSiteCalled = $false
Mock -CommandName Get-SPSite -MockWith {
if ($global:SPDscGetSPSiteCalled)
{
return ""
# Mock Get-SPSite for SPSSE on Get-TargetResource Call
if ($Global:SPDscHelper.CurrentStubBuildNumber.Build -gt 13000)
{
$global:SPDscGetSPSiteCalledCount = 0
Mock -CommandName Get-SPSite -MockWith {
if ($global:SPDscGetSPSiteCalledCount -lt 4)
{
++$global:SPDscGetSPSiteCalledCount
return $null
}
else
{
return ""
}
} -ParameterFilter {
$Identity -eq "http://site.sharepoint.com"
}
else
{
$global:SPDscGetSPSiteCalled = $true
return $null
}
else
{
$global:SPDscGetSPSiteCalled = $false
Mock -CommandName Get-SPSite -MockWith {
if ($global:SPDscGetSPSiteCalled)
{
return ""
}
else
{
$global:SPDscGetSPSiteCalled = $true
return $null
}
}
}



Mock -CommandName Start-Process -MockWith {
return @{
ExitCode = 0
Expand Down Expand Up @@ -294,7 +317,7 @@ try
Assert-MockCalled Set-SPSite
}

It "Should return true from the test method" {
It "Should return false from the test method" {
Test-TargetResource @testParams | Should -Be $false
}
}
Expand Down Expand Up @@ -349,8 +372,9 @@ try
Context -Name "The site exists, but doesn't have default groups configured" -Fixture {
BeforeAll {
$testParams = @{
Url = "http://site.sharepoint.com"
OwnerAlias = "DEMO\User"
Url = "http://site.sharepoint.com"
OwnerAlias = "DEMO\User"
CreateDefaultGroups = $true
}

Mock -CommandName Get-SPSite -MockWith {
Expand All @@ -375,7 +399,7 @@ try
(Get-TargetResource @testParams).CreateDefaultGroups | Should -Be $false
}

It "Should return true from the test method" {
It "Should return false from the test method" {
Test-TargetResource @testParams | Should -Be $false
}

Expand Down