|
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 |
}; |