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

(-)a/t/db_dependent/api/v1/patrons.t (-2 / +153 lines)
Lines 18-23 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 7;
20
use Test::More tests => 7;
21
use Test::MockModule;
21
use Test::Mojo;
22
use Test::Mojo;
22
use Test::Warn;
23
use Test::Warn;
23
24
Lines 26-31 use t::lib::Mocks; Link Here
26
27
27
use C4::Auth;
28
use C4::Auth;
28
use Koha::Database;
29
use Koha::Database;
30
use Koha::Exceptions::Patron;
31
use Koha::Exceptions::Patron::Attribute;
32
use Koha::Patron::Attributes;
29
use Koha::Patron::Debarments qw/AddDebarment/;
33
use Koha::Patron::Debarments qw/AddDebarment/;
30
34
31
my $schema  = Koha::Database->new->schema;
35
my $schema  = Koha::Database->new->schema;
Lines 127-136 subtest 'add() tests' => sub { Link Here
127
    $schema->storage->txn_rollback;
131
    $schema->storage->txn_rollback;
128
132
129
    subtest 'librarian access tests' => sub {
133
    subtest 'librarian access tests' => sub {
130
        plan tests => 21;
134
        plan tests => 22;
131
135
132
        $schema->storage->txn_begin;
136
        $schema->storage->txn_begin;
133
137
138
        my $extended_attrs_exception;
139
        my $type = 'hey';
140
        my $code = 'ho';
141
        my $attr = "Let's go";
142
143
        # Mock early, so existing mandatory attributes don't break all the tests
144
        my $mocked_patron = Test::MockModule->new('Koha::Patron');
145
        $mocked_patron->mock(
146
            'extended_attributes',
147
            sub {
148
149
                if ($extended_attrs_exception) {
150
                    if ( $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::NonRepeatable'
151
                        or $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint'
152
                      )
153
                    {
154
                        $extended_attrs_exception->throw(
155
                            attribute => Koha::Patron::Attribute->new(
156
                                { code => $code, attribute => $attr }
157
                            )
158
                        );
159
                    }
160
                    else {
161
                        $extended_attrs_exception->throw( type => $type );
162
                    }
163
                }
164
                return [];
165
            }
166
        );
167
134
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
168
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
135
        my $newpatron = $patron->to_api;
169
        my $newpatron = $patron->to_api;
136
        # delete RO attributes
170
        # delete RO attributes
Lines 210-215 subtest 'add() tests' => sub { Link Here
210
              ->json_like( '/conflict' => qr/(borrowers\.)?cardnumber/ ); }
244
              ->json_like( '/conflict' => qr/(borrowers\.)?cardnumber/ ); }
211
            qr/DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key '(borrowers\.)?cardnumber'/;
245
            qr/DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key '(borrowers\.)?cardnumber'/;
212
246
247
        subtest 'extended_attributes handling tests' => sub {
248
249
            plan tests => 19;
250
251
            my $patrons_count = Koha::Patrons->search->count;
252
253
            $extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
254
            $t->post_ok(
255
                "//$userid:$password@/api/v1/patrons" => json => {
256
                    "firstname"   => "Katrina",
257
                    "surname"     => "Fischer",
258
                    "address"     => "Somewhere",
259
                    "category_id" => "ST",
260
                    "city"        => "Konstanz",
261
                    "library_id"  => "MPL"
262
                }
263
            )->status_is(400)
264
              ->json_is( '/error' =>
265
                  "Missing mandatory extended attribute (type=$type)" );
266
267
            is( Koha::Patrons->search->count, $patrons_count, 'No patron added' );
268
269
            $extended_attrs_exception = 'Koha::Exceptions::Patron::Attribute::InvalidType';
270
            $t->post_ok(
271
                "//$userid:$password@/api/v1/patrons" => json => {
272
                    "firstname"   => "Katrina",
273
                    "surname"     => "Fischer",
274
                    "address"     => "Somewhere",
275
                    "category_id" => "ST",
276
                    "city"        => "Konstanz",
277
                    "library_id"  => "MPL"
278
                }
279
            )->status_is(400)
280
              ->json_is( '/error' =>
281
                  "Tried to use an invalid attribute type. type=$type" );
282
283
            is( Koha::Patrons->search->count, $patrons_count, 'No patron added' );
284
285
            $extended_attrs_exception = 'Koha::Exceptions::Patron::Attribute::NonRepeatable';
286
            $t->post_ok(
287
                "//$userid:$password@/api/v1/patrons" => json => {
288
                    "firstname"   => "Katrina",
289
                    "surname"     => "Fischer",
290
                    "address"     => "Somewhere",
291
                    "category_id" => "ST",
292
                    "city"        => "Konstanz",
293
                    "library_id"  => "MPL"
294
                }
295
            )->status_is(400)
296
              ->json_is( '/error' =>
297
                  "Tried to add more than one non-repeatable attributes. type=$code value=$attr" );
298
299
            is( Koha::Patrons->search->count, $patrons_count, 'No patron added' );
300
301
            $extended_attrs_exception = 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint';
302
            $t->post_ok(
303
                "//$userid:$password@/api/v1/patrons" => json => {
304
                    "firstname"   => "Katrina",
305
                    "surname"     => "Fischer",
306
                    "address"     => "Somewhere",
307
                    "category_id" => "ST",
308
                    "city"        => "Konstanz",
309
                    "library_id"  => "MPL"
310
                }
311
            )->status_is(400)
312
              ->json_is( '/error' =>
313
                  "Your action breaks a unique constraint on the attribute. type=$code value=$attr" );
314
315
            is( Koha::Patrons->search->count, $patrons_count, 'No patron added' );
316
317
            $mocked_patron->unmock('extended_attributes');
318
            # Temporarily get rid of mandatory attribute types
319
            Koha::Patron::Attribute::Types->search({ mandatory => 1 })->delete;
320
            # Create a couple attribute attribute types
321
            my $repeatable_1 = $builder->build_object(
322
                {
323
                    class => 'Koha::Patron::Attribute::Types',
324
                    value => {
325
                        mandatory     => 0,
326
                        repeatable    => 1,
327
                        unique        => 0,
328
                        category_code => 'ST'
329
                    }
330
                }
331
            );
332
            my $repeatable_2 = $builder->build_object(
333
                {
334
                    class => 'Koha::Patron::Attribute::Types',
335
                    value => {
336
                        mandatory     => 0,
337
                        repeatable    => 1,
338
                        unique        => 0,
339
                        category_code => 'ST'
340
                    }
341
                }
342
            );
343
344
            my $patron_id = $t->post_ok(
345
                "//$userid:$password@/api/v1/patrons" => json => {
346
                    "firstname"   => "Katrina",
347
                    "surname"     => "Fischer",
348
                    "address"     => "Somewhere",
349
                    "category_id" => "ST",
350
                    "city"        => "Konstanz",
351
                    "library_id"  => "MPL",
352
                    "extended_attributes" => [
353
                        { type => $repeatable_1->code, value => 'a' },
354
                        { type => $repeatable_1->code, value => 'b' },
355
                        { type => $repeatable_1->code, value => 'c' },
356
                        { type => $repeatable_2->code, value => 'd' },
357
                        { type => $repeatable_2->code, value => 'e' }
358
                    ]
359
                }
360
            )->status_is(201, 'Patron added')->tx->res->json->{patron_id};
361
            my $extended_attributes = join( ' ', sort map {$_->attribute} Koha::Patrons->find($patron_id)->extended_attributes->as_list);
362
            is( $extended_attributes, 'a b c d e', 'Extended attributes are stored correctly');
363
        };
364
213
        $schema->storage->txn_rollback;
365
        $schema->storage->txn_rollback;
214
    };
366
    };
215
};
367
};
216
- 

Return to bug 28002