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