File: DynamicFieldsConfigExport.t

package info (click to toggle)
znuny 6.5.18-1
  • links: PTS
  • area: non-free
  • in suites: forky, sid
  • size: 205,344 kB
  • sloc: perl: 1,038,694; xml: 74,551; javascript: 65,276; sql: 23,574; sh: 417; makefile: 63
file content (266 lines) | stat: -rw-r--r-- 9,183 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# --
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;
use utf8;

use vars (qw($Self));

use Kernel::System::VariableCheck qw(:all);

$Kernel::OM->ObjectParamAdd(
    'Kernel::System::UnitTest::Helper' => {
        RestoreDatabase => 1,
    },
);

my $ZnunyHelperObject    = $Kernel::OM->Get('Kernel::System::ZnunyHelper');
my $ConfigObject         = $Kernel::OM->Get('Kernel::Config');
my $UnitTestHelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $YAMLObject           = $Kernel::OM->Get('Kernel::System::YAML');

# Create dynamic fields to test export
my @DynamicFieldConfigs = (
    {
        Name       => $UnitTestHelperObject->GetRandomID(),
        Label      => 'Dynamic field export test 1',
        ObjectType => 'Ticket',
        FieldType  => 'Text',
        Config     => {
            DefaultValue => "",
        },
    },
    {
        Name       => $UnitTestHelperObject->GetRandomID(),
        Label      => 'Dynamic field export test 2',
        ObjectType => 'Ticket',
        FieldType  => 'Text',
        Config     => {
            DefaultValue => "",
        },
    },
);

my $DynamicFieldsCreated = $ZnunyHelperObject->_DynamicFieldsCreate(@DynamicFieldConfigs);

$Self->True(
    scalar $DynamicFieldsCreated,
    'Dynamic fields must have been created successfully.',
);

my $InternalDynamicFieldName = $ConfigObject->Get('Process::DynamicFieldProcessManagementProcessID');
my @OptionalConfigKeys       = (qw(ChangeTime CreateTime ID InternalField ValidID));

