Containers/Import-Testing.ps1

<#
 .Synopsis
  Imports test functions into the container
 .Description
  Imports the standard test functions into the container
 .Parameter ContainerName
  Name of the container. Can be provided in the settings.json
 .Parameter Credential
  Based on the version of BC/NAV, it will contain the credentials or the SQL credentials
 .Parameter ArtifactUrl
  The artifact Url used to create the container to determine the platform, optional
 .Parameter includeTestLibrariesOnly
  Add the switch if you want to only include the test libraries and not the standard tests
 .Example
  Import-Testing
#>

function Import-Testing {
    Param(
        [Parameter(Mandatory=$false)]
        [string] $ContainerName,
        [Parameter(Mandatory=$false)]
        [pscredential] $Credential,
        [Parameter(Mandatory=$false)]
        [switch] $includeLibrariesOnly
    )

    $ContainerName = Get-NewContainerName -ContainerName $ContainerName

    if ($null -eq $Credential) {
        $NewCredential = Get-CredentialFromEnvironmentJson
        if ($NewCredential -eq $false) {
            $Password = (ConvertTo-SecureString "Password" -AsPlainText -Force)
            $Credential = [PSCredential]::new('admin', $Password)
        }
        else {
            $Credential = $NewCredential
            $Password = $NewCredential.Password
        }
    } else {
        $Password = $Credential.Password
    }

    $platform = Get-PlatformFromContainer -ContainerName $ContainerName

    $isUseStandardTest = Get-UseStandardTest -MajorVersion $platform.Major
    if (!$isUseStandardTest -and ($platform.Major -gt 14)) {
        Disable-SymbolLoading -ContainerName $ContainerName
        Start-Sleep -Seconds 5
    }

    $startParameters = @{}
    if ($platform.Major -le 14) {
        $startParameters.Add('sqlCredential', [PSCredential]::new('sa', $Password))
    } else {
        $startParameters.Add('credential', $Credential)
        $startParameters.Add('doNotUseRuntimePackages', $true)
    }
    if (!$isUseStandardTest) {
        $startParameters.Add('doNotUpdateSymbols', $true)
    }

    if ($includeLibrariesOnly.IsPresent) {
        $startParameters.Add('includeTestLibrariesOnly', $true)
    } else {
        $startParameters.Add('includeTestLibrariesOnly', $false)
    }

    $locale = Get-EnvironmentKeyValue -KeyName "locale"
    if ($locale -ne "") {
        if ($locale -eq 'na') {
            $locale = 'US'
        }
        $startParameters.Add('testToolkitCountry', $locale)
    }

    Import-TestToolkitToBcContainer -containerName $ContainerName -ImportAction Overwrite @startParameters

    if (!$isUseStandardTest -and ($platform.Major -ne 14) -and ($platform.Major -ne 11)) {
        Write-Output "Synchronizing Objects"
        Invoke-ScriptInBcContainer -containerName $ContainerName -ScriptBlock {
            Sync-NAVTenant -Mode ForceSync -ServerInstance NAV -CommitPerTable -Force
        }
    }

    if (!$isUseStandardTest -and ($platform.Major -gt 14)) {
        Enable-SymbolLoading -ContainerName $ContainerName
        Write-Output "Waiting for finsql"
        Start-Sleep -Seconds 240
        Compile-ObjectsInNavContainer -containerName $ContainerName -filter "version list=*Test*" -sqlCredential ([PSCredential]::new('sa', $Password)) -SynchronizeSchemaChanges Force
    }
}
Export-ModuleMember Import-Testing