3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 4;
22 use t::lib::TestBuilder;
26 use Koha::ImportBatchProfiles;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new();
31 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33 my $t = Test::Mojo->new('Koha::REST::V1');
35 subtest 'list profiles' => sub {
38 $schema->storage->txn_begin;
40 Koha::ImportBatchProfiles->search()->delete;
42 my $ibp1 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'a_ibp' } });
43 my $ibp2 = $builder->build_object({ class => 'Koha::ImportBatchProfiles', value => { name => 'b_ibp' } });
45 my $patron = $builder->build_object(
47 class => 'Koha::Patrons',
54 my $pwd = 'thePassword123';
55 $patron->set_password( { password => $pwd, skip_validation => 1 } );
57 my $uid = $patron->userid;
59 $t->get_ok("//$uid:$pwd@/api/v1/import_batch_profiles?_order_by=+name")
61 ->json_is('/0/name', $ibp1->name)
62 ->json_is('/1/name', $ibp2->name);
65 $schema->storage->txn_rollback;
69 subtest 'add profile' => sub {
72 $schema->storage->txn_begin;
74 Koha::ImportBatchProfiles->search()->delete;
76 my $patron = $builder->build_object(
78 class => 'Koha::Patrons',
85 my $pwd = 'thePassword123';
86 $patron->set_password( { password => $pwd, skip_validation => 1 } );
88 my $uid = $patron->userid;
90 name => 'profileName',
91 overlay_action => 'overlay_action'
93 $t->post_ok("//$uid:$pwd@/api/v1/import_batch_profiles", json => $post_data)
95 ->json_has('/profile_id')
96 ->json_is('/name', $post_data->{name})
97 ->json_is('/overlay_action', $post_data->{overlay_action});
100 $schema->storage->txn_rollback;
104 subtest 'edit profile' => sub {
107 $schema->storage->txn_begin;
109 Koha::ImportBatchProfiles->search()->delete;
111 my $patron = $builder->build_object(
113 class => 'Koha::Patrons',
120 my $pwd = 'thePassword123';
121 $patron->set_password( { password => $pwd, skip_validation => 1 } );
123 my $uid = $patron->userid;
125 my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles', value => { name => 'someProfile' }});
131 $t->put_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id, json => $post_data)
133 ->json_is('/profile_id', $ibp->id)
134 ->json_is('/name', $post_data->{name});
136 $ibp->discard_changes;
138 is($ibp->name, $post_data->{name}, 'profile name should be the updated one');
141 $schema->storage->txn_rollback;
145 subtest 'delete profile' => sub {
148 $schema->storage->txn_begin;
150 Koha::ImportBatchProfiles->search()->delete;
152 my $patron = $builder->build_object(
154 class => 'Koha::Patrons',
161 my $pwd = 'thePassword123';
162 $patron->set_password( { password => $pwd, skip_validation => 1 } );
164 my $uid = $patron->userid;
166 my $ibp = $builder->build_object({class => 'Koha::ImportBatchProfiles'});
168 $t->delete_ok("//$uid:$pwd@/api/v1/import_batch_profiles/".$ibp->id)
171 my $search = Koha::ImportBatchProfiles->find($ibp->id);
173 is($search, undef, 'profile should be erased');
176 $schema->storage->txn_rollback;