| 
      
            Line 0
          
      
      
        Link Here
      
     | 
  
          
            
              | 0 | 
              -   | 
              1 | 
              #!/usr/bin/env perl  | 
            
            
               | 
               | 
              2 | 
               | 
            
            
              | 3 | 
              # This file is part of Koha.  | 
            
            
              | 4 | 
              #  | 
            
            
              | 5 | 
              # Koha is free software; you can redistribute it and/or modify it  | 
            
            
              | 6 | 
              # under the terms of the GNU General Public License as published by  | 
            
            
              | 7 | 
              # the Free Software Foundation; either version 3 of the License, or  | 
            
            
              | 8 | 
              # (at your option) any later version.  | 
            
            
              | 9 | 
              #  | 
            
            
              | 10 | 
              # Koha is distributed in the hope that it will be useful, but  | 
            
            
              | 11 | 
              # WITHOUT ANY WARRANTY; without even the implied warranty of  | 
            
            
              | 12 | 
              # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  | 
            
            
              | 13 | 
              # GNU General Public License for more details.  | 
            
            
              | 14 | 
              #  | 
            
            
              | 15 | 
              # You should have received a copy of the GNU General Public License  | 
            
            
              | 16 | 
              # along with Koha; if not, see <http://www.gnu.org/licenses>.  | 
            
            
              | 17 | 
               | 
            
            
              | 18 | 
              use Modern::Perl;  | 
            
            
              | 19 | 
               | 
            
            
              | 20 | 
              use utf8;  | 
            
            
              | 21 | 
              use Encode;  | 
            
            
              | 22 | 
               | 
            
            
              | 23 | 
              use Test::More tests => 2;  | 
            
            
              | 24 | 
              use Test::MockModule;  | 
            
            
              | 25 | 
              use Test::Mojo;  | 
            
            
              | 26 | 
              use Test::Warn;  | 
            
            
              | 27 | 
               | 
            
            
              | 28 | 
              use t::lib::Mocks;  | 
            
            
              | 29 | 
              use t::lib::TestBuilder;  | 
            
            
              | 30 | 
               | 
            
            
              | 31 | 
              use Mojo::JSON qw(encode_json);  | 
            
            
              | 32 | 
               | 
            
            
              | 33 | 
              use C4::Auth;  | 
            
            
              | 34 | 
              use C4::Biblio      qw( DelBiblio );  | 
            
            
              | 35 | 
              use C4::Circulation qw( AddIssue AddReturn );  | 
            
            
              | 36 | 
               | 
            
            
              | 37 | 
              use Koha::Biblios;  | 
            
            
              | 38 | 
              use Koha::Database;  | 
            
            
              | 39 | 
              use Koha::Checkouts;  | 
            
            
              | 40 | 
              use Koha::Old::Checkouts;  | 
            
            
              | 41 | 
               | 
            
            
              | 42 | 
              use Mojo::JSON qw(encode_json);  | 
            
            
              | 43 | 
               | 
            
            
              | 44 | 
              my $schema  = Koha::Database->new->schema;  | 
            
            
              | 45 | 
              my $builder = t::lib::TestBuilder->new;  | 
            
            
              | 46 | 
               | 
            
            
              | 47 | 
              t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );  | 
            
            
              | 48 | 
               | 
            
            
              | 49 | 
              my $t = Test::Mojo->new('Koha::REST::V1'); | 
            
            
              | 50 | 
               | 
            
            
              | 51 | 
              subtest 'get() tests' => sub { | 
            
            
              | 52 | 
               | 
            
            
              | 53 | 
                  plan tests => 22;  | 
            
            
              | 54 | 
               | 
            
            
              | 55 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 56 | 
               | 
            
            
              | 57 | 
                  my $patron = $builder->build_object(  | 
            
            
              | 58 | 
                      { | 
            
            
              | 59 | 
                          class => 'Koha::Patrons',  | 
            
            
              | 60 | 
                          value => { flags => 0 } | 
            
            
              | 61 | 
                      }  | 
            
            
              | 62 | 
                  );  | 
            
            
              | 63 | 
                  my $password = 'thePassword123';  | 
            
            
              | 64 | 
                  $patron->set_password( { password => $password, skip_validation => 1 } ); | 
            
            
              | 65 | 
                  $patron->discard_changes;  | 
            
            
              | 66 | 
                  my $userid = $patron->userid;  | 
            
            
              | 67 | 
               | 
            
            
              | 68 | 
                  my $biblio = $builder->build_sample_biblio(  | 
            
            
              | 69 | 
                      { | 
            
            
              | 70 | 
                          title  => 'The unbearable lightness of being',  | 
            
            
              | 71 | 
                          author => 'Milan Kundera'  | 
            
            
              | 72 | 
                      }  | 
            
            
              | 73 | 
                  );  | 
            
            
              | 74 | 
               | 
            
            
              | 75 | 
                  my $formatted = $biblio->metadata->record->as_formatted;  | 
            
            
              | 76 | 
                  DelBiblio( $biblio->id );  | 
            
            
              | 77 | 
               | 
            
            
              | 78 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber )->status_is(403);  | 
            
            
              | 79 | 
               | 
            
            
              | 80 | 
                  $patron->flags(4)->store;  | 
            
            
              | 81 | 
               | 
            
            
              | 82 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 83 | 
                          . $biblio->biblionumber => { Accept => 'application/weird+format' } )->status_is(400); | 
            
            
              | 84 | 
               | 
            
            
              | 85 | 
                  $t->get_ok(  | 
            
            
              | 86 | 
                      "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber => { Accept => 'application/json' } ) | 
            
            
              | 87 | 
                      ->status_is(200)->json_is( '/title', 'The unbearable lightness of being' )  | 
            
            
              | 88 | 
                      ->json_is( '/author', 'Milan Kundera' );  | 
            
            
              | 89 | 
               | 
            
            
              | 90 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 91 | 
                          . $biblio->biblionumber => { Accept => 'application/marcxml+xml' } )->status_is(200); | 
            
            
              | 92 | 
               | 
            
            
              | 93 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 94 | 
                          . $biblio->biblionumber => { Accept => 'application/marc-in-json' } )->status_is(200); | 
            
            
              | 95 | 
               | 
            
            
              | 96 | 
                  $t->get_ok(  | 
            
            
              | 97 | 
                      "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber => { Accept => 'application/marc' } ) | 
            
            
              | 98 | 
                      ->status_is(200);  | 
            
            
              | 99 | 
               | 
            
            
              | 100 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber => { Accept => 'text/plain' } ) | 
            
            
              | 101 | 
                      ->status_is(200)->content_is($formatted);  | 
            
            
              | 102 | 
               | 
            
            
              | 103 | 
                  my $biblio_exist = $builder->build_sample_biblio();  | 
            
            
              | 104 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 105 | 
                          . $biblio_exist->biblionumber => { Accept => 'application/marc' } )->status_is(404) | 
            
            
              | 106 | 
                      ->json_is( '/error', 'Object not found.' );  | 
            
            
              | 107 | 
               | 
            
            
              | 108 | 
                  subtest 'marc-in-json encoding tests' => sub { | 
            
            
              | 109 | 
               | 
            
            
              | 110 | 
                      plan tests => 3;  | 
            
            
              | 111 | 
               | 
            
            
              | 112 | 
                      my $title_with_diacritics = "L'insoutenable légèreté de l'être";  | 
            
            
              | 113 | 
               | 
            
            
              | 114 | 
                      my $biblio = $builder->build_sample_biblio(  | 
            
            
              | 115 | 
                          { | 
            
            
              | 116 | 
                              title  => $title_with_diacritics,  | 
            
            
              | 117 | 
                              author => "Milan Kundera"  | 
            
            
              | 118 | 
                          }  | 
            
            
              | 119 | 
                      );  | 
            
            
              | 120 | 
               | 
            
            
              | 121 | 
                      DelBiblio( $biblio->id );  | 
            
            
              | 122 | 
               | 
            
            
              | 123 | 
                      my $result = $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 124 | 
                              . $biblio->biblionumber => { Accept => 'application/marc-in-json' } )->status_is(200)->tx->res->body; | 
            
            
              | 125 | 
               | 
            
            
              | 126 | 
                      my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics );  | 
            
            
              | 127 | 
               | 
            
            
              | 128 | 
                      like( $result, qr/\Q$encoded_title/, "The title is not double encoded" );  | 
            
            
              | 129 | 
                  };  | 
            
            
              | 130 | 
               | 
            
            
              | 131 | 
                  subtest 'marcxml encoding tests' => sub { | 
            
            
              | 132 | 
                      plan tests => 3;  | 
            
            
              | 133 | 
               | 
            
            
              | 134 | 
                      my $marcflavour = C4::Context->preference('marcflavour'); | 
            
            
              | 135 | 
                      t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );  | 
            
            
              | 136 | 
               | 
            
            
              | 137 | 
                      my $title_with_diacritics = "L'insoutenable légèreté de l'être";  | 
            
            
              | 138 | 
               | 
            
            
              | 139 | 
                      my $biblio = $builder->build_sample_biblio(  | 
            
            
              | 140 | 
                          { | 
            
            
              | 141 | 
                              title  => $title_with_diacritics,  | 
            
            
              | 142 | 
                              author => "Milan Kundera"  | 
            
            
              | 143 | 
                          }  | 
            
            
              | 144 | 
                      );  | 
            
            
              | 145 | 
               | 
            
            
              | 146 | 
                      my $record = $biblio->metadata->record;  | 
            
            
              | 147 | 
                      $record->leader('     nam         3  4500'); | 
            
            
              | 148 | 
                      $biblio->metadata->metadata( $record->as_xml_record('UNIMARC') ); | 
            
            
              | 149 | 
                      $biblio->metadata->store;  | 
            
            
              | 150 | 
               | 
            
            
              | 151 | 
                      DelBiblio( $biblio->id );  | 
            
            
              | 152 | 
               | 
            
            
              | 153 | 
                      my $result = $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/"  | 
            
            
              | 154 | 
                              . $biblio->biblionumber => { Accept => 'application/marcxml+xml' } )->status_is(200)->tx->res->body; | 
            
            
              | 155 | 
               | 
            
            
              | 156 | 
                      my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics );  | 
            
            
              | 157 | 
               | 
            
            
              | 158 | 
                      like( $result, qr/\Q$encoded_title/, "The title is not double encoded" );  | 
            
            
              | 159 | 
                      t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );  | 
            
            
              | 160 | 
                  };  | 
            
            
              | 161 | 
               | 
            
            
              | 162 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 163 | 
              };  | 
            
            
              | 164 | 
               | 
            
            
              | 165 | 
              subtest 'list() tests' => sub { | 
            
            
              | 166 | 
               | 
            
            
              | 167 | 
                  plan tests => 17;  | 
            
            
              | 168 | 
               | 
            
            
              | 169 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 170 | 
               | 
            
            
              | 171 | 
                  my $patron = $builder->build_object(  | 
            
            
              | 172 | 
                      { | 
            
            
              | 173 | 
                          class => 'Koha::Patrons',  | 
            
            
              | 174 | 
                          value => { flags => 0 } | 
            
            
              | 175 | 
                      }  | 
            
            
              | 176 | 
                  );  | 
            
            
              | 177 | 
                  my $password = 'thePassword123';  | 
            
            
              | 178 | 
                  $patron->set_password( { password => $password, skip_validation => 1 } ); | 
            
            
              | 179 | 
                  $patron->discard_changes;  | 
            
            
              | 180 | 
                  my $userid = $patron->userid;  | 
            
            
              | 181 | 
               | 
            
            
              | 182 | 
                  t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );  | 
            
            
              | 183 | 
               | 
            
            
              | 184 | 
                  my $title_with_diacritics = "L'insoutenable légèreté de l'être";  | 
            
            
              | 185 | 
                  my $biblio                = $builder->build_sample_biblio(  | 
            
            
              | 186 | 
                      { | 
            
            
              | 187 | 
                          title  => $title_with_diacritics,  | 
            
            
              | 188 | 
                          author => "Milan Kundera",  | 
            
            
              | 189 | 
                      }  | 
            
            
              | 190 | 
                  );  | 
            
            
              | 191 | 
               | 
            
            
              | 192 | 
                  my $record = $biblio->metadata->record;  | 
            
            
              | 193 | 
                  $record->leader('     nam         3  4500'); | 
            
            
              | 194 | 
                  $biblio->metadata->metadata( $record->as_xml_record('UNIMARC') )->store; | 
            
            
              | 195 | 
               | 
            
            
              | 196 | 
                  my $biblio_id_1 = $biblio->id;  | 
            
            
              | 197 | 
               | 
            
            
              | 198 | 
                  t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );  | 
            
            
              | 199 | 
                  my $biblio_id_2 = $builder->build_sample_biblio->id;  | 
            
            
              | 200 | 
               | 
            
            
              | 201 | 
                  DelBiblio($biblio_id_1);  | 
            
            
              | 202 | 
                  DelBiblio($biblio_id_2);  | 
            
            
              | 203 | 
               | 
            
            
              | 204 | 
                  my $query = encode_json( [ { biblio_id => $biblio_id_1 }, { biblio_id => $biblio_id_2 } ] ); | 
            
            
              | 205 | 
               | 
            
            
              | 206 | 
                  $t->get_ok("//$userid:$password@/api/v1/deleted/biblios?q=$query")->status_is(403); | 
            
            
              | 207 | 
               | 
            
            
              | 208 | 
                  $patron->flags(4)->store;  | 
            
            
              | 209 | 
               | 
            
            
              | 210 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/weird+format' } ) | 
            
            
              | 211 | 
                      ->status_is(400);  | 
            
            
              | 212 | 
               | 
            
            
              | 213 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/json' } ) | 
            
            
              | 214 | 
                      ->status_is(200);  | 
            
            
              | 215 | 
               | 
            
            
              | 216 | 
                  my $result =  | 
            
            
              | 217 | 
                      $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marcxml+xml' } ) | 
            
            
              | 218 | 
                      ->status_is(200)->tx->res->body;  | 
            
            
              | 219 | 
               | 
            
            
              | 220 | 
                  my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics );  | 
            
            
              | 221 | 
                  like( $result, qr/\Q$encoded_title/, "The title is not double encoded" );  | 
            
            
              | 222 | 
               | 
            
            
              | 223 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marc-in-json' } ) | 
            
            
              | 224 | 
                      ->status_is(200);  | 
            
            
              | 225 | 
               | 
            
            
              | 226 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marc' } ) | 
            
            
              | 227 | 
                      ->status_is(200);  | 
            
            
              | 228 | 
               | 
            
            
              | 229 | 
                  $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'text/plain' } )->status_is(200); | 
            
            
              | 230 | 
               | 
            
            
              | 231 | 
                  # DELETE any biblio with ISBN = TOMAS  | 
            
            
              | 232 | 
                  Koha::Biblios->search( { 'biblioitem.isbn' => 'TOMAS' }, { join => ['biblioitem'] } )->delete; | 
            
            
              | 233 | 
               | 
            
            
              | 234 | 
                  my $isbn_query   = encode_json( { isbn => 'TOMAS' } ); | 
            
            
              | 235 | 
                  my $tomas_biblio = $builder->build_sample_biblio( { isbn => 'TOMAS' } ); | 
            
            
              | 236 | 
                  DelBiblio( $tomas_biblio->id );  | 
            
            
              | 237 | 
                  $t->get_ok( "//$userid:$password@/api/v1/biblios?q=$isbn_query" => { Accept => 'text/plain' } )->status_is(200); | 
            
            
              | 238 | 
               | 
            
            
              | 239 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 240 | 
              };  |