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

(-)a/Koha/Authority.pm (-8 / +20 lines)
Lines 95-102 sub controlled_indicators { Link Here
95
        ? 'UNIMARCAUTH'
95
        ? 'UNIMARCAUTH'
96
        : 'MARC21';
96
        : 'MARC21';
97
    if( !$record ) {
97
    if( !$record ) {
98
        $record = MARC::Record->new_from_xml(
98
        $record = $self->record;
99
            $self->marcxml, 'UTF-8', $flavour );
100
    }
99
    }
101
100
102
    if( !$self->{_report_tag} ) {
101
    if( !$self->{_report_tag} ) {
Lines 125-136 Return a list of identifiers of the authors which are in 024$2$a Link Here
125
sub get_identifiers {
124
sub get_identifiers {
126
    my ( $self, $params ) = @_;
125
    my ( $self, $params ) = @_;
127
126
128
    my $flavour =
127
    my $record = $self->record;
129
      C4::Context->preference('marcflavour') eq 'UNIMARC'
130
      ? 'UNIMARCAUTH'
131
      : 'MARC21';
132
    my $record =
133
      MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour );
134
128
135
    my @identifiers;
129
    my @identifiers;
136
    for my $field ( $record->field('024') ) {
130
    for my $field ( $record->field('024') ) {
Lines 143-148 sub get_identifiers { Link Here
143
    return \@identifiers;
137
    return \@identifiers;
144
}
138
}
145
139
140
=head3 record
141
142
    my $record = $authority->record()
143
144
Return the MARC::Record for this authority
145
146
=cut
147
148
sub record {
149
    my ( $self ) = @_;
150
151
    my $flavour =
152
      C4::Context->preference('marcflavour') eq 'UNIMARC'
153
      ? 'UNIMARCAUTH'
154
      : 'MARC21';
155
    return MARC::Record->new_from_xml( $self->marcxml, 'UTF-8', $flavour );
156
}
157
146
=head2 Class Methods
158
=head2 Class Methods
147
159
148
=head3 type
160
=head3 type
(-)a/Koha/REST/V1/Authorities.pm (+102 lines)
Line 0 Link Here
1
package Koha::REST::V1::Authorities;
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 Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Authorities;
23
24
use List::MoreUtils qw( any );
25
use MARC::Record::MiJ;
26
27
use Try::Tiny qw( catch try );
28
29
=head1 API
30
31
=head2 Methods
32
33
=head3 get
34
35
Controller function that handles retrieving a single authority object
36
37
=cut
38
39
sub get {
40
    my $c = shift->openapi->valid_input or return;
41
42
    my $authority = Koha::Authorities->find( { authid => $c->validation->param('authority_id') } );
43
    unless ( $authority ) {
44
        return $c->render(
45
            status  => 404,
46
            openapi => {
47
                error => "Object not found."
48
            }
49
        );
50
    }
51
52
    return try {
53
54
        if ( $c->req->headers->accept =~ m/application\/json/ ) {
55
            return $c->render(
56
                status => 200,
57
                json   => $authority->to_api
58
            );
59
        }
60
        else {
61
            my $record = $authority->record;
62
63
            $c->respond_to(
64
                marcxml => {
65
                    status => 200,
66
                    format => 'marcxml',
67
                    text   => $record->as_xml_record
68
                },
69
                mij => {
70
                    status => 200,
71
                    format => 'mij',
72
                    data   => $record->to_mij
73
                },
74
                marc => {
75
                    status => 200,
76
                    format => 'marc',
77
                    text   => $record->as_usmarc
78
                },
79
                txt => {
80
                    status => 200,
81
                    format => 'text/plain',
82
                    text   => $record->as_formatted
83
                },
84
                any => {
85
                    status  => 406,
86
                    openapi => [
87
                        "application/json",
88
                        "application/marcxml+xml",
89
                        "application/marc-in-json",
90
                        "application/marc",
91
                        "text/plain"
92
                    ]
93
                }
94
            );
95
        }
96
    }
97
    catch {
98
        $c->unhandled_exception($_);
99
    };
100
}
101
102
1;
(-)a/api/v1/swagger/paths/authorities.yaml (+52 lines)
Line 0 Link Here
1
---
2
"/authorities/{authority_id}":
3
  get:
4
    x-mojo-to: Authorities#get
5
    operationId: getAuthority
6
    tags:
7
      - authorities
8
    summary: Get authority
9
    parameters:
10
      - $ref: "../swagger.yaml#/parameters/authority_id_pp"
11
    produces:
12
      - application/json
13
      - application/marcxml+xml
14
      - application/marc-in-json
15
      - application/marc
16
      - text/plain
17
    responses:
18
      "200":
19
        description: An authority
20
      "401":
21
        description: Authentication required
22
        schema:
23
          $ref: "../swagger.yaml#/definitions/error"
24
      "403":
25
        description: Access forbidden
26
        schema:
27
          $ref: "../swagger.yaml#/definitions/error"
28
      "404":
29
        description: Authority not found
30
        schema:
31
          $ref: "../swagger.yaml#/definitions/error"
32
      "406":
33
        description: Not acceptable
34
        schema:
35
          type: array
36
          description: Accepted content-types
37
          items:
38
            type: string
39
      "500":
40
        description: |
41
          Internal server error. Possible `error_code` attribute values:
42
43
          * `internal_server_error`
44
        schema:
45
          $ref: "../swagger.yaml#/definitions/error"
46
      "503":
47
        description: Under maintenance
48
        schema:
49
          $ref: "../swagger.yaml#/definitions/error"
50
    x-koha-authorization:
51
      permissions:
52
        catalogue: "1"
(-)a/api/v1/swagger/swagger.yaml (+8 lines)
Lines 137-142 paths: Link Here
137
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
137
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
138
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
138
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
139
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
139
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
140
  "/authorities/{authority_id}":
141
    $ref: paths/authorities.yaml#/~1authorities~1{authority_id}
140
  "/biblios/{biblio_id}":
142
  "/biblios/{biblio_id}":
141
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
143
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
142
  "/biblios/{biblio_id}/checkouts":
144
  "/biblios/{biblio_id}/checkouts":
Lines 336-341 parameters: Link Here
336
    name: agreement_period_id
338
    name: agreement_period_id
337
    required: true
339
    required: true
338
    type: integer
340
    type: integer
341
  authority_id_pp:
342
    description: Authority identifier
343
    in: path
344
    name: authority_id
345
    required: true
346
    type: integer
339
  identity_provider_id_pp:
347
  identity_provider_id_pp:
340
    description: Authentication provider internal identifier
348
    description: Authentication provider internal identifier
341
    in: path
349
    in: path
(-)a/t/db_dependent/Koha/Authorities.t (-1 / +37 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 8;
22
use Test::More tests => 9;
23
use MARC::Field;
23
use MARC::Field;
24
use MARC::File::XML;
24
use MARC::File::XML;
25
use MARC::Record;
25
use MARC::Record;
Lines 285-288 subtest 'get_identifiers' => sub { Link Here
285
    );
285
    );
286
};
286
};
287
287
288
subtest 'record tests' => sub {
289
    plan tests => 3;
290
291
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
292
    my $record = MARC::Record->new();
293
    $record->add_fields(
294
        [
295
            '100', ' ', ' ',
296
            a => 'Lastname, Firstname',
297
            b => 'b',
298
            c => 'c',
299
            i => 'i'
300
        ],
301
        [
302
            '024', '', '',
303
            a => '0000-0002-1234-5678',
304
            2 => 'orcid',
305
            6 => 'https://orcid.org/0000-0002-1234-5678'
306
        ],
307
        [
308
            '024', '', '',
309
            a => '01234567890',
310
            2 => 'scopus',
311
            6 => 'https://www.scopus.com/authid/detail.uri?authorId=01234567890'
312
        ],
313
    );
314
    my $authid = C4::AuthoritiesMarc::AddAuthority($record, undef, 'PERSO_NAME');
315
    my $authority = Koha::Authorities->find($authid);
316
    my $authority_record = $authority->record;
317
    is ($authority_record->field('100')->subfield('a'), 'Lastname, Firstname');
318
    my @fields_024 = $authority_record->field('024');
319
    is ($fields_024[0]->subfield('a'), '0000-0002-1234-5678');
320
    is ($fields_024[1]->subfield('a'), '01234567890');
321
322
};
323
288
$schema->storage->txn_rollback;
324
$schema->storage->txn_rollback;
(-)a/t/db_dependent/api/v1/authorities.t (-1 / +112 lines)
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 => 1;
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 C4::Auth;
32
33
use Koha::Authorities;
34
35
my $schema  = Koha::Database->new->schema;
36
my $builder = t::lib::TestBuilder->new;
37
38
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39
40
my $t = Test::Mojo->new('Koha::REST::V1');
41
42
subtest 'get() tests' => sub {
43
44
    plan tests => 20;
45
46
    $schema->storage->txn_begin;
47
48
    my $patron = $builder->build_object(
49
        {
50
            class => 'Koha::Patrons',
51
            value => { flags => 0 }
52
        }
53
    );
54
    my $password = 'thePassword123';
55
    $patron->set_password( { password => $password, skip_validation => 1 } );
56
    $patron->discard_changes;
57
    my $userid = $patron->userid;
58
59
    my $authority = $builder->build_object({ 'class' => 'Koha::Authorities', value => {
60
      marcxml => q|<?xml version="1.0" encoding="UTF-8"?>
61
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
62
    <controlfield tag="001">1001</controlfield>
63
    <datafield tag="110" ind1=" " ind2=" ">
64
        <subfield code="9">102</subfield>
65
        <subfield code="a">My Corporation</subfield>
66
    </datafield>
67
</record>|
68
    } });
69
70
    $t->get_ok("//$userid:$password@/api/v1/authorities/" . $authority->authid)
71
      ->status_is(403);
72
73
    $patron->flags(4)->store;
74
75
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
76
                => { Accept => 'application/weird+format' } )
77
      ->status_is(400);
78
79
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
80
                 => { Accept => 'application/json' } )
81
      ->status_is(200)
82
      ->json_is( '/authid', $authority->authid )
83
      ->json_is( '/authtypecode', $authority->authtypecode );
84
85
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
86
                 => { Accept => 'application/marcxml+xml' } )
87
      ->status_is(200);
88
89
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
90
                 => { Accept => 'application/marc-in-json' } )
91
      ->status_is(200);
92
93
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
94
                 => { Accept => 'application/marc' } )
95
      ->status_is(200);
96
97
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
98
                 => { Accept => 'text/plain' } )
99
      ->status_is(200)
100
      ->content_is(q|LDR 00079     2200049   4500
101
001     1001
102
110    _9102
103
       _aMy Corporation|);
104
105
    $authority->delete;
106
    $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->authid
107
                 => { Accept => 'application/marc' } )
108
      ->status_is(404)
109
      ->json_is( '/error', 'Object not found.' );
110
111
    $schema->storage->txn_rollback;
112
};

Return to bug 31794