Bug 14544: Get rid of AddShelf
[koha.git] / t / db_dependent / Virtualshelves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 1;
5
6 use C4::Context;
7 use Koha::DateUtils;
8 use Koha::Virtualshelf;
9 use Koha::Virtualshelves;
10
11 use t::lib::TestBuilder;
12
13 my $dbh = C4::Context->dbh;
14 $dbh->{AutoCommit} = 0;
15
16 $dbh->do(q|DELETE FROM virtualshelves|);
17
18 my $builder = t::lib::TestBuilder->new;
19
20 subtest 'CRUD' => sub {
21     plan tests => 11;
22     my $patron = $builder->build({
23         source => 'Borrower',
24     });
25
26     my $number_of_shelves = Koha::Virtualshelves->search->count;
27
28     is( $number_of_shelves, 0, 'No shelves should exist' );
29
30     my $shelf = Koha::Virtualshelf->new({
31             shelfname => "my first shelf",
32             owner => $patron->{borrowernumber},
33             category => 1,
34         }
35     )->store;
36
37     is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
38
39     $number_of_shelves = Koha::Virtualshelves->search->count;
40     is( $number_of_shelves, 1, '1 shelf should have been inserted' );
41     is( $shelf->allow_add, 0, 'The default value for allow_add should be 1' );
42     is( $shelf->allow_delete_own, 1, 'The default value for allow_delete_own should be 0' );
43     is( $shelf->allow_delete_other, 0, 'The default value for allow_delete_other should be 0' );
44     is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
45
46     my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
47
48     is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
49
50     # Insert with the same name
51     eval {
52         $shelf = Koha::Virtualshelf->new({
53                 shelfname => "my first shelf",
54                 owner => $patron->{borrowernumber},
55                 category => 1,
56             }
57         )->store;
58     };
59     is( ref($@), 'Koha::Exception::DuplicateObject' );
60     $number_of_shelves = Koha::Virtualshelves->search->count;
61     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
62
63     my $another_patron = $builder->build({
64         source => 'Borrower',
65     });
66
67     $shelf = Koha::Virtualshelf->new({
68             shelfname => "my first shelf",
69             owner => $another_patron->{borrowernumber},
70             category => 1,
71         }
72     )->store;
73     $number_of_shelves = Koha::Virtualshelves->search->count;
74     is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
75 };