View | Details | Raw Unified | Return to bug 16994
Collapse All | Expand All

(-)a/t/db_dependent/MarcModificationTemplates.t (-3 / +257 lines)
Lines 3-12 Link Here
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::NoWarnings;
5
use Test::NoWarnings;
6
use Test::More tests => 131;
6
use Test::More tests => 134;
7
7
8
use Koha::Database;
8
use Koha::Database;
9
use Koha::SimpleMARC;
9
use Koha::SimpleMARC;
10
use JSON qw( encode_json decode_json );
10
11
11
use t::lib::Mocks;
12
use t::lib::Mocks;
12
13
Lines 14-20 use_ok("MARC::Field"); Link Here
14
use_ok("MARC::Record");
15
use_ok("MARC::Record");
15
use_ok(
16
use_ok(
16
    'C4::MarcModificationTemplates',
17
    'C4::MarcModificationTemplates',
17
    qw( AddModificationTemplate AddModificationTemplateAction GetModificationTemplateAction GetModificationTemplateActions ModModificationTemplateAction MoveModificationTemplateAction DelModificationTemplate DelModificationTemplateAction ModifyRecordWithTemplate GetModificationTemplates )
18
    qw( AddModificationTemplate AddModificationTemplateAction GetModificationTemplateAction GetModificationTemplateActions ModModificationTemplateAction MoveModificationTemplateAction DelModificationTemplate DelModificationTemplateAction ModifyRecordWithTemplate GetModificationTemplates ExportModificationTemplates ImportModificationTemplates )
18
);
19
);
19
20
20
my $schema = Koha::Database->new->schema;
21
my $schema = Koha::Database->new->schema;
Lines 1076-1078 subtest "Bug 32950: Moving subfield preserves values in repeatable fields" => su Link Here
1076
1077
1077
    DelModificationTemplate($template_id);
1078
    DelModificationTemplate($template_id);
