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

(-)a/Koha/Database/DataInconsistency.pm (-3 / +8 lines)
Lines 122-134 sub invalid_item_type { Link Here
122
            }
122
            }
123
        }
123
        }
124
    } else {
124
    } else {
125
        my $biblioitems_with_invalid_itemtype =
125
        my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
126
            Koha::Biblioitems->search( { biblionumber => $ids, itemtype => { not_in => \@itemtypes } } );
126
            {
127
                biblionumber => $ids,
128
                -and         => [ itemtype => { not_in => \@itemtypes }, itemtype => { '!=' => '' } ]
129
            }
130
        );
127
        if ( $biblioitems_with_invalid_itemtype->count ) {
131
        if ( $biblioitems_with_invalid_itemtype->count ) {
128
            while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
132
            while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
129
                push @errors, __x(
133
                push @errors, __x(
130
                    "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a valid itemtype value",
134
                    "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a valid itemtype value ({itemtype})",
131
                    biblioitemnumber => $biblioitem->biblioitemnumber,
135
                    biblioitemnumber => $biblioitem->biblioitemnumber,
136
                    itemtype         => $biblioitem->itemtype,
132
                );
137
                );
133
            }
138
            }
134
        }
139
        }
(-)a/t/db_dependent/Koha/Database/DataInconsistency.t (-2 / +81 lines)
Lines 1-7 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
2
3
#use Test::NoWarnings;
3
#use Test::NoWarnings;
4
use Test::More tests => 2;
4
use Test::More tests => 3;
5
5
6
use Koha::Items;
6
use Koha::Items;
7
use Koha::Database::DataInconsistency;
7
use Koha::Database::DataInconsistency;
Lines 184-186 subtest 'no_item_type' => sub { Link Here
184
    };
184
    };
185
    $schema->storage->txn_rollback();
185
    $schema->storage->txn_rollback();
186
};
186
};
187
- 
187
188
subtest 'invalid_item_type' => sub {
189
190
    plan tests => 2;
191
192
    $schema->storage->txn_begin();
193
194
    my $biblio_ok = $builder->build_sample_biblio;
195
    my $item_ok   = $builder->build_sample_item( { biblionumber => $biblio_ok->biblionumber } );
196
    my $biblio_ko = $builder->build_sample_biblio;
197
    my $item_ko   = $builder->build_sample_item( { biblionumber => $biblio_ko->biblionumber } );
198
199
    my $biblios = Koha::Biblios->search( { biblionumber => [ $biblio_ok->biblionumber, $biblio_ko->biblionumber ] } );
200
201
    my $itemtype         = $builder->build_object( { class => 'Koha::ItemTypes' } );
202
    my $deleted_itemtype = $itemtype->itemtype;
203
    $itemtype->delete;
204
205
    subtest 'item-level_itypes = 1' => sub {
206
        plan tests => 2;
207
        t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
208
        subtest 'ok' => sub {
209
            plan tests => 1;
210
            my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
211
            is_deeply( \@errors, [] );
212
        };
213
        subtest 'itype is invalid' => sub {
214
            plan tests => 2;
215
            $item_ko->set( { itype => $deleted_itemtype } );
216
            Koha::Object::store($item_ko);    # Do not call Koha::Item->store, it will set itype
217
218
            my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
219
            is_deeply(
220
                \@errors,
221
                [
222
                    sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
223
                    $item_ko->itemnumber, $item_ko->biblionumber, $item_ko->itype
224
                ]
225
            );
226
227
            $item_ko->set( { itype => '' } );
228
            Koha::Object::store($item_ko);    # Do not call Koha::Item->store, it will set itype
229
            @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
230
231
            # Does not alert here, it's caught already in no_item_type
232
            is_deeply( \@errors, [] );
233
        };
234
    };
235
236
    subtest 'item-level_itypes = 0' => sub {
237
        plan tests => 2;
238
        t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
239
        subtest 'ok' => sub {
240
            plan tests => 1;
241
            my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
242
            is_deeply( \@errors, [] );
243
        };
244
        subtest 'itemtype is invalid' => sub {
245
            plan tests => 2;
246
            $biblio_ko->biblioitem->set( { itemtype => $deleted_itemtype } )->store;
247
248
            my @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
249
            is_deeply(
250
                \@errors,
251
                [
252
                    sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value (%s)",
253
                    $biblio_ko->biblioitem->biblioitemnumber, $biblio_ko->biblioitem->itemtype
254
                ]
255
            );
256
257
            $biblio_ko->biblioitem->set( { itemtype => ' ' } )->store;
258
            @errors = Koha::Database::DataInconsistency->invalid_item_type($biblios);
259
260
            # Does not alert here, it's caught already in no_item_type
261
            is_deeply( \@errors, [] );
262
        };
263
    };
264
265
    $schema->storage->txn_rollback();
266
};

Return to bug 40777