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