Skip to content

Commit dfac7e5

Browse files
Merge branch '6.0/article-search-max-results' into 6.0-trunk
2 parents 26f2b00 + 63cfb19 commit dfac7e5

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

share/html/Helpers/Autocomplete/Articles

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,21 @@ $articles->Limit(
8989
SUBCLAUSE => 'excludeautocomplete'
9090
) if @not_in;
9191

92+
my $count = $articles->CountAll;
9293
my @suggestions;
93-
while (my $a = $articles->Next) {
94-
next if $right and not $a->CurrentUserHasRight($right);
95-
my $value = $a->$return;
96-
my $suggestion = { label => $a->Name, value => $value };
97-
$m->callback( CallbackName => "ModifySuggestion", suggestion => $suggestion, article => $a );
98-
push @suggestions, $suggestion;
94+
# RT::Articles->AddRecord filters by ShowArticle right, so some DB records
95+
# may be skipped. Keep paging until we collect max visible articles.
96+
OUTER: while (@suggestions < $max) {
97+
while (my $a = $articles->Next) {
98+
next if $right and not $a->CurrentUserHasRight($right);
99+
my $value = $a->$return;
100+
my $suggestion = { label => $a->Name, value => $value };
101+
$m->callback( CallbackName => "ModifySuggestion", suggestion => $suggestion, article => $a );
102+
push @suggestions, $suggestion;
103+
last OUTER if @suggestions >= $max;
104+
}
105+
$articles->NextPage;
106+
last if $articles->FirstRow >= $count;
99107
}
100108
return @suggestions if defined wantarray;
101109
</%INIT>

t/web/case-sensitivity.t

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,64 @@ my $cf;
182182
is_deeply( JSON::from_json( $m->content ), $result, 'Found by Content' );
183183
}
184184

185+
$m->logout;
186+
187+
# Create a user with limited rights
188+
my $user = RT::User->new(RT->SystemUser);
189+
my ($user_id, $user_msg) = $user->Create(
190+
Name => 'limited_user_' . $$,
191+
Password => 'password',
192+
Privileged => 1,
193+
);
194+
ok($user_id, "Created limited user: $user_msg");
195+
$m->login($user->Name, 'password');
196+
197+
# test articles autocomplete with rights-based filtering and max limit
198+
{
199+
# Create a second article class
200+
my $class1 = RT::Class->new(RT->SystemUser);
201+
my ($class1_id, $class1_msg) = $class1->Create(
202+
Name => 'Visible-' . $$,
203+
Description => 'Visible class',
204+
);
205+
ok($class1_id, "Created visible class: $class1_msg");
206+
207+
my $class2 = RT::Class->new(RT->SystemUser);
208+
my ($class2_id, $class2_msg) = $class2->Create(
209+
Name => 'Hidden-' . $$,
210+
Description => 'Hidden class',
211+
);
212+
ok($class2_id, "Created hidden class: $class2_msg");
213+
214+
# Grant ShowArticle right only on the visible class
215+
my ($ok, $msg) = $user->PrincipalObj->GrantRight(
216+
Right => 'ShowArticle',
217+
Object => $class1,
218+
);
219+
ok($ok, "Granted ShowArticle on visible class: $msg");
220+
221+
# Create 3 articles with the same search term
222+
# Article 1 is visible, article 2 is hidden, article 3 is visible
223+
my @articles;
224+
my @classes = ($class1, $class2, $class1);
225+
for my $i (1..3) {
226+
my $art = RT::Article->new(RT->SystemUser);
227+
my ($id, $art_msg) = $art->Create(
228+
Class => $classes[$i-1]->Name,
229+
Name => "MaxResults Article $i " . $$,
230+
Summary => "MaxResults test article $i",
231+
);
232+
ok($id, "Created article $i: $art_msg");
233+
push @articles, $art;
234+
}
235+
236+
# Test with max=2: should get 2 results from the 2 visible articles
237+
$m->get_ok('/Helpers/Autocomplete/Articles?return=id&term=MaxResults&max=2');
238+
my $results = JSON::from_json($m->content);
239+
240+
is(scalar @$results, 2, 'Returns 2 results when max=2 and 2 visible articles exist');
241+
is($results->[0]->{value}, $articles[0]->id, 'First visible article is returned');
242+
is($results->[1]->{value}, $articles[2]->id, 'Third article (second visible) is returned');
243+
}
244+
185245
done_testing();

0 commit comments

Comments
 (0)