1078
};
1079
};
1079
- 
1080
1081
# Test export and import functionality
1082
subtest 'Export and Import MARC modification templates' => sub {
1083
    plan tests => 16;
1084
1085
    $dbh->do(q|DELETE FROM marc_modification_templates|);
1086
1087
    my $template_id = AddModificationTemplate("Test Template for Export/Import");
1088
    ok( $template_id, "Template created successfully" );
1089
1090
    ok(
1091
        AddModificationTemplateAction(
1092
            $template_id,
1093
            'update_field',
1094
            '1',
1095
            '020',
1096
            'a',
1097
            '__BRANCHCODE__-',
1098
            undef,
1099
            undef,
1100
            undef,
1101
            undef,
1102
            undef,
1103
            undef,
1104
            undef,
1105
            undef,
1106
            undef,
1107
            undef,
1108
            undef,
1109
            'Add branch code prefix to ISBN'
1110
        ),
1111
        "First action added"
1112
    );
1113
1114
    ok(
1115
        AddModificationTemplateAction(
1116
            $template_id,
1117
            'add_field',
1118
            '1',
1119
            '952',
1120
            'a',
1121
            'EXPORTED',
1122
            undef,
1123
            undef,
1124
            undef,
1125
            undef,
1126
            undef,
1127
            undef,
1128
            undef,
1129
            undef,
1130
            undef,
1131
            undef,
1132
            undef,
1133
            'Mark as exported'
1134
        ),
1135
        "Second action added"
1136
    );
1137
1138
    my $json = ExportModificationTemplates( [$template_id] );
1139
    ok( defined $json && length($json) > 0, "Template exported successfully" );
1140
1141
    my $decoded = eval { decode_json($json) };
1142
    ok( !$@ && $decoded,                                                 "JSON is valid" );
1143
    ok( exists $decoded->{version},                                      "JSON has version" );
1144
    ok( exists $decoded->{templates} && @{ $decoded->{templates} } == 1, "JSON has one template" );
1145
1146
    my $skip_existing = 0;
1147
    my $result        = ImportModificationTemplates( $json, $skip_existing );
1148
1149
    ok( $result->{success} == 1,               "Template imported successfully with skip_existing=0" );
1150
    ok( $result->{skipped} == 0,               "No templates skipped when skip_existing=0" );
1151
    ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" );
1152
1153
    # Test skip_existing=1: Template should be skipped if it already exists
1154
    $result = ImportModificationTemplates( $json, 1 );
1155
    ok( $result->{success} == 0,               "Template not imported when skip_existing=1 and template exists" );
1156
    ok( $result->{skipped} == 1,               "Template skipped when skip_existing=1" );
1157
    ok( scalar( @{ $result->{errors} } ) == 0, "No import errors with skip_existing=1" );
1158
1159
    # Verify the original template actions are still intact after skip
1160
    my @actions = GetModificationTemplateActions($template_id);
1161
    is( scalar(@actions),      2,              "Template still has 2 actions after skip" );
1162
    is( $actions[0]->{action}, 'update_field', "First action still update_field" );
1163
    is( $actions[1]->{action}, 'add_field',    "Second action still add_field" );
1164
1165
    DelModificationTemplate($template_id);
1166
};
1167
1168
# Test skip_existing parameter with overwrite behavior
1169
subtest 'Import with skip_existing=0 overwrites existing templates' => sub {
1170
    plan tests => 12;
1171
1172
    $dbh->do(q|DELETE FROM marc_modification_templates|);
1173
1174
    # Create a template with specific actions
1175
    my $template_id = AddModificationTemplate("Overwrite Test Template");
1176
    ok( $template_id, "Template created successfully" );
1177
1178
    ok(
1179
        AddModificationTemplateAction(
1180
            $template_id,
1181
            'update_field',
1182
            '1',
1183
            '020',
1184
            'a',
1185
            'ORIGINAL_VALUE',
1186
            undef,
1187
            undef,
1188
            undef,
1189
            undef,
1190
            undef,
1191
            undef,
1192
            undef,
1193
            undef,
1194
            undef,
1195
            undef,
1196
            undef,
1197
            'Original action'
1198
        ),
1199
        "Original action added"
1200
    );
1201
1202
    # Export the template
1203
    my $json = ExportModificationTemplates( [$template_id] );
1204
    ok( defined $json && length($json) > 0, "Template exported successfully" );
1205
1206
    # Modify the template in the database (add a new action)
1207
    ok(
1208
        AddModificationTemplateAction(
1209
            $template_id,
1210
            'add_field',
1211
            '1',
1212
            '999',
1213
            'a',
1214
            'MODIFIED',
1215
            undef,
1216
            undef,
1217
            undef,
1218
            undef,
1219
            undef,
1220
            undef,
1221
            undef,
1222
            undef,
1223
            undef,
1224
            undef,
1225
            undef,
1226
            'Modified action'
1227
        ),
1228
        "Modified action added to existing template"
1229
    );
1230
1231
    # Verify template now has 2 actions
1232
    my @actions = GetModificationTemplateActions($template_id);
1233
    is( scalar(@actions), 2, "Template has 2 actions before import" );
1234
1235
    # Import with skip_existing=0 (should overwrite)
1236
    my $result = ImportModificationTemplates( $json, 0 );
1237
    ok( $result->{success} == 1,               "Template imported successfully with skip_existing=0" );
1238
    ok( $result->{skipped} == 0,               "No templates skipped" );
1239
    ok( $result->{overwrite} == 1,             "One template overwritten" );
1240
    ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" );
1241
1242
    # Verify the template was overwritten (should have only 1 action now)
1243
    @actions = GetModificationTemplateActions($template_id);
1244
    is( scalar(@actions),           1,                "Template has 1 action after overwrite" );
1245
    is( $actions[0]->{action},      'update_field',   "Action is update_field (original)" );
1246
    is( $actions[0]->{field_value}, 'ORIGINAL_VALUE', "Field value is original value" );
1247
1248
    # Clean up
1249
    DelModificationTemplate($template_id);
1250
};
1251
1252
# Test skip_existing parameter with skip behavior
1253
subtest 'Import with skip_existing=1 skips existing templates' => sub {
1254
    plan tests => 11;
1255
1256
    $dbh->do(q|DELETE FROM marc_modification_templates|);
1257
1258
    # Create a template with specific actions
1259
    my $template_id = AddModificationTemplate("Skip Test Template");
1260
    ok( $template_id, "Template created successfully" );
1261
1262
    ok(
1263
        AddModificationTemplateAction(
1264
            $template_id,
1265
            'update_field',
1266
            '1',
1267
            '020',
1268
            'a',
1269
            'ORIGINAL_VALUE',
1270
            undef,
1271
            undef,
1272
            undef,
1273
            undef,
1274
            undef,
1275
            undef,
1276
            undef,
1277
            undef,
1278
            undef,
1279
            undef,
1280
            undef,
1281
            'Original action'
1282
        ),
1283
        "Original action added"
1284
    );
1285
1286
    # Export the template
1287
    my $json = ExportModificationTemplates( [$template_id] );
1288
    ok( defined $json && length($json) > 0, "Template exported successfully" );
1289
1290
    # Modify the template in the database (add a new action)
1291
    ok(
1292
        AddModificationTemplateAction(
1293
            $template_id,
1294
            'add_field',
1295
            '1',
1296
            '999',
1297
            'a',
1298
            'MODIFIED',
1299
            undef,
1300
            undef,
1301
            undef,
1302
            undef,
1303
            undef,
1304
            undef,
1305
            undef,
1306
            undef,
1307
            undef,
1308
            undef,
1309
            undef,
1310
            'Modified action'
1311
        ),
1312
        "Modified action added to existing template"
1313
    );
1314
1315
    # Verify template now has 2 actions
1316
    my @actions = GetModificationTemplateActions($template_id);
1317
    is( scalar(@actions), 2, "Template has 2 actions before import" );
1318
1319
    # Import with skip_existing=1 (should skip)
1320
    my $result = ImportModificationTemplates( $json, 1 );
1321
    ok( $result->{success} == 0,               "Template not imported when skip_existing=1" );
1322
    ok( $result->{skipped} == 1,               "Template skipped when skip_existing=1" );
1323
    ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" );
1324
1325
    # Verify the template was NOT modified (still has 2 actions)
1326
    @actions = GetModificationTemplateActions($template_id);
1327
    is( scalar(@actions),      2,              "Template still has 2 actions after skip" );
1328
    is( $actions[0]->{action}, 'update_field', "First action is still update_field" );
1329
    is( $actions[1]->{action}, 'add_field',    "Second action is still add_field" );
1330
1331
    # Clean up
1332
    DelModificationTemplate($template_id);
1333
};

Return to bug 16994