# Test export
my @Tests = (
    {
        Name         => 'Export as Perl structure with internal fields and all config keys',
        ExportParams => {
            Format                => 'perl',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name         => 'Export as Perl structure with internal fields and limited config keys',
        ExportParams => {
            Format                => 'perl',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 0,
        },
    },
    {
        Name         => 'Export as Perl structure without internal fields and all config keys',
        ExportParams => {
            Format                => 'perl',
            IncludeInternalFields => 0,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name         => 'Export as Perl structure without internal fields and limited config keys',
        ExportParams => {
            Format                => 'perl',
            IncludeInternalFields => 0,
            IncludeAllConfigKeys  => 0,
        },
    },
    {
        Name         => 'Export as YAML structure with internal fields and all config keys',
        ExportParams => {
            Format                => 'yml',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name         => 'Export as YAML structure with internal fields and limited config keys',
        ExportParams => {
            Format                => 'yml',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 0,
        },
    },
    {
        Name         => 'Export as YAML structure without internal fields and all config keys',
        ExportParams => {
            Format                => 'yml',
            IncludeInternalFields => 0,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name         => 'Export as YAML structure without internal fields and limited config keys',
        ExportParams => {
            Format                => 'yml',
            IncludeInternalFields => 0,
            IncludeAllConfigKeys  => 0,
        },
    },
    {
        Name         => 'Export as value (array) with internal fields and all config keys',
        ExportParams => {
            Format                => 'var',
            Result                => 'array',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name         => 'Export as value (hash) with internal fields and all config keys',
        ExportParams => {
            Format                => 'var',
            Result                => 'hash',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 1,
        },
    },
    {
        Name =>
            'Export as value (hash) with internal fields and all config keys, but restricted to the one created dynamic field',
        ExportParams => {
            Format                => 'var',
            Result                => 'hash',
            IncludeInternalFields => 1,
            IncludeAllConfigKeys  => 1,
            DynamicFields         => [
                $DynamicFieldConfigs[0]->{Name},
            ],
        },
    },
);

TEST:
for my $Test (@Tests) {
    my $Export = $ZnunyHelperObject->_DynamicFieldsConfigExport( %{ $Test->{ExportParams} } );

    # Turn export into Perl structure.
    if ( $Test->{ExportParams}->{Format} eq 'perl' ) {
        $Export =~ s{\A(\$VAR1)}{\$Export};
        eval $Export;    ## nofilter(TidyAll::Plugin::Znuny::Perl::PerlCritic)
    }
    elsif ( $Test->{ExportParams}->{Format} eq 'yml' ) {
        $Export = $YAMLObject->Load(
            Data => $Export,
        );
    }
    elsif ( $Test->{ExportParams}->{Format} eq 'var' ) {

        # do nothing, export is already a perl value
    }
    else {
        return;
    }

    # turn hash into array
    if ( defined $Test->{ExportParams}->{Result} && $Test->{ExportParams}->{Result} eq 'hash' ) {
        $Export = [ values %{$Export} ];
    }

    # check if export has been restricted to the given dynamic fields
    my %RestrictToDynamicFields;
    if ( IsArrayRefWithData( $Test->{ExportParams}->{DynamicFields} ) ) {
        %RestrictToDynamicFields = map { $_ => 1 } @{ $Test->{ExportParams}->{DynamicFields} };
        for my $ExportedDynamicFieldConfig ( @{$Export} ) {
            $Self->True(
                $RestrictToDynamicFields{ $ExportedDynamicFieldConfig->{Name} },
                "$Test->{Name} - Dynamic field $ExportedDynamicFieldConfig->{Name} must not be contained in export.",
            );
        }
    }

    # Check for created dynamic fields
    EXPECTEDDYNAMICFIELDCONFIG:
    for my $ExpectedDynamicFieldConfig (@DynamicFieldConfigs) {
        next EXPECTEDDYNAMICFIELDCONFIG
            if %RestrictToDynamicFields && !$RestrictToDynamicFields{ $ExpectedDynamicFieldConfig->{Name} };

        my @ExportedDynamicFieldConfigs = grep { $_->{Name} eq $ExpectedDynamicFieldConfig->{Name} } @{$Export};

        $Self->Is(
            scalar @ExportedDynamicFieldConfigs,
            1,
            "$Test->{Name} - Dynamic field must be found in export.",
        ) || next TEST;

        my $ExportedDynamicFieldConfig = shift @ExportedDynamicFieldConfigs;

        # Compare some field values
        for my $Field (qw(Label ObjectType FieldType)) {
            $Self->Is(
                $ExportedDynamicFieldConfig->{$Field},
                $ExpectedDynamicFieldConfig->{$Field},
                "$Test->{Name} - Value of field $Field must match expected one.",
            );
        }

        # Internal fields must be included if parameter IncludeInternalFields has been given.
        # This will be tested with one of the standard OTRS dynamic fields of process management.
        my @ExportedInternalDynamicFieldConfigs = grep { $_->{Name} eq $InternalDynamicFieldName } @{$Export};
        if (
            $Test->{ExportParams}->{IncludeInternalFields}
            && (
                !%RestrictToDynamicFields
                || $RestrictToDynamicFields{$InternalDynamicFieldName}
            )
            )
        {
            $Self->Is(
                scalar @ExportedInternalDynamicFieldConfigs,
                1,
                "$Test->{Name} - Internal dynamic field $InternalDynamicFieldName must be found in export.",
            );
        }
        else {
            $Self->Is(
                scalar @ExportedInternalDynamicFieldConfigs,
                0,
                "$Test->{Name} - Internal dynamic field $InternalDynamicFieldName must not be found in export.",
            );
        }

        # Check that certain config keys are (not) present in the export.
        for my $OptionalConfigKey (@OptionalConfigKeys) {
            if ( $Test->{ExportParams}->{IncludeAllConfigKeys} ) {
                $Self->True(
                    exists $ExportedDynamicFieldConfig->{$OptionalConfigKey},
                    "$Test->{Name} - Config key $OptionalConfigKey must be found in export.",
                );
            }
            else {
                $Self->False(
                    exists $ExportedDynamicFieldConfig->{$OptionalConfigKey},
                    "$Test->{Name} - Config key $OptionalConfigKey must not be found in export.",
                );
            }
        }
    }
}

1;