Skip to content

Inject the CalDAV backend into BirthdayService - #329

Open
0z5a wants to merge 2 commits into
tchapi:mainfrom
0z5a:fix-328-birthday-backend
Open

0z5a wants to merge 2 commits into
tchapi:mainfrom
0z5a:fix-328-birthday-backend

Conversation

@0z5a

@0z5a 0z5a commented Nov 19, 2026 •

Copy link
Copy Markdown

Fixes #328.

I started from #328 and checked it against the code. BirthdayService writes the birthday events through $calendarBackend, which is only set by setBackend(). The DAV server sets it through BirthdayCalendarPlugin, and dav:sync-birthday-calendar sets it in its constructor, but AddressBookController calls syncUser() after saving and after deleting an address book without setting it. As soon as an included address book has a contact with a BDAY, updateCalendar() calls createCalendarObject() on null and the admin returns a 500. The address book itself is already saved at that point, but the birthday calendar is not updated.

Following review, this is fixed at the source: BirthdayService now gets its Sabre\CalDAV\Backend\PDO from the container as a constructor argument (a lazy service, so building it does not open the database connection), and setBackend() is removed. BirthdayCalendarPlugin and dav:sync-birthday-calendar no longer deal with the backend, and no caller has to remember to set it. The first version of this PR set the backend in the admin controller instead; that is reverted.

How I found it

I followed the stack trace from the issue to BirthdayService::updateCalendar() and looked for every setBackend() call. The existing testTheBirthdayCalendarFlagIsSaved turns the flag on too, but the test user's address book has no cards, so updateCalendar() is never reached. With one card that has a birthday, the same form submission gives the 500.

Where this can show up in practice

Anyone who uses the admin dashboard to turn on "Include in birthday calendar" for an address book that already has contacts with birthdays, which is the usual case for that option. Deleting another address book of the same user from the admin hits the same error. The birthday calendar then stays as it was until a card is changed over CardDAV or the sync command is run. This is my estimate beyond the report in #328.

Testing

  • Two tests in AddressBookControllerTest: saving the address book with a birthday contact and the flag on, and deleting a second address book of the same user. Both check for the redirect and for the birthday event. Against main both fail with Call to a member function createCalendarObject() on null.
  • New DavTest case: deleting a card through the /dav/ route removes its birthday event.
  • The service, plugin and command tests no longer call setBackend().
  • Full suite with vendor/bin/phpunit --process-isolation on PHP 8.6 with SQLite: 315 tests, 0 failures, 1 skipped (the LDAP-missing diagnostics test, since the extension is installed locally).
  • vendor/bin/psalm, php-cs-fixer and bin/console lint:container: clean. bin/console list still works when the database is unreachable.
  • Not run: the MySQL and PostgreSQL jobs, and PHP 8.2 to 8.4 from the CI matrix.

Saving or deleting an address book from the admin syncs the user's
birthday calendar, but the birthday service only gets its CalDAV
backend from the DAV server plugin and the sync command. With a
contact that has a birthday, the sync then failed with "Call to a
member function createCalendarObject() on null" and the admin
showed a 500.

Set the backend the same way the sync command does before both
syncUser() calls.

Fixes tchapi#328
@tchapi tchapi self-assigned this Nov 20, 2026
@tchapi tchapi added the bug Something isn't working label Nov 20, 2026
@tchapi

tchapi commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @0z5a

Rather than setting the backend in a third place, I'd rather fix it at the source I think: BirthdayService should get its backend from the container, so that no caller has to remember setBackend(). Something along these lines in config/services.yaml:

App\Services\BirthdayService:
    lazy: true # Building it opens the database connection, we don't want that
    arguments:
        # The sabre/dav backend
        $calendarBackend: !service
            class: Sabre\CalDAV\Backend\PDO
            arguments:
                - !service
                    class: PDO
                    factory: ['@doctrine.dbal.default_connection', 'getNativeConnection']
        $birthdayReminderOffset: "%birthday_reminder_offset%"

With that:

  • BirthdayService takes the backend as a constructor argument and setBackend() can be removed
  • BirthdayCalendarPlugin and SyncBirthdayCalendars no longer need to deal with the backend at all (since it's in the service)

WDYT?

The birthday service only got its CalDAV backend through setBackend(),
which the DAV server plugin and the sync command called, so any other
caller had to remember it. Get the backend from the container instead,
as a constructor argument, and remove setBackend(). The plugin and the
command no longer deal with the backend, and the admin address book
actions no longer need to set it.

The service is lazy: building the backend opens the database
connection, and the sync command is built on every console run.
@0z5a

0z5a commented Nov 23, 2026

Copy link
Copy Markdown
Author

Agreed, that's cleaner. Done in 63a9a6e, on top of the earlier commit:

  • BirthdayService now gets the Sabre\CalDAV\Backend\PDO through its constructor, and setBackend() is removed.
  • services.yaml is your sketch unchanged: lazy: true, plus an inline backend built on doctrine.dbal.default_connection's native connection. No code anywhere passes table names to the CalDAV backend, so it only needs the PDO.
  • BirthdayCalendarPlugin now takes just the service, and SyncBirthdayCalendars no longer touches the connection.
  • I reverted the admin controller setBackend() calls and removed the two getConnection entries in psalm-baseline.xml.

On 7.4, lazy: true works without proxy-manager-bridge. The flag is needed: the sync command gets built on every console run, so without it every bin/console call opens the DB. This was already true before, because the command called getNativeConnection() in its constructor. With the flag, bin/console list works even when DATABASE_URL is unreachable. Without it, the command fails with "Connection refused".

Tests:

  • The two admin address book tests for Admin UI: enabling a birthday calendar for address book returns 500 #328 are unchanged. They still fail against main with createCalendarObject() on null.
  • I removed setBackend() from the service, plugin and command tests.
  • I added a new DavTest case that deletes a card through the /dav/ route and checks that the birthday event is removed.

The full suite passes locally on SQLite with PHP 8.6: 315 tests, 0 failures. One test was skipped, the LDAP-missing diagnostics test, because the extension is installed here. Psalm, php-cs-fixer and lint:container are clean. I didn't run MySQL/PostgreSQL or PHP 8.2/8.3, so those are left to CI.

@0z5a 0z5a changed the title Set the birthday calendar backend in the admin address book actions Inject the CalDAV backend into BirthdayService Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin UI: enabling a birthday calendar for address book returns 500

2 participants