4 use Test::More tests => 2;
8 use Koha::Virtualshelves;
9 use Koha::Virtualshelfshares;
11 use t::lib::TestBuilder;
13 my $dbh = C4::Context->dbh;
14 $dbh->{AutoCommit} = 0;
16 $dbh->do(q|DELETE FROM virtualshelves|);
17 $dbh->do(q|DELETE FROM virtualshelfshares|);
19 my $builder = t::lib::TestBuilder->new;
21 subtest 'CRUD' => sub {
23 my $patron = $builder->build({
27 my $number_of_shelves = Koha::Virtualshelves->search->count;
29 is( $number_of_shelves, 0, 'No shelves should exist' );
31 my $shelf = Koha::Virtualshelf->new({
32 shelfname => "my first shelf",
33 owner => $patron->{borrowernumber},
38 is( ref( $shelf ), 'Koha::Virtualshelf', 'The constructor should return a valid object' );
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' );
47 my $retrieved_shelf = Koha::Virtualshelves->find( $shelf->shelfnumber );
49 is( $retrieved_shelf->shelfname, $shelf->shelfname, 'Find should correctly return the shelfname' );
51 # Insert with the same name
53 $shelf = Koha::Virtualshelf->new({
54 shelfname => "my first shelf",
55 owner => $patron->{borrowernumber},
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' );
64 my $another_patron = $builder->build({
68 $shelf = Koha::Virtualshelf->new({
69 shelfname => "my first shelf",
70 owner => $another_patron->{borrowernumber},
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');
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' );
83 subtest 'Sharing' => sub {
85 my $patron_wants_to_share = $builder->build({
88 my $share_with_me = $builder->build({
91 my $just_another_patron = $builder->build({
95 my $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
96 is( $number_of_shelves_shared, 0, 'No shelves should exist' );
98 my $shelf_to_share = Koha::Virtualshelf->new({
99 shelfname => "my first shelf",
100 owner => $patron_wants_to_share->{borrowernumber},
105 my $shelf_not_to_share = Koha::Virtualshelf->new({
106 shelfname => "my second shelf",
107 owner => $patron_wants_to_share->{borrowernumber},
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' );
117 my $another_shared_shelf = eval { $shelf_to_share->share('this is another valid key') }; # Just to have 2 shares in DB
119 $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
120 is( $number_of_shelves_shared, 2, '2 shares should have been inserted' );
122 my $is_accepted = eval {
123 $shared_shelf->accept( 'this is an invalid key', $share_with_me->{borrowernumber} );
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' );
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' );
131 is( $shelf_to_share->is_shared, 1 );
132 is( $shelf_not_to_share->is_shared, 0 );
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" );
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' );
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' );
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' );