Bug 10132: Unit tests
[koha.git] / t / db_dependent / Koha / Libraries.t
blob3bf66ecc83193d9fa80c4f959c7fa1adacc4806a
1 #!/usr/bin/perl
3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 11;
24 use Koha::Library;
25 use Koha::Libraries;
26 use Koha::LibraryCategory;
27 use Koha::LibraryCategories;
28 use Koha::Database;
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
37 my $nb_of_libraries = Koha::Libraries->search->count;
38 my $nb_of_categories = Koha::LibraryCategories->search->count;
39 my $new_library_1 = Koha::Library->new({
40 branchcode => 'my_bc_1',
41 branchname => 'my_branchname_1',
42 branchnotes => 'my_branchnotes_1',
43 marcorgcode => 'US-MyLib',
44 })->store;
45 my $new_library_2 = Koha::Library->new({
46 branchcode => 'my_bc_2',
47 branchname => 'my_branchname_2',
48 branchnotes => 'my_branchnotes_2',
49 })->store;
50 my $new_category_1 = Koha::LibraryCategory->new({
51 categorycode => 'my_cc_1',
52 categoryname => 'my_categoryname_1',
53 codedescription => 'my_codedescription_1',
54 categorytype => 'properties',
55 } )->store;
56 my $new_category_2 = Koha::LibraryCategory->new( {
57 categorycode => 'my_cc_2',
58 categoryname => 'my_categoryname_2',
59 codedescription => 'my_codedescription_2',
60 categorytype => 'searchdomain',
61 } )->store;
62 my $new_category_3 = Koha::LibraryCategory->new( {
63 categorycode => 'my_cc_3',
64 categoryname => 'my_categoryname_3',
65 codedescription => 'my_codedescription_3',
66 categorytype => 'searchdomain',
67 } )->store;
69 is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
70 is( Koha::LibraryCategories->search->count, $nb_of_categories + 3, 'The 3 library categories should have been added' );
72 $new_library_1->add_to_categories( [$new_category_1] );
73 $new_library_2->add_to_categories( [$new_category_2] );
74 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
75 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
76 is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 1, '1 library should have been linked to the category 1' );
78 $retrieved_library_1->update_categories( [ $new_category_2, $new_category_3 ] );
79 is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 2, '2 libraries should have been linked to the category 2' );
81 my $retrieved_category_2 = Koha::LibraryCategories->find( $new_category_2->categorycode );
82 is( $retrieved_category_2->libraries->count, 2, '2 libraries should have been linked to the category_2' );
83 is( $retrieved_category_2->categorycode, uc('my_cc_2'), 'The Koha::LibraryCategory constructor should have upercased the categorycode' );
85 $retrieved_library_1->delete;
86 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
88 $retrieved_category_2->delete;
89 is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
91 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
92 is( $new_library_1->get_effective_marcorgcode, 'US-MyLib', 'If defined, use library\'s own marc org code');
93 is( $new_library_2->get_effective_marcorgcode, 'US-Default', 'If not defined library\' marc org code, use the one from system preferences');
95 $schema->storage->txn_rollback;