Tests/Translate-App.Tests.ps1

Describe 'Translate-App' {
    InModuleScope Tecman.Tfs.Tools {    
        function New-SampleApp {
            Param(
                # path to create the sample app in
                [Parameter(Mandatory=$true)]
                [string]
                $Path
            )

            if (!(Test-Path (Join-Path $Path 'Translations'))) {
                New-Item (Join-Path $Path 'Translations') -Type Directory
            }
            
            Set-Content -Path (Join-Path $Path (Join-Path 'Translations' 'Sample.g.xlf')) -Value `
                '<?xml version="1.0" encoding="utf-8"?>
                <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
                    <file datatype="xml" source-language="en-US" target-language="en-US" original="Sample App">
                        <body>
                            <group id="body">
                                <trans-unit id="1">
                                    <source>Document No.</source>
                                </trans-unit>
                                <trans-unit id="2">
                                    <source>Customer</source>
                                </trans-unit>
                                <trans-unit id="3">
                                    <source>Vendor</source>
                                </trans-unit>
                                <trans-unit id="4">
                                    <source>Item</source>
                                </trans-unit>
                            </group>
                        </body>
                    </file>
                </xliff>'

            Set-Content -Path (Join-Path $Path 'environment.json') -Value `
                '{
                    "translationSource": "\\Translations\\Sample.g.xlf",
                    "translations": [
                        {"language": "fr", "country": "FR"},
                        {"language": "nl", "country": "NL"},
                        {"language": "de", "country": "DE"}
                    ]}'

        }

        function New-TranslationDictionary {
            Param(
                # path to create the sample dictionary file in
                [Parameter(Mandatory=$true)]
                [string]
                $Path
            )

            Set-Content -Path $Path -Value `
                '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                <dictionary>
                  <phrase>
                    <code>R2-N2-C1900000002-P8629-L999</code>
                    <A1031>Belegnr.</A1031>
                    <A1033>Document No.</A1033>
                    <A1036>No Document</A1036>
                    <A1043>Documentnr.</A1043>
                  </phrase>
                  <phrase>
                    <code>R2-N2-C1900000002-P8629-L999</code>
                    <A1031>Debitor</A1031>
                    <A1033>Customer</A1033>
                    <A1036>Client</A1036>
                    <A1043>Klant</A1043>
                  </phrase>
                  <phrase>
                    <code>R2-N2-C1900000002-P8629-L999</code>
                    <A1031>Kreditor</A1031>
                    <A1033>Vendor</A1033>
                    <A1036>Fournisseur</A1036>
                    <A1043>Leverancier</A1043>
                  </phrase>
                  <phrase>
                    <code>R2-N2-C1900000002-P8629-L999</code>
                    <A1031>Item</A1031>
                    <A1033>Item</A1033>
                    <A1036>Item</A1036>
                    <A1043>Item</A1043>
                  </phrase>
                </dictionary>'

        }

        Context 'There is no translationKey set in the config file' {
            Mock Get-TFSConfigKeyValue {return ''} -ParameterFilter {$KeyName -eq 'translationDictionaryPath'}
            Mock Get-TFSConfigKeyValue {return ''} -ParameterFilter {$KeyName -eq 'translationKey'}
            It 'shouldn''t call the Azure service' {
                New-SampleApp -Path $TestDrive
                Mock Invoke-WebRequest {}
                Translate-App -SourceDir $TestDrive
                Assert-MockCalled Invoke-WebRequest -Times 0
            }
        }

        Context 'There is a translationKey set in the config file' {
            Mock Get-TFSConfigKeyValue {return ''} -ParameterFilter {$KeyName -eq 'translationDictionaryPath'}
            Mock Get-TFSConfigKeyValue {return 'this is not blank'} -ParameterFilter {$KeyName -eq 'translationKey'}
            It 'should call the Azure service if there is a key in the config file' {
                New-SampleApp -Path $TestDrive
                Mock Invoke-WebRequest {}
                Translate-App -SourceDir $TestDrive
                Assert-MockCalled Invoke-WebRequest
            }
        }

        Context 'there is dictionary file and a path set in the config file' {
            Mock Get-TFSConfigKeyValue {return ''} -ParameterFilter {$KeyName -eq 'translationKey'}
            Mock Get-TFSConfigKeyValue {return (Join-Path $TestDrive 'dictionary.xml')} -ParameterFilter {$KeyName -eq 'translationDictionaryPath'}
            It 'should use the dictionary if the path to one is specified' {
                New-SampleApp -Path $TestDrive
                New-TranslationDictionary -Path (Join-Path $TestDrive 'dictionary.xml')
                Translate-App -SourceDir $TestDrive
                Test-TranslationIsComplete -SourceDir $TestDrive | should be $true                
            }
        }
    }
}