Lines 1-12
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
4 |
use Test::More tests => 1; |
4 |
use Test::More tests => 2; |
5 |
|
5 |
|
6 |
use C4::Context; |
6 |
use C4::Context; |
7 |
use Koha::DateUtils; |
7 |
use Koha::DateUtils; |
8 |
use Koha::Virtualshelf; |
|
|
9 |
use Koha::Virtualshelves; |
8 |
use Koha::Virtualshelves; |
|
|
9 |
use Koha::Virtualshelfshares; |
10 |
|
10 |
|
11 |
use t::lib::TestBuilder; |
11 |
use t::lib::TestBuilder; |
12 |
|
12 |
|
Lines 14-19
my $dbh = C4::Context->dbh;
Link Here
|
14 |
$dbh->{AutoCommit} = 0; |
14 |
$dbh->{AutoCommit} = 0; |
15 |
|
15 |
|
16 |
$dbh->do(q|DELETE FROM virtualshelves|); |
16 |
$dbh->do(q|DELETE FROM virtualshelves|); |
|
|
17 |
$dbh->do(q|DELETE FROM virtualshelfshares|); |
17 |
|
18 |
|
18 |
my $builder = t::lib::TestBuilder->new; |
19 |
my $builder = t::lib::TestBuilder->new; |
19 |
|
20 |
|
Lines 56-62
subtest 'CRUD' => sub {
Link Here
|
56 |
} |
57 |
} |
57 |
)->store; |
58 |
)->store; |
58 |
}; |
59 |
}; |
59 |
is( ref($@), 'Koha::Exception::DuplicateObject' ); |
60 |
is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' ); |
60 |
$number_of_shelves = Koha::Virtualshelves->search->count; |
61 |
$number_of_shelves = Koha::Virtualshelves->search->count; |
61 |
is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' ); |
62 |
is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' ); |
62 |
|
63 |
|
Lines 78-80
subtest 'CRUD' => sub {
Link Here
|
78 |
$number_of_shelves = Koha::Virtualshelves->search->count; |
79 |
$number_of_shelves = Koha::Virtualshelves->search->count; |
79 |
is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' ); |
80 |
is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' ); |
80 |
}; |
81 |
}; |
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 |
}; |