diff --git a/integ_tests/test_collection_sorting.py b/integ_tests/test_collection_sorting.py new file mode 100644 index 00000000..918496aa --- /dev/null +++ b/integ_tests/test_collection_sorting.py @@ -0,0 +1,117 @@ +# This file is part of Radicale - CalDAV and CardDAV server +# Copyright © 2026-2026 Max Berger +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Radicale. If not, see . + + +""" +Test for checking if the collections are sorted correctly. +""" + +import pathlib +import re +from typing import Any, Generator + +import pytest +from playwright.sync_api import Page, expect + +from integ_tests.common import SHARING_HTPASSWD, login, start_radicale_server + + +@pytest.fixture +def radicale_server(tmp_path: pathlib.Path) -> Generator[str, Any, None]: + yield from start_radicale_server(tmp_path, SHARING_HTPASSWD) + + +def create_named_collection(page: Page, name: str) -> None: + page.click('.fabcontainer a[data-name="new"]') + page.fill('#createcollectionscene input[data-name="displayname"]', name) + page.click('#createcollectionscene button[data-name="submit"]') + expect(page.locator("#createcollectionscene")).to_be_hidden() + + +def test_collection_sorting(page: Page, radicale_server: str) -> None: + config = SHARING_HTPASSWD + + # 1. Admin logs in and creates "Z" and "A" + login(page, radicale_server, config) + create_named_collection(page, "Z") + create_named_collection(page, "A") + + # 2. Admin shares "M" to "max" + create_named_collection(page, "M") + article_m = page.locator("article:not(.hidden)").filter( + has=page.locator("[data-name='title']", has_text="M") + ) + article_m.hover() + article_m.locator("a[data-name='share']").click(force=True) + + page.click('button[data-name="sharebymap"]') + page.locator('input[data-name="shareuser"]').fill(config.user_username) + page.locator('input[data-name="sharehref"]').fill("m-shared") + page.click('#createeditsharescene button[data-name="submit"]') + page.click('#sharecollectionscene button[data-name="cancel"]') + + # 3. Admin logs out + page.click('a[data-name="logout"]') + + # 4. Max logs in + page.fill('#loginscene input[data-name="user"]', config.user_username) + page.fill('#loginscene input[data-name="password"]', "userpassword") + page.click('button:has-text("Next")') + + # 5. Max creates his own "B" and "Y" + create_named_collection(page, "Y") + create_named_collection(page, "B") + + # 6. Max enables the shared collection "M" + page.click('a[data-name="incomingshares"]') + row = page.locator("tr[data-name='incomingsharerowtemplate']:not(.hidden)") + expect(row.locator("input[data-name='pathortoken']")).to_have_value( + re.compile("m-shared") + ) + row.locator("input[data-name='enabled']").check() + row.locator("input[data-name='shown']").check() + page.click('#incomingsharingscene button[data-name="close"]') + + # 7. Verify the order + # Expected: "B", "Y" (Owned), then "M" (Shared) + # Wait for articles to be rendered + expect(page.locator("article:not(.hidden) [data-name='title']")).to_have_count(3) + + titles = page.locator( + "article:not(.hidden) [data-name='title']" + ).all_text_contents() + titles = [t.strip() for t in titles if t.strip()] + + assert titles == ["B", "Y", "M"], f"Expected order ['B', 'Y', 'M'], got {titles}" + + # 8. Max creates "A" + create_named_collection(page, "A") + + # Wait for articles to be rendered (now 4) + expect(page.locator("article:not(.hidden) [data-name='title']")).to_have_count(4) + + titles = page.locator( + "article:not(.hidden) [data-name='title']" + ).all_text_contents() + titles = [t.strip() for t in titles if t.strip()] + + # Expected: "A", "B", "Y", then "M" + assert titles == [ + "A", + "B", + "Y", + "M", + ], f"Expected order ['A', 'B', 'Y', 'M'], got {titles}" diff --git a/radicale/web/internal_data/js/api/api.js b/radicale/web/internal_data/js/api/api.js index 788b6f2a..30ee46bb 100644 --- a/radicale/web/internal_data/js/api/api.js +++ b/radicale/web/internal_data/js/api/api.js @@ -103,11 +103,6 @@ export function get_collections(user, password, collection, callback) { collections.push(parsedCollection); } } - collections.sort(function (a, b) { - /** @type {string} */ let ca = a.displayname || a.href; - /** @type {string} */ let cb = b.displayname || b.href; - return ca.localeCompare(cb); - }); callback(collections, null); } else { callback(null, "No valid XML received"); diff --git a/radicale/web/internal_data/js/scenes/CollectionsScene.js b/radicale/web/internal_data/js/scenes/CollectionsScene.js index 2e7c0e3d..707be7c8 100644 --- a/radicale/web/internal_data/js/scenes/CollectionsScene.js +++ b/radicale/web/internal_data/js/scenes/CollectionsScene.js @@ -23,6 +23,7 @@ import { delete_collection } from "../api/api.js"; import { get_auth_header } from "../api/common.js"; import { Collection, CollectionType, Permission } from "../models/collection.js"; import { collectionsCache } from "../utils/collections_cache.js"; +import { extract_title } from "../utils/collection_utils.js"; import { ErrorHandler } from "../utils/error.js"; import { bytesToHumanReadable, completeHref, get_element, get_element_by_id } from "../utils/misc.js"; import { UrlTextHandler } from "../utils/url_text.js"; @@ -139,6 +140,23 @@ export class CollectionsScene { * @param {boolean} clear_error */ _show_collections(collections, shares, clear_error) { + collections.sort((a, b) => { + const getShare = (col) => (shares || []).find( + s => (s.ShareType === "map") && + decodeURIComponent(s.PathOrToken || "").replace(/\/+$/, "") === decodeURIComponent(col.href || "").replace(/\/+$/, "")); + + const shareA = getShare(a); + const shareB = getShare(b); + + const ownedA = !shareA || shareA.Owner === this._user; + const ownedB = !shareB || shareB.Owner === this._user; + + if (ownedA && !ownedB) return -1; + if (!ownedA && ownedB) return 1; + + return extract_title(a).localeCompare(extract_title(b)); + }); + /** @type {HTMLElement} */ let navBar = get_element(document, "#logoutview"); let heightOfNavBar = navBar.offsetHeight + "px"; this._html_scene.style.marginTop = heightOfNavBar; diff --git a/radicale/web/internal_data/js/utils/collection_utils.js b/radicale/web/internal_data/js/utils/collection_utils.js index 4e6d8695..a649513d 100644 --- a/radicale/web/internal_data/js/utils/collection_utils.js +++ b/radicale/web/internal_data/js/utils/collection_utils.js @@ -44,7 +44,7 @@ export function extractUsernameFromPrincipalCollection(principal_collection) { export function extract_title(collection) { if (collection.displayname && collection.displayname.length > 0) { return collection.displayname; - } else if (collection.type = CollectionType.PRINCIPAL) { + } else if (collection.type === CollectionType.PRINCIPAL) { return extractUsernameFromPrincipalCollection(collection) } else return decodeURIComponent(collection.href);