Bug 16551: Display the name of lists to the search results at the OPAC
[koha_ffzg] / t / db_dependent / Virtualshelves.t
index aa8b323..c867056 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use Modern::Perl;
-use Test::More tests => 4;
+use Test::More tests => 6;
 use DateTime::Duration;
 
 use C4::Context;
@@ -12,14 +12,11 @@ use Koha::Virtualshelfcontents;
 
 use t::lib::TestBuilder;
 
+my $builder = t::lib::TestBuilder->new;
+
 my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
-
-$dbh->do(q|DELETE FROM virtualshelfshares|);
-$dbh->do(q|DELETE FROM virtualshelfcontents|);
-$dbh->do(q|DELETE FROM virtualshelves|);
-
-my $builder = t::lib::TestBuilder->new;
+teardown();
 
 subtest 'CRUD' => sub {
     plan tests => 13;
@@ -60,7 +57,8 @@ subtest 'CRUD' => sub {
             }
         )->store;
     };
-    is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject' );
+    is( ref($@), 'Koha::Exceptions::Virtualshelves::DuplicateObject',
+        'Exception on duplicate name' );
     $number_of_shelves = Koha::Virtualshelves->search->count;
     is( $number_of_shelves, 1, 'To be sure the number of shelves is still 1' );
 
