Bug 28445: Use the task queue for the batch delete and update items tool
[koha.git] / t / db_dependent / Koha / Item / Attributes.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Test::More tests=> 10;
20 use utf8;
21
22 use Koha::Database;
23 use Koha::Caches;
24
25 use C4::Biblio;
26 use Koha::Item::Attributes;
27 use Koha::MarcSubfieldStructures;
28
29 use t::lib::TestBuilder;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new;
35 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
36 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
37
38 my $cache = Koha::Caches->get_instance;
39 $cache->clear_from_cache("MarcStructure-0-");
40 $cache->clear_from_cache("MarcStructure-1-");
41 $cache->clear_from_cache("default_value_for_mod_marc-");
42 $cache->clear_from_cache("MarcSubfieldStructure-");
43
44 # 952 $x $é $y are not linked with a kohafield
45 # $952$x $é repeatable
46 # $952$y is not repeatable
47 setup_mss();
48
49 $item->more_subfields_xml(undef)->store; # Shouldn't be needed, but we want to make sure
50 my $attributes = $item->additional_attributes;
51 is( ref($attributes), 'Koha::Item::Attributes' );
52 is( $attributes->to_marcxml, undef );
53 is_deeply($attributes->to_hashref, {});
54
55 my $some_marc_xml = q{<?xml version="1.0" encoding="UTF-8"?>
56 <collection
57   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
58   xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
59   xmlns="http://www.loc.gov/MARC21/slim">
60
61 <record>
62   <leader>         a              </leader>
63   <datafield tag="999" ind1=" " ind2=" ">
64     <subfield code="x">value for x 1</subfield>
65     <subfield code="x">value for x 2</subfield>
66     <subfield code="y">value for y</subfield>
67     <subfield code="é">value for é 1</subfield>
68     <subfield code="é">value for é 2</subfield>
69     <subfield code="z">value for z 1|value for z 2</subfield>
70   </datafield>
71 </record>
72
73 </collection>};
74
75 $item->more_subfields_xml($some_marc_xml)->store;
76
77 $attributes = $item->additional_attributes;
78 is( ref($attributes), 'Koha::Item::Attributes' );
79 is( $attributes->{'x'}, "value for x 1|value for x 2");
80 is( $attributes->{'y'}, "value for y");
81 is( $attributes->{'é'}, "value for é 1|value for é 2");
82 is( $attributes->{'z'}, "value for z 1|value for z 2");
83
84 is( $attributes->to_marcxml, $some_marc_xml );
85 is_deeply(
86     $attributes->to_hashref,
87     {
88         'x' => "value for x 1|value for x 2",
89         'y' => "value for y",
90         'é' => "value for é 1|value for é 2",
91         'z' => "value for z 1|value for z 2",
92     }
93 );
94
95 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
96
97 sub setup_mss {
98
99     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
100
101     Koha::MarcSubfieldStructures->search(
102         {
103             frameworkcode => '',
104             tagfield => $itemtag,
105             tagsubfield => 'é',
106         }
107     )->delete;    # In case it exist already
108
109     Koha::MarcSubfieldStructure->new(
110         {
111             frameworkcode => '',
112             tagfield      => $itemtag,
113             tagsubfield   => 'é',
114             kohafield     => undef,
115             repeatable    => 1,
116             tab           => 10,
117         }
118     )->store;
119
120     Koha::MarcSubfieldStructures->search(
121         {
122             frameworkcode => '',
123             tagfield      => $itemtag,
124             tagsubfield   => [ 'x', 'y' ]
125         }
126     )->update( { kohafield => undef } );
127
128     Koha::MarcSubfieldStructures->search(
129         {
130             frameworkcode => '',
131             tagfield => $itemtag,
132             tagsubfield => [ 'x', 'é' ],
133         }
134     )->update( { repeatable => 1 } );
135
136     Koha::MarcSubfieldStructures->search(
137         {
138             frameworkcode => '',
139             tagfield => $itemtag,
140             tagsubfield => ['y'],
141         }
142     )->update( { repeatable => 0 } );
143
144     my $i = 0;
145     for my $sf ( qw( x y é z ) ) {
146         Koha::MarcSubfieldStructures->search(
147             { frameworkcode => '', tagfield => $itemtag, tagsubfield => $sf } )
148           ->update( { display_order => $i++ } );
149     }
150
151 }