View | Details | Raw Unified | Return to bug 14544
Collapse All | Expand All

(-)a/C4/Installer/PerlDependencies.pm (+5 lines)
Lines 104-109 our $PERL_DEPS = { Link Here
104
        'required' => '1',
104
        'required' => '1',
105
        'min_ver'  => '1.103'
105
        'min_ver'  => '1.103'
106
    },
106
    },
107
    'Exception::Class' => {
108
        'usage'    => 'Core',
109
        'required' => '1.39',
110
        'min_ver'  => '1.39'
111
    },
107
    'HTML::Scrubber' => {
112
    'HTML::Scrubber' => {
108
        'usage'    => 'Core',
113
        'usage'    => 'Core',
109
        'required' => '1',
114
        'required' => '1',
(-)a/C4/VirtualShelves.pm (-94 / +1 lines)
Lines 45-51 BEGIN { Link Here
45
            &ShelfPossibleAction
45
            &ShelfPossibleAction
46
            &DelFromShelf
46
            &DelFromShelf
47
            &GetBibliosShelves
47
            &GetBibliosShelves
48
            &AddShare &AcceptShare &RemoveShare &IsSharedList
49
    );
48
    );
50
        @EXPORT_OK = qw(
49
        @EXPORT_OK = qw(
51
            &GetAllShelves &ShelvesMax
50
            &GetAllShelves &ShelvesMax
Lines 390-396 sub ShelfPossibleAction { Link Here
390
        }
389
        }
391
    }
390
    }
392
    elsif($action eq 'acceptshare') {
391
    elsif($action eq 'acceptshare') {
393
        #the key for accepting is checked later in AcceptShare
392
        #the key for accepting is checked later in Koha::Virtualshelf->share
394
        #you must not be the owner, list must be private
393
        #you must not be the owner, list must be private
395
        if( $shelf->{category}==1 ) {
394
        if( $shelf->{category}==1 ) {
396
            return (0, 8) if $shelf->{owner}==$user;
395
            return (0, 8) if $shelf->{owner}==$user;
Lines 526-623 sub HandleDelBorrower { Link Here
526
    #Koha::Virtualshelf->new->delete too.
525
    #Koha::Virtualshelf->new->delete too.
527
}
526
}
528
527
529
=head2 AddShare
530
531
     AddShare($shelfnumber, $key);
532
533
Adds a share request to the virtualshelves table.
534
Authorization must have been checked, and a key must be supplied. See script
535
opac-shareshelf.pl for an example.
536
This request is not yet confirmed. So it has no borrowernumber, it does have an
537
expiry date.
538
539
=cut
540
541
sub AddShare {
542
    my ($shelfnumber, $key)= @_;
543
    return if !$shelfnumber || !$key;
544
545
    my $dbh = C4::Context->dbh;
546
    my $sql = "INSERT INTO virtualshelfshares (shelfnumber, invitekey, sharedate) VALUES (?, ?, NOW())";
547
    $dbh->do($sql, undef, ($shelfnumber, $key));
548
    return !$dbh->err;
549
}
550
551
=head2 AcceptShare
552
553
     my $result= AcceptShare($shelfnumber, $key, $borrowernumber);
554
555
Checks acceptation of a share request.
556
Key must be found for this shelf. Invitation must not have expired.
557
Returns true when accepted, false otherwise.
558
559
=cut
560
561
sub AcceptShare {
562
    my ($shelfnumber, $key, $borrowernumber)= @_;
563
    return if !$shelfnumber || !$key || !$borrowernumber;
564
565
    my $sql;
566
    my $dbh = C4::Context->dbh;
567
    $sql="
568
UPDATE virtualshelfshares
569
SET invitekey=NULL, sharedate=NOW(), borrowernumber=?
570
WHERE shelfnumber=? AND invitekey=? AND (sharedate + INTERVAL ? DAY) >NOW()
571
    ";
572
    my $i= $dbh->do($sql, undef, ($borrowernumber, $shelfnumber, $key,  SHARE_INVITATION_EXPIRY_DAYS));
573
    return if !defined($i) || !$i || $i eq '0E0'; #not found
574
    return 1;
575
}
576
577
=head2 IsSharedList
578
579
     my $bool= IsSharedList( $shelfnumber );
580
581
IsSharedList checks if a (private) list has shares.
582
Note that such a check would not be useful for public lists. A public list has
583
no shares, but is visible for anyone by nature..
584
Used to determine the list type in the display of Your lists (all private).
585
Returns boolean value.
586
587
=cut
588
589
sub IsSharedList {
590
    my ($shelfnumber) = @_;
591
    my $dbh = C4::Context->dbh;
592
    my $sql="SELECT id FROM virtualshelfshares WHERE shelfnumber=? AND borrowernumber IS NOT NULL";
593
    my $sth = $dbh->prepare($sql);
594
    $sth->execute($shelfnumber);
595
    my ($rv)= $sth->fetchrow_array;
596
    return defined($rv);
597
}
598
599
=head2 RemoveShare
600
601
     RemoveShare( $user, $shelfnumber );
602
603
RemoveShare removes a share for specific shelf and borrower.
604
Returns true if a record could be deleted.
605
606
=cut
607
608
sub RemoveShare {
609
    my ($user, $shelfnumber)= @_;
610
    my $dbh = C4::Context->dbh;
611
    my $sql="
612
DELETE FROM virtualshelfshares
613
WHERE borrowernumber=? AND shelfnumber=?
614
    ";
615
    my $n= $dbh->do($sql,undef,($user, $shelfnumber));
616
    return if !defined($n) || !$n || $n eq '0E0'; #nothing removed
617
    return 1;
618
}
619
620
621
sub GetShelfCount {
528
sub GetShelfCount {
622
    my ($owner, $category) = @_;
529
    my ($owner, $category) = @_;
623
    my @params;
530
    my @params;
(-)a/C4/VirtualShelves/Page.pm (-2 / +4 lines)
Lines 389-395 sub shelfpage { Link Here
389
                }
389
                }
390
                #remove a share
390
                #remove a share
391
                if(/REMSHR/) {
391
                if(/REMSHR/) {
392
                    RemoveShare($loggedinuser, $number);
392
                    my $shelf = Koha::Virtualshelves->find( $number );
393
                    $shelf->delete_share( $loggedinuser );
393
                    delete $shelflist->{$number} if exists $shelflist->{$number};
394
                    delete $shelflist->{$number} if exists $shelflist->{$number};
394
                    $stay=0;
395
                    $stay=0;
395
                    next;
396
                    next;
Lines 461-467 sub shelfpage { Link Here
461
        $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
462
        $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
462
        $numberCanManage++ if $canmanage;    # possibly outmoded
463
        $numberCanManage++ if $canmanage;    # possibly outmoded
463
        if ( $shelflist->{$element}->{'category'} eq '1' ) {
464
        if ( $shelflist->{$element}->{'category'} eq '1' ) {
464
            $shelflist->{$element}->{shares} = IsSharedList($element);
465
            my $shelf = Koha::Virtualshelves->find( $element );
466
            $shelflist->{$element}->{shares} = $shelf->is_shared;
465
            push( @shelveslooppriv, $shelflist->{$element} );
467
            push( @shelveslooppriv, $shelflist->{$element} );
466
        } else {
468
        } else {
467
            push( @shelvesloop, $shelflist->{$element} );
469
            push( @shelvesloop, $shelflist->{$element} );
(-)a/Koha/Exceptions.pm (+34 lines)
Line 0 Link Here
1
package Koha::Exceptions;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::Exception' => {
8
        description => 'Something went wrong!',
9
    },
10
11
    'Koha::Exceptions::DuplicateObject' => {
12
        isa => 'Koha::Exceptions::Exception',
13
        description => 'Same object already exists',
14
    },
15
16
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
17
        isa => 'Koha::Exceptions::DuplicateObject',
18
        description => "poeut",
19
    },
20
    'Koha::Exceptions::Virtualshelves::InvalidInviteKey' => {
21
        isa => 'Koha::Exceptions::Exception',
22
        description => 'Invalid key on accepting the share',
23
    },
24
    'Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing' => {
25
        isa => 'Koha::Exceptions::Exception',
26
        description=> 'Invalid key on sharing a shelf',
27
    },
28
    'Koha::Exceptions::Virtualshelves::ShareHasExpired' => {
29
        isa => 'Koha::Exceptions::Exception',
30
        description=> 'Cannot share this shelf, the share has expired',
31
    }
32
);
33
34
1;
(-)a/Koha/Virtualshelf.pm (-1 / +45 lines)
Lines 21-27 use Carp; Link Here
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::DateUtils qw( dt_from_string );
23
use Koha::DateUtils qw( dt_from_string );
24
use Koha::Exception::DuplicateObject;
24
use Koha::Exceptions;
25
use Koha::Virtualshelfshare;
26
use Koha::Virtualshelfshares;
25
27
26
use base qw(Koha::Object);
28
use base qw(Koha::Object);
27
29
Lines 93-98 sub is_shelfname_valid { Link Here
93
    return $count ? 0 : 1;
95
    return $count ? 0 : 1;
94
}
96
}
95
97
98
sub get_shares {
99
    my ( $self ) = @_;
100
    return $self->{_result}->virtualshelfshares;
101
}
102
103
sub share {
104
    my ( $self, $key ) = @_;
105
    unless ( $key ) {
106
        Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
107
    }
108
    Koha::Virtualshelfshare->new(
109
        {
110
            shelfnumber => $self->shelfnumber,
111
            invitekey => $key,
112
            sharedate => dt_from_string,
113
        }
114
    )->store;
115
}
116
117
sub is_shared {
118
    my ( $self ) = @_;
119
    return  $self->get_shares->search(
120
        {
121
            borrowernumber => { '!=' => undef },
122
        }
123
    )->count;
124
}
125
126
sub remove_share {
127
    my ( $self, $borrowernumber ) = @_;
128
    my $shelves = Koha::Virtualshelfshares->search(
129
        {
130
            shelfnumber => $self->shelfnumber,
131
            borrowernumber => $borrowernumber,
132
        }
133
    );
134
    return 0 unless $shelves->count;
135
136
    # Only 1 share with 1 patron can exist
137
    return $shelves->next->delete;
138
}
139
96
sub type {
140
sub type {
97
    return 'Virtualshelve';
141
    return 'Virtualshelve';
98
}
142
}
(-)a/Koha/Virtualshelfshare.pm (+74 lines)
Line 0 Link Here
1
package Koha::Virtualshelfshare;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
use DateTime;
22
use DateTime::Duration;
23
24
use Koha::Database;
25
use Koha::DateUtils;
26
use Koha::Exceptions;
27
28
use base qw(Koha::Object);
29
30
use constant SHARE_INVITATION_EXPIRY_DAYS => 14; #two weeks to accept
31
32
=head1 NAME
33
34
Koha::Virtualshelfshare - Koha Virtualshelfshare Object class
35
36
=head1 API
37
38
=head2 Class Methods
39
40
=cut
41
42
=head3 type
43
44
=cut
45
46
sub accept {
47
    my ( $self, $invitekey, $borrowernumber ) = @_;
48
    if ( $self->has_expired ) {
49
        Koha::Exceptions::Virtualshelves::ShareHasExpired->throw;
50
    }
51
    if ( $self->invitekey ne $invitekey ) {
52
        Koha::Exceptions::Virtualshelves::InvalidInviteKey->throw;
53
    }
54
    $self->invitekey(undef);
55
    $self->sharedate(dt_from_string);
56
    $self->borrowernumber($borrowernumber);
57
    $self->store;
58
}
59
60
sub has_expired {
61
    my ($self) = @_;
62
    my $dt_sharedate     = dt_from_string( $self->sharedate, 'sql' );
63
    my $today            = dt_from_string;
64
    my $expiration_delay = DateTime::Duration->new( days => SHARE_INVITATION_EXPIRY_DAYS );
65
    my $has_expired = DateTime->compare( $today, $dt_sharedate->add_duration($expiration_delay) );
66
    # Note: has_expired = 0 if the share expires today
67
    return $has_expired == 1 ? 1 : 0
68
}
69
70
sub type {
71
    return 'Virtualshelfshare';
72
}
73
74
1;
(-)a/Koha/Virtualshelfshares.pm (+50 lines)
Line 0 Link Here
1
package Koha::Virtualshelfshares;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
use Koha::Virtualshelf;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::Virtualshelfshares - Koha Virtualshelfshares Object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub type {
43
    return 'Virtualshelfshare';
44
}
45
46
sub object_class {
47
    return 'Koha::Virtualshelfshare';
48
}
49
50
1;
(-)a/opac/opac-shareshelf.pl (-14 / +33 lines)
Lines 122-141 sub show_accept { Link Here
122
    #errorcode 5: should be private list
122
    #errorcode 5: should be private list
123
    #errorcode 8: should not be owner
123
    #errorcode 8: should not be owner
124
124
125
    my $dbkey = keytostring( stringtokey( $param->{key}, 0 ), 1 );
125
    # We could have used ->find with the share id, but we don't want to change
126
    if ( AcceptShare( $param->{shelfnumber}, $dbkey, $param->{loggedinuser} ) )
126
    # the url sent to the patron
127
    {
127
    my $shared_shelf = Koha::Virtualshelfshares->search(
128
        notify_owner($param);
128
        {
129
129
            shelfnumber => $param->{shelfnumber},
130
        #redirect to view of this shared list
130
        },
131
        print $param->{query}->redirect(
131
        {
132
            -uri    => SHELVES_URL . $param->{shelfnumber},
132
            order_by => 'sharedate desc',
133
            -cookie => $param->{cookie}
133
            limit => 1,
134
        );
134
        }
135
        exit;
135
    );
136
    }
136
137
    else {
137
    if ( $shared_shelf ) {
138
        $shared_shelf = $shared_shelf->next;
139
        my $key = keytostring( stringtokey( $param->{key}, 0 ), 1 );
140
        my $is_accepted = eval { $shared_shelf->accept( $key, $param->{loggedinuser} ) };
141
        if ( $is_accepted ) {
142
            notify_owner($param);
143
144
            #redirect to view of this shared list
145
            print $param->{query}->redirect(
146
                -uri    => SHELVES_URL . $param->{shelfnumber},
147
                -cookie => $param->{cookie}
148
            );
149
            exit;
150
        }
138
        $param->{errcode} = 7;    #not accepted (key not found or expired)
151
        $param->{errcode} = 7;    #not accepted (key not found or expired)
152
    } else {
153
        # This shelf is not shared
139
    }
154
    }
140
}
155
}
141
156
Lines 200-206 sub send_invitekey { Link Here
200
        my @newkey = randomlist( KEYLENGTH, 64 );    #generate a new key
215
        my @newkey = randomlist( KEYLENGTH, 64 );    #generate a new key
201
216
202
        #add a preliminary share record
217
        #add a preliminary share record
203
        if ( !AddShare( $param->{shelfnumber}, keytostring( \@newkey, 1 ) ) ) {
218
        my $shelf = Koha::Virtualshelves->find( $param->{shelfnumber} );
219
        my $key = keytostring( \@newkey, 1 );
220
        my $is_shared = eval { $shelf->share( $key ); };
221
        # TODO Better error handling, catch the exceptions
222
        if ( $@ or not $is_shared ) {
204
            push @{ $param->{fail_addr} }, $a;
223
            push @{ $param->{fail_addr} }, $a;
205
            next;
224
            next;
206
        }
225
        }
(-)a/t/db_dependent/VirtualShelves.t (-74 lines)
Lines 122-201 for my $i (0..9){ Link Here
122
    $used{$key}++;
122
    $used{$key}++;
123
}
123
}
124
124
125
#----------------------- TEST AddShare ----------------------------------------#
126
127
#first count the number of shares in the table
128
my $sql_sharecount="select count(*) from virtualshelfshares";
129
my $cnt1=$dbh->selectrow_array($sql_sharecount);
130
131
#try to add a share without shelfnumber: should fail
132
AddShare(0, 'abcdefghij');
133
my $cnt2=$dbh->selectrow_array($sql_sharecount);
134
is($cnt1,$cnt2, "Did not add an invalid share record");
135
136
#add another share: should be okay
137
#AddShare assumes that you tested if category==private (so we could actually
138
#be doing something illegal here :)
139
my $n=$shelves[0]->{number};
140
if($n<0) {
141
    ok(1, 'Skip AddShare for shelf -1');
142
}
143
else {
144
    AddShare($n, 'abcdefghij');
145
    my $cnt3=$dbh->selectrow_array($sql_sharecount);
146
    is(1+$cnt2, $cnt3, "Added one new share record with invitekey");
147
}
148
149
#----------------------- TEST AcceptShare -------------------------------------#
150
151
# test accepting a wrong key
152
my $testkey= 'keyisgone9';
153
my $acctest="delete from virtualshelfshares where invitekey=?";
154
$dbh->do($acctest,undef,($testkey)); #just be sure it does not exist
155
$acctest="select shelfnumber from virtualshelves";
156
my ($accshelf)= $dbh->selectrow_array($acctest);
157
is(AcceptShare($accshelf,$testkey,$borrowernumbers[0]),undef,'Did not accept invalid key');
158
159
# test accepting a good key
160
if( AddShare($accshelf,$testkey) && $borrowernumbers[0] ) {
161
    is(AcceptShare($accshelf, $testkey, $borrowernumbers[0]),1,'Accepted share');
162
}
163
else { #cannot accept if addshared failed somehow
164
    ok(1, 'Skipped second AcceptShare test');
165
}
166
167
#----------------------- TEST IsSharedList ------------------------------------#
168
169
for my $i (0..9) {
170
    my $sql="select count(*) from virtualshelfshares where shelfnumber=? and borrowernumber is not null";
171
    my $sh=$shelves[$i]->{number};
172
    my ($n)=$dbh->selectrow_array($sql,undef,($sh));
173
    is(IsSharedList($sh),$n? 1: '', "Checked IsSharedList for shelf $sh");
174
}
175
176
#----------------TEST Koha::Virtualshelf->delete & DelFromShelf functions------------------------#
177
178
for my $i (0..9){
179
    my $shelfnumber = $shelves[$i]->{number};
180
    if($shelfnumber<0) {
181
        ok(1, 'Skip Koha::Virtualshelf->delete for shelf -1');
182
        next;
183
    }
184
    my $status = Koha::Virtualshelves->find($shelfnumber)->delete;
185
    is($status, 1, "deleted shelf $shelfnumber and its contents");
186
}
187
188
#----------------------- TEST RemoveShare -------------------------------------#
189
190
my $remshr_test="select borrowernumber, shelfnumber from virtualshelfshares where borrowernumber is not null";
191
my @remshr_shelf= $dbh->selectrow_array($remshr_test);
192
if(@remshr_shelf) {
193
    is(RemoveShare(@remshr_shelf),1,'Removed a shelf share');
194
}
195
else {
196
    ok(1,'Skipped RemoveShare test');
197
}
198
199
#----------------------- SOME SUBS --------------------------------------------#
125
#----------------------- SOME SUBS --------------------------------------------#
200
126
201
sub randomname {
127
sub randomname {
(-)a/t/db_dependent/Virtualshelves.t (-4 / +72 lines)
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
};

Return to bug 14544