Bug 17781 - Improper branchcode set during renewal
[koha.git] / t / db_dependent / Koha.t
blob3d5e2030d70d6e4dea92f729faceef67d84aa362
1 #!/usr/bin/perl
3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
6 use Modern::Perl;
7 use C4::Context;
8 use Koha::DateUtils qw(dt_from_string);
9 use Koha::AuthorisedValue;
10 use Koha::AuthorisedValueCategories;
12 use Test::More tests => 8;
13 use DateTime::Format::MySQL;
15 BEGIN {
16 use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesByCategory GetItemTypesCategorized));
17 use_ok('C4::Members');
20 my $dbh = C4::Context->dbh;
21 $dbh->{AutoCommit} = 0;
22 $dbh->{RaiseError} = 1;
24 subtest 'Authorized Values Tests' => sub {
25 plan tests => 3;
27 my $data = {
28 category => 'CATEGORY',
29 authorised_value => 'AUTHORISED_VALUE',
30 lib => 'LIB',
31 lib_opac => 'LIBOPAC',
32 imageurl => 'IMAGEURL'
35 my $avc = Koha::AuthorisedValueCategories->find($data->{category});
36 Koha::AuthorisedValueCategory->new({ category_name => $data->{category} })->store unless $avc;
37 # Insert an entry into authorised_value table
38 my $insert_success = Koha::AuthorisedValue->new(
39 { category => $data->{category},
40 authorised_value => $data->{authorised_value},
41 lib => $data->{lib},
42 lib_opac => $data->{lib_opac},
43 imageurl => $data->{imageurl}
45 )->store;
46 ok( $insert_success, "Insert data in database" );
49 # Clean up
50 if($insert_success){
51 my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
52 my $sth = $dbh->prepare($query);
53 $sth->execute($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
56 SKIP: {
57 eval { require Test::Deep; import Test::Deep; };
58 skip "Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@;
59 Koha::AuthorisedValueCategory->new({ category_name => 'BUG10656' })->store;
60 Koha::AuthorisedValue->new(
61 { category => 'BUG10656',
62 authorised_value => 'ZZZ',
63 lib => 'Z_STAFF',
64 lib_opac => 'A_PUBLIC',
65 imageurl => ''
67 )->store;
68 Koha::AuthorisedValue->new(
69 { category => 'BUG10656',
70 authorised_value => 'AAA',
71 lib => 'A_STAFF',
72 lib_opac => 'Z_PUBLIC',
73 imageurl => ''
75 )->store;
77 # the next one sets lib_opac to NULL; in that case, the staff
78 # display value is meant to be used.
79 Koha::AuthorisedValue->new(
80 { category => 'BUG10656',
81 authorised_value => 'DDD',
82 lib => 'D_STAFF',
83 lib_opac => undef,
84 imageurl => ''
86 )->store;
88 my $authvals = GetAuthorisedValues('BUG10656');
89 cmp_deeply(
90 $authvals,
93 id => ignore(),
94 category => 'BUG10656',
95 authorised_value => 'AAA',
96 lib => 'A_STAFF',
97 lib_opac => 'Z_PUBLIC',
98 imageurl => '',
101 id => ignore(),
102 category => 'BUG10656',
103 authorised_value => 'DDD',
104 lib => 'D_STAFF',
105 lib_opac => undef,
106 imageurl => '',
109 id => ignore(),
110 category => 'BUG10656',
111 authorised_value => 'ZZZ',
112 lib => 'Z_STAFF',
113 lib_opac => 'A_PUBLIC',
114 imageurl => '',
117 'list of authorised values in staff mode sorted by staff label (bug 10656)'
119 $authvals = GetAuthorisedValues('BUG10656', 1);
120 cmp_deeply(
121 $authvals,
124 id => ignore(),
125 category => 'BUG10656',
126 authorised_value => 'ZZZ',
127 lib => 'A_PUBLIC',
128 lib_opac => 'A_PUBLIC',
129 imageurl => '',
132 id => ignore(),
133 category => 'BUG10656',
134 authorised_value => 'DDD',
135 lib => 'D_STAFF',
136 lib_opac => undef,
137 imageurl => '',
140 id => ignore(),
141 category => 'BUG10656',
142 authorised_value => 'AAA',
143 lib => 'Z_PUBLIC',
144 lib_opac => 'Z_PUBLIC',
145 imageurl => '',
148 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
154 subtest 'Itemtype info Tests' => sub {
155 like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
156 like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
157 like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
160 ### test for C4::Koha->GetDailyQuote()
161 SKIP:
163 eval { require Test::Deep; import Test::Deep; };
164 skip "Test::Deep required to run the GetDailyQuote tests.", 1 if $@;
166 subtest 'Daily Quotes Test' => sub {
167 plan tests => 4;
169 SKIP: {
171 skip "C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok('C4::Koha','GetDailyQuote');
173 # Fill the quote table with the default needed and a spare
174 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
175 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
176 (25,'Richard Nixon','When the President does it, that means that it is not illegal.','0000-00-00 00:00:00'),
177 (3,'Abraham Lincoln','Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.','0000-00-00 00:00:00');";
178 $dbh->do($sql);
180 my $expected_quote = {
181 id => 3,
182 source => 'Abraham Lincoln',
183 text => 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.',
184 timestamp => re('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'), #'0000-00-00 00:00:00',
187 # test quote retrieval based on id
189 my $quote = GetDailyQuote('id'=>3);
190 cmp_deeply ($quote, $expected_quote, "Got a quote based on id.") or
191 diag('Be sure to run this test on a clean install of sample data.');
193 # test quote retrieval based on today's date
195 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
196 my $sth = C4::Context->dbh->prepare($query);
197 $sth->execute(DateTime::Format::MySQL->format_datetime( dt_from_string() ), $expected_quote->{'id'});
199 DateTime::Format::MySQL->format_datetime( dt_from_string() ) =~ m/(\d{4}-\d{2}-\d{2})/;
200 $expected_quote->{'timestamp'} = re("^$1");
202 # $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() ); # update the timestamp of expected quote data
204 $quote = GetDailyQuote(); # this is the "default" mode of selection
205 cmp_deeply ($quote, $expected_quote, "Got a quote based on today's date.") or
206 diag('Be sure to run this test on a clean install of sample data.');
208 # test random quote retrieval
210 $quote = GetDailyQuote('random'=>1);
211 ok ($quote, "Got a random quote.");
217 subtest 'ISBN tests' => sub {
218 plan tests => 6;
220 my $isbn13 = "9780330356473";
221 my $isbn13D = "978-0-330-35647-3";
222 my $isbn10 = "033035647X";
223 my $isbn10D = "0-330-35647-X";
224 is( xml_escape(undef), '',
225 'xml_escape() returns empty string on undef input' );
226 my $str = q{'"&<>'};
228 xml_escape($str),
229 '&apos;&quot;&amp;&lt;&gt;&apos;',
230 'xml_escape() works as expected'
232 is( $str, q{'"&<>'}, '... and does not change input in place' );
233 is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
234 '0590353403', '_isbn_cleanup removes hyphens' );
235 is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
236 '0590353403', '_isbn_cleanup removes parenthetical' );
237 is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
238 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
242 subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
243 plan tests => 7;
245 my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
246 Koha::AuthorisedValueCategory->new({ category_name => 'ITEMTYPECAT' })->store unless $avc;
247 my $insertGroup = Koha::AuthorisedValue->new(
248 { category => 'ITEMTYPECAT',
249 authorised_value => 'Quertyware',
251 )->store;
253 ok($insertGroup, "Create group Qwertyware");
255 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
256 my $insertSth = C4::Context->dbh->prepare($query);
257 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
258 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
259 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
261 # Azertyware should not exist.
262 my @results = GetItemTypesByCategory('Azertyware');
263 is(scalar @results, 0, 'GetItemTypesByCategory: Invalid category returns nothing');
265 @results = GetItemTypesByCategory('Qwertyware');
266 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
267 is_deeply(\@results,\@expected,'GetItemTypesByCategory: valid category returns itemtypes');
269 # add more data since GetItemTypesCategorized's search is more subtle
270 $insertGroup = Koha::AuthorisedValue->new(
271 { category => 'ITEMTYPECAT',
272 authorised_value => 'Varyheavybook',
274 )->store;
276 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
278 my $hrCat = GetItemTypesCategorized();
279 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
280 ok($hrCat->{Veryheavybook} &&
281 $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
283 $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
284 $hrCat = GetItemTypesCategorized();
285 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
287 my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
288 @results = ();
289 foreach my $key (@only) {
290 push @results, $key if exists $hrCat->{$key};
292 @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
293 is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
296 subtest 'GetItemTypes test' => sub {
297 plan tests => 1;
298 $dbh->do(q|DELETE FROM itemtypes|);
299 $dbh->do(q|INSERT INTO itemtypes(itemtype, description) VALUES ('a', 'aa desc'), ('b', 'zz desc'), ('d', 'dd desc'), ('c', 'yy desc')|);
300 my $itemtypes = C4::Koha::GetItemTypes( style => 'array' );
301 $itemtypes = [ map { $_->{itemtype} } @$itemtypes ];
302 is_deeply( $itemtypes, [ 'a', 'd', 'c', 'b' ], 'GetItemTypes(array) should return itemtypes ordered by description');
305 $dbh->rollback();