Bug 35138: (follow-up) Mark DisplayLibraryFacets as zebra only
[koha.git] / t / db_dependent / CourseReserves.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
20 use Test::More tests => 26;
21
22 use Koha::Database;
23 use t::lib::TestBuilder;
24
25 BEGIN {
26     use_ok('C4::Biblio', qw( AddBiblio ));
27     use_ok('C4::CourseReserves', qw( GetCourse ModCourse GetCourses DelCourse GetCourseInstructors ModCourseInstructors GetCourseItem ModCourseItem GetCourseReserve ModCourseReserve GetCourseReserves DelCourseReserve SearchCourses GetItemCourseReservesInfo ));
28     use_ok('C4::Context');
29     use_ok('MARC::Field');
30     use_ok('MARC::Record');
31 }
32
33 my $schema = Koha::Database->schema;
34 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36
37 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
38 my $itemtype = $builder->build( { source => 'Itemtype', value => { notforloan => 0 } } )->{itemtype};
39
40 # Create 10 sample borrowers
41 my @borrowers = ();
42 foreach (1..10) {
43     push @borrowers, $builder->build({ source => 'Borrower' });
44 }
45
46 # Create the a record with an item
47 my $record = MARC::Record->new;
48 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
49 my $itemnumber = Koha::Item->new(
50     {
51         biblionumber  => $biblionumber,
52         homebranch    => $branchcode,
53         holdingbranch => $branchcode,
54         itype         => $itemtype
55     },
56 )->store->itemnumber;
57
58 my $course_id = ModCourse(
59     course_name => "Test Course",
60     staff_note  => "Test staff note",
61     public_note => "Test public note",
62 );
63
64 ok( $course_id, "ModCourse created course successfully" );
65
66 $course_id = ModCourse(
67     course_id  => $course_id,
68     staff_note => "Test staff note 2",
69     enabled   => 'no',
70 );
71
72 my $course = GetCourse($course_id);
73
74 ok( $course->{'course_name'} eq "Test Course",       "GetCourse returned correct course" );
75 ok( $course->{'staff_note'}  eq "Test staff note 2", "ModCourse updated course succesfully" );
76 is( $course->{'enabled'}, 'no', "Test Course is disabled" );
77
78 my $courses = GetCourses();
79 is( ref($courses), 'ARRAY', "GetCourses returns an array" );
80 my @match = map {$_->{course_name} eq 'Test Course'} @$courses;
81 ok( scalar(@match) > 0, "GetCourses returns valid array of course data" );
82
83 ModCourseInstructors( mode => 'add', course_id => $course_id, borrowernumbers => [ $borrowers[0]->{'borrowernumber'} ] );
84 $course = GetCourse($course_id);
85 ok( $course->{'instructors'}->[0]->{'borrowernumber'} == $borrowers[0]->{'borrowernumber'}, "ModCourseInstructors added instructors correctly" );
86
87 my $course_instructors = GetCourseInstructors($course_id);
88 ok( $course_instructors->[0]->{'borrowernumber'} eq $borrowers[0]->{'borrowernumber'}, "GetCourseInstructors returns valid data" );
89
90 my $ci_id = ModCourseItem( 'itemnumber' => $itemnumber );
91 ok( $ci_id, "ModCourseItem returned valid data" );
92
93 my $course_item = GetCourseItem( 'ci_id' => $ci_id );
94 ok( $course_item->{'itemnumber'} eq $itemnumber, "GetCourseItem returns valid data" );
95
96 my $cr_id = ModCourseReserve( 'course_id' => $course_id, 'ci_id' => $ci_id );
97 ok( $cr_id, "ModCourseReserve returns valid data" );
98
99 my $course_reserve = GetCourseReserve( 'course_id' => $course_id, 'ci_id' => $ci_id );
100 ok( $course_reserve->{'cr_id'} eq $cr_id, "GetCourseReserve returns valid data" );
101
102 my $course_reserves = GetCourseReserves( 'course_id' => $course_id );
103 ok( $course_reserves->[0]->{'ci_id'} eq $ci_id, "GetCourseReserves returns valid data." );
104
105 ## Check for regression of Bug 15530
106 $course_id = ModCourse(
107     course_id  => $course_id,
108     enabled   => 'yes',
109 );
110 $course = GetCourse($course_id);
111 is( $course->{'enabled'}, 'yes', "Test Course is enabled" );
112 $course_item = GetCourseItem( 'ci_id' => $ci_id );
113 is( $course_item->{enabled}, 'yes', "Course item is enabled after modding disabled course" );
114 my $disabled_course_id = ModCourse(
115     course_name => "Disabled Course",
116     enabled     => 'no',
117 );
118 my $disabled_course = GetCourse( $disabled_course_id );
119 is( $disabled_course->{'enabled'}, 'no', "Disabled Course is disabled" );
120 my $cr_id2 = ModCourseReserve( 'course_id' => $disabled_course_id, 'ci_id' => $ci_id );
121 $course_item = GetCourseItem( 'ci_id' => $ci_id );
122 is( $course_item->{enabled}, 'yes', "Course item is enabled after modding disabled course" );
123 ## End check for regression of Bug 15530
124
125 my $info = GetItemCourseReservesInfo( itemnumber => $itemnumber );
126 ok( $info->[0]->{'itemnumber'} eq $itemnumber, "GetItemReservesInfo returns valid data." );
127
128 DelCourseReserve( 'cr_id' => $cr_id );
129 $course_reserve = GetCourseReserve( 'cr_id' => $cr_id );
130 ok( !defined( $course_reserve->{'cr_id'} ), "DelCourseReserve functions correctly" );
131
132 DelCourse($course_id);
133 $course = GetCourse($course_id);
134 ok( !defined( $course->{'course_id'} ), "DelCourse deleted course successfully" );
135
136 $courses = SearchCourses(); # FIXME Lack of tests for SearchCourses
137 is( ref($courses), 'ARRAY', 'SearchCourses should not crash and return an arrayref' );
138
139 $schema->storage->txn_rollback;