Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[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("MarcSubfieldStructure-");
42
43 # 952 $x $é $y are not linked with a kohafield
44 # $952$x $é repeatable
45 # $952$y is not repeatable
46 setup_mss();
47
48 $item->more_subfields_xml(undef)->store; # Shouldn't be needed, but we want to make sure
49 my $attributes = $item->additional_attributes;
50 is( ref($attributes), 'Koha::Item::Attributes' );
51 is( $attributes->to_marcxml, undef );
52 is_deeply($attributes->to_hashref, {});
53
54 my $some_marc_xml = q{<?xml version="1.0" encoding="UTF-8"?>
55 <collection
56   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
57   xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
58   xmlns="http://www.loc.gov/MARC21/slim">
59
60 <record>
61   <leader>         a              </leader>
62   <datafield tag="999" ind1=" " ind2=" ">
63     <subfield code="x">value for x 1</subfield>
64     <subfield code="x">value for x 2</subfield>
65     <subfield code="y">value for y</subfield>
66     <subfield code="é">value for é 1</subfield>
67     <subfield code="é">value for é 2</subfield>
68     <subfield code="z">value for z 1 | value for z 2</subfield>
69   </datafield>
70 </record>
71
72 </collection>};
73
74 $item->more_subfields_xml($some_marc_xml)->store;
75
76 $attributes = $item->additional_attributes;
77 is( ref($attributes), 'Koha::Item::Attributes' );
78 is( $attributes->{'x'}, "value for x 1 | value for x 2");
79 is( $attributes->{'y'}, "value for y");
80 is( $attributes->{'é'}, "value for é 1 | value for é 2");
81 is( $attributes->{'z'}, "value for z 1 | value for z 2");
82
83 is( $attributes->to_marcxml, $some_marc_xml );
84 is_deeply(
85     $attributes->to_hashref,
86     {
87         'x' => "value for x 1 | value for x 2",
88         'y' => "value for y",
89         'é' => "value for é 1 | value for é 2",
90         'z' => "value for z 1 | value for z 2",
91     }
92 );
93
94 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
95
96 sub setup_mss {
97
98     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
99
100     Koha::MarcSubfieldStructures->search(
101         {
102             frameworkcode => '',
103             tagfield => $itemtag,
104             tagsubfield => 'é',
105         }
106     )->delete;    # In case it exist already
107
108     Koha::MarcSubfieldStructure->new(
109         {
110             frameworkcode => '',
111             tagfield      => $itemtag,
112             tagsubfield   => 'é',
113             kohafield     => undef,
114             repeatable    => 1,
115             tab           => 10,
116         }
117     )->store;
118
119     Koha::MarcSubfieldStructures->search(
120         {
121             frameworkcode => '',
122             tagfield      => $itemtag,
123             tagsubfield   => [ 'x', 'y' ]
124         }
125     )->update( { kohafield => undef } );
126
127     Koha::MarcSubfieldStructures->search(
128         {
129             frameworkcode => '',
130             tagfield => $itemtag,
131             tagsubfield => [ 'x', 'é' ],
132         }
133     )->update( { repeatable => 1 } );
134
135     Koha::MarcSubfieldStructures->search(
136         {
137             frameworkcode => '',
138             tagfield => $itemtag,
139             tagsubfield => ['y'],
140         }
141     )->update( { repeatable => 0 } );
142
143     my $i = 0;
144     for my $sf ( qw( x y é z ) ) {
145         Koha::MarcSubfieldStructures->search(
146             { frameworkcode => '', tagfield => $itemtag, tagsubfield => $sf } )
147           ->update( { display_order => $i++ } );
148     }
149
150 }