[Security Solution] [KB Management] Fix sorting by name in the kb entries table (#209141)

Fixes https://github.com/elastic/kibana/issues/199253

## Summary

Fixes the issue where KB entries in knowledge base management were not
sorting correctly by name. Previously, entries with names starting with
uppercase letters appeared before those with lowercase names, instead of
following a case-insensitive alphabetical order. This update ensures
proper sorting regardless of letter case.

Before:
<img width="3120" alt="image"
src="https://github.com/user-attachments/assets/1657fce4-abba-4672-aaa8-8c5c6c660c98"
/>


After:
<img width="3120" alt="image"
src="https://github.com/user-attachments/assets/10d1f3fc-f6db-4b5b-b67b-8b8ae7250dd4"
/>



How to test:
- See instructions in the issue. Create uppercase and lowercase KB
entries (e.g. A,a,b,B,c,C. Create the entries in that order).
- Reorder the table based on name and check the ordering makes sense.


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [x] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Kenneth Kreindler 2025-02-11 10:18:02 +00:00 committed by GitHub
parent 9635cfa526
commit a1356ffb9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 109 additions and 1 deletions

View file

@ -222,6 +222,114 @@ describe('KnowledgeBaseSettingsManagement', () => {
});
});
it('renders entries in correct order', async () => {
(useKnowledgeBaseEntries as jest.Mock).mockReturnValue({
data: {
data: [
{
id: '1',
createdAt: '2024-10-21T18:54:14.773Z',
createdBy: 'u_user_id_1',
updatedAt: '2024-10-23T17:33:15.933Z',
updatedBy: 'u_user_id_1',
users: [{ name: 'Test User 1' }],
name: 'A',
namespace: 'default',
type: 'document',
kbResource: 'user',
source: 'user',
text: 'Very nice text',
},
{
id: '2',
createdAt: '2024-10-25T09:55:56.596Z',
createdBy: 'u_user_id_2',
updatedAt: '2024-10-25T09:55:56.596Z',
updatedBy: 'u_user_id_2',
users: [],
name: 'b',
namespace: 'default',
type: 'index',
index: 'index-1',
field: 'semantic_field1',
description: 'Test description',
queryDescription: 'Test query instruction',
},
{
id: '3',
createdAt: '2024-10-25T09:55:56.596Z',
createdBy: 'u_user_id_2',
updatedAt: '2024-10-25T09:55:56.596Z',
updatedBy: 'u_user_id_2',
users: [],
name: 'B',
namespace: 'default',
type: 'index',
index: 'index-1',
field: 'semantic_field1',
description: 'Test description',
queryDescription: 'Test query instruction',
},
{
id: '4',
createdAt: '2024-10-25T09:55:56.596Z',
createdBy: 'u_user_id_1',
updatedAt: '2024-10-25T09:55:56.596Z',
updatedBy: 'u_user_id_1',
users: [{ name: 'Test User 1' }],
name: 'a',
namespace: 'default',
type: 'index',
index: 'index-2',
field: 'semantic_field2',
description: 'Test description',
queryDescription: 'Test query instruction',
},
],
},
isFetching: false,
refetch: jest.fn(),
});
render(<KnowledgeBaseSettingsManagement dataViews={mockDataViews} />, {
wrapper: Wrapper,
});
await waitFor(() => {
expect(screen.getByTestId('knowledge-base-entries-table')).toBeInTheDocument();
expect(screen.getByText('A')).toBeInTheDocument();
expect(screen.getByText('B')).toBeInTheDocument();
expect(screen.getByText('a')).toBeInTheDocument();
expect(screen.getByText('b')).toBeInTheDocument();
});
// Order ascending
await userEvent.click(screen.getByText('Name'));
await waitFor(() => {
// Upper case letters should come before lower case letters
expect(screen.getByText('A').compareDocumentPosition(screen.getByText('a'))).toBe(4);
expect(screen.getByText('B').compareDocumentPosition(screen.getByText('b'))).toBe(4);
expect(screen.getByText('A').compareDocumentPosition(screen.getByText('B'))).toBe(4);
expect(screen.getByText('a').compareDocumentPosition(screen.getByText('B'))).toBe(4);
expect(screen.getByText('a').compareDocumentPosition(screen.getByText('b'))).toBe(4);
});
// Order decending
await userEvent.click(screen.getByText('Name'));
await waitFor(() => {
// Lower case letters should come before upper case letters
expect(screen.getByText('A').compareDocumentPosition(screen.getByText('a'))).toBe(2);
expect(screen.getByText('B').compareDocumentPosition(screen.getByText('b'))).toBe(2);
expect(screen.getByText('A').compareDocumentPosition(screen.getByText('B'))).toBe(2);
expect(screen.getByText('a').compareDocumentPosition(screen.getByText('B'))).toBe(2);
expect(screen.getByText('a').compareDocumentPosition(screen.getByText('b'))).toBe(2);
});
});
it('opens the flyout when add document button is clicked', async () => {
const openFlyoutMock = jest.fn();
(useFlyoutModalVisibility as jest.Mock).mockReturnValue({

View file

@ -168,7 +168,7 @@ export const useKnowledgeBaseTable = () => {
render: (entry: KnowledgeBaseEntryResponse) => (
<NameColumn entry={entry} existingIndices={existingIndices} />
),
sortable: ({ name }: KnowledgeBaseEntryResponse) => name,
sortable: ({ name }: KnowledgeBaseEntryResponse) => name.toLocaleLowerCase() + name, // Ensures that the sorting is case-insensitive and that the sorting is stable
width: '30%',
},
{