tests/Customization/Commands.Tests.ps1

<#
    Integration Test: modern commands (appaction), defect batch 6
    H14: Add-XrmCommand -ContextEntity / -TooltipTitle (contextentity lookup, contextvalue, buttontooltiptitle).
    H15: Get-XrmCommands -EntityLogicalName.
    Also: component type of appaction read from the organization (-SolutionUniqueName), Set-XrmCommand -Name ignored with a warning, -WhatIf.
#>

. "$PSScriptRoot\..\_TestConfig.ps1";

$suffix = Get-Random -Minimum 10000 -Maximum 99999;
$commandIds = [System.Collections.Generic.List[Guid]]::new();

Write-Section "Setup - publisher and solution";
$publisherRef = $Global:XrmClient | Add-XrmPublisher -UniqueName "pdocmd$suffix" -DisplayName "PDO Cmd $suffix" -Prefix "pdc" -OptionValuePrefix (10000 + ($suffix % 89999)) -Description "Integration test publisher";
$solutionUniqueName = "pdocmdsol$suffix";
$solutionRef = $Global:XrmClient | Add-XrmSolution -UniqueName $solutionUniqueName -DisplayName "PDO Cmd $suffix" -PublisherReference $publisherRef -Version "1.0.0.0" -Description "Integration test solution";
Assert-Test "Publisher and solution created" { $null -ne $publisherRef -and $null -ne $solutionRef };

try {
    $accountMetadata = $Global:XrmClient | Get-XrmEntityMetadata -LogicalName "account" -Filter Entity;
    $componentType = $Global:XrmClient | Get-XrmSolutionComponentType -LogicalName "appaction";

    # ============================================================
    Write-Section "Add-XrmCommand -ContextEntity -TooltipTitle -SolutionUniqueName (H14)";
    # ============================================================
    $uniqueName = "pdc_cmd$suffix";
    $addError = $null;
    $cmdRef = $null;
    try {
        $cmdRef = $Global:XrmClient | Add-XrmCommand -Name "PDO Cmd $suffix" -UniqueName $uniqueName -Type 0 -Context 1 -Location 0 -ContextEntity "account" `
            -ButtonLabelText "Label $suffix" -TooltipTitle "Tooltip $suffix" -SolutionUniqueName $solutionUniqueName -ErrorAction Stop;
    }
    catch { $addError = $_.Exception.Message; }
    if ($cmdRef) { $commandIds.Add($cmdRef.Id); }
    Assert-Test "Command created, without error ($addError)" { $null -eq $addError -and $null -ne $cmdRef -and $cmdRef.LogicalName -eq "appaction" };

    $command = $Global:XrmClient | Get-XrmRecord -LogicalName "appaction" -Id $cmdRef.Id -Columns "contextentity", "contextvalue", "buttontooltiptitle", "buttonlabeltext" -AsEntity;
    Assert-Test "contextentity = entity row of account (MetadataId), contextvalue = account" {
        $command["contextentity"].LogicalName -eq "entity" -and $command["contextentity"].Id -eq $accountMetadata.MetadataId -and $command["contextvalue"] -eq "account";
    };
    Assert-Test "buttontooltiptitle and buttonlabeltext written" { $command["buttontooltiptitle"] -eq "Tooltip $suffix" -and $command["buttonlabeltext"] -eq "Label $suffix" };
    $components = @($Global:XrmClient | Get-XrmSolutionComponents -SolutionUniqueName $solutionUniqueName);
    Assert-Test "Command in the solution with the component type of the organization ($componentType)" {
        @($components | Where-Object { $_.objectid -eq $cmdRef.Id -and $_.componenttype_Value.Value -eq $componentType }).Count -eq 1;
    };

    # ============================================================
    Write-Section "Add-XrmCommand errors and -WhatIf";
    # ============================================================
    $contextError = $null;
    try { $Global:XrmClient | Add-XrmCommand -Name "PDO Cmd2 $suffix" -UniqueName "pdc_cmdb$suffix" -Type 0 -Context 1 -Location 0 -ErrorAction Stop | Out-Null; } catch { $contextError = $_.Exception.Message; }
    Assert-Test "Context 1 without -ContextEntity: actionable error" { $contextError -like "*-ContextEntity*" };
    $tableError = $null;
    try { $Global:XrmClient | Add-XrmCommand -Name "PDO Cmd3 $suffix" -UniqueName "pdc_cmdc$suffix" -Type 0 -Context 1 -Location 0 -ContextEntity "pdo_nosuchtable$suffix" -ErrorAction Stop | Out-Null; } catch { $tableError = $_.Exception.Message; }
    Assert-Test "Unknown table: error" { $tableError -like "*'pdo_nosuchtable$suffix' not found*" };

    $whatIfErrors = $null;
    $whatIfResult = $Global:XrmClient | Add-XrmCommand -Name "PDO Cmd4 $suffix" -UniqueName "pdc_cmdd$suffix" -Type 0 -Context 1 -Location 0 -ContextEntity "account" -SolutionUniqueName $solutionUniqueName -WhatIf -ErrorVariable whatIfErrors;
    $whatIfRow = $Global:XrmClient | Get-XrmRecord -LogicalName "appaction" -AttributeName "uniquename" -Value "pdc_cmdd$suffix" -IfExists;
    Assert-Test "-WhatIf: nothing returned, no error, no command" { $null -eq $whatIfResult -and $whatIfErrors.Count -eq 0 -and $null -eq $whatIfRow };

    # ============================================================
    Write-Section "Get-XrmCommands -EntityLogicalName (H15)";
    # ============================================================
    $getError = $null;
    $accountCommands = @();
    try { $accountCommands = @($Global:XrmClient | Get-XrmCommands -EntityLogicalName "account" -Columns "uniquename", "contextvalue" -ErrorAction Stop); } catch { $getError = $_.Exception.Message; }
    Assert-Test "account: no error, test command found, only account commands ($($accountCommands.Count) rows) ($getError)" {
        $null -eq $getError -and @($accountCommands | Where-Object { $_.uniquename -eq $uniqueName }).Count -eq 1 -and @($accountCommands | Where-Object { $_.contextvalue -ne "account" }).Count -eq 0;
    };
    $contactCommands = @($Global:XrmClient | Get-XrmCommands -EntityLogicalName "contact" -Columns "uniquename");
    Assert-Test "contact: test command not listed" { @($contactCommands | Where-Object { $_.uniquename -eq $uniqueName }).Count -eq 0 };
    Assert-Test "-Unpublished removed (commands have no draft)" { -not (Get-Command Get-XrmCommands).Parameters.ContainsKey("Unpublished") };

    # ============================================================
    Write-Section "Set-XrmCommand";
    # ============================================================
    $warnings = $null;
    $setRef = $Global:XrmClient | Set-XrmCommand -CommandReference $cmdRef -Name "Renamed $suffix" -ButtonLabelText "Label2 $suffix" -TooltipTitle "Tooltip2 $suffix" -Hidden $true -SolutionUniqueName $solutionUniqueName -WarningVariable warnings -WarningAction SilentlyContinue;
    $command = $Global:XrmClient | Get-XrmRecord -LogicalName "appaction" -Id $cmdRef.Id -Columns "name", "buttontooltiptitle", "buttonlabeltext", "hidden" -AsEntity;
    Assert-Test "-Name: one warning, name unchanged" { $warnings.Count -eq 1 -and $command["name"] -eq "PDO Cmd $suffix" };
    Assert-Test "Label, tooltip and hidden updated, reference returned" {
        $command["buttonlabeltext"] -eq "Label2 $suffix" -and $command["buttontooltiptitle"] -eq "Tooltip2 $suffix" -and $command["hidden"] -eq $true -and $setRef.Id -eq $cmdRef.Id;
    };
    $components = @($Global:XrmClient | Get-XrmSolutionComponents -SolutionUniqueName $solutionUniqueName);
    Assert-Test "-SolutionUniqueName again: still one component (idempotent)" { @($components | Where-Object { $_.objectid -eq $cmdRef.Id }).Count -eq 1 };

    # ============================================================
    Write-Section "Remove-XrmCommand";
    # ============================================================
    $Global:XrmClient | Remove-XrmCommand -CommandReference $cmdRef;
    Assert-Test "Command deleted" { $null -eq ($Global:XrmClient | Get-XrmRecord -LogicalName "appaction" -Id $cmdRef.Id -IfExists) };
    [void]$commandIds.Remove($cmdRef.Id);
}
finally {
    Write-Section "Cleanup";
    foreach ($commandId in $commandIds) {
        try { $Global:XrmClient | Remove-XrmRecord -LogicalName "appaction" -Id $commandId; } catch { }
    }
    try { $Global:XrmClient | Remove-XrmRecord -LogicalName "solution" -Id $solutionRef.Id; } catch { }
    try { $Global:XrmClient | Remove-XrmRecord -LogicalName "publisher" -Id $publisherRef.Id; } catch { }
    Assert-Test "Publisher removed" { $null -eq ($Global:XrmClient | Get-XrmRecord -LogicalName "publisher" -Id $publisherRef.Id -IfExists) };
}

Write-TestSummary;