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

(-)a/Koha/Database/DataInconsistency.pm (-7 / +12 lines)
Lines 19-37 sub invalid_item_library { Link Here
19
        if ( not $item->homebranch and not $item->holdingbranch ) {
19
        if ( not $item->homebranch and not $item->holdingbranch ) {
20
            push @errors,
20
            push @errors,
21
                __x(
21
                __x(
22
                "Item with itemnumber={itemnumber} does not have homebranch and holdingbranch defined",
22
                "Item with itemnumber={itemnumber} does not have home and holding library defined",
23
                itemnumber => $item->itemnumber
23
                itemnumber => $item->itemnumber
24
                );
24
                );
25
        } elsif ( not $item->homebranch ) {
25
        } elsif ( not $item->homebranch ) {
26
            push @errors,
26
            push @errors,
27
                __x(
27
                __x(
28
                "Item with itemnumber={itemnumber} does not have homebranch defined",
28
                "Item with itemnumber={itemnumber} does not have a home library defined",
29
                itemnumber => $item->itemnumber
29
                itemnumber => $item->itemnumber
30
                );
30
                );
31
        } else {
31
        } else {
32
            push @errors,
32
            push @errors,
33
                __x(
33
                __x(
34
                "Item with itemnumber={itemnumber} does not have holdingbranch defined",
34
                "Item with itemnumber={itemnumber} does not have a holding library defined",
35
                itemnumber => $item->itemnumber
35
                itemnumber => $item->itemnumber
36
                );
36
                );
37
        }
37
        }
Lines 67-79 sub no_item_type { Link Here
67
            while ( my $item = $items_without_itype->next ) {
67
            while ( my $item = $items_without_itype->next ) {
68
                if ( defined $item->biblioitem->itemtype && $item->biblioitem->itemtype ne '' ) {
68
                if ( defined $item->biblioitem->itemtype && $item->biblioitem->itemtype ne '' ) {
69
                    push @errors, __x(
69
                    push @errors, __x(
70
                        "Item with itemnumber={itemnumber} does not have a itype value, biblio's item type will be used ({itemtype})",
70
                        "Item with itemnumber={itemnumber} does not have an itype value, biblio's item type will be used ({itemtype})",
71
                        itemnumber => $item->itemnumber,
71
                        itemnumber => $item->itemnumber,
72
                        itemtype   => $item->biblioitem->itemtype,
72
                        itemtype   => $item->biblioitem->itemtype,
73
                    );
73
                    );
74
                } else {
74
                } else {
75
                    push @errors, __x(
75
                    push @errors, __x(
76
                        "Item with itemnumber={itemnumber} does not have a itype value, additionally no item type defined for biblionumber={biblionumber}",
76
                        "Item with itemnumber={itemnumber} does not have an itype value, additionally no item type defined for biblionumber={biblionumber}",
77
                        itemnumber   => $item->itemnumber,
77
                        itemnumber   => $item->itemnumber,
78
                        biblionumber => $item->biblioitem->biblionumber,
78
                        biblionumber => $item->biblioitem->biblionumber,
79
                    );
79
                    );
Lines 81-91 sub no_item_type { Link Here
81
            }
81
            }
82
        }
82
        }
83
    } else {
83
    } else {
84
        my $biblioitems_without_itemtype = Koha::Biblioitems->search( { biblionumber => $ids, itemtype => undef } );
84
        my $biblioitems_without_itemtype = Koha::Biblioitems->search(
85
            {
86
                biblionumber => $ids,
87
                -or          => [ itemtype => undef, itemtype => '' ]
88
            }
89
        );
85
        if ( $biblioitems_without_itemtype->count ) {
90
        if ( $biblioitems_without_itemtype->count ) {
86
            while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
91
            while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
87
                push @errors, __x(
92
                push @errors, __x(
88
                    "Biblioitem with biblioitemnumber={biblioitemnumber} does not have a itemtype value",
93
                    "Biblioitem with biblioitemnumber={biblioitemnumber} does not have an itemtype value",
89
                    biblioitemnumber => $biblioitem->biblioitemnumber,
94
                    biblioitemnumber => $biblioitem->biblioitemnumber,
90
                );
95
                );
91
            }
96
            }
(-)a/t/db_dependent/Koha/Database/DataInconsistency.t (-1 / +186 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
3
#use Test::NoWarnings;
4
use Test::More tests => 2;
5
6
use Koha::Items;
7
use Koha::Database::DataInconsistency;
8
9
use t::lib::TestBuilder;
10
use t::lib::Mocks;
11
12
my $builder = t::lib::TestBuilder->new;
13
14
my $schema = Koha::Database->new()->schema();
15
16
subtest 'invalid_item_library' => sub {
17
18
    plan tests => 4;
19
20
    $schema->storage->txn_begin();
21
22
    my $biblio_ok = $builder->build_sample_biblio;
23
    my $item_ok   = $builder->build_sample_item( { biblionumber => $biblio_ok->biblionumber } );
24
    my $biblio_ko = $builder->build_sample_biblio;
25
    my $item_ko   = $builder->build_sample_item( { biblionumber => $biblio_ko->biblionumber } );
26
27
    my $items = Koha::Items->search( { biblionumber => [ $biblio_ok->biblionumber, $biblio_ko->biblionumber ] } );
28
29
    subtest 'ok' => sub {
30
        plan tests => 1;
31
32
        my @errors = Koha::Database::DataInconsistency->invalid_item_library($items);
33
        is_deeply( \@errors, [] );
34
35
    };
36
    subtest 'no homebranch, no holdingbranch' => sub {
37
        plan tests => 1;
38
        $item_ko->set( { homebranch => undef, holdingbranch => undef } )->store;
39
40
        my @errors = Koha::Database::DataInconsistency->invalid_item_library($items);
41
        is_deeply(
42
            \@errors,
43
            [ sprintf "Item with itemnumber=%s does not have home and holding library defined", $item_ko->itemnumber ]
44
        );
45
    };
46
47
    subtest 'no homebranch' => sub {
48
        plan tests => 1;
49
        $item_ko->set( { homebranch => undef, holdingbranch => $item_ok->holdingbranch } )->store;
50
51
        my @errors = Koha::Database::DataInconsistency->invalid_item_library($items);
52
        is_deeply(
53
            \@errors,
54
            [ sprintf "Item with itemnumber=%s does not have a home library defined", $item_ko->itemnumber ]
55
        );
56
    };
57
58
    subtest 'no holdingbranch' => sub {
59
        plan tests => 1;
60
        $item_ko->set( { homebranch => $item_ok->homebranch, holdingbranch => undef } )->store;
61
62
        my @errors = Koha::Database::DataInconsistency->invalid_item_library($items);
63
        is_deeply(
64
            \@errors,
65
            [ sprintf "Item with itemnumber=%s does not have a holding library defined", $item_ko->itemnumber ]
66
        );
67
    };
68
69
    $schema->storage->txn_rollback();
70
};
71
72
subtest 'no_item_type' => sub {
73
74
    plan tests => 2;
75
76
    $schema->storage->txn_begin();
77
78
    my $biblio_ok = $builder->build_sample_biblio;
79
    my $item_ok   = $builder->build_sample_item( { biblionumber => $biblio_ok->biblionumber } );
80
    my $biblio_ko = $builder->build_sample_biblio;
81
    my $item_ko   = $builder->build_sample_item( { biblionumber => $biblio_ko->biblionumber } );
82
83
    my $biblios = Koha::Biblios->search( { biblionumber => [ $biblio_ok->biblionumber, $biblio_ko->biblionumber ] } );
84
85
    subtest 'item-level_itypes = 1' => sub {
86
        plan tests => 3;
87
        t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
88
        subtest 'ok' => sub {
89
            plan tests => 1;
90
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
91
            is_deeply( \@errors, [] );
92
        };
93
        subtest 'itype => undef' => sub {
94
            plan tests => 2;
95
            $item_ko->set( { itype => undef } );
96
            Koha::Object::store($item_ko);    # Do not call Koha::Item->store, it will set itype
97
            $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store;
98
99
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
100
            is_deeply(
101
                \@errors,
102
                [
103
                    sprintf
104
                        "Item with itemnumber=%s does not have an itype value, biblio's item type will be used (%s)",
105
                    $item_ko->itemnumber, $biblio_ko->itemtype
106
                ]
107
            );
108
109
            $biblio_ko->biblioitem->set( { itemtype => undef } )->store;
110
            @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
111
            is_deeply(
112
                \@errors,
113
                [
114
                    sprintf
115
                        "Item with itemnumber=%s does not have an itype value, additionally no item type defined for biblionumber=%s",
116
                    $item_ko->itemnumber, $biblio_ko->biblionumber
117
                ]
118
            );
119
        };
120
        subtest 'itype => ""' => sub {
121
            plan tests => 2;
122
            $item_ko->set( { itype => "" } );
123
            Koha::Object::store($item_ko);    # Do not call Koha::Item->store, it will set itype
124
            $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store;
125
126
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
127
            is_deeply(
128
                \@errors,
129
                [
130
                    sprintf
131
                        "Item with itemnumber=%s does not have an itype value, biblio's item type will be used (%s)",
132
                    $item_ko->itemnumber, $biblio_ko->itemtype
133
                ]
134
            );
135
136
            $biblio_ko->biblioitem->set( { itemtype => undef } )->store;
137
            @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
138
            is_deeply(
139
                \@errors,
140
                [
141
                    sprintf
142
                        "Item with itemnumber=%s does not have an itype value, additionally no item type defined for biblionumber=%s",
143
                    $item_ko->itemnumber, $biblio_ko->biblionumber
144
                ]
145
            );
146
        };
147
    };
148
149
    subtest 'item-level_itypes = 0' => sub {
150
        plan tests => 3;
151
        t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
152
        subtest 'ok' => sub {
153
            plan tests => 1;
154
            $biblio_ko->biblioitem->set( { itemtype => $item_ok->itype } )->store;
155
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
156
            is_deeply( \@errors, [] );
157
        };
158
        subtest 'itemtype => undef' => sub {
159
            plan tests => 1;
160
            $biblio_ko->biblioitem->set( { itemtype => undef } )->store;
161
162
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
163
            is_deeply(
164
                \@errors,
165
                [
166
                    sprintf "Biblioitem with biblioitemnumber=%s does not have an itemtype value",
167
                    $biblio_ko->biblioitem->biblioitemnumber
168
                ]
169
            );
170
        };
171
        subtest 'itemtype => ""' => sub {
172
            plan tests => 1;
173
            $biblio_ko->biblioitem->set( { itemtype => '' } )->store;
174
175
            my @errors = Koha::Database::DataInconsistency->no_item_type($biblios);
176
            is_deeply(
177
                \@errors,
178
                [
179
                    sprintf "Biblioitem with biblioitemnumber=%s does not have an itemtype value",
180
                    $biblio_ko->biblioitem->biblioitemnumber
181
                ]
182
            );
183
        };
184
    };
185
    $schema->storage->txn_rollback();
186
};

Return to bug 40777