@@ -81,6 +79,8 @@ subtest 'CRUD' => sub {
     is( $is_deleted, 1, 'The shelf has been deleted correctly' );
     $number_of_shelves = Koha::Virtualshelves->search->count;
     is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' );
+
+    teardown();
 };
 
 subtest 'Sharing' => sub {
@@ -131,8 +131,8 @@ subtest 'Sharing' => sub {
     $is_accepted = $shared_shelf->accept( 'this is a valid key', $share_with_me->{borrowernumber} );
     ok( defined($is_accepted), 'The share should have been accepted if the key valid' );
 
-    is( $shelf_to_share->is_shared, 1 );
-    is( $shelf_not_to_share->is_shared, 0 );
+    is( $shelf_to_share->is_shared, 1, 'first shelf is shared' );
+    is( $shelf_not_to_share->is_shared, 0, 'second shelf is not shared' );
 
     is( $shelf_to_share->is_shared_with( $patron_wants_to_share->{borrowernumber} ), 0 , "The shelf should not be shared with the owner" );
     is( $shelf_to_share->is_shared_with( $share_with_me->{borrowernumber} ), 1 , "The shelf should be shared with share_with_me" );
@@ -149,6 +149,8 @@ subtest 'Sharing' => sub {
     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' );
     $number_of_shelves_shared = Koha::Virtualshelfshares->search->count;
     is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' );
+
+    teardown();
 };
 
 subtest 'Shelf content' => sub {
@@ -195,9 +197,10 @@ subtest 'Shelf content' => sub {
     # Patron 2 will try to remove a content
     # allow_add = 0, allow_delete_own = 1, allow_delete_other = 0 => Default values
     my $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
-    is( $number_of_deleted_biblios, 0, );
+    is( $number_of_deleted_biblios, 0, 'Patron 2 removed nothing' );
+    # Now try with patron 1
     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio1->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
-    is( $number_of_deleted_biblios, 1, );
+    is( $number_of_deleted_biblios, 1, 'Patron 1 removed biblio' );
 
     $number_of_contents = Koha::Virtualshelfcontents->search->count;
     is( $number_of_contents, 1, 'To be sure the content has been deleted' );
@@ -206,7 +209,7 @@ subtest 'Shelf content' => sub {
     $shelf->allow_delete_own(0);
     $shelf->store;
     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron1->{borrowernumber} } );
-    is( $number_of_deleted_biblios, 1, );
+    is( $number_of_deleted_biblios, 1, 'Patron 1 removed another biblio' );
     $number_of_contents = Koha::Virtualshelfcontents->search->count;
     is( $number_of_contents, 0, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=0)' );
     $shelf->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
@@ -223,7 +226,7 @@ subtest 'Shelf content' => sub {
     is( $number_of_contents, 3, 'The biblio should have been added to the shelf by the patron 2' );
 
     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio3->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
-    is( $number_of_deleted_biblios, 1, );
+    is( $number_of_deleted_biblios, 1, 'Biblio 3 deleted by patron 2' );
     $number_of_contents = Koha::Virtualshelfcontents->search->count;
     is( $number_of_contents, 2, 'The biblio should have been deleted to the shelf by the patron, it is his own content (allow_delete_own=1) ' );
 
@@ -232,9 +235,11 @@ subtest 'Shelf content' => sub {
     $shelf->store;
 
     $number_of_deleted_biblios = $shelf->remove_biblios( { biblionumbers => [ $biblio2->{biblionumber} ], borrowernumber => $patron2->{borrowernumber} } );
-    is( $number_of_deleted_biblios, 1, );
+    is( $number_of_deleted_biblios, 1, 'Biblio 2 deleted by patron 2' );
     $number_of_contents = Koha::Virtualshelfcontents->search->count;
     is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' );
+
+    teardown();
 };
 
 subtest 'Shelf permissions' => sub {
@@ -340,4 +345,155 @@ subtest 'Shelf permissions' => sub {
 
     is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
     is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
+
+    teardown();
+};
+
+subtest 'Get shelves' => sub {
+    plan tests => 4;
+    my $patron1 = $builder->build({
+        source => 'Borrower',
+    });
+    my $patron2 = $builder->build({
+        source => 'Borrower',
+    });
+
+    my $private_shelf1_1 = Koha::Virtualshelf->new({
+            shelfname => "private shelf 1 for patron 1",
+            owner => $patron1->{borrowernumber},
+            category => 1,
+        }
+    )->store;
+    my $private_shelf1_2 = Koha::Virtualshelf->new({
+            shelfname => "private shelf 2 for patron 1",
+            owner => $patron1->{borrowernumber},
+            category => 1,
+        }
+    )->store;
+    my $private_shelf2_1 = Koha::Virtualshelf->new({
+            shelfname => "private shelf 1 for patron 2",
+            owner => $patron2->{borrowernumber},
+            category => 1,
+        }
+    )->store;
+    my $public_shelf1_1 = Koha::Virtualshelf->new({
+            shelfname => "public shelf 1 for patron 1",
+            owner => $patron1->{borrowernumber},
+            category => 2,
+        }
+    )->store;
+    my $public_shelf1_2 = Koha::Virtualshelf->new({
+            shelfname => "public shelf 2 for patron 1",
+            owner => $patron1->{borrowernumber},
+            category => 2,
+        }
+    )->store;
+
+    my $private_shelves = Koha::Virtualshelves->get_private_shelves;
+    is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
+    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
+    is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
+
+    $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
+    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
+    is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
+
+    my $public_shelves = Koha::Virtualshelves->get_public_shelves;
+    is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
+
+    teardown();
+};
+
+subtest 'Get shelves containing biblios' => sub {
+
+    plan tests => 9;
+    my $patron1 = $builder->build( { source => 'Borrower', } );
+    my $patron2 = $builder->build( { source => 'Borrower', } );
+    my $biblio1 = $builder->build( { source => 'Biblio', } );
+    my $biblio2 = $builder->build( { source => 'Biblio', } );
+    my $biblio3 = $builder->build( { source => 'Biblio', } );
+    my $biblio4 = $builder->build( { source => 'Biblio', } );
+
+    my $shelf1 = Koha::Virtualshelf->new(
+        {   shelfname    => "my first shelf",
+            owner        => $patron1->{borrowernumber},
+            category     => 1,
+        }
+    )->store;
+    my $shelf2 = Koha::Virtualshelf->new(
+        {   shelfname    => "my x second shelf", # 'x' to make it sorted after 'third'
+            owner        => $patron2->{borrowernumber},
+            category     => 1,
+        }
+    )->store;
+    my $shelf3 = Koha::Virtualshelf->new(
+        {   shelfname    => "my third shelf",
+            owner        => $patron1->{borrowernumber},
+            category     => 2,
+        }
+    )->store;
+
+    my $content1 = $shelf1->add_biblio( $biblio1->{biblionumber}, $patron1->{borrowernumber} );
+    my $content2 = $shelf1->add_biblio( $biblio2->{biblionumber}, $patron1->{borrowernumber} );
+    my $content3 = $shelf2->add_biblio( $biblio2->{biblionumber}, $patron2->{borrowernumber} );
+    my $content4 = $shelf2->add_biblio( $biblio3->{biblionumber}, $patron2->{borrowernumber} );
+    my $content5 = $shelf2->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
+    my $content6 = $shelf3->add_biblio( $biblio4->{biblionumber}, $patron2->{borrowernumber} );
+
+    my $shelves_with_biblio1_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio1->{biblionumber},
+        }
+    );
+    is ( $shelves_with_biblio1_for_any_patrons->count, 0, 'shelf1 is private and should not be displayed if patron is not logged in' );
+
+    my $shelves_with_biblio4_for_any_patrons = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio4->{biblionumber},
+        }
+    );
+    is ( $shelves_with_biblio4_for_any_patrons->count, 1, 'shelf3 is public and should be displayed for any patrons' );
+    is ( $shelves_with_biblio4_for_any_patrons->next->shelfname, $shelf3->shelfname, 'The correct shelf (3) should be displayed' );
+
+    my $shelves_with_biblio1_for_other_patrons = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio1->{biblionumber},
+            borrowernumber => $patron2->{borrowernumber},
+        }
+    );
+    is ( $shelves_with_biblio1_for_other_patrons->count, 0, 'shelf1 is private and should not be displayed for other patrons' );
+
+    my $shelves_with_biblio1_for_owner = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio1->{biblionumber},
+            borrowernumber => $patron1->{borrowernumber},
+        }
+    );
+    is ( $shelves_with_biblio1_for_owner->count, 1, 'shelf1 is private and should be displayed for the owner' );
+
+    my $shelves_with_biblio2_for_patron1 = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio2->{biblionumber},
+            borrowernumber => $patron1->{borrowernumber},
+        }
+    );
+    is ( $shelves_with_biblio2_for_patron1->count, 1, 'Only shelf1 should be displayed for patron 1 and biblio 1' );
+    is ( $shelves_with_biblio2_for_patron1->next->shelfname, $shelf1->shelfname, 'The correct shelf (1) should be displayed for patron 1' );
+
+    my $shelves_with_biblio4_for_patron2 = Koha::Virtualshelves->get_shelves_containing_record(
+        {
+            biblionumber => $biblio4->{biblionumber},
+            borrowernumber => $patron2->{borrowernumber},
+        }
+    );
+    is ( $shelves_with_biblio4_for_patron2->count, 2, 'Patron should shown private and public lists for a given biblio' );
+    is ( $shelves_with_biblio4_for_patron2->next->shelfname, $shelf3->shelfname, 'The shelves should be sorted by shelfname' );
+
+    teardown();
 };
+
+sub teardown {
+    $dbh->do(q|DELETE FROM virtualshelfshares|);
+    $dbh->do(q|DELETE FROM virtualshelfcontents|);
+    $dbh->do(q|DELETE FROM virtualshelves|);
+}