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

(-)a/C4/Utils/DataTables/VirtualShelves.pm (-2 / +13 lines)
Lines 63-70 sub search { Link Here
63
        push @args,       "%$shelfname%";
63
        push @args,       "%$shelfname%";
64
    }
64
    }
65
    if ( defined $owner and $owner ne '' ) {
65
    if ( defined $owner and $owner ne '' ) {
66
        push @where_strs, '( bo.firstname LIKE ? OR bo.surname LIKE ? )';
66
        $owner =~ s/^\s+|\s+$//g;    # Trim leading/trailing spaces
67
        push @args, "%$owner%", "%$owner%";
67
        $owner =~ s/\s+/ /g;         # Collapse multiple internal spaces to one
68
        my @name_parts = split ' ', $owner;
69
70
        if ( @name_parts == 2 ) {
71
            push @where_strs, '( bo.firstname LIKE ? AND bo.surname LIKE ? )';
72
            push @args, "%$name_parts[0]%", "%$name_parts[1]%";
73
74
        } else {
75
76
            push @where_strs, '(bo.firstname LIKE ? OR bo.surname LIKE ?)';
77
            push @args, "%$owner%", "%$owner%";
78
        }
68
    }
79
    }
69
    if ( defined $sortby and $sortby ne '' ) {
80
    if ( defined $sortby and $sortby ne '' ) {
70
        push @where_strs, 'sortfield = ?';
81
        push @where_strs, 'sortfield = ?';
(-)a/t/db_dependent/Virtualshelves.t (-1 / +65 lines)
Lines 129-134 subtest 'CRUD' => sub { Link Here
129
    teardown();
129
    teardown();
130
};
130
};
131
131
132
subtest 'Owner filter logic' => sub {
133
    plan tests => 16;
134
135
    my ( @where_strs, @args );
136
137
    my $test_logic = sub {
138
        my ($owner) = @_;
139
        @where_strs = ();
140
        @args       = ();
141
142
        if ( defined $owner and $owner ne '' ) {
143
            $owner =~ s/^\s+|\s+$//g;    # Trim leading/trailing spaces
144
            $owner =~ s/\s+/ /g;         # Normalize internal whitespace
145
146
            return ( [], [] ) if $owner eq '';
147
148
            my @name_parts = split ' ', $owner;
149
150
            if ( @name_parts == 2 ) {
151
                push @where_strs, '( bo.firstname LIKE ? AND bo.surname LIKE ? )';
152
                push @args, "%$name_parts[0]%", "%$name_parts[1]%";
153
            } else {
154
                push @where_strs, '(bo.firstname LIKE ? OR bo.surname LIKE ?)';
155
                push @args, "%$owner%", "%$owner%";
156
            }
157
        }
158
159
        return ( \@where_strs, \@args );
160
    };
161
162
    my ( $where, $args );
163
164
    ( $where, $args ) = $test_logic->(undef);
165
    is_deeply( $where, [], 'No where clause when owner is undef' );
166
    is_deeply( $args,  [], 'No args when owner is undef' );
167
168
    ( $where, $args ) = $test_logic->('');
169
    is_deeply( $where, [], 'No where clause when owner is empty' );
170
    is_deeply( $args,  [], 'No args when owner is empty' );
171
172
    ( $where, $args ) = $test_logic->('John Smith');
173
    is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'AND clause for two name parts' );
174
    is_deeply( $args,  [ '%John%', '%Smith%' ],                           'Args match both names' );
175
176
    ( $where, $args ) = $test_logic->('  John Smith  ');
177
    is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'Trimmed whitespace around two name parts' );
178
    is_deeply( $args,  [ '%John%', '%Smith%' ],                           'Args match trimmed names' );
179
180
    ( $where, $args ) = $test_logic->('John     Smith');
181
    is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'Normalized internal whitespace' );
182
    is_deeply( $args,  [ '%John%', '%Smith%' ],                           'Args match normalized names' );
183
184
    ( $where, $args ) = $test_logic->('John ');
185
    is_deeply( $where, ['(bo.firstname LIKE ? OR bo.surname LIKE ?)'], 'Single name with trailing space' );
186
    is_deeply( $args,  [ '%John%', '%John%' ],                         'Args match single name trimmed' );
187
188
    ( $where, $args ) = $test_logic->('   ');
189
    is_deeply( $where, [], 'No where clause when owner is only whitespace' );
190
    is_deeply( $args,  [], 'No args when owner is only whitespace' );
191
192
    ( $where, $args ) = $test_logic->('   John    ');
193
    is_deeply( $where, ['(bo.firstname LIKE ? OR bo.surname LIKE ?)'], 'Single name with leading/trailing spaces' );
194
    is_deeply( $args,  [ '%John%', '%John%' ],                         'Args match cleaned single name' );
195
};
196
132
subtest 'Sharing' => sub {
197
subtest 'Sharing' => sub {
133
    plan tests => 21;
198
    plan tests => 21;
134
    my $patron_wants_to_share = $builder->build(
199
    my $patron_wants_to_share = $builder->build(
135
- 

Return to bug 39427