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

(-)a/C4/Items.pm (-4 / +13 lines)
Lines 2645-2656 sub _SearchItems_build_where_fragment { Link Here
2645
2645
2646
=head2 SearchItems
2646
=head2 SearchItems
2647
2647
2648
    my ($items, $total) = SearchItemsByField($filters, $params);
2648
    my ($items, $total) = SearchItems($filter, $params);
2649
2649
2650
Perform a search among items
2650
Perform a search among items
2651
2651
2652
$filters is a reference to an array of filters, where each filter is a hash with
2652
$filter is a reference to a hash which can be a filter, or a combination of filters.
2653
the following keys:
2653
2654
A filter has the following keys:
2654
2655
2655
=over 2
2656
=over 2
2656
2657
Lines 2662-2668 the following keys: Link Here
2662
2663
2663
=back
2664
=back
2664
2665
2665
A logical AND is used to combine filters.
2666
A combination of filters hash the following keys:
2667
2668
=over 2
2669
2670
=item * conjunction: 'AND' or 'OR'
2671
2672
=item * filters: array ref of filters
2673
2674
=back
2666
2675
2667
$params is a reference to a hash that can contain the following parameters:
2676
$params is a reference to a hash that can contain the following parameters:
2668
2677
(-)a/Koha/Item/Search/Field.pm (+4 lines)
Lines 19-24 sub AddItemSearchField { Link Here
19
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
19
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
20
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
20
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
21
21
22
    return unless ($name and $label and $tagfield);
23
22
    my $dbh = C4::Context->dbh;
24
    my $dbh = C4::Context->dbh;
23
    my $query = q{
25
    my $query = q{
24
        INSERT INTO items_search_fields (name, label, tagfield, tagsubfield, authorised_values_category)
26
        INSERT INTO items_search_fields (name, label, tagfield, tagsubfield, authorised_values_category)
Lines 36-41 sub ModItemSearchField { Link Here
36
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
38
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
37
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
39
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
38
40
41
    return unless ($name and $label and $tagfield);
42
39
    my $dbh = C4::Context->dbh;
43
    my $dbh = C4::Context->dbh;
40
    my $query = q{
44
    my $query = q{
41
        UPDATE items_search_fields
45
        UPDATE items_search_fields
(-)a/t/db_dependent/Items.t (-1 / +126 lines)
Lines 20-27 use Modern::Perl; Link Here
20
20
21
use MARC::Record;
21
use MARC::Record;
22
use C4::Biblio;
22
use C4::Biblio;
23
use C4::Branch;
23
24
24
use Test::More tests => 3;
25
use Test::More tests => 4;
25
26
26
BEGIN {
27
BEGIN {
27
    use_ok('C4::Items');
28
    use_ok('C4::Items');
Lines 37-44 subtest 'General Add, Get and Del tests' => sub { Link Here
37
    $dbh->{AutoCommit} = 0;
38
    $dbh->{AutoCommit} = 0;
38
    $dbh->{RaiseError} = 1;
39
    $dbh->{RaiseError} = 1;
39
40
41
40
    # Helper biblio.
42
    # Helper biblio.
41
    diag("Creating biblio instance for testing.");
43
    diag("Creating biblio instance for testing.");
44
    C4::Context->set_preference('marcflavour', 'MARC21');
42
    my ($bibnum, $bibitemnum) = get_biblio();
45
    my ($bibnum, $bibitemnum) = get_biblio();
43
46
44
    # Add an item.
47
    # Add an item.
Lines 75-82 subtest 'GetHiddenItemnumbers tests' => sub { Link Here
75
    $dbh->{RaiseError} = 1;
78
    $dbh->{RaiseError} = 1;
76
79
77
    # Create a new biblio
80
    # Create a new biblio
81
    C4::Context->set_preference('marcflavour', 'MARC21');
78
    my ($biblionumber, $biblioitemnumber) = get_biblio();
82
    my ($biblionumber, $biblioitemnumber) = get_biblio();
79
83
84
    # Add branches if they don't exist
85
    if (not defined GetBranchDetail('CPL')) {
86
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
87
    }
88
    if (not defined GetBranchDetail('MPL')) {
89
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
90
    }
91
80
    # Add two items
92
    # Add two items
81
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
93
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
82
            { homebranch => 'CPL',
94
            { homebranch => 'CPL',
Lines 142-147 subtest 'GetHiddenItemnumbers tests' => sub { Link Here
142
    $dbh->rollback;
154
    $dbh->rollback;
143
};
155
};
144
156
157
subtest 'SearchItems test' => sub {
158
    plan tests => 10;
159
160
    # Start transaction
161
    $dbh->{AutoCommit} = 0;
162
    $dbh->{RaiseError} = 1;
163
164
    C4::Context->set_preference('marcflavour', 'MARC21');
165
    my ($biblionumber) = get_biblio();
166
167
    # Add branches if they don't exist
168
    if (not defined GetBranchDetail('CPL')) {
169
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
170
    }
171
    if (not defined GetBranchDetail('MPL')) {
172
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
173
    }
174
175
    my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
176
177
    # Add two items
178
    my (undef, undef, $item1_itemnumber) = AddItem({
179
        homebranch => 'CPL',
180
        holdingbranch => 'CPL',
181
    }, $biblionumber);
182
    my (undef, undef, $item2_itemnumber) = AddItem({
183
        homebranch => 'MPL',
184
        holdingbranch => 'MPL',
185
    }, $biblionumber);
186
187
    my ($items, $total_results);
188
189
    ($items, $total_results) = SearchItems();
190
    is($total_results, $initial_items_count + 2, "Created 2 new items");
191
    is(scalar @$items, $total_results, "SearchItems() returns all items");
192
193
    ($items, $total_results) = SearchItems(undef, {rows => 1});
194
    is($total_results, $initial_items_count + 2);
195
    is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
196
197
    # Search all items where homebranch = 'CPL'
198
    my $filter = {
199
        field => 'homebranch',
200
        query => 'CPL',
201
        operator => '=',
202
    };
203
    ($items, $total_results) = SearchItems($filter);
204
    ok($total_results > 0, "There is at least one CPL item");
205
    my $all_items_are_CPL = 1;
206
    foreach my $item (@$items) {
207
        if ($item->{homebranch} ne 'CPL') {
208
            $all_items_are_CPL = 0;
209
            last;
210
        }
211
    }
212
    ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
213
214
    # Search all items where homebranch != 'CPL'
215
    $filter = {
216
        field => 'homebranch',
217
        query => 'CPL',
218
        operator => '!=',
219
    };
220
    ($items, $total_results) = SearchItems($filter);
221
    ok($total_results > 0, "There is at least one non-CPL item");
222
    my $all_items_are_not_CPL = 1;
223
    foreach my $item (@$items) {
224
        if ($item->{homebranch} eq 'CPL') {
225
            $all_items_are_not_CPL = 0;
226
            last;
227
        }
228
    }
229
    ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
230
231
    # Search all items where biblio title (245$a) is like 'Silence in the %'
232
    $filter = {
233
        field => 'marc:245$a',
234
        query => 'Silence in the %',
235
        operator => 'like',
236
    };
237
    ($items, $total_results) = SearchItems($filter);
238
    ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
239
240
    # Search all items where biblio title is 'Silence in the library'
241
    # and homebranch is 'CPL'
242
    $filter = {
243
        conjunction => 'AND',
244
        filters => [
245
            {
246
                field => 'marc:245$a',
247
                query => 'Silence in the %',
248
                operator => 'like',
249
            },
250
            {
251
                field => 'homebranch',
252
                query => 'CPL',
253
                operator => '=',
254
            },
255
        ],
256
    };
257
    ($items, $total_results) = SearchItems($filter);
258
    my $found = 0;
259
    foreach my $item (@$items) {
260
        if ($item->{itemnumber} == $item1_itemnumber) {
261
            $found = 1;
262
            last;
263
        }
264
    }
265
    ok($found, "item1 found");
266
267
    $dbh->rollback;
268
};
269
145
# Helper method to set up a Biblio.
270
# Helper method to set up a Biblio.
146
sub get_biblio {
271
sub get_biblio {
147
    my $bib = MARC::Record->new();
272
    my $bib = MARC::Record->new();
(-)a/t/db_dependent/Koha/Item/Search/Field.t (+39 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 11;
5
6
use C4::Context;
7
8
use_ok('Koha::Item::Search::Field');
9
import Koha::Item::Search::Field qw(AddItemSearchField ModItemSearchField
10
    DelItemSearchField GetItemSearchField GetItemSearchFields);
11
12
my $dbh = C4::Context->dbh;
13
$dbh->{AutoCommit} = 0;
14
15
# Start with empty table.
16
foreach my $field (GetItemSearchFields()) {
17
    DelItemSearchField($field->{name});
18
}
19
is(scalar GetItemSearchFields(), 0, "No existing search field");
20
21
# Missing keys in hashref parameter, so returns undef
22
ok(not (defined AddItemSearchField()), "missing keys => fail");
23
ok(not (defined AddItemSearchField({})), "missing keys => fail");
24
ok(not (defined AddItemSearchField({name => 'foo'})), "missing keys => fail");
25
ok(not (defined AddItemSearchField({name => 'foo', label => 'Foo'})), "missing keys => fail");
26
27
# Success, the field hashref is returned
28
ok('HASH' eq ref AddItemSearchField({name => 'foo', label => 'Foo', tagfield => '001'}), "successful add");
29
30
# Check the table now contains one row.
31
is(scalar GetItemSearchFields(), 1, "Table now contains one row");
32
my $field = GetItemSearchField('foo');
33
is_deeply($field, {name => 'foo', label => 'Foo', tagfield => '001', tagsubfield => undef, authorised_values_category => undef});
34
35
ok((defined ModItemSearchField({name => 'foo', label => 'Foobar', tagfield => '100', 'tagsubfield' => 'a'})), "successful mod");
36
$field = GetItemSearchField('foo');
37
is_deeply($field, {name => 'foo', label => 'Foobar', tagfield => '100', tagsubfield => 'a', authorised_values_category => undef});
38
39
$dbh->rollback;
(-)a/t/db_dependent/SQLHelper.t (-2 / +6 lines)
Lines 10-16 use YAML; Link Here
10
use C4::Debug;
10
use C4::Debug;
11
use C4::SQLHelper qw(:all);
11
use C4::SQLHelper qw(:all);
12
12
13
use Test::More tests => 20;
13
use Test::More tests => 21;
14
14
15
use_ok('C4::SQLHelper');
15
use_ok('C4::SQLHelper');
16
16
Lines 63-65 $status=DeleteInTable("borrowers",{borrowernumber=>$borrtmp}); Link Here
63
ok($status>0 && !($@), "DeleteInTable OK");
63
ok($status>0 && !($@), "DeleteInTable OK");
64
$status=DeleteInTable("branches", {branchcode => 'ZZZZ'});
64
$status=DeleteInTable("branches", {branchcode => 'ZZZZ'});
65
ok($status>0 && !($@), "DeleteInTable (branch) OK");
65
ok($status>0 && !($@), "DeleteInTable (branch) OK");
66
- 
66
67
my @biblio_columns = C4::SQLHelper::GetColumns('biblio');
68
my @expected_columns = qw(biblionumber frameworkcode author title unititle notes
69
    serial seriestitle copyrightdate timestamp datecreated abstract);
70
is_deeply([sort @biblio_columns], [sort @expected_columns], "GetColumns('biblio') returns all columns of biblio table");

Return to bug 11425