Bug 9302: Use patron-title.inc
[koha.git] / t / db_dependent / AudioAlerts.t
blobf7cc825fd1d20ae71979c06c08cb1a9db768cdf3
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 33;
22 BEGIN {
23 use_ok('Koha::AudioAlert');
24 use_ok('Koha::AudioAlerts');
27 my $schema = Koha::Database->new()->schema();
28 $schema->storage->txn_begin();
30 my $dbh = C4::Context->dbh;
31 $dbh->{RaiseError} = 1;
33 map { $_->delete() } Koha::AudioAlerts->search();
35 ## Check the basics
36 # Creating 3 audio alerts named a, b and c
37 my $a = Koha::AudioAlert->new( { selector => 'A', sound => 'test.wav' } )->store();
38 is( $a->precedence, 1, "First alert has a precedence of 1" );
40 my $b = Koha::AudioAlert->new( { selector => 'B', sound => 'sound.mp3' } )->store();
41 is( $b->precedence, 2, "Second alert has a precedence of 2" );
43 my $c = Koha::AudioAlert->new( { selector => 'C', sound => 'test.ogg' } )->store();
44 is( $c->precedence, 3, "Third alert has a precedence of 3" );
46 ## Check precedence getting methods
47 # Testing get_last_precedence and get_next_precedence
49 is( Koha::AudioAlerts->get_last_precedence(), 3, "Last prececence should be 3" );
50 is( Koha::AudioAlerts->get_next_precedence(), 4, "Next prececence should be 4" );
52 ## Check edge cases
53 # Testing edge cases for moving ( up from 1, down from the last precedence )
55 $a->move('up');
56 is( $a->precedence, 1, "First alert still has a precedence of 1" );
58 $c->move('down');
59 is( $c->precedence, 3, "Third alert still has a precedence of 3" );
61 ## Check moving
62 # Moving A down by one
63 $a->move('down');
64 $a = Koha::AudioAlerts->find( $a->id );
65 $b = Koha::AudioAlerts->find( $b->id );
66 $c = Koha::AudioAlerts->find( $c->id );
67 is( $a->precedence, 2, "Alert A has a precedence of 2" );
68 is( $b->precedence, 1, "Alert B has a precedence of 1" );
69 is( $c->precedence, 3, "Alert C has a precedence of 3" );
71 # Moving A up by one, should restore original order
72 $a->move('up');
73 $a = Koha::AudioAlerts->find( $a->id );
74 $b = Koha::AudioAlerts->find( $b->id );
75 $c = Koha::AudioAlerts->find( $c->id );
76 is( $a->precedence, 1, "Alert A has a precedence of 1" );
77 is( $b->precedence, 2, "Alert B has a precedence of 2" );
78 is( $c->precedence, 3, "Alert C has a precedence of 3" );
80 # Moving A to the bottom
81 $a->move('bottom');
82 $a = Koha::AudioAlerts->find( $a->id );
83 $b = Koha::AudioAlerts->find( $b->id );
84 $c = Koha::AudioAlerts->find( $c->id );
85 is( $a->precedence, 3, "Alert A has a precedence of 3" );
86 is( $b->precedence, 1, "Alert B has a precedence of 1" );
87 is( $c->precedence, 2, "Alert C has a precedence of 2" );
89 # Moving A to the top, should restore original order
90 $a->move('top');
91 $a = Koha::AudioAlerts->find( $a->id );
92 $b = Koha::AudioAlerts->find( $b->id );
93 $c = Koha::AudioAlerts->find( $c->id );
94 is( $a->precedence, 1, "Alert A has a precedence of 1" );
95 is( $b->precedence, 2, "Alert B has a precedence of 2" );
96 is( $c->precedence, 3, "Alert C has a precedence of 3" );
98 ## Test searching, should be ordered by precedence by default
99 # Test searching, default search should be ordered by precedence
100 $a->move('bottom');
101 # Changed precedence order from database insert order
102 # Insert order was a, b, c. Precedence order is now b, c, a.
103 ( $b, $c, $a ) = Koha::AudioAlerts->search();
105 is( $b->selector, 'B', 'First sound is indeed B' );
106 is( $b->precedence, 1, "Alert B has a precedence of 1" );
108 is( $c->selector, 'C', "Second sound is indeed C" );
109 is( $c->precedence, 2, "Alert C has a precedence of 2" );
111 is( $a->selector, 'A', 'Third sound is indeed A' );
112 is( $a->precedence, 3, "Alert A has a precedence of 3" );
114 ## Test fix precedences, should remove gaps in precedences
115 # Testing precedence fixing. Should remove gaps from precedence list.
116 $a->precedence( 0 )->store();
117 $b->precedence( 50 )->store();
118 $c->precedence( 100 )->store();
119 is( $a->precedence, 0, "Alert A has a precedence of 0" );
120 is( $b->precedence, 50, "Alert B has a precedence of 50" );
121 is( $c->precedence, 100, "Alert C has a precedence of 100" );
123 # Running fix_precedences()
124 Koha::AudioAlerts->fix_precedences();
125 $a = Koha::AudioAlerts->find( $a->id );
126 $b = Koha::AudioAlerts->find( $b->id );
127 $c = Koha::AudioAlerts->find( $c->id );
128 is( $a->precedence, 1, "Alert A has a precedence of 1" );
129 is( $b->precedence, 2, "Alert B has a precedence of 2" );
130 is( $c->precedence, 3, "Alert C has a precedence of 3" );
133 $schema->storage->txn_rollback();