Bug 14544: Move share routines to Koha::Virtualshelfshare and Koha::Virtualshelfshares
[koha.git] / t / db_dependent / Virtualshelves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 2;
5
6 use C4::Context;
7 use Koha::DateUtils;
8 use Koha::Virtualshelves;
9 use Koha::Virtualshelfshares;
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 $dbh->do(q|DELETE FROM virtualshelfshares|);
18
19 my $builder = t::lib::TestBuilder->new;
20
21 subtest 'CRUD' => sub {
22     plan tests => 13;
23     my $patron = $builder->build({
24         source => 'Borrower',
25     });
26
27     my $number_of_shelves = Koha::Virtualshelves->search->count;
28
29     is( $number_of_shelves, 0, 'No shelves should exist' );
30
31     my $shelf = Koha::Virtualshelf->new({
32             shelfname => "my first shelf",
33             owner => $patron->{borrowernumber},
34             category => 1,
35         }
36     )->store;
37
38     is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
39
40     $number_of_shelves = Koha::Virtualshelves->search->count;
41     is( $number_of_shelves, 1, '1 shelf should have been inserted' );
42     is( $shelf->allow_add, 0, 'The default value for allow_add should be 1' );
43     is( $shelf->allow_delete_own, 1, 'The default value for allow_delete_own should be 0' );
44     is( $shelf->allow_delete_other, 0, 'The default value for allow_delete_other should be 0' );
45     is( output_pref($shelf->created_on), output_pref(dt_from_string), 'The creation time should have been set to today' );
46
47     my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
48
49     is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
50
51     # Insert with the same name
52     eval {
53         $shelf = Koha::Virtualshelf->new({
54                 shelfname => "my first shelf",
55                 owner => $patron->{borrowernumber},
56                 category => 1,
57             }
58         )->store;
59     };
60     is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' );
61     $number_of_shelves = Koha::Virtualshelves->search->count;
62     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
63
64     my $another_patron = $builder->build({
65         source => 'Borrower',
66     });
67
68     $shelf = Koha::Virtualshelf->new({
69             shelfname => "my first shelf",
70             owner => $another_patron->{borrowernumber},
71             category => 1,
72         }
73     )->store;
74     $number_of_shelves = Koha::Virtualshelves->search->count;
75     is( $number_of_shelves, 2, 'Another patron should be able to create a shelf with an existing shelfname');
76
77     my $is_deleted = Koha::Virtualshelves->find( $shelf->shelfnumber )->delete;
78     is( $is_deleted, 1, 'The shelf has been deleted correctly' );
79     $number_of_shelves = Koha::Virtualshelves->search->count;
80     is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
81 };
82
83 subtest 'Sharing' => sub {
84     plan tests => 18;
85     my $patron_wants_to_share = $builder->build({
86         source => 'Borrower',
87     });
88     my $share_with_me = $builder->build({
89         source => 'Borrower',
90     });
91     my $just_another_patron = $builder->build({
92         source => 'Borrower',
93     });
94
95     my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
96     is( $number_of_shelves_shared, 0, 'No shelves should exist' );
97
98     my $shelf_to_share = Koha::Virtualshelf->new({
99             shelfname => "my first shelf",
100             owner => $patron_wants_to_share->{borrowernumber},
101             category => 1,
102         }
103     )->store;
104
105     my $shelf_not_to_share = Koha::Virtualshelf->new({
106             shelfname => "my second shelf",
107             owner => $patron_wants_to_share->{borrowernumber},
108             category => 1,
109         }
110     )->store;
111
112     my $shared_shelf = eval { $shelf_to_share->share };
113     is ( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing', 'Do not share if no key given' );
114     $shared_shelf = eval { $shelf_to_share->share('this is a valid key') };
115     is( ref( $shared_shelf ), 'Koha::Virtualshelfshare', 'On sharing, the method should return a valid Koha::Virtualshelfshare object' );
116
117     my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
118
119     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
120     is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
121
122     my $is_accepted = eval {
123         $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
124     };
125     is( $is_accepted, undef, 'The share should have not been accepted if the key is invalid' );
126     is( ref( $@ ), 'Koha::Exceptions::Virtualshelves::InvalidInviteKey', 'accept with an invalid key should raise an exception' );
127
128     $is_accepted = $shared_shelf->accept( 'this is a valid key', $share_with_me->{borrowernumber} );
129     ok( defined($is_accepted), 'The share should have been accepted if the key valid' );
130
131     is( $shelf_to_share->is_shared, 1 );
132     is( $shelf_not_to_share->is_shared, 0 );
133
134     is( $shelf_to_share->is_shared_with( $patron_wants_to_share->{borrowernumber} ), 0 , "The shelf should not be shared with the owner" );
135     is( $shelf_to_share->is_shared_with( $share_with_me->{borrowernumber} ), 1 , "The shelf should be shared with share_with_me" );
136     is( $shelf_to_share->is_shared_with( $just_another_patron->{borrowernumber} ), 0, "The shelf should not be shared with just_another_patron" );
137
138     is( $shelf_to_share->remove_share( $just_another_patron->{borrowernumber} ), 0, 'No share should be removed if the share has not been done with this patron' );
139     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
140     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
141
142     is( $shelf_not_to_share->remove_share( $share_with_me->{borrowernumber} ), 0, '0 share should have been removed if the shelf is not share' );
143     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
144     is( $number_of_shelves_shared, 2, 'To be sure no shares have been removed' );
145
146     is( $shelf_to_share->remove_share( $share_with_me->{borrowernumber} ), 1, '1 share should have been removed if the shelf was shared with this patron' );
147     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
148     is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
149 };