From fa45972c19e22e621b20e3299a4edeb03eefec9e Mon Sep 17 00:00:00 2001 From: Jaggob <37583151+Jaggob@users.noreply.github.com> Date: Sat, 26 Sep 2026 13:34:40 +0200 Subject: [PATCH 1/5] Let a file reach the pad its row names, and the one the row replaced A .pad file had to name its row's pad, or nothing opened. A restore that put a new pad in place of one that was gone or behind, and the recovery of a file without a row, left files behind that still named the old pad: an older version restored through Nextcloud's versions, or a file whose rewrite failed. They were refused for good, and the file's higher revision of the old pad made the row's pad look behind to the trash, the sweep and a restore. The row now remembers the pad it replaced (replaced_pad_id, a new column). BoundPadResolver decides for every open, read and sync: a file naming the row's pad reaches it as before; a file naming the replaced pad reaches the row's pad, with its revision counted as none; anything else is refused as before. An open that may write the file, and a sync, rewrite it to name the row's pad. The row never follows the file. sync-app.sh upgrades the app in the test stack so the migration runs. The docs of the integrity check say what it does: count rows without a file. --- docs/api-reference.md | 15 +- docs/architecture.md | 38 ++-- docs/legacy-ownpad-migration.md | 2 +- .../Version000004Date20260926120000.php | 44 +++++ lib/Service/Binding.php | 7 + lib/Service/BindingService.php | 39 ++-- lib/Service/BoundPadResolver.php | 98 +++++++++++ lib/Service/LifecycleService.php | 6 + lib/Service/LivePadHtmlFetcher.php | 8 +- lib/Service/OwedDeletions.php | 4 + lib/Service/PadFileService.php | 24 +++ lib/Service/PadOpenService.php | 29 ++- lib/Service/PadSyncService.php | 32 ++-- lib/Service/PublicPadContextService.php | 3 +- lib/Service/RestoreService.php | 9 +- lib/Service/SettleOnOpen.php | 18 +- tests/e2e/docker/sync-app.sh | 7 + tests/phpunit/Support/BuildsBoundPads.php | 35 ++++ .../phpunit/Support/InMemoryBindingTable.php | 13 +- tests/phpunit/Support/SettlesOnOpen.php | 10 +- tests/phpunit/Support/WiresTheLifecycle.php | 3 + tests/phpunit/unit/BindingServiceTest.php | 105 +++++++---- tests/phpunit/unit/BindingTest.php | 9 +- tests/phpunit/unit/BoundPadResolverTest.php | 166 ++++++++++++++++++ tests/phpunit/unit/LifecycleServiceTest.php | 40 +++++ tests/phpunit/unit/LivePadHtmlFetcherTest.php | 73 +++++--- tests/phpunit/unit/OwedDeletionsTest.php | 28 ++- .../unit/PadLifecycleControllerTest.php | 16 +- tests/phpunit/unit/PadOpenServiceTest.php | 121 +++++++++++-- .../phpunit/unit/PadSessionControllerTest.php | 16 +- tests/phpunit/unit/PadSyncServiceTest.php | 134 ++++++++++++-- .../unit/PublicPadContextServiceTest.php | 42 +++-- .../unit/PublicViewerControllerTest.php | 14 +- tests/phpunit/unit/RestoreServiceTest.php | 39 +++- tests/phpunit/unit/SettleOnOpenTest.php | 93 +++++----- 35 files changed, 1097 insertions(+), 243 deletions(-) create mode 100644 lib/Migration/Version000004Date20260926120000.php create mode 100644 lib/Service/BoundPadResolver.php create mode 100644 tests/phpunit/Support/BuildsBoundPads.php create mode 100644 tests/phpunit/unit/BoundPadResolverTest.php diff --git a/docs/api-reference.md b/docs/api-reference.md index 8f43b8c5..81d30dc5 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -175,14 +175,14 @@ solely by the separate external-pad policy, not by these two settings. - Controller: `PadSessionController::open` - Params: `file=/path/file.pad` - Result: secure open URL. - - Behavior: read-only (no auto-mutation of `.pad` metadata), CSRF-protected. + - Behavior: CSRF-protected. The file's row decides which pad opens. The `.pad` file is written only when it names the pad its row replaced and the user may write it: it then names the row's pad (see "Which pad a file reaches" in `docs/architecture.md`). - Protected mode: response includes one Etherpad session `Set-Cookie` header. - `POST /api/v1/pads/open-by-id` - Controller: `PadSessionController::openById` - Params: `fileId=` - Result: secure open URL via stable Nextcloud `fileId`. - - Behavior: read-only (no auto-mutation of `.pad` metadata), CSRF-protected. + - Behavior: CSRF-protected. The file's row decides which pad opens. The `.pad` file is written only when it names the pad its row replaced and the user may write it: it then names the row's pad (see "Which pad a file reaches" in `docs/architecture.md`). - Protected mode: response includes one Etherpad session `Set-Cookie` header. - `GET /api/v1/pads/content/{fileId}` @@ -390,14 +390,11 @@ solely by the separate external-pad policy, not by these two settings. - `POST /api/v1/admin/consistency-check` - Controller: `AdminController::consistencyCheck` - Auth: admin only - - Purpose: optional structural integrity check across binding table and `.pad` files. - - Result includes: + - Purpose: optional integrity check of the binding table: rows whose file is gone from the file cache. No `.pad` file is read. + - Result: + - `ok`, and a translated `message` that says whether anything was found - `binding_without_file_count` - - `file_without_binding_count` - - `invalid_frontmatter_count` - - `frontmatter_scanned` - - `frontmatter_skipped` - - `samples` (bounded debug sample lists per issue class) + - `samples.bindings_without_file`: up to 25 such rows (`file_id`, `pad_id`, `access_mode`, `state`) - `POST /api/v1/admin/settle-pending` - Controller: `AdminController::settlePending` diff --git a/docs/architecture.md b/docs/architecture.md index 7f009b5d..4e9bab36 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -14,6 +14,8 @@ Etherpad is the editing source of truth; the `.pad` file acts as binding storage - Owns mapping `file_id <-> pad_id` and states (`active`, `pending_delete`, `restore_pending`). - Hands a row out as a `Binding`, and a row the sweep takes as a `WaitingBinding` with its file's path; the sweep asks for deletions owed by `FileLocation`. - Only managed internal pads are bound. External pads are represented solely by `.pad` frontmatter and snapshots. +- `lib/Service/BoundPadResolver.php` + - Which pad a `.pad` file of this app's opens, reads and syncs: the one its row names (see "Which pad a file reaches"). - `lib/Service/LifecycleService.php` - Where a `.pad` file's trash and restore arrive, from the listeners and the API; the trash is done here (see Trash/Restore). - `lib/Service/RestoreService.php` @@ -43,10 +45,7 @@ Etherpad is the editing source of truth; the `.pad` file acts as binding storage - Guards creation only; existing pads of a disabled type keep working. - Resolves a template's access mode to an enabled one instead of failing. - `lib/Service/ConsistencyCheckService.php` - - Optional admin integrity scan: - - bindings without file - - `.pad` files without binding - - invalid/mismatching frontmatter on bound files + - Optional admin integrity check: binding rows whose file is gone from the file cache, counted, with a bounded sample. - `lib/Controller/ViewerController.php` - Compatibility redirect adapter: - resolves `.pad` path/id to stable Nextcloud files viewer URL. @@ -86,6 +85,7 @@ checked-in runtime assets in `js/`. - `deleted_at` - `created_at` - `updated_at` + - `replaced_pad_id` (migration `Version000004Date20260926120000`): the pad the row's pad replaced, when a restore or a recovery put a new one in its place (see "Which pad a file reaches"). Nextcloud runs the migration when the app's version rises; `tests/e2e/docker/sync-app.sh` runs it for a working tree synced into the test stack. - stores internal managed pads only; external `ext.*` rows from earlier development versions are removed by `Version000003Date20260512230000` - `.pad` file - Frontmatter: format, binding metadata, state, export metadata. @@ -96,6 +96,23 @@ checked-in runtime assets in `js/`. - `PadFileLockRetryService::putContentWithSyncLockRetry(...)` persists that content to the Nextcloud file. - Stored snapshots are read only by restore and by the forced-sync comparison; the read-only viewer no longer uses them at all (see `LivePadHtmlFetcher`). +## Which pad a file reaches + +A `.pad` file names a pad, and whoever may write the file can make it name any pad. Its row in `ep_pad_bindings` is written only by the server, so the row decides which of this app's pads the file opens, reads and syncs (`BoundPadResolver`). The row never follows the file, and the file follows the row only where the row can prove it replaced the pad the file names. + +- The file names the row's pad, in the row's access mode: that pad. +- The file names the pad the row replaced (`replaced_pad_id`): the row's pad. A restore that put a new pad in place of one that was gone or behind remembers the old one, and so does the recovery of a file without a row, which makes a new pad. A file that still names the old pad reaches the new one: an older version of it restored through Nextcloud's versions, or one whose rewrite failed. + - An open that may write the file rewrites it to name the row's pad, with the row's access mode and link and no snapshot revision, if the file still holds what the open read. It logs `A .pad file named the pad its row replaced; it now names the row's.` as a warning. A rewrite that fails is logged at debug, and the row's pad opens all the same. + - A sync writes the row's pad's snapshot into the file, under the row's pad, with the same line. + - Whatever only reads - an open without write permission, a public share, the read-only content view, sync status - leaves the file as it is, and says so at debug. +- The file names any other pad, or the row's pad in another access mode: refused, `Binding pad ID mismatch.` or `Binding access mode mismatch.` (see "Errors of the API"). This comes before the row's state is looked at, so such a file neither decides nor waits for a row that is not its own. + +A row remembers one pad, the one its latest change of pad replaced. A file that names a pad from before that is refused like any other. + +A file that names the pad its row replaced holds that pad's `snapshot_rev`, not one of the row's pad. Wherever the file's revision is held against the row's pad - sync and sync status, the trash, the sweep's rest of a trash, a restore - it counts as none: a higher revision of the old pad would make the row's pad look behind, and a trash would leave the old pad's text in the file and the row's pad over. Only then; a file that names the row's pad keeps its revision. + +Pads on another server have no row, and none of this applies to them: their frontmatter decides. The legacy migration is the only way a pad goes from a file into a row (`docs/legacy-ownpad-migration.md`); a restore and a recovery always make a new pad. + ## Main Flows ### 1) Create @@ -114,7 +131,7 @@ Primary flow (native viewer): 2. `src/viewer-main.js` resolves Etherpad open data via API: - preferred: `POST /api/v1/pads/open-by-id` (`fileId`, CSRF `requesttoken`) - fallback: `POST /api/v1/pads/open` (`file`, CSRF `requesttoken`) if no stable `fileId` is available -3. `PadSessionController` validates frontmatter/binding and resolves secure open URL: +3. `PadSessionController` validates the frontmatter, has the row decide which pad it opens (see "Which pad a file reaches"), and resolves a secure open URL: - `protected`: session URL via `PadSessionService` - external: validate the stored external URL and return a read-only snapshot/open target without DB binding lookup - `public`: direct/read-only URL as appropriate @@ -239,12 +256,12 @@ Primary flow (native viewer): - A file that cannot be read, for any reason, is one more way to have no fresh snapshot: the user's delete goes through. - No fresh snapshot, or the delete fails: the pad stays as it is and the row becomes `pending_delete`; Nextcloud's trash succeeds either way. A protected pad whose group Etherpad cannot list counts as a delete that failed, so the sweep asks once more; past the row it has taken by then, a group it still cannot list is given up and the pad goes alone. A delete through WebDAV (Files UI, clients) holds the file's lock while the trash is decided, so there it is always this path. The sweep finishes the trash on its next run, usually within five minutes: the pad's content goes into the trashed file, then row and pad go (see below). Until then a public pad stays reachable by its URL, a restore takes the pad back, and the admin page counts it as a pending delete. - Restore without a binding row: provision a new pad from `.pad` frontmatter/snapshot. -- Restore of a waiting row (`pending_delete` or `restore_pending`), whatever `delete_on_trash` says now: read the file's `snapshot_rev`, then ask Etherpad about the row's pad. The pad id comes from the row, never from the file. +- Restore of a waiting row (`pending_delete` or `restore_pending`), whatever `delete_on_trash` says now: read the file's `snapshot_rev`, then ask Etherpad about the row's pad. The pad id comes from the row, never from the file; a file that names the pad the row replaced holds no revision of the row's pad (see "Which pad a file reaches"). - It exists with at least that many revisions: the row becomes `active` again on that same pad, which may hold edits the snapshot missed. If the sweep took row and pad in the meantime, finishing the trash, the restore finds no row when it reads again and makes a new pad from the file, which holds the snapshot the sweep wrote first. - Etherpad answers that it does not exist, or it has fewer revisions (created again since, or back from an older backup): a new pad from the file's snapshot, and the file records the new pad's revision count. A pad with fewer revisions is left in place and logged before anything else is tried; whoever wrote into it has only that copy. Of a pad that does not exist, what is left - an empty group - goes. - Of two restores that race for one file only one writes it, and a restore that fails takes back what it made (`RestoreService::restoreOntoNewPad`). - Reading the file, asking Etherpad, and seeding a new pad take a while. Through WebDAV the file stays locked meanwhile, and a delete is refused (423, measured against NC 34.0.3); without that lock - file locking switched off, or a way in that takes none - the file can be deleted again, and its trash leaves a row that waits as it is. So the row is changed, and a new pad claimed, only while the file is still where it was - also when it could not be read: moved, and the row is left to its trash (`file_moved`), and a new pad let go. - - That leaves the moment between the claim and the write, which follow each other directly. A delete of the file there holds the file's lock, so the write fails. Usually the rollback takes the row and the new pad back before the delete's trash gets to the row; the file then goes to the trash without a row, and a later restore makes a pad from its snapshot. Should the trash owe the row first, row and new pad stay with it, and the sweep holds the new pad to the file's snapshot revision - still the old pad's - as it holds any pad it deletes. A new pad with fewer revisions, the usual case, is behind: the row goes, and the pad is left in place, logged with its id. One with as many goes with the row; one with more - a file that records no revision - has its content written into the file first. Nothing is lost either way: the new pad was seeded from the file. A trash that knew the new pad for the file's own copy belongs to the repair path. + - That leaves the moment between the claim and the write, which follow each other directly. A delete of the file there holds the file's lock, so the write fails. Usually the rollback takes the row and the new pad back before the delete's trash gets to the row; the file then goes to the trash without a row, and a later restore makes a pad from its snapshot. Should the trash owe the row first, row and new pad stay with it. The file still names the old pad, the one the row replaced, so its snapshot revision counts for none (see "Which pad a file reaches"): the sweep writes the new pad, seeded from the file, into it, and row and pad go. - No answer, or the file cannot be read: `restore_pending`, and neither pad nor file is touched. A row that waits again moves to the back of the queue (`updated_at`). - A restore whose pad's step fails still restores the file, with its versions and without its trash entry; the failure goes no further than the log. A pad left unrestored shows there as one of these lines: - `Could not restore the pad of a file back from the trash. The file itself is restored.` at `error`, once for each way in that fails, `via` naming it (`hook` or `event`). A core restore passes twice (see Event Integration), so a line from the hook can be followed by an event pass that restored the pad after all. @@ -257,7 +274,7 @@ Primary flow (native viewer): - Only a row nobody has touched for a minute: a trash that has just made it a deletion owed finishes it undisturbed, and an outage costs one call to Etherpad and one log line per row and minute, however often the file is opened, anonymously through a share included. - It reaches what the sweep cannot, or only late: a file only its owner's session can read (encrypted with the owner's key, or on a storage whose credentials live in the session), and a deletion owed that only the daily run would reach. - A row it did not decide says the pad is still being restored (`waiting_binding`), `retryable` (see "Errors of the API"); `docs/api-reference.md` lists why. -- Only a database that fails again in the middle of the rollback can leave a row naming a pad the file does not; opening the file then answers `Binding pad ID mismatch.` A restore through the API (`POST /api/v1/pads/restore`) still answers such a failure with an error. +- Only a database that fails again in the middle of the rollback can leave a row naming the new pad while the file still names the old one. The row remembers the old one, so the file opens the new pad, and an open that may write the file rewrites it (see "Which pad a file reaches"). A restore through the API (`POST /api/v1/pads/restore`) still answers such a failure with an error. - `PendingBindingService` settles waiting rows in age buckets (every 5 minutes, then hourly, then daily) and from the admin page, by where the file is now: - in Files (`restore_pending`, or `pending_delete` whose restore never came): the decision a restore takes, except that a sweep writes no file. A pad that is gone or behind releases the row, and the file offers its own recovery when it is next opened; of a pad that is gone, what is left (an empty group) goes too. - in its owner's trash (`pending_delete`): the rest of the trash, written on the owner's own storage. A pad past the file's `snapshot_rev` goes into the file (one at it is there already), then row and pad are deleted; a restore that comes first keeps the pad. A pad that is gone: the row goes, and for a group pad what is left of it (an empty group). A pad that is behind is logged with its id and left in place, and the row goes. Either way the file keeps the snapshot it has. A pad that cannot be deleted once its row is gone is left over and logged with its id; its content is in the file. @@ -278,9 +295,8 @@ Primary flow (native viewer): ### 6) Admin Integrity Check (optional) 1. Admin runs `POST /api/v1/admin/consistency-check`. -2. Service scans DB/file metadata consistency. -3. Returns aggregate counters and bounded sample lists for diagnostics. -4. External `.pad` files without bindings are expected and are excluded from missing-binding diagnostics. +2. `ConsistencyCheckService` counts binding rows whose file is gone from the file cache, and returns the count with a bounded sample. +3. It reads no `.pad` file: a file without a row, or one that names another pad than its row, shows only when it is opened. ## Main Frontend Modules diff --git a/docs/legacy-ownpad-migration.md b/docs/legacy-ownpad-migration.md index 4a26858d..09280c9f 100644 --- a/docs/legacy-ownpad-migration.md +++ b/docs/legacy-ownpad-migration.md @@ -39,7 +39,7 @@ This prevents the legacy format from being a backdoor: a malicious user can't dr ### Same origin — protected import switch -The pad-id in a legacy `.pad` is written by whoever wrote the file, and for a group pad it also names the Etherpad group a later session is minted for — a session grants what the group holds, not only the pad the file named. Where this Nextcloud is the only writer on its Etherpad server, a group pad it made carries a binding and the collision rule above covers it — barring one orphaned by a failed cleanup, which the admin settings' consistency check surfaces. Where it is not, a foreign group pad has no binding here, and nothing distinguishes it from the pads this migration exists for: a genuine Ownpad pad lives in a group Ownpad created, never in one of ours, so provenance is exactly what cannot be checked. +The pad-id in a legacy `.pad` is written by whoever wrote the file, and for a group pad it also names the Etherpad group a later session is minted for — a session grants what the group holds, not only the pad the file named. Where this Nextcloud is the only writer on its Etherpad server, a group pad it made carries a binding and the collision rule above covers it — barring one orphaned by a failed cleanup. Where it is not, a foreign group pad has no binding here, and nothing distinguishes it from the pads this migration exists for: a genuine Ownpad pad lives in a group Ownpad created, never in one of ours, so provenance is exactly what cannot be checked. `allow_legacy_protected_import` (default `no`) therefore refuses the import outright. It is meant to be turned on only where no other software creates group pads on the same Etherpad server, not as a window during a migration — the forgery is available to any user for as long as it is on. It applies only where no binding exists yet; existing bindings stay with the collision rule above, which also lets a migration that wrote the row and died before the file write still finish. Public pads are unaffected: a pad anyone holding its id can read is no more reachable for having a file bound to it. diff --git a/lib/Migration/Version000004Date20260926120000.php b/lib/Migration/Version000004Date20260926120000.php new file mode 100644 index 00000000..7d85cf3b --- /dev/null +++ b/lib/Migration/Version000004Date20260926120000.php @@ -0,0 +1,44 @@ +hasTable(BindingService::TABLE)) { + return null; + } + $table = $schema->getTable(BindingService::TABLE); + if ($table->hasColumn('replaced_pad_id')) { + return null; + } + $table->addColumn('replaced_pad_id', 'string', [ + 'length' => 255, + 'notnull' => false, + ]); + return $schema; + } +} diff --git a/lib/Service/Binding.php b/lib/Service/Binding.php index fc8e7b33..1ff1a5b6 100644 --- a/lib/Service/Binding.php +++ b/lib/Service/Binding.php @@ -28,6 +28,12 @@ public function __construct( */ public readonly ?int $deletedAt = null, public readonly int $updatedAt = 0, + /** + * The pad this row's pad replaced, when a restore or a recovery put + * it in its place: the one other pad a file of this row may still + * name (docs/architecture.md, "Which pad a file reaches"). + */ + public readonly ?string $replacedPadId = null, ) { } @@ -45,6 +51,7 @@ public static function fromRow(array $row): self { state: DbRows::string($row, 'state'), deletedAt: DbRows::nullableInt($row, 'deleted_at'), updatedAt: DbRows::int($row, 'updated_at'), + replacedPadId: DbRows::nullableString($row, 'replaced_pad_id'), ); } diff --git a/lib/Service/BindingService.php b/lib/Service/BindingService.php index e3e52532..d0bea14b 100644 --- a/lib/Service/BindingService.php +++ b/lib/Service/BindingService.php @@ -10,10 +10,7 @@ namespace OCA\EtherpadNextcloud\Service; use OCA\EtherpadNextcloud\Exception\BindingNotCreatedException; -use OCA\EtherpadNextcloud\Exception\BindingMismatchException; use OCA\EtherpadNextcloud\Exception\BindingException; -use OCA\EtherpadNextcloud\Exception\MissingBindingException; -use OCA\EtherpadNextcloud\Exception\WaitingBindingException; use OCA\EtherpadNextcloud\Util\DbRows; use OCA\EtherpadNextcloud\Util\PadAccessMode; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -123,6 +120,9 @@ public function transition(int $fileId, string $padId, string $from, string $to) * pending_delete means that: going there sets it anew, going anywhere * else clears it. A row that stays there keeps it: its age decides how * often a sweep tries it, and only updated_at moves. + * + * A row that ends up naming another pad remembers the one it named + * (replaced_pad_id): its file may still name that one. */ public function rebind(int $fileId, string $fromPadId, string $from, string $toPadId, string $to): bool { $now = $this->timeFactory->getTime(); @@ -131,6 +131,9 @@ public function rebind(int $fileId, string $fromPadId, string $from, string $toP ->set('pad_id', $qb->createNamedParameter($toPadId)) ->set('state', $qb->createNamedParameter($to)) ->set('updated_at', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)); + if ($toPadId !== $fromPadId) { + $qb->set('replaced_pad_id', $qb->createNamedParameter($fromPadId)); + } if ($to !== self::STATE_PENDING_DELETE) { $qb->set('deleted_at', $qb->createNamedParameter(null, IQueryBuilder::PARAM_NULL)); } elseif ($from !== self::STATE_PENDING_DELETE) { @@ -251,7 +254,11 @@ private function findWaitingByAge(string $state, string $ageColumn, int $minAgeS return $rows; } - public function createBinding(int $fileId, string $padId, string $accessMode): void { + /** + * $replacedPadId: the pad the file named, when the new pad takes its + * place (a recovery); see rebind(). + */ + public function createBinding(int $fileId, string $padId, string $accessMode, ?string $replacedPadId = null): void { $this->assertAccessMode($accessMode); $now = $this->timeFactory->getTime(); @@ -265,6 +272,9 @@ public function createBinding(int $fileId, string $padId, string $accessMode): v 'deleted_at' => $qb->createNamedParameter(null, IQueryBuilder::PARAM_NULL), 'created_at' => $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT), 'updated_at' => $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT), + 'replaced_pad_id' => $replacedPadId === null + ? $qb->createNamedParameter(null, IQueryBuilder::PARAM_NULL) + : $qb->createNamedParameter($replacedPadId), ]); try { @@ -278,27 +288,6 @@ public function createBinding(int $fileId, string $padId, string $accessMode): v } } - public function assertConsistentMapping(int $fileId, string $padId, string $accessMode): void { - $this->assertAccessMode($accessMode); - $binding = $this->findByFileId($fileId); - if ($binding === null) { - throw new MissingBindingException('No binding exists for this file.'); - } - if ($binding->padId !== $padId) { - throw new BindingMismatchException('Binding pad ID mismatch.'); - } - if ($binding->accessMode !== $accessMode) { - throw new BindingMismatchException('Binding access mode mismatch.'); - } - if ($binding->isWaiting()) { - throw new WaitingBindingException('Pad binding is not active.'); - } - if ($binding->state !== self::STATE_ACTIVE) { - // A state the sweep never takes up, so nothing to wait for either. - throw new BindingException('Pad binding is not active.'); - } - } - /** * Remove one file's active row, and only while it still names this pad. * diff --git a/lib/Service/BoundPadResolver.php b/lib/Service/BoundPadResolver.php new file mode 100644 index 00000000..54af70ad --- /dev/null +++ b/lib/Service/BoundPadResolver.php @@ -0,0 +1,98 @@ +bindingService->findByFileId($fileId); + if ($binding === null) { + throw new MissingBindingException('No binding exists for this file.'); + } + // Before the state: a file that names neither pad is not this row's + // to settle or wait for. + $bound = $this->followingRow($pad, $binding); + if ($bound === $pad) { + if ($binding->padId !== $pad->padId) { + throw new BindingMismatchException('Binding pad ID mismatch.'); + } + if ($binding->accessMode !== $pad->accessMode) { + throw new BindingMismatchException('Binding access mode mismatch.'); + } + } + if ($binding->isWaiting()) { + throw new WaitingBindingException('Pad binding is not active.'); + } + if ($binding->state !== BindingService::STATE_ACTIVE) { + // A state the sweep never takes up, so nothing to wait for either. + throw new BindingException('Pad binding is not active.'); + } + if ($bound !== $pad) { + $this->logger->debug('A .pad file names the pad its row replaced; the row\'s pad is used.', [ + 'app' => 'etherpad_nextcloud', + 'fileId' => $fileId, + ]); + } + return $bound; + } + + /** + * For a caller that holds the row already, a trash, a sweep or a + * restore: $pad as namingPad() makes it when it names the pad $binding's + * replaced, so the revision it has is not held against the row's pad; + * otherwise $pad as it is. + */ + public function followingRow(ParsedPadFile $pad, Binding $binding): ParsedPadFile { + if ($pad->padId !== $binding->replacedPadId || $pad->padId === $binding->padId) { + return $pad; + } + return $this->padFileService->namingPad($pad, $binding->padId, $binding->accessMode, $this->etherpadClient->buildPadUrl($binding->padId)); + } + + /** A file that named the replaced pad has been written to name the row's. */ + public function repaired(int $fileId): void { + $this->logger->warning('A .pad file named the pad its row replaced; it now names the row\'s.', [ + 'app' => 'etherpad_nextcloud', + 'fileId' => $fileId, + ]); + } +} diff --git a/lib/Service/LifecycleService.php b/lib/Service/LifecycleService.php index 8dc846e0..cf1de461 100644 --- a/lib/Service/LifecycleService.php +++ b/lib/Service/LifecycleService.php @@ -36,6 +36,7 @@ public function __construct( private ITimeFactory $timeFactory, private TrashSnapshotWriters $snapshotWriters, private RestoreService $restoreService, + private BoundPadResolver $boundPads, ) { } @@ -121,6 +122,11 @@ public function handleTrash(File $file): array { try { $snapshots = $this->snapshotWriters->for($file, $padId); $pad = $snapshots->read(); + if (!$pad instanceof TrashSnapshotMiss) { + // A file that names the pad its row replaced holds no revision + // of the row's pad, and takes the row's pad's content and name. + $pad = $this->boundPads->followingRow($pad, $binding); + } if ($pad instanceof TrashSnapshotMiss || !$snapshots->writeAtTrash($pad)) { // Without a fresh snapshot the pad may hold what the file lacks, // so it stays as it is and its deletion is owed. The sweep takes diff --git a/lib/Service/LivePadHtmlFetcher.php b/lib/Service/LivePadHtmlFetcher.php index 411b2389..5ac2489a 100644 --- a/lib/Service/LivePadHtmlFetcher.php +++ b/lib/Service/LivePadHtmlFetcher.php @@ -25,7 +25,7 @@ public function __construct( private EtherpadClient $etherpadClient, private ExternalPadExportFetcher $externalPadExportFetcher, private SnapshotHtmlSanitizer $htmlSanitizer, - private BindingService $bindingService, + private BoundPadResolver $boundPads, ) { } @@ -35,16 +35,16 @@ public function __construct( * The binding check lives here, not at each call site: it is what stops * an edited `.pad` file from pointing this app's API key at somebody * else's pad, and the signed-in and public paths must not be able to - * disagree about it. + * disagree about it. The pad read is the one BoundPadResolver says. */ public function fetchForPadFile(ParsedPadFile $pad, int $fileId): LivePadHtml { if ($pad->isExternal) { return $this->toPayload($this->externalPadExportFetcher->fetchExternalPublicPadHtml($pad->externalPadUrl())); } - $this->bindingService->assertConsistentMapping($fileId, $pad->padId, $pad->accessMode); + $bound = $this->boundPads->resolve($fileId, $pad); - return $this->toPayload($this->etherpadClient->getHTMLForPreview($pad->padId)); + return $this->toPayload($this->etherpadClient->getHTMLForPreview($bound->padId)); } private function toPayload(string $html): LivePadHtml { diff --git a/lib/Service/OwedDeletions.php b/lib/Service/OwedDeletions.php index d34455c7..4cfe777d 100644 --- a/lib/Service/OwedDeletions.php +++ b/lib/Service/OwedDeletions.php @@ -42,6 +42,7 @@ public function __construct( private UserNodeResolver $userNodeResolver, private LoggerInterface $logger, private ITimeFactory $timeFactory, + private BoundPadResolver $boundPads, ) { } @@ -74,6 +75,9 @@ public function finishTrash(File $file, RunBudget $budget): SettleOutcome { if ($pad instanceof TrashSnapshotMiss) { return $this->waitAgain($fileId, $padId, $pad); } + // A file that names the pad its row replaced holds no revision of the + // row's pad, and takes the row's pad's content and name. + $pad = $this->boundPads->followingRow($pad, $binding); $timeout = $budget->nextCallTimeout(); if ($timeout === null) { diff --git a/lib/Service/PadFileService.php b/lib/Service/PadFileService.php index 13644145..09b0a82a 100644 --- a/lib/Service/PadFileService.php +++ b/lib/Service/PadFileService.php @@ -236,6 +236,30 @@ public function withRestoredSnapshot( return $this->serialize($frontmatter, $this->buildSnapshotBody($text, $html)); } + /** + * $pad as it reads once it names another pad: that pad's id, mode and + * address, and no snapshot revision, since the one it has counts the + * pad it named before. Its text stays until a sync or a trash writes the + * new pad's into it. Nothing is written here. + */ + public function namingPad(ParsedPadFile $pad, string $padId, string $accessMode, string $padUrl): ParsedPadFile { + $frontmatter = $pad->frontmatter; + $frontmatter['pad_id'] = $padId; + $frontmatter['access_mode'] = $accessMode; + $frontmatter['pad_url'] = $padUrl; + $frontmatter['snapshot_rev'] = -1; + $frontmatter['updated_at'] = $this->nowIso(); + return new ParsedPadFile( + frontmatter: $frontmatter, + body: $pad->body, + padId: $padId, + accessMode: $accessMode, + padUrl: $padUrl, + isExternal: false, + snapshotRev: -1, + ); + } + public function withExportSnapshot(ParsedPadFile $pad, PadSnapshot $snapshot): string { $frontmatter = $pad->frontmatter; $frontmatter['updated_at'] = $this->nowIso(); diff --git a/lib/Service/PadOpenService.php b/lib/Service/PadOpenService.php index 0643fe21..deb61a04 100644 --- a/lib/Service/PadOpenService.php +++ b/lib/Service/PadOpenService.php @@ -30,6 +30,7 @@ public function __construct( private ExternalPadExportFetcher $externalPadExportFetcher, private PadSessionService $padSessionService, private LoggerInterface $logger, + private BoundPadResolver $boundPads, ) { } @@ -82,12 +83,38 @@ private function openNode(string $uid, string $displayName, File $node, string $ // not issue a session that writes on the pad server. $mayWrite = $node->isUpdateable(); if (!$pad->isExternal) { - $this->settleOnOpen->settleThenAssert($node, $fileId, $pad); + $bound = $this->settleOnOpen->settleThenResolve($node, $fileId, $pad); + if ($bound !== $pad && $mayWrite) { + $this->followRow($node, $fileId, $content, $bound); + } + $pad = $bound; } return $this->buildOpenContext($uid, $displayName, $absolutePath, $fileId, $pad, $mayWrite); } + /** + * The file named the pad its row replaced, and whoever opens it may + * write it: it is written to name the row's pad, once, over what this + * open read and nothing newer. Should that not work, the row's pad + * opens all the same, and the next open or sync tries again. + */ + private function followRow(File $node, int $fileId, string $read, ParsedPadFile $bound): void { + try { + if ($node->getContent() !== $read) { + return; + } + $node->putContent($this->padFileService->serialize($bound->frontmatter, $bound->body)); + $this->boundPads->repaired($fileId); + } catch (\Throwable $e) { + $this->logger->debug('Could not rewrite a .pad file to its row\'s pad; the row\'s pad opens all the same.', [ + 'app' => 'etherpad_nextcloud', + 'fileId' => $fileId, + ...SafeError::context($e), + ]); + } + } + private function buildOpenContext( string $uid, string $displayName, diff --git a/lib/Service/PadSyncService.php b/lib/Service/PadSyncService.php index 7dc00c09..846a4de8 100644 --- a/lib/Service/PadSyncService.php +++ b/lib/Service/PadSyncService.php @@ -30,7 +30,7 @@ public function __construct( private PadFileService $padFileService, private UserNodeResolver $userNodeResolver, private PadFileLockRetryService $lockRetryService, - private BindingService $bindingService, + private BoundPadResolver $boundPads, private EtherpadClient $etherpadClient, private ExternalPadExportFetcher $externalPadExportFetcher, private LoggerInterface $logger, @@ -54,18 +54,21 @@ public function syncById(string $uid, int $fileId, bool $force): PadSyncResult { try { $pad = $this->padFileService->readPad((string)$node->getContent()); - $padId = $pad->padId; - $accessMode = $pad->accessMode; $isExternal = $pad->isExternal; - if (!$isExternal) { - $this->bindingService->assertConsistentMapping($fileId, $padId, $accessMode); - } - + $synced = $isExternal ? $pad : $this->boundPads->resolve($fileId, $pad); + $padId = $synced->padId; + $accessMode = $synced->accessMode; if ($isExternal) { return $this->syncExternalPad($node, $fileId, $pad, $force); } - return $this->syncInternalPad($node, $fileId, $pad, $force); + // A file that named the pad its row replaced has no revision of + // the row's pad, so this always writes the pad's content and name. + $result = $this->syncInternalPad($node, $fileId, $synced, $force); + if ($synced !== $pad) { + $this->boundPads->repaired($fileId); + } + return $result; } catch (PadFileLockRetryExhaustedException $e) { $lockRetries = $e->getRetryAttempts(); return $this->lockedSyncResponse($e->getLockedException(), $fileId, $absolutePath, $padId, $accessMode, $isExternal, $force, $lockRetries); @@ -82,21 +85,18 @@ public function syncStatusById(string $uid, int $fileId): PadSyncStatus { $node = $this->userNodeResolver->resolveUserFileNodeById($uid, $fileId); $pad = $this->padFileService->readPad((string)$node->getContent()); - $padId = $pad->padId; - $accessMode = $pad->accessMode; - $isExternal = $pad->isExternal; - if (!$isExternal) { - $this->bindingService->assertConsistentMapping($fileId, $padId, $accessMode); - } - if ($isExternal) { + if ($pad->isExternal) { return new PadSyncStatus( status: self::STATUS_UNAVAILABLE, inSync: null, reason: 'external_no_revision', ); } + // A file that named the pad its row replaced is out of sync with the + // row's pad, whatever its revision says. + $pad = $this->boundPads->resolve($fileId, $pad); - $currentRev = $this->etherpadClient->getRevisionsCount($padId); + $currentRev = $this->etherpadClient->getRevisionsCount($pad->padId); $snapshotRev = $pad->snapshotRev; $inSync = $snapshotRev >= $currentRev; diff --git a/lib/Service/PublicPadContextService.php b/lib/Service/PublicPadContextService.php index 6d839365..2e19f76d 100644 --- a/lib/Service/PublicPadContextService.php +++ b/lib/Service/PublicPadContextService.php @@ -62,7 +62,8 @@ public function resolve(string $token, mixed $fileParam, ?IShare $cachedShare = $isExternal = $pad->isExternal; if (!$isExternal) { - $this->settleOnOpen->settleThenAssert($node, $fileId, $pad); + // A visitor never writes the file; the row's pad opens all the same. + $pad = $this->settleOnOpen->settleThenResolve($node, $fileId, $pad); } $openTarget = $this->publicPadOpenService->open($pad, $resolved->readOnly, $token); diff --git a/lib/Service/RestoreService.php b/lib/Service/RestoreService.php index b9640210..0627917c 100644 --- a/lib/Service/RestoreService.php +++ b/lib/Service/RestoreService.php @@ -60,6 +60,7 @@ public function __construct( private ProvisionedPadRollback $provisionedPadRollback, private TestFaults $testFaults, private UserNodeResolver $userNodeResolver, + private BoundPadResolver $boundPads, ) { } @@ -176,6 +177,10 @@ private function settleWaitingBinding(File $file, Binding $binding, bool $mayRep // No revision to hold the pad to, so no decision either. return $this->deferRestore($fileId, $padId, $state, $readError); } + // A file that names the pad its row replaced holds no revision of + // the row's pad: a higher one of the pad before would make the + // row's pad look behind. + $pad = $this->boundPads->followingRow($pad, $binding); $timeout = $budget?->nextCallTimeout(); if ($budget !== null && $timeout === null) { // Reading the file took what the run had left. @@ -520,7 +525,9 @@ private function restoreWithoutBinding(File $file, int $fileId): array { $pad->padId, 'restore without binding', function (string $newPadId) use ($fileId, $pad): bool { - $this->bindingService->createBinding($fileId, $newPadId, $pad->accessMode); + // The file may still name the pad it named, in an older + // version say: the row remembers it (BoundPadResolver). + $this->bindingService->createBinding($fileId, $newPadId, $pad->accessMode, replacedPadId: $pad->padId); return true; }, ); diff --git a/lib/Service/SettleOnOpen.php b/lib/Service/SettleOnOpen.php index 79462bba..e4e6e432 100644 --- a/lib/Service/SettleOnOpen.php +++ b/lib/Service/SettleOnOpen.php @@ -44,28 +44,28 @@ public function __construct( private SettleLock $settleLock, private ITimeFactory $timeFactory, private LoggerInterface $logger, + private BoundPadResolver $boundPads, ) { } /** - * BindingService::assertConsistentMapping() for the file an open has - * just read ($pad), after a row that waits has been decided once: the - * row then answers as it is - active, gone, or still waiting. Deciding - * costs Etherpad calls and writes to the row, so this is for an open, - * never for a path that polls. + * BoundPadResolver::resolve() for the file an open has just read ($pad), + * after a row that waits has been decided once: the row then answers as + * it is - active, gone, or still waiting. Deciding costs Etherpad calls + * and writes to the row, so this is for an open, never for a path that + * polls. * * $fileId is $file's, as the caller has it already. * * @throws BindingException */ - public function settleThenAssert(File $file, int $fileId, ParsedPadFile $pad): void { + public function settleThenResolve(File $file, int $fileId, ParsedPadFile $pad): ParsedPadFile { try { - $this->bindingService->assertConsistentMapping($fileId, $pad->padId, $pad->accessMode); - return; + return $this->boundPads->resolve($fileId, $pad); } catch (WaitingBindingException) { $this->settle($file, $fileId, $pad); } - $this->bindingService->assertConsistentMapping($fileId, $pad->padId, $pad->accessMode); + return $this->boundPads->resolve($fileId, $pad); } private function settle(File $file, int $fileId, ParsedPadFile $pad): void { diff --git a/tests/e2e/docker/sync-app.sh b/tests/e2e/docker/sync-app.sh index fd664942..f6a430a8 100755 --- a/tests/e2e/docker/sync-app.sh +++ b/tests/e2e/docker/sync-app.sh @@ -32,3 +32,10 @@ compose exec -T -u root nextcloud chown -R www-data:www-data "$app" # report a pass. compose restart nextcloud >/dev/null compose exec -T -u www-data nextcloud sh -c 'for _ in $(seq 1 60); do php occ status 2>/dev/null | grep -q "installed: true" && exit 0; sleep 1; done; exit 1' + +# Nextcloud runs an app's migrations when info.xml's version is above the +# one it has installed, and a branch leaves the version as it is. Marked +# older, the app is upgraded as a release upgrades it, and gets the +# migrations it has not run yet. On a new stack up.sh enables the app +# after this, which runs them all. +compose exec -T -u www-data nextcloud sh -c 'if [ "$(php occ config:app:get etherpad_nextcloud enabled 2>/dev/null)" = yes ]; then php occ config:app:set etherpad_nextcloud installed_version --value 0.0.0 && php occ upgrade; fi' >/dev/null diff --git a/tests/phpunit/Support/BuildsBoundPads.php b/tests/phpunit/Support/BuildsBoundPads.php new file mode 100644 index 00000000..7fb5a1e0 --- /dev/null +++ b/tests/phpunit/Support/BuildsBoundPads.php @@ -0,0 +1,35 @@ +createMock(EtherpadClient::class); + $etherpad->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::PAD_BASE . $padId); + return new BoundPadResolver( + $bindings, + $padFiles ?? new PadFileService(new FixedClock()), + $etherpad, + $logger ?? $this->createMock(LoggerInterface::class), + ); + } +} diff --git a/tests/phpunit/Support/InMemoryBindingTable.php b/tests/phpunit/Support/InMemoryBindingTable.php index 89275d9b..3afc5f07 100644 --- a/tests/phpunit/Support/InMemoryBindingTable.php +++ b/tests/phpunit/Support/InMemoryBindingTable.php @@ -19,8 +19,17 @@ * a condition left out changes which rows are hit, and a test sees it. */ final class InMemoryBindingTable implements IDBConnection { - /** @param list> $rows */ - public function __construct(public array $rows) { + /** @var list> */ + public array $rows; + + /** + * A row given without replaced_pad_id has it NULL, as the column + * defaults to in the table. + * + * @param list> $rows + */ + public function __construct(array $rows) { + $this->rows = array_map(static fn (array $row): array => $row + ['replaced_pad_id' => null], $rows); } public function getQueryBuilder(): IQueryBuilder { diff --git a/tests/phpunit/Support/SettlesOnOpen.php b/tests/phpunit/Support/SettlesOnOpen.php index e9938663..adafacbd 100644 --- a/tests/phpunit/Support/SettlesOnOpen.php +++ b/tests/phpunit/Support/SettlesOnOpen.php @@ -10,6 +10,9 @@ use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; +use OCA\EtherpadNextcloud\Service\BoundPadResolver; +use OCA\EtherpadNextcloud\Service\EtherpadClient; +use OCA\EtherpadNextcloud\Service\PadFileService; use OCA\EtherpadNextcloud\Service\RestoreService; use OCA\EtherpadNextcloud\Service\SettleLock; use OCA\EtherpadNextcloud\Service\SettleOnOpen; @@ -23,14 +26,17 @@ * as an enum cannot be doubled. */ trait SettlesOnOpen { - private function settleOnOpen(BindingService $bindings, ?RestoreService $restores = null): SettleOnOpen { - $logger = $this->createMock(LoggerInterface::class); + private function settleOnOpen(BindingService $bindings, ?RestoreService $restores = null, ?LoggerInterface $logger = null): SettleOnOpen { + $logger ??= $this->createMock(LoggerInterface::class); + $etherpad = $this->createMock(EtherpadClient::class); + $etherpad->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId); return new SettleOnOpen( $bindings, $restores ?? $this->silentRestores(), new SettleLock(new InMemoryLockingProvider(), $logger), new FixedClock(), $logger, + new BoundPadResolver($bindings, new PadFileService(new FixedClock()), $etherpad, $logger), ); } diff --git a/tests/phpunit/Support/WiresTheLifecycle.php b/tests/phpunit/Support/WiresTheLifecycle.php index 2d1b1bab..8c5f4e9e 100644 --- a/tests/phpunit/Support/WiresTheLifecycle.php +++ b/tests/phpunit/Support/WiresTheLifecycle.php @@ -11,6 +11,7 @@ use OCA\EtherpadNextcloud\Service\AppConfigService; use OCA\EtherpadNextcloud\Service\BindingService; +use OCA\EtherpadNextcloud\Service\BoundPadResolver; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\LifecycleService; use OCA\EtherpadNextcloud\Service\ManagedPadLifecycle; @@ -77,6 +78,7 @@ private function lifecycleService( new FixedClock(), new TrashSnapshotWriters($etherpad, $padFiles, $logger, $testFaults), $restores ?? $this->wireRestoreService($bindings, $etherpad, $padFiles, $padLifecycle, $appConfig, $logger, $padLifecycleLogger, $secureRandom, $testFaults), + new BoundPadResolver($bindings, $padFiles, $etherpad, $logger), ); } @@ -138,6 +140,7 @@ private function wireRestoreService( $testFaults, // Unless a test says otherwise, a file stays where it was read. $nodes ?? $this->createMock(UserNodeResolver::class), + new BoundPadResolver($bindings, $padFiles, $etherpad, $logger), ); } diff --git a/tests/phpunit/unit/BindingServiceTest.php b/tests/phpunit/unit/BindingServiceTest.php index 60b05ae8..a882248b 100644 --- a/tests/phpunit/unit/BindingServiceTest.php +++ b/tests/phpunit/unit/BindingServiceTest.php @@ -5,10 +5,6 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; use OCA\EtherpadNextcloud\Exception\BindingNotCreatedException; -use OCA\EtherpadNextcloud\Exception\BindingMismatchException; -use OCA\EtherpadNextcloud\Exception\BindingException; -use OCA\EtherpadNextcloud\Exception\MissingBindingException; -use OCA\EtherpadNextcloud\Exception\WaitingBindingException; use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\FileLocation; @@ -20,36 +16,6 @@ use PHPUnit\Framework\TestCase; class BindingServiceTest extends TestCase { - /** - * The file's own row is the one compared, and it has to name the pad, - * in the access mode, and be active. Another file's row is no binding - * for this one, and a mode the app does not know is refused first. - */ - public function testAssertConsistentMappingHoldsTheFilesRowToPadModeAndState(): void { - $cases = [ - 'consistent' => [self::bindingRow(10, 'pad-a', BindingService::STATE_ACTIVE), 'pad-a', BindingService::ACCESS_PUBLIC, null, null], - 'another pad' => [self::bindingRow(10, 'pad-a', BindingService::STATE_ACTIVE), 'pad-b', BindingService::ACCESS_PUBLIC, BindingMismatchException::class, 'Binding pad ID mismatch.'], - 'another mode' => [self::bindingRow(10, 'pad-a', BindingService::STATE_ACTIVE, BindingService::ACCESS_PROTECTED), 'pad-a', BindingService::ACCESS_PUBLIC, BindingMismatchException::class, 'Binding access mode mismatch.'], - // Waiting is its own kind, so the one who opens the file is told it is on its way back. - 'a deletion owed' => [self::bindingRow(10, 'pad-a', BindingService::STATE_PENDING_DELETE), 'pad-a', BindingService::ACCESS_PUBLIC, WaitingBindingException::class, 'Pad binding is not active.'], - 'a restore undecided' => [self::bindingRow(10, 'pad-a', BindingService::STATE_RESTORE_PENDING), 'pad-a', BindingService::ACCESS_PUBLIC, WaitingBindingException::class, 'Pad binding is not active.'], - // No sweep takes up a state it does not know, so nothing waits for one either. - 'a state the app does not know' => [self::bindingRow(10, 'pad-a', 'trashed'), 'pad-a', BindingService::ACCESS_PUBLIC, BindingException::class, 'Pad binding is not active.'], - 'another file\'s row' => [self::bindingRow(11, 'pad-a', BindingService::STATE_ACTIVE), 'pad-a', BindingService::ACCESS_PUBLIC, MissingBindingException::class, 'No binding exists for this file.'], - 'unknown mode' => [self::bindingRow(10, 'pad-a', BindingService::STATE_ACTIVE), 'pad-a', 'legacy', BindingException::class, 'Unsupported access mode: legacy'], - ]; - foreach ($cases as $case => [$row, $padId, $accessMode, $exception, $message]) { - // A waiting row of another file sits alongside: only file 10's is compared. - $service = $this->serviceOver([self::bindingRow(9, 'pad-9', BindingService::STATE_PENDING_DELETE), $row]); - try { - $service->assertConsistentMapping(10, $padId, $accessMode); - $this->assertNull($exception, $case . ': accepted'); - } catch (BindingException $e) { - $this->assertSame([$exception, $message], [$e::class, $e->getMessage()], $case); - } - } - } - /** * An insert the database does not take - a row another request made at * the same moment, or the database gone - is its own exception, with the @@ -240,11 +206,74 @@ public function testRebindMovesARowOnlyWhileItNamesThatPadInThatState(): void { self::assertTrue($service->rebind(1, 'old', BindingService::STATE_PENDING_DELETE, 'new', BindingService::STATE_ACTIVE)); self::assertSame([ - ['file_id' => 1, 'pad_id' => 'new', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'updated_at' => 500], + // The pad it named is the one its file may still name. + ['file_id' => 1, 'pad_id' => 'new', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'updated_at' => 500, 'replaced_pad_id' => 'old'], self::bindingRow(2, 'other', BindingService::STATE_PENDING_DELETE), ], $table->rows); } + /** + * Only a pad put in another's place replaces it: a row that keeps its pad + * through a change of state keeps the pad it replaced too. + */ + public function testAChangeOfStateKeepsThePadTheRowReplaced(): void { + $table = new InMemoryBindingTable([['replaced_pad_id' => 'before'] + self::bindingRow(1, 'pad', BindingService::STATE_ACTIVE)]); + $service = new BindingService($table, new FixedClock(500)); + + self::assertTrue($service->transition(1, 'pad', BindingService::STATE_ACTIVE, BindingService::STATE_PENDING_DELETE)); + + self::assertSame('before', $table->rows[0]['replaced_pad_id']); + } + + /** A recovery's row says which pad the file named; any other new row replaced none. */ + public function testANewRowSaysWhichPadItReplaced(): void { + foreach (['a recovery' => ['old', 'old'], 'a create' => [null, null]] as $case => [$replaced, $written]) { + $qb = new class implements IQueryBuilder { + /** @var array */ + public array $values = []; + /** @var list */ + private array $parameters = []; + + public function insert(string $table): self { + return $this; + } + + public function values(array $values): self { + foreach ($values as $column => $parameter) { + $this->values[$column] = $this->parameters[(int)substr($parameter, 2)]; + } + return $this; + } + + public function createNamedParameter(mixed $value, mixed $type = null): string { + $this->parameters[] = $value; + return ':p' . (count($this->parameters) - 1); + } + + public function executeStatement(): int { + return 1; + } + }; + $db = new class ($qb) implements IDBConnection { + public function __construct(private IQueryBuilder $qb) { + } + + public function getQueryBuilder(): IQueryBuilder { + return $this->qb; + } + + public function escapeLikeParameter(string $param): string { + return $param; + } + }; + + (new BindingService($db, new FixedClock(500)))->createBinding(10, 'new', BindingService::ACCESS_PUBLIC, replacedPadId: $replaced); + + self::assertSame($written, $qb->values['replaced_pad_id'], $case); + self::assertSame('new', $qb->values['pad_id'], $case); + } + } + /** * deleted_at says the file is in the trash. A restore that has to wait * has its file back, so the date goes, whichever way the row got there. @@ -266,7 +295,7 @@ public function testTransitionToPendingDeleteDatesTheDeletionAnew(): void { self::assertTrue($service->transition(1, 'pad', BindingService::STATE_RESTORE_PENDING, BindingService::STATE_PENDING_DELETE)); self::assertSame( - ['file_id' => 1, 'pad_id' => 'pad', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 500, 'updated_at' => 500], + ['file_id' => 1, 'pad_id' => 'pad', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 500, 'updated_at' => 500, 'replaced_pad_id' => null], $table->rows[0], ); } @@ -346,7 +375,7 @@ public function testARowThatStaysOwedKeepsItsDate(): void { self::assertTrue($service->transition(1, 'pad', BindingService::STATE_PENDING_DELETE, BindingService::STATE_PENDING_DELETE)); self::assertSame( - ['file_id' => 1, 'pad_id' => 'pad', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 100, 'updated_at' => 500], + ['file_id' => 1, 'pad_id' => 'pad', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 100, 'updated_at' => 500, 'replaced_pad_id' => null], $table->rows[0], ); } @@ -355,7 +384,7 @@ public function testARowThatStaysOwedKeepsItsDate(): void { private static function bindingRow(int $fileId, string $padId, string $state, string $accessMode = BindingService::ACCESS_PUBLIC): array { // Dated only as a deletion owed, as the table holds it: leaving that state clears the date. $deletedAt = $state === BindingService::STATE_PENDING_DELETE ? 100 : null; - return ['file_id' => $fileId, 'pad_id' => $padId, 'access_mode' => $accessMode, 'state' => $state, 'deleted_at' => $deletedAt, 'updated_at' => 100]; + return ['file_id' => $fileId, 'pad_id' => $padId, 'access_mode' => $accessMode, 'state' => $state, 'deleted_at' => $deletedAt, 'updated_at' => 100, 'replaced_pad_id' => null]; } } diff --git a/tests/phpunit/unit/BindingTest.php b/tests/phpunit/unit/BindingTest.php index 20e736ac..f7a74f0a 100644 --- a/tests/phpunit/unit/BindingTest.php +++ b/tests/phpunit/unit/BindingTest.php @@ -30,16 +30,19 @@ public function testReadsEachColumnIntoItsPlace(): void { 'deleted_at' => 100, 'created_at' => 50, 'updated_at' => 200, + 'replaced_pad_id' => 'nc-before', ]); - $this->assertEquals(new Binding(7, 'nc-abc', BindingService::ACCESS_PROTECTED, BindingService::STATE_PENDING_DELETE, 100, 200), $binding); + $this->assertEquals(new Binding(7, 'nc-abc', BindingService::ACCESS_PROTECTED, BindingService::STATE_PENDING_DELETE, 100, 200, 'nc-before'), $binding); } /** Only a deletion owed has a date for it; a row without one says so, not 0. */ public function testARowWithoutADeletionOwedHasNoDateForIt(): void { - $binding = Binding::fromRow(['file_id' => 7, 'pad_id' => 'nc-abc', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'updated_at' => 200]); + $binding = Binding::fromRow(['file_id' => 7, 'pad_id' => 'nc-abc', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'updated_at' => 200, 'replaced_pad_id' => null]); $this->assertNull($binding->deletedAt); + // Nor has a row that replaced no pad a pad it replaced. + $this->assertNull($binding->replacedPadId); } /** @@ -49,7 +52,7 @@ public function testARowWithoutADeletionOwedHasNoDateForIt(): void { * error where the row is read instead. */ public function testAColumnTheQueryLeftOutIsAnError(): void { - $row = ['file_id' => 7, 'pad_id' => 'nc-abc', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 100, 'updated_at' => 200, 'file_path' => null, 'waiting_since' => 100]; + $row = ['file_id' => 7, 'pad_id' => 'nc-abc', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => BindingService::STATE_PENDING_DELETE, 'deleted_at' => 100, 'updated_at' => 200, 'replaced_pad_id' => null, 'file_path' => null, 'waiting_since' => 100]; $reads = [ 'Binding' => static fn (array $r): mixed => Binding::fromRow($r), 'WaitingBinding' => static fn (array $r): mixed => WaitingBinding::fromRow($r), diff --git a/tests/phpunit/unit/BoundPadResolverTest.php b/tests/phpunit/unit/BoundPadResolverTest.php new file mode 100644 index 00000000..f26f9138 --- /dev/null +++ b/tests/phpunit/unit/BoundPadResolverTest.php @@ -0,0 +1,166 @@ + [self::row(10, 'pad-a'), 'pad-a', BindingService::ACCESS_PUBLIC, 'pad-a', null], + 'the pad it replaced' => [self::row(10, 'pad-b', replaced: 'pad-a'), 'pad-a', BindingService::ACCESS_PUBLIC, 'pad-b', null], + // The row's mode comes along: the file named the pad in the mode it had. + 'the pad it replaced, in another mode' => [self::row(10, 'pad-b', mode: BindingService::ACCESS_PROTECTED, replaced: 'pad-a'), 'pad-a', BindingService::ACCESS_PUBLIC, 'pad-b', null], + 'another pad' => [self::row(10, 'pad-a'), 'pad-b', BindingService::ACCESS_PUBLIC, null, [BindingMismatchException::class, 'Binding pad ID mismatch.']], + // A pad the row replaced is the one exception, not a pass for any other. + 'neither pad' => [self::row(10, 'pad-b', replaced: 'pad-a'), 'pad-c', BindingService::ACCESS_PUBLIC, null, [BindingMismatchException::class, 'Binding pad ID mismatch.']], + 'another mode' => [self::row(10, 'pad-a', mode: BindingService::ACCESS_PROTECTED), 'pad-a', BindingService::ACCESS_PUBLIC, null, [BindingMismatchException::class, 'Binding access mode mismatch.']], + // Waiting is its own kind, so the one who opens the file is told it is on its way back. + 'a deletion owed' => [self::row(10, 'pad-a', BindingService::STATE_PENDING_DELETE), 'pad-a', BindingService::ACCESS_PUBLIC, null, [WaitingBindingException::class, 'Pad binding is not active.']], + 'a restore undecided' => [self::row(10, 'pad-a', BindingService::STATE_RESTORE_PENDING), 'pad-a', BindingService::ACCESS_PUBLIC, null, [WaitingBindingException::class, 'Pad binding is not active.']], + 'the pad it replaced, while it waits' => [self::row(10, 'pad-b', BindingService::STATE_RESTORE_PENDING, replaced: 'pad-a'), 'pad-a', BindingService::ACCESS_PUBLIC, null, [WaitingBindingException::class, 'Pad binding is not active.']], + // A file that names neither pad is not the row's to wait for. + 'another pad, while it waits' => [self::row(10, 'pad-a', BindingService::STATE_RESTORE_PENDING), 'pad-b', BindingService::ACCESS_PUBLIC, null, [BindingMismatchException::class, 'Binding pad ID mismatch.']], + // No sweep takes up a state it does not know, so nothing waits for one either. + 'a state the app does not know' => [self::row(10, 'pad-a', 'trashed'), 'pad-a', BindingService::ACCESS_PUBLIC, null, [BindingException::class, 'Pad binding is not active.']], + 'another file\'s row' => [self::row(11, 'pad-a'), 'pad-a', BindingService::ACCESS_PUBLIC, null, [MissingBindingException::class, 'No binding exists for this file.']], + ]; + foreach ($cases as $case => [$row, $padId, $accessMode, $reaches, $refusal]) { + // A waiting row of another file sits alongside: only file 10's decides. + $resolver = $this->boundPads(new BindingService(new InMemoryBindingTable([self::row(9, 'pad-9', BindingService::STATE_PENDING_DELETE), $row]), new FixedClock())); + try { + $bound = $resolver->resolve(10, self::pad($padId, $accessMode)); + $this->assertNull($refusal, $case . ': reached ' . $bound->padId); + $this->assertSame($reaches, $bound->padId, $case); + } catch (BindingException $e) { + $this->assertSame($refusal, [$e::class, $e->getMessage()], $case); + } + } + } + + /** + * A file that names the pad its row replaced is read as naming the + * row's: its id, mode and link, and no revision of its own, since the + * one it has is the replaced pad's. The rest of the file stays, and + * nothing is written; that is the caller's to decide. A file that names + * the row's pad comes back as it is. + */ + public function testAFileNamingThePadItsRowReplacedIsReadAsNamingTheRows(): void { + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->once())->method('debug')->with( + 'A .pad file names the pad its row replaced; the row\'s pad is used.', + ['app' => 'etherpad_nextcloud', 'fileId' => 10], + ); + $logger->expects($this->never())->method('warning'); + $bindings = new BindingService(new InMemoryBindingTable([self::row(10, 'g.new', mode: BindingService::ACCESS_PROTECTED, replaced: 'g.old')]), new FixedClock()); + $pad = self::pad('g.old', BindingService::ACCESS_PUBLIC, rev: 42, body: 'Text of the old pad'); + + $bound = $this->boundPads($bindings, $logger, new PadFileService(new FixedClock(2000000000)))->resolve(10, $pad); + + $this->assertSame( + ['g.new', BindingService::ACCESS_PROTECTED, self::PAD_BASE . 'g.new', -1, false, 'Text of the old pad'], + [$bound->padId, $bound->accessMode, $bound->padUrl, $bound->snapshotRev, $bound->isExternal, $bound->body], + ); + $this->assertSame([ + 'file_id' => 10, + 'pad_id' => 'g.new', + 'access_mode' => BindingService::ACCESS_PROTECTED, + 'snapshot_rev' => -1, + 'updated_at' => '2033-05-18T03:33:20+00:00', + 'pad_url' => self::PAD_BASE . 'g.new', + ], array_intersect_key($bound->frontmatter, array_flip(['file_id', 'pad_id', 'access_mode', 'pad_url', 'snapshot_rev', 'updated_at']))); + $this->assertSame(PadFileService::FORMAT_V1, $bound->frontmatter['format']); + + $own = self::pad('g.new', BindingService::ACCESS_PROTECTED); + $this->assertSame($own, $this->boundPads($bindings)->resolve(10, $own)); + } + + /** + * For a caller that holds the row, whatever its state: only a file that + * names the pad the row replaced is read as naming the row's. Neither + * the row's own pad nor any other is touched; whether those may pass is + * the caller's rule. + */ + public function testFollowingRowTakesOnlyThePadTheRowReplaced(): void { + $resolver = $this->boundPads($this->createMock(BindingService::class)); + $cases = [ + 'the pad it replaced' => [self::binding('pad-b', 'pad-a'), 'pad-a', 'pad-b'], + 'the pad it replaced, while it waits' => [self::binding('pad-b', 'pad-a', BindingService::STATE_PENDING_DELETE), 'pad-a', 'pad-b'], + 'the row\'s pad' => [self::binding('pad-b', 'pad-a'), 'pad-b', null], + 'another pad' => [self::binding('pad-b', 'pad-a'), 'pad-c', null], + 'a row that replaced none' => [self::binding('pad-b', null), 'pad-a', null], + // A row that names the pad it replaced has nothing to follow. + 'a row that replaced its own pad' => [self::binding('pad-a', 'pad-a'), 'pad-a', null], + ]; + foreach ($cases as $case => [$binding, $padId, $follows]) { + $pad = self::pad($padId, BindingService::ACCESS_PUBLIC, rev: 7); + $bound = $resolver->followingRow($pad, $binding); + if ($follows === null) { + $this->assertSame($pad, $bound, $case); + } else { + $this->assertSame([$follows, -1], [$bound->padId, $bound->snapshotRev], $case); + } + } + } + + /** A file written to name its row's pad is said once, as a warning. */ + public function testARepairIsWarnedOf(): void { + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->once())->method('warning')->with( + 'A .pad file named the pad its row replaced; it now names the row\'s.', + ['app' => 'etherpad_nextcloud', 'fileId' => 10], + ); + + $this->boundPads($this->createMock(BindingService::class), $logger)->repaired(10); + } + + /** @return array */ + private static function row(int $fileId, string $padId, string $state = BindingService::STATE_ACTIVE, string $mode = BindingService::ACCESS_PUBLIC, ?string $replaced = null): array { + return [ + 'file_id' => $fileId, + 'pad_id' => $padId, + 'access_mode' => $mode, + 'state' => $state, + 'deleted_at' => null, + 'updated_at' => 100, + 'replaced_pad_id' => $replaced, + ]; + } + + private static function binding(string $padId, ?string $replaced, string $state = BindingService::STATE_ACTIVE): Binding { + return new Binding(10, $padId, BindingService::ACCESS_PUBLIC, $state, replacedPadId: $replaced); + } + + private static function pad(string $padId, string $accessMode, int $rev = -1, string $body = ''): ParsedPadFile { + return new ParsedPadFile( + frontmatter: ['format' => PadFileService::FORMAT_V1, 'file_id' => 10, 'pad_id' => $padId, 'access_mode' => $accessMode, 'snapshot_rev' => $rev, 'updated_at' => '2026-01-01T00:00:00+00:00'], + body: $body, + padId: $padId, + accessMode: $accessMode, + padUrl: '', + isExternal: false, + snapshotRev: $rev, + ); + } +} diff --git a/tests/phpunit/unit/LifecycleServiceTest.php b/tests/phpunit/unit/LifecycleServiceTest.php index d94fc54a..166fd9f0 100644 --- a/tests/phpunit/unit/LifecycleServiceTest.php +++ b/tests/phpunit/unit/LifecycleServiceTest.php @@ -22,6 +22,8 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use OCA\EtherpadNextcloud\Tests\Support\FixedClock; +use OCA\EtherpadNextcloud\Tests\Support\InMemoryBindingTable; use OCA\EtherpadNextcloud\Tests\Support\PadFiles; use OCA\EtherpadNextcloud\Tests\Support\WiresTheLifecycle; @@ -226,6 +228,44 @@ public function testNoSnapshotIsTakenThatTheFileHasAlready(): void { } + /** + * A file that still names the pad its row replaced, at that pad's higher + * revision, holds nothing of the row's pad: the trash writes the row's + * pad into it, under the row's name, and the pad and row go as for any + * file. Held against the old pad's revision, the row's pad looked behind, + * and the file went to the trash with the old pad's text and name. + */ + public function testATrashOfAFileNamingThePadItsRowReplacedTakesTheRowsPad(): void { + $table = new InMemoryBindingTable([[ + 'file_id' => 110, 'pad_id' => 'pad-new', 'access_mode' => BindingService::ACCESS_PUBLIC, + 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'updated_at' => 100, 'replaced_pad_id' => 'pad-old', + ]]); + $padFiles = new PadFileService(new FixedClock()); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId); + $etherpadClient->method('getRevisionsCount')->with('pad-new')->willReturn(3); + $etherpadClient->method('getText')->with('pad-new')->willReturn('Text of the new pad'); + $etherpadClient->method('getHTML')->with('pad-new')->willReturn('

Text of the new pad

'); + $etherpadClient->expects($this->once())->method('deletePad')->with('pad-new'); + $written = null; + $file = $this->createMock(File::class); + $file->method('getId')->willReturn(110); + $file->method('getName')->willReturn('Old.pad'); + $file->method('getContent')->willReturn($padFiles->buildInitialDocument(110, 'pad-old', BindingService::ACCESS_PUBLIC, new PadSnapshot('Text of the old pad', '

Text of the old pad

', 50), 'https://pad.example.test/p/pad-old')); + $file->expects($this->once())->method('putContent')->willReturnCallback(static function (string $content) use (&$written): void { + $written = $content; + }); + + $result = $this->lifecycleService(bindings: new BindingService($table, new FixedClock()), etherpad: $etherpadClient, padFiles: $padFiles)->handleTrash($file); + + $this->assertSame([LifecycleResult::TRASHED, true, false], [$result['status'], $result['snapshot_persisted'], $result['delete_pending']]); + $this->assertSame([], $table->rows); + $pad = $padFiles->readPad((string)$written); + $this->assertSame(['pad-new', 3, 'https://pad.example.test/p/pad-new'], [$pad->padId, $pad->snapshotRev, $pad->padUrl]); + $this->assertStringContainsString('Text of the new pad', $pad->body); + $this->assertStringNotContainsString('Text of the old pad', $pad->body); + } + private function buildTrashService(BindingService $bindingService, EtherpadClient $etherpadClient, bool $deleteOnTrash = true, ?LoggerInterface $logger = null, ?UserNodeResolver $nodes = null): LifecycleService { return $this->lifecycleService( bindings: $bindingService, diff --git a/tests/phpunit/unit/LivePadHtmlFetcherTest.php b/tests/phpunit/unit/LivePadHtmlFetcherTest.php index 6b5b2394..9ab6a00b 100644 --- a/tests/phpunit/unit/LivePadHtmlFetcherTest.php +++ b/tests/phpunit/unit/LivePadHtmlFetcherTest.php @@ -4,8 +4,9 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; +use OCA\EtherpadNextcloud\Exception\BindingMismatchException; use OCA\EtherpadNextcloud\Exception\ExternalPadException; -use OCA\EtherpadNextcloud\Exception\BindingException; +use OCA\EtherpadNextcloud\Exception\MissingBindingException; use OCA\EtherpadNextcloud\Exception\WaitingBindingException; use OCA\EtherpadNextcloud\Exception\EtherpadClientException; use OCA\EtherpadNextcloud\Service\BindingService; @@ -14,9 +15,14 @@ use OCA\EtherpadNextcloud\Service\LivePadHtmlFetcher; use OCA\EtherpadNextcloud\Service\ParsedPadFile; use OCA\EtherpadNextcloud\Service\SnapshotHtmlSanitizer; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; +use OCA\EtherpadNextcloud\Tests\Support\FixedClock; +use OCA\EtherpadNextcloud\Tests\Support\InMemoryBindingTable; use PHPUnit\Framework\TestCase; class LivePadHtmlFetcherTest extends TestCase { + use BuildsBoundPads; + public function testOwnPadIsFetchedOverTheApiAndSanitized(): void { $client = $this->createMock(EtherpadClient::class); $client->expects($this->once()) @@ -30,42 +36,52 @@ public function testOwnPadIsFetchedOverTheApiAndSanitized(): void { $this->assertFalse($result->isEmpty); } - /** A row that still waits reaches the caller as it was thrown, and nothing is fetched. */ + /** A row that still waits reaches the caller, and nothing is fetched. */ public function testAWaitingBindingReachesTheCallerAsItIs(): void { - $waiting = new WaitingBindingException('Pad binding is not active.'); - $bindings = $this->createMock(BindingService::class); - $bindings->method('assertConsistentMapping')->willThrowException($waiting); $client = $this->createMock(EtherpadClient::class); $client->expects($this->never())->method('getHTMLForPreview'); - $this->expectExceptionObject($waiting); - $this->buildFetcher($client, bindingService: $bindings)->fetchForPadFile($this->pad(padId: 'pad-a'), 138); + $this->expectException(WaitingBindingException::class); + $this->buildFetcher($client, rows: [self::row('g.group$pad', BindingService::STATE_RESTORE_PENDING)])->fetchForPadFile($this->pad(), 138); } /** * The check that stops a `.pad` file from pointing this app's API key * at somebody else's pad. It runs on every content request, not only * when the file was opened — the file is writable by whoever may write - * it, and the fetch is what does the reading. + * it, and the fetch is what does the reading. Neither a pad the row + * never named nor one without a row is read. */ - public function testAPadIdThatDoesNotMatchTheBindingIsNotFetched(): void { - $bindings = $this->createMock(BindingService::class); - $bindings->expects($this->once()) - ->method('assertConsistentMapping') - ->with(138, 'g.group$somebody-elses', BindingService::ACCESS_PROTECTED) - ->willThrowException(new BindingException('pad_id does not match the stored binding.')); + public function testAPadNoRowGivesTheFileIsNotFetched(): void { + foreach ([ + 'another pad than the row\'s' => [[self::row('g.group$pad', replaced: 'g.group$before')], BindingMismatchException::class], + 'no row at all' => [[], MissingBindingException::class], + ] as $case => [$rows, $refusal]) { + $client = $this->createMock(EtherpadClient::class); + $client->expects($this->never())->method('getHTMLForPreview'); + try { + $this->buildFetcher($client, rows: $rows)->fetchForPadFile($this->pad(padId: 'g.group$somebody-elses'), 138); + $this->fail($case . ': fetched'); + } catch (\Exception $e) { + $this->assertInstanceOf($refusal, $e, $case); + } + } + } + /** A file that still names the pad its row replaced shows the row's. */ + public function testAFileNamingThePadItsRowReplacedShowsTheRowsPad(): void { $client = $this->createMock(EtherpadClient::class); - $client->expects($this->never())->method('getHTMLForPreview'); + $client->expects($this->once())->method('getHTMLForPreview')->with('g.group$new')->willReturn('

Now

'); + + $result = $this->buildFetcher($client, rows: [self::row('g.group$new', replaced: 'g.group$old')]) + ->fetchForPadFile($this->pad(padId: 'g.group$old'), 138); - $this->expectException(BindingException::class); - $this->buildFetcher($client, bindingService: $bindings) - ->fetchForPadFile($this->pad(padId: 'g.group$somebody-elses'), 138); + $this->assertSame('

Now

', $result->html); } public function testForeignPadIsFetchedOverItsPublicExportWithoutABindingCheck(): void { $bindings = $this->createMock(BindingService::class); - $bindings->expects($this->never())->method('assertConsistentMapping'); + $bindings->expects($this->never())->method('findByFileId'); $external = $this->createMock(ExternalPadExportFetcher::class); $external->expects($this->once()) @@ -73,7 +89,7 @@ public function testForeignPadIsFetchedOverItsPublicExportWithoutABindingCheck() ->with('https://remote.example/p/Test') ->willReturn('

Remote

'); - $result = $this->buildFetcher(externalPadExportFetcher: $external, bindingService: $bindings) + $result = $this->buildFetcher(externalPadExportFetcher: $external, bindings: $bindings) ->fetchForPadFile($this->externalPad(), 138); $this->assertSame('

Remote

', $result->html); @@ -142,16 +158,29 @@ private function externalPad( ); } + /** File 138's row. */ + private static function row(string $padId, string $state = BindingService::STATE_ACTIVE, ?string $replaced = null): array { + return ['file_id' => 138, 'pad_id' => $padId, 'access_mode' => BindingService::ACCESS_PROTECTED, 'state' => $state, 'deleted_at' => null, 'updated_at' => 100, 'replaced_pad_id' => $replaced]; + } + + /** + * Over $rows, or $bindings; unless a test says otherwise, file 138's row + * names the pad the file does. + * + * @param ?list> $rows + */ private function buildFetcher( ?EtherpadClient $etherpadClient = null, ?ExternalPadExportFetcher $externalPadExportFetcher = null, - ?BindingService $bindingService = null, + ?array $rows = null, + ?BindingService $bindings = null, ): LivePadHtmlFetcher { + $bindings ??= new BindingService(new InMemoryBindingTable($rows ?? [self::row('g.group$pad')]), new FixedClock()); return new LivePadHtmlFetcher( $etherpadClient ?? $this->createMock(EtherpadClient::class), $externalPadExportFetcher ?? $this->createMock(ExternalPadExportFetcher::class), new SnapshotHtmlSanitizer(), - $bindingService ?? $this->createMock(BindingService::class), + $this->boundPads($bindings), ); } } diff --git a/tests/phpunit/unit/OwedDeletionsTest.php b/tests/phpunit/unit/OwedDeletionsTest.php index d38f8f03..a368763b 100644 --- a/tests/phpunit/unit/OwedDeletionsTest.php +++ b/tests/phpunit/unit/OwedDeletionsTest.php @@ -21,6 +21,7 @@ use OCA\EtherpadNextcloud\Service\TrashSnapshotWriters; use OCA\EtherpadNextcloud\Service\UserNodeResolver; use OCA\EtherpadNextcloud\Service\WaitingBinding; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCA\EtherpadNextcloud\Tests\Support\PadFiles; use OCP\Files\File; @@ -37,6 +38,7 @@ * Unanswered when Etherpad gave none. */ class OwedDeletionsTest extends TestCase { + use BuildsBoundPads; use PadFiles; /** @@ -90,6 +92,27 @@ public function testFinishTrashWritesTheSnapshotThenTakesTheRowBeforeThePad(): v $this->assertSame(['snapshot', 'row', 'pad'], $order); } + /** + * A trashed file that still names the pad its row replaced holds no + * revision of the row's pad, however high the one it has: the row's pad + * is written into it and goes as any other. Held against the old pad's + * revision, the row's pad looked behind, and the row was let go with the + * pad left over and the file holding the old pad's text. + */ + public function testATrashedFileNamingThePadItsRowReplacedGetsTheRowsPad(): void { + // The formatter reads the file as naming 'pad'. + $bindingService = $this->pendingTrashRow(111, 'pad-new', replaced: 'pad'); + $bindingService->expects($this->once())->method('deleteInState')->with(111, 'pad-new', BindingService::STATE_PENDING_DELETE)->willReturn(true); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('getRevisionsCount')->with('pad-new')->willReturn(5); + $etherpadClient->expects($this->once())->method('getText')->with('pad-new')->willReturn('the row\'s pad'); + $etherpadClient->expects($this->once())->method('deletePad')->with('pad-new'); + $file = $this->trashedFile(111, snapshotRev: 50); + $file->expects($this->once())->method('putContent')->with('doc-after'); + + $this->assertSame(SettleOutcome::Settled, $this->owed($bindingService, $etherpadClient)->finishTrash($file, new RunBudget(new FixedClock(), 20.0))); + } + /** * A restore that took the row between the sweep's read and its delete * has the pad: the sweep's delete finds no row in pending_delete, and @@ -541,9 +564,9 @@ public function testNoCallIsStartedOnceTheReadTookTheRunsTime(): void { } /** Owed since $deletedAt; $updatedAt past that once a run moved it back. */ - private function pendingTrashRow(int $fileId, string $padId, int $updatedAt = 100, ?int $deletedAt = 100): BindingService&MockObject { + private function pendingTrashRow(int $fileId, string $padId, int $updatedAt = 100, ?int $deletedAt = 100, ?string $replaced = null): BindingService&MockObject { $bindingService = $this->createMock(BindingService::class); - $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $padId, accessMode: BindingService::ACCESS_PUBLIC, state: BindingService::STATE_PENDING_DELETE, deletedAt: $deletedAt, updatedAt: $updatedAt)); + $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $padId, accessMode: BindingService::ACCESS_PUBLIC, state: BindingService::STATE_PENDING_DELETE, deletedAt: $deletedAt, updatedAt: $updatedAt, replacedPadId: $replaced)); return $bindingService; } @@ -583,6 +606,7 @@ private function owed(BindingService $bindings, EtherpadClient $etherpad, bool $ $nodes ?? $this->createMock(UserNodeResolver::class), $logger, new FixedClock(), + $this->boundPads($bindings, $logger), ); } } diff --git a/tests/phpunit/unit/PadLifecycleControllerTest.php b/tests/phpunit/unit/PadLifecycleControllerTest.php index 8255a658..f3058b76 100644 --- a/tests/phpunit/unit/PadLifecycleControllerTest.php +++ b/tests/phpunit/unit/PadLifecycleControllerTest.php @@ -33,8 +33,10 @@ use OCP\Lock\LockedException; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; class PadLifecycleControllerTest extends TestCase { + use BuildsBoundPads; use \OCA\EtherpadNextcloud\Tests\Support\BuildsErrorMappers; public function testSyncByIdRejectsInvalidFileId(): void { @@ -86,8 +88,9 @@ public function testSyncByIdRetriesLockedWriteAndEventuallySucceeds(): void { $bindingService = $this->createMock(BindingService::class); $bindingService->expects($this->once()) - ->method('assertConsistentMapping') - ->with(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PROTECTED); + ->method('findByFileId') + ->with(138) + ->willReturn(new Binding(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->method('getRevisionsCount')->with('g.ABCDEFGHIJKLMNOP$pad-1')->willReturn(5); @@ -140,7 +143,7 @@ public function testSyncByIdReturnsLockedWhenWriteRemainsLocked(): void { $padFileService->method('withExportSnapshot')->with($this->identicalTo($parsedPad), new PadSnapshot('hello', '

hello

', 5))->willReturn('updated-content'); $bindingService = $this->createMock(BindingService::class); - $bindingService->method('assertConsistentMapping'); + $bindingService->method('findByFileId')->willReturn(new Binding(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->method('getRevisionsCount')->willReturn(5); @@ -189,7 +192,7 @@ public function testSyncByIdForcedProtectedSyncDoesNotRewriteWhenRevisionIsUncha $padFileService->method('getSnapshotPartsFromBody')->with($parsedPad->body)->willReturn(['text' => 'hello', 'html' => '

hello

']); $bindingService = $this->createMock(BindingService::class); - $bindingService->method('assertConsistentMapping'); + $bindingService->method('findByFileId')->willReturn(new Binding(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->expects($this->once()) @@ -246,7 +249,8 @@ public function testSyncByIdForcedExternalSyncDoesNotRewriteWhenTextIsUnchanged( $padFileService->method('getSnapshotPartsFromBody')->with($parsedPad->body)->willReturn(['text' => 'same text', 'html' => '']); $bindingService = $this->createMock(BindingService::class); - $bindingService->method('assertConsistentMapping'); + // A pad on another server has no row to ask. + $bindingService->expects($this->never())->method('findByFileId'); $externalPadExportFetcher = $this->createMock(ExternalPadExportFetcher::class); $externalPadExportFetcher->expects($this->once()) @@ -526,7 +530,7 @@ private function buildController( $userNodeResolver = new UserNodeResolver($resolvedRootFolder, $this->createMock(LoggerInterface::class)); $lockRetryService = $this->buildNoSleepLockRetryService(); $padMetadataService = new PadMetadataService($resolvedPadFileService, $padPaths, $userNodeResolver, $lockRetryService, $resolvedEtherpadClient, $resolvedExternalPadExportFetcher, $resolvedBindingService, $logger); - $padSyncService = new PadSyncService($resolvedPadFileService, $userNodeResolver, $lockRetryService, $resolvedBindingService, $resolvedEtherpadClient, $resolvedExternalPadExportFetcher, $logger); + $padSyncService = new PadSyncService($resolvedPadFileService, $userNodeResolver, $lockRetryService, $this->boundPads($resolvedBindingService, $logger), $resolvedEtherpadClient, $resolvedExternalPadExportFetcher, $logger); $padLifecycleOperations = $padLifecycleOperations ?? $this->createMock(LifecycleService::class); $urlGenerator = $this->createMock(IURLGenerator::class); diff --git a/tests/phpunit/unit/PadOpenServiceTest.php b/tests/phpunit/unit/PadOpenServiceTest.php index bc1b61c5..e9c1f8c4 100644 --- a/tests/phpunit/unit/PadOpenServiceTest.php +++ b/tests/phpunit/unit/PadOpenServiceTest.php @@ -4,9 +4,11 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; +use OCA\EtherpadNextcloud\Exception\BindingMismatchException; use OCA\EtherpadNextcloud\Exception\ExternalPadException; use OCA\EtherpadNextcloud\Exception\EtherpadClientException; use OCA\EtherpadNextcloud\Exception\WaitingBindingException; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\ExternalPadExportFetcher; @@ -19,12 +21,16 @@ use OCA\EtherpadNextcloud\Service\UserNodeResolver; use OCA\EtherpadNextcloud\Util\PathNormalizer; use OCA\EtherpadNextcloud\Service\ParsedPadFile; +use OCA\EtherpadNextcloud\Service\PadSnapshot; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; +use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCA\EtherpadNextcloud\Tests\Support\SettlesOnOpen; use OCP\Files\File; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; class PadOpenServiceTest extends TestCase { + use BuildsBoundPads; use SettlesOnOpen; public function testOpenByPathRejectsEmptyNormalizedPath(): void { @@ -155,10 +161,8 @@ public function testFallsBackToTheReadOnlyViewWhenTheReadOnlyUrlCannotBeResolved * address, and lets the mapper say why. */ public function testAWaitingBindingReachesTheCallerAsItIs(): void { - $waiting = new WaitingBindingException('Pad binding is not active.'); $bindings = $this->createMock(BindingService::class); - $bindings->method('assertConsistentMapping')->willThrowException($waiting); - $bindings->method('findByFileId')->willReturn(self::waitingRow(138, 'pad-1', BindingService::ACCESS_PUBLIC)); + $bindings->method('findByFileId')->willReturn(self::waitingRow(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PROTECTED)); $restores = $this->createMock(RestoreService::class); $restores->expects($this->once())->method('settleOpenedFile')->willReturn(SettleOutcome::Unanswered); $session = $this->createMock(PadSessionService::class); @@ -166,7 +170,7 @@ public function testAWaitingBindingReachesTheCallerAsItIs(): void { $client = $this->createMock(EtherpadClient::class); $client->expects($this->never())->method($this->anything()); - $this->expectExceptionObject($waiting); + $this->expectException(WaitingBindingException::class); $this->openWith( BindingService::ACCESS_PROTECTED, @@ -184,13 +188,12 @@ public function testAWaitingBindingReachesTheCallerAsItIs(): void { */ public function testAWaitingRowTheOpenTakesBackOpens(): void { $bindings = $this->createMock(BindingService::class); - $calls = 0; - $bindings->expects($this->exactly(2))->method('assertConsistentMapping')->willReturnCallback(static function () use (&$calls): void { - if (++$calls === 1) { - throw new WaitingBindingException('Pad binding is not active.'); - } - }); - $bindings->method('findByFileId')->willReturn(self::waitingRow(138, 'pad-1', BindingService::ACCESS_PUBLIC)); + // Waiting at the first look and while it is decided, taken back after. + $bindings->method('findByFileId')->willReturnOnConsecutiveCalls( + self::waitingRow(138, 'pad-1', BindingService::ACCESS_PUBLIC), + self::waitingRow(138, 'pad-1', BindingService::ACCESS_PUBLIC), + new Binding(138, 'pad-1', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE), + ); $restores = $this->createMock(RestoreService::class); $restores->expects($this->once()) ->method('settleOpenedFile') @@ -211,6 +214,74 @@ public function testAWaitingRowTheOpenTakesBackOpens(): void { $this->assertSame('https://pad.example.test/p/pad-1', $target->url); } + /** + * A file that still names the pad its row replaced opens the row's pad. + * Whoever may write the file gets it rewritten to name that pad, once, + * and only over what the open read; anyone else opens the row's pad + * and leaves the file as it is. A rewrite that fails keeps nothing from + * opening. + */ + public function testAFileNamingThePadItsRowReplacedOpensTheRowsPad(): void { + foreach ([ + 'may write' => [true, false, true, false], + 'may only read' => [false, false, false, false], + 'changed meanwhile' => [true, true, false, false], + 'the write fails' => [true, false, false, true], + ] as $case => [$updateable, $changedMeanwhile, $rewritten, $writeFails]) { + $formatter = new PadFileService(new FixedClock()); + $stale = $formatter->withExportSnapshot( + $formatter->readPad($formatter->buildInitialDocument(138, 'old-pad', BindingService::ACCESS_PUBLIC)), + new PadSnapshot('old text', '', 500), + ); + $file = $this->createMock(File::class); + $file->method('getId')->willReturn(138); + $file->method('isUpdateable')->willReturn($updateable); + $file->method('getContent')->willReturnOnConsecutiveCalls($stale, $changedMeanwhile ? $stale . 'edited' : $stale); + $written = null; + $file->method('putContent')->willReturnCallback(static function (string $content) use (&$written, $writeFails): void { + if ($writeFails) { + throw new \RuntimeException('disk full'); + } + $written = $content; + }); + $bindings = $this->createMock(BindingService::class); + $bindings->method('findByFileId')->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $client = $this->createMock(EtherpadClient::class); + $client->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId); + $client->method('getReadOnlyPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId . '/ro'); + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($rewritten ? $this->once() : $this->never())->method('warning')->with('A .pad file named the pad its row replaced; it now names the row\'s.', $this->anything()); + $debugs = []; + $logger->method('debug')->willReturnCallback(static function (string $message) use (&$debugs): void { + $debugs[] = $message; + }); + + $target = $this->openService($formatter, $file, $bindings, $client, $logger)->openById('alice', 'Alice', 138); + + $this->assertSame('r-new-pad', $target->padId, $case); + $this->assertStringStartsWith('https://pad.example.test/p/r-new-pad', $target->url, $case); + $this->assertSame($writeFails, in_array('Could not rewrite a .pad file to its row\'s pad; the row\'s pad opens all the same.', $debugs, true), $case); + if ($rewritten) { + $after = $formatter->readPad((string)$written); + $this->assertSame(['r-new-pad', -1, 'old text'], [$after->padId, $after->snapshotRev, $formatter->getSnapshotPartsFromBody($after->body)['text']], $case); + } else { + $this->assertNull($written, $case); + } + } + } + + /** A file that names any other pad than its row's, or the one that replaced, opens nothing. */ + public function testAFileNamingAPadItsRowDoesNotGiveOpensNothing(): void { + $bindings = $this->createMock(BindingService::class); + $bindings->method('findByFileId')->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $session = $this->createMock(PadSessionService::class); + $session->expects($this->never())->method($this->anything()); + + $this->expectException(BindingMismatchException::class); + + $this->openWith(BindingService::ACCESS_PROTECTED, updateable: true, padSessionService: $session, padId: 'g.ABCDEFGHIJKLMNOP$somebody-elses', bindingService: $bindings); + } + /** The rule for an external pad's metadata is ParsedPadFile::externalPadUrl()'s; the open holds it. */ public function testAForeignPadWithBrokenMetadataIsTheLinksProblem(): void { $this->expectException(ExternalPadException::class); @@ -247,22 +318,47 @@ private function openWith( )); $client = $etherpadClient ?? $this->createMock(EtherpadClient::class); + // Unless a test says otherwise, the file's row names the pad the file does. + if ($bindingService === null) { + $bindingService = $this->createMock(BindingService::class); + $bindingService->method('findByFileId')->willReturn(new Binding(138, $padId, $accessMode, BindingService::STATE_ACTIVE)); + } $service = new PadOpenService( $padFileService, $this->createMock(PathNormalizer::class), $userNodeResolver, new PadFileLockRetryService(static function (int $delay): void { }), - $this->settleOnOpen($bindingService ?? $this->createMock(BindingService::class), $restoreService), + $this->settleOnOpen($bindingService, $restoreService), $client, $this->createMock(ExternalPadExportFetcher::class), $padSessionService ?? $this->createMock(PadSessionService::class), $this->createMock(LoggerInterface::class), + $this->boundPads($bindingService), ); return $service->openById('alice', 'Alice', 138); } + /** Over a real PadFileService, a file of its own and rows of its own. */ + private function openService(PadFileService $formatter, File $file, BindingService $bindings, EtherpadClient $client, LoggerInterface $logger): PadOpenService { + $userNodeResolver = $this->createMock(UserNodeResolver::class); + $userNodeResolver->method('resolveUserFileNodeById')->willReturn($file); + return new PadOpenService( + $formatter, + $this->createMock(PathNormalizer::class), + $userNodeResolver, + new PadFileLockRetryService(static function (int $delay): void { + }), + $this->settleOnOpen($bindings, logger: $logger), + $client, + $this->createMock(ExternalPadExportFetcher::class), + $this->createMock(PadSessionService::class), + $logger, + $this->boundPads($bindings, $logger), + ); + } + private function buildService(PathNormalizer $padPaths, UserNodeResolver $userNodeResolver): PadOpenService { $padFileService = $this->createMock(PadFileService::class); return new PadOpenService( @@ -275,6 +371,7 @@ private function buildService(PathNormalizer $padPaths, UserNodeResolver $userNo $this->createMock(ExternalPadExportFetcher::class), $this->createMock(PadSessionService::class), $this->createMock(LoggerInterface::class), + $this->boundPads($this->createMock(BindingService::class)), ); } } diff --git a/tests/phpunit/unit/PadSessionControllerTest.php b/tests/phpunit/unit/PadSessionControllerTest.php index d2c117da..0968f153 100644 --- a/tests/phpunit/unit/PadSessionControllerTest.php +++ b/tests/phpunit/unit/PadSessionControllerTest.php @@ -7,6 +7,7 @@ use OCA\EtherpadNextcloud\Controller\PadControllerErrorMapper; use OCA\EtherpadNextcloud\Controller\PadSessionController; use OCA\EtherpadNextcloud\Service\AppConfigService; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\ExternalPadExportFetcher; @@ -32,10 +33,12 @@ use OCP\Lock\LockedException; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; class PadSessionControllerTest extends TestCase { use \OCA\EtherpadNextcloud\Tests\Support\BuildsErrorMappers; + use BuildsBoundPads; use SettlesOnOpen; public function testOpenByIdRejectsInvalidFileId(): void { @@ -89,9 +92,10 @@ public function testOpenByIdRetriesLockedReadAndEventuallySucceeds(): void { )); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->once()) - ->method('assertConsistentMapping') - ->with(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PUBLIC); + $bindingService->expects($this->atLeastOnce()) + ->method('findByFileId') + ->with(138) + ->willReturn(new Binding(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->expects($this->once()) @@ -129,6 +133,7 @@ public function testOpenByIdRetriesLockedReadAndEventuallySucceeds(): void { $this->createMock(ExternalPadExportFetcher::class), $this->createMock(PadSessionService::class), $logger, + $this->boundPads($bindingService, $logger), ); $l10n = $this->createMock(\OCP\IL10N::class); $l10n->method('t')->willReturnCallback(static fn (string $text, array $params = []): string => $text); @@ -216,7 +221,8 @@ public function testOpenByIdReturnsExternalPadUrlForExternalPads(): void { $padFileService->expects($this->never())->method('getSnapshotPartsFromBody'); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->never())->method('assertConsistentMapping'); + // A pad on another server has no row to ask. + $bindingService->expects($this->never())->method('findByFileId'); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->expects($this->never())->method('buildPadUrl'); @@ -253,6 +259,7 @@ public function testOpenByIdReturnsExternalPadUrlForExternalPads(): void { $externalPadExportFetcher, $this->createMock(PadSessionService::class), $logger, + $this->boundPads($bindingService, $logger), ); $l10n = $this->createMock(\OCP\IL10N::class); $l10n->method('t')->willReturnCallback(static fn (string $text, array $params = []): string => $text); @@ -363,6 +370,7 @@ private function buildController( $resolvedExternalPadExportFetcher, $this->createMock(PadSessionService::class), $logger, + $this->boundPads($resolvedBindingService, $logger), ); $urlGenerator = $this->createMock(IURLGenerator::class); $urlGenerator->method('linkToRoute')->willReturnCallback( diff --git a/tests/phpunit/unit/PadSyncServiceTest.php b/tests/phpunit/unit/PadSyncServiceTest.php index 3325548a..7ca916b7 100644 --- a/tests/phpunit/unit/PadSyncServiceTest.php +++ b/tests/phpunit/unit/PadSyncServiceTest.php @@ -4,10 +4,13 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; +use OCA\EtherpadNextcloud\Exception\BindingMismatchException; use OCA\EtherpadNextcloud\Exception\NotAPadFileException; use OCA\EtherpadNextcloud\Exception\ExternalPadException; use OCA\EtherpadNextcloud\Exception\WaitingBindingException; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; +use OCA\EtherpadNextcloud\Service\BoundPadResolver; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\ExternalPadExportFetcher; use OCA\EtherpadNextcloud\Service\PadFileLockRetryService; @@ -16,12 +19,16 @@ use OCA\EtherpadNextcloud\Service\PadSnapshot; use OCA\EtherpadNextcloud\Service\ParsedPadFile; use OCA\EtherpadNextcloud\Service\UserNodeResolver; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCP\Files\File; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; class PadSyncServiceTest extends TestCase { + use BuildsBoundPads; + public function testSyncStatusReturnsUnavailableForExternalPads(): void { $file = $this->createMock(File::class); $file->method('getContent')->willReturn('frontmatter'); @@ -44,7 +51,7 @@ public function testSyncStatusReturnsUnavailableForExternalPads(): void { )); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->never())->method('assertConsistentMapping'); + $bindingService->expects($this->never())->method('findByFileId'); $result = $this->buildService($padFileService, $userNodeResolver, $bindingService) ->syncStatusById('alice', 138); @@ -60,7 +67,6 @@ public function testSyncStatusReturnsUnavailableForExternalPads(): void { * a service that wrapped it would take the code away. */ public function testAWaitingBindingReachesTheCallerAsItIs(): void { - $waiting = new WaitingBindingException('Pad binding is not active.'); $file = $this->createMock(File::class); $file->method('getName')->willReturn('Notes.pad'); $file->method('getContent')->willReturn('frontmatter'); @@ -69,15 +75,15 @@ public function testAWaitingBindingReachesTheCallerAsItIs(): void { $padFileService = $this->createMock(PadFileService::class); $padFileService->method('readPad')->willReturn(new ParsedPadFile([], '', 'pad-a', BindingService::ACCESS_PUBLIC, '', false, 3)); $bindingService = $this->createMock(BindingService::class); - $bindingService->method('assertConsistentMapping')->willThrowException($waiting); + $bindingService->method('findByFileId')->willReturn(new Binding(138, 'pad-a', BindingService::ACCESS_PUBLIC, BindingService::STATE_RESTORE_PENDING)); $service = $this->buildService($padFileService, $userNodeResolver, $bindingService); foreach (['sync' => static fn () => $service->syncById('alice', 138, false), 'status' => static fn () => $service->syncStatusById('alice', 138)] as $case => $call) { try { $call(); $this->fail($case . ': nothing thrown'); - } catch (WaitingBindingException $e) { - $this->assertSame($waiting, $e, $case); + } catch (WaitingBindingException) { + $this->addToAssertionCount(1); } } } @@ -106,8 +112,9 @@ public function testSyncStatusReportsOutOfSyncInternalPad(): void { $bindingService = $this->createMock(BindingService::class); $bindingService->expects($this->once()) - ->method('assertConsistentMapping') - ->with(138, 'g.ABC$pad', BindingService::ACCESS_PROTECTED); + ->method('findByFileId') + ->with(138) + ->willReturn(new Binding(138, 'g.ABC$pad', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->expects($this->once()) @@ -266,7 +273,7 @@ public function testSyncExternalPadStoresOnlyTextSnapshot(): void { ->willReturn('updated-frontmatter'); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->never())->method('assertConsistentMapping'); + $bindingService->expects($this->never())->method('findByFileId'); $etherpadClient = $this->createMock(EtherpadClient::class); $externalPadExportFetcher = $this->createMock(ExternalPadExportFetcher::class); @@ -342,6 +349,101 @@ public function testTheFirstSyncAfterARestoreFromTheSnapshotFetchesTheNewPad(): $this->assertSame('new edit', $formatter->getSnapshotPartsFromBody($formatter->readPad((string)$written)->body)['text']); } + /** + * A file that still names the pad its row replaced has no revision of + * the row's pad, however high its own: the sync writes the row's pad + * into it, name and all, and says so once. Its status is out of sync. + */ + public function testAFileNamingThePadItsRowReplacedIsSyncedFromTheRowsPad(): void { + $formatter = new PadFileService(new FixedClock()); + $stale = $formatter->withExportSnapshot( + $formatter->readPad($formatter->buildInitialDocument(138, 'old-pad', BindingService::ACCESS_PUBLIC)), + new PadSnapshot('old text', '', 500), + ); + $file = $this->createMock(File::class); + $file->method('getName')->willReturn('Notes.pad'); + $file->method('getContent')->willReturn($stale); + $userNodeResolver = $this->createMock(UserNodeResolver::class); + $userNodeResolver->method('resolveUserFileNodeById')->with('alice', 138)->willReturn($file); + $userNodeResolver->method('toUserAbsolutePath')->willReturn('/Notes.pad'); + $bindingService = $this->createMock(BindingService::class); + $bindingService->method('findByFileId')->with(138)->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('getRevisionsCount')->with('r-new-pad')->willReturn(3); + $etherpadClient->method('getText')->with('r-new-pad')->willReturn('the pad now'); + $etherpadClient->method('getHTML')->with('r-new-pad')->willReturn('

the pad now

'); + $written = null; + $lockRetryService = $this->createMock(PadFileLockRetryService::class); + $lockRetryService->expects($this->once())->method('putContentWithSyncLockRetry')->willReturnCallback(static function (File $node, string $content) use (&$written): int { + $written = $content; + return 0; + }); + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->once())->method('warning')->with('A .pad file named the pad its row replaced; it now names the row\'s.', $this->anything()); + $service = $this->buildService($formatter, $userNodeResolver, $bindingService, $etherpadClient, $lockRetryService, logger: $logger); + + $status = $service->syncStatusById('alice', 138); + $result = $service->syncById('alice', 138, false); + + $this->assertSame([PadSyncService::STATUS_OUT_OF_SYNC, -1], [$status->status, $status->snapshotRev]); + $this->assertSame([PadSyncService::STATUS_UPDATED, 'r-new-pad'], [$result->status, $result->padId]); + $after = $formatter->readPad((string)$written); + $this->assertSame(['r-new-pad', 3, 'the pad now'], [$after->padId, $after->snapshotRev, $formatter->getSnapshotPartsFromBody($after->body)['text']]); + $this->assertSame(138, (int)$after->frontmatter['file_id']); + } + + /** A file that names its row's pad is synced as ever, whatever pad the row replaced: no repair to speak of. */ + public function testASyncOfAFileNamingItsRowsPadSpeaksOfNoRepair(): void { + $formatter = new PadFileService(new FixedClock()); + $file = $this->createMock(File::class); + $file->method('getName')->willReturn('Notes.pad'); + $file->method('getContent')->willReturn($formatter->withExportSnapshot( + $formatter->readPad($formatter->buildInitialDocument(138, 'r-new-pad', BindingService::ACCESS_PUBLIC)), + new PadSnapshot('text', '', 1), + )); + $userNodeResolver = $this->createMock(UserNodeResolver::class); + $userNodeResolver->method('resolveUserFileNodeById')->willReturn($file); + $userNodeResolver->method('toUserAbsolutePath')->willReturn('/Notes.pad'); + $bindingService = $this->createMock(BindingService::class); + $bindingService->method('findByFileId')->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('getRevisionsCount')->willReturn(3); + $etherpadClient->method('getText')->willReturn('the pad now'); + $etherpadClient->method('getHTML')->willReturn(''); + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->never())->method('warning'); + + $result = $this->buildService($formatter, $userNodeResolver, $bindingService, $etherpadClient, $this->createMock(PadFileLockRetryService::class), logger: $logger)->syncById('alice', 138, false); + + $this->assertSame(PadSyncService::STATUS_UPDATED, $result->status); + } + + /** A file that names any other pad than its row's, or the one that replaced, is not synced. */ + public function testAFileNamingAPadItsRowDoesNotGiveItIsNotSynced(): void { + $file = $this->createMock(File::class); + $file->method('getName')->willReturn('Notes.pad'); + $file->method('getContent')->willReturn('frontmatter'); + $userNodeResolver = $this->createMock(UserNodeResolver::class); + $userNodeResolver->method('resolveUserFileNodeById')->willReturn($file); + $userNodeResolver->method('toUserAbsolutePath')->willReturn('/Notes.pad'); + $padFileService = $this->createMock(PadFileService::class); + $padFileService->method('readPad')->willReturn(new ParsedPadFile(['pad_id' => 'somebody-elses'], '', 'somebody-elses', BindingService::ACCESS_PUBLIC, '', false, 3)); + $bindingService = $this->createMock(BindingService::class); + $bindingService->method('findByFileId')->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->expects($this->never())->method('getRevisionsCount'); + $service = $this->buildService($padFileService, $userNodeResolver, $bindingService, $etherpadClient); + + foreach (['sync' => static fn () => $service->syncById('alice', 138, false), 'status' => static fn () => $service->syncStatusById('alice', 138)] as $case => $call) { + try { + $call(); + $this->fail($case . ': synced'); + } catch (BindingMismatchException) { + $this->addToAssertionCount(1); + } + } + } + /** * An external .pad without a link is the link's problem, not Etherpad * failing; a file that is no .pad is refused as such. @@ -369,6 +471,10 @@ public function testSyncRefusesWhatIsNoPadToSyncForWhatItIs(): void { } } + /** + * Over $bindingService, the real BoundPadResolver; without one, a + * resolver that takes each file as its row has it. + */ private function buildService( ?PadFileService $padFileService = null, ?UserNodeResolver $userNodeResolver = null, @@ -376,15 +482,23 @@ private function buildService( ?EtherpadClient $etherpadClient = null, ?PadFileLockRetryService $lockRetryService = null, ?ExternalPadExportFetcher $externalPadExportFetcher = null, + ?LoggerInterface $logger = null, ): PadSyncService { + $logger ??= $this->createMock(LoggerInterface::class); + if ($bindingService !== null) { + $boundPads = $this->boundPads($bindingService, $logger, $padFileService instanceof MockObject ? null : $padFileService); + } else { + $boundPads = $this->createMock(BoundPadResolver::class); + $boundPads->method('resolve')->willReturnArgument(1); + } return new PadSyncService( $padFileService ?? $this->createMock(PadFileService::class), $userNodeResolver ?? $this->createMock(UserNodeResolver::class), $lockRetryService ?? $this->createMock(PadFileLockRetryService::class), - $bindingService ?? $this->createMock(BindingService::class), + $boundPads, $etherpadClient ?? $this->createMock(EtherpadClient::class), $externalPadExportFetcher ?? $this->createMock(ExternalPadExportFetcher::class), - $this->createMock(LoggerInterface::class), + $logger, ); } } diff --git a/tests/phpunit/unit/PublicPadContextServiceTest.php b/tests/phpunit/unit/PublicPadContextServiceTest.php index 86bc0539..41bd30c5 100644 --- a/tests/phpunit/unit/PublicPadContextServiceTest.php +++ b/tests/phpunit/unit/PublicPadContextServiceTest.php @@ -8,6 +8,7 @@ use OCA\EtherpadNextcloud\Service\RestoreService; use OCA\EtherpadNextcloud\Service\SettleOutcome; use PHPUnit\Framework\MockObject\MockObject; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\LivePadHtml; use OCA\EtherpadNextcloud\Service\LivePadHtmlFetcher; @@ -68,8 +69,9 @@ public function testResolveBuildsPublicPadContextFromCachedShare(): void { $bindings = $this->createMock(BindingService::class); $bindings->expects($this->once()) - ->method('assertConsistentMapping') - ->with(42, 'g.group$pad', BindingService::ACCESS_PROTECTED); + ->method('findByFileId') + ->with(42) + ->willReturn(new Binding(42, 'g.group$pad', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $openService = $this->createMock(PublicPadOpenService::class); $openService->expects($this->once()) @@ -159,14 +161,12 @@ public function testResolveContentResolvesTheShareAgainBeforeFetching(): void { public function testAPublicOpenDecidesAWaitingRowWithTheSharedFile(): void { $file = $this->sharedFile(); $bindings = $this->createMock(BindingService::class); - $calls = 0; - $bindings->expects($this->exactly(2))->method('assertConsistentMapping')->with(42, 'g.group$pad', BindingService::ACCESS_PROTECTED) - ->willReturnCallback(static function () use (&$calls): void { - if (++$calls === 1) { - throw new WaitingBindingException('Pad binding is not active.'); - } - }); - $bindings->method('findByFileId')->willReturn(self::waitingRow(42, 'g.group$pad', BindingService::ACCESS_PROTECTED)); + // Waiting at the first look and while it is decided, taken back after. + $bindings->expects($this->exactly(3))->method('findByFileId')->with(42)->willReturnOnConsecutiveCalls( + self::waitingRow(42, 'g.group$pad', BindingService::ACCESS_PROTECTED), + self::waitingRow(42, 'g.group$pad', BindingService::ACCESS_PROTECTED), + new Binding(42, 'g.group$pad', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE), + ); $restores = $this->createMock(RestoreService::class); $restores->expects($this->once())->method('settleOpenedFile')->with($this->identicalTo($file))->willReturn(SettleOutcome::Settled); $openService = $this->createMock(PublicPadOpenService::class); @@ -178,20 +178,36 @@ public function testAPublicOpenDecidesAWaitingRowWithTheSharedFile(): void { /** A row that still waits once decided reaches the caller as it is, and nothing is opened. */ public function testARowThatStillWaitsReachesTheCallerAsItIs(): void { $file = $this->sharedFile(); - $waiting = new WaitingBindingException('Pad binding is not active.'); $bindings = $this->createMock(BindingService::class); - $bindings->method('assertConsistentMapping')->willThrowException($waiting); $bindings->method('findByFileId')->willReturn(self::waitingRow(42, 'g.group$pad', BindingService::ACCESS_PROTECTED)); $restores = $this->createMock(RestoreService::class); $restores->expects($this->once())->method('settleOpenedFile')->willReturn(SettleOutcome::Unanswered); $openService = $this->createMock(PublicPadOpenService::class); $openService->expects($this->never())->method('open'); - $this->expectExceptionObject($waiting); + $this->expectException(WaitingBindingException::class); $this->contextService($file, $bindings, $restores, $openService)->resolve('token', '', $this->shareOf($file)); } + /** + * A shared file that still names the pad its row replaced opens the + * row's pad for the visitor, who never writes the file. + */ + public function testAPublicOpenOfAFileNamingThePadItsRowReplacedOpensTheRowsPad(): void { + $file = $this->sharedFile(); + $file->expects($this->never())->method('putContent'); + $bindings = $this->createMock(BindingService::class); + $bindings->method('findByFileId')->willReturn(new Binding(42, 'g.group$new', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE, replacedPadId: 'g.group$pad')); + $openService = $this->createMock(PublicPadOpenService::class); + $openService->expects($this->once()) + ->method('open') + ->with($this->callback(static fn (ParsedPadFile $pad): bool => $pad->padId === 'g.group$new' && $pad->snapshotRev === -1), true, 'token') + ->willReturn(new PublicPadOpenTarget('', '', '', true)); + + $this->contextService($file, $bindings, $this->createMock(RestoreService::class), $openService)->resolve('token', '', $this->shareOf($file)); + } + private function sharedFile(): File&MockObject { $file = $this->createMock(File::class); $file->method('getName')->willReturn('Shared.pad'); diff --git a/tests/phpunit/unit/PublicViewerControllerTest.php b/tests/phpunit/unit/PublicViewerControllerTest.php index 95d50ac3..d1b5d204 100644 --- a/tests/phpunit/unit/PublicViewerControllerTest.php +++ b/tests/phpunit/unit/PublicViewerControllerTest.php @@ -6,6 +6,7 @@ use OCA\EtherpadNextcloud\Controller\PublicViewerController; use OCA\EtherpadNextcloud\Controller\PublicViewerControllerErrorMapper; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\ExternalPadExportFetcher; @@ -77,9 +78,10 @@ public function testProtectedReadOnlyPublicShareReturnsSnapshotWithoutEtherpadSe $padFileService->expects($this->never())->method('getSnapshotPartsFromBody'); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->once()) - ->method('assertConsistentMapping') - ->with(42, 'g.abcdefghijklmnop$Shared', BindingService::ACCESS_PROTECTED); + $bindingService->expects($this->atLeastOnce()) + ->method('findByFileId') + ->with(42) + ->willReturn(new Binding(42, 'g.abcdefghijklmnop$Shared', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); $etherpadClient = $this->createMock(EtherpadClient::class); $etherpadClient->expects($this->never())->method('getReadOnlyPadUrl'); @@ -144,7 +146,8 @@ public function testPublicExternalPadShareReturnsNormalizedUrlAndAContentUrl(): $padFileService->expects($this->never())->method('getSnapshotPartsFromBody'); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->never())->method('assertConsistentMapping'); + // A pad on another server has no row to ask. + $bindingService->expects($this->never())->method('findByFileId'); $fetcher = $this->createMock(ExternalPadExportFetcher::class); $fetcher->expects($this->once()) @@ -210,7 +213,8 @@ public function testOpenPadDataRejectsExternalProtectedMetadata(): void { )); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->never())->method('assertConsistentMapping'); + // A pad on another server has no row to ask. + $bindingService->expects($this->never())->method('findByFileId'); $etherpadClient = $this->createMock(EtherpadClient::class); $fetcher = $this->createMock(ExternalPadExportFetcher::class); diff --git a/tests/phpunit/unit/RestoreServiceTest.php b/tests/phpunit/unit/RestoreServiceTest.php index 76b15f21..3b01a789 100644 --- a/tests/phpunit/unit/RestoreServiceTest.php +++ b/tests/phpunit/unit/RestoreServiceTest.php @@ -649,6 +649,33 @@ public function testRestoreReplacesAPadThatIsBehindTheSnapshot(): void { $this->assertSame(1, $this->restoredRevision); } + /** + * A file that still names the pad its row replaced holds no revision of + * the row's pad, however high the one it has: while Etherpad has the + * row's pad, the file gets it back as it is. Held against the old pad's + * revision, the row's pad looked behind and was replaced by a new pad + * made from the old pad's text. + */ + public function testARestoreDoesNotHoldTheRowsPadToThePadItReplaced(): void { + $fileId = 98; + $bindingService = $this->createMock(BindingService::class); + $bindingService->expects($this->once()) + ->method('transition') + ->with($fileId, 'old-pad', BindingService::STATE_PENDING_DELETE, BindingService::STATE_ACTIVE) + ->willReturn(true); + $bindingService->expects($this->never())->method('rebind'); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('getRevisionsCount')->with('old-pad')->willReturn(7); + $etherpadClient->expects($this->never())->method('createPad'); + $file = $this->padFile($fileId, 'Restored.pad'); + $file->expects($this->never())->method('putContent'); + + $result = $this->buildPendingDeleteRestoreService($fileId, 'old-pad', $bindingService, $etherpadClient, snapshotRev: 500, replaced: 'pad-before', filePadId: 'pad-before') + ->restore($file); + + $this->assertSame(['status' => LifecycleResult::RESTORED, 'old_pad_id' => 'old-pad', 'new_pad_id' => 'old-pad'], array_intersect_key($result, array_flip(['status', 'old_pad_id', 'new_pad_id']))); + } + /** * Without the file there is no revision to hold the pad to, so the row * waits for a later check rather than being settled blind. @@ -1131,9 +1158,10 @@ public function testRestoreWithoutBindingRecreatesManagedPublicPad(): void { ->method('findByFileId') ->with($fileId) ->willReturn(null); + // The pad the file named is the one it may still name, in an older version say. $bindingService->expects($this->once()) ->method('createBinding') - ->with($fileId, $newPadId, BindingService::ACCESS_PUBLIC); + ->with($fileId, $newPadId, BindingService::ACCESS_PUBLIC, $oldPadId); $padFileService = $this->createMock(PadFileService::class); $parsedPad = new ParsedPadFile( @@ -1438,14 +1466,16 @@ private function buildPendingDeleteRestoreService( ?LoggerInterface $padLifecycleLogger = null, ?TestFaults $testFaults = null, ?UserNodeResolver $nodes = null, + ?string $replaced = null, + ?string $filePadId = null, ): RestoreService { - $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $oldPadId, accessMode: $accessMode, state: $state)); + $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $oldPadId, accessMode: $accessMode, state: $state, replacedPadId: $replaced)); $padFileService = $this->createMock(PadFileService::class); $parsedPad = new ParsedPadFile( frontmatter: [], body: 'body', - padId: $oldPadId, + padId: $filePadId ?? $oldPadId, accessMode: BindingService::ACCESS_PUBLIC, padUrl: '', isExternal: false, @@ -1459,6 +1489,9 @@ function (ParsedPadFile $pad, string $text, string $html, string $padId, string return 'doc-after'; }, ); + $padFileService->method('namingPad')->willReturnCallback( + static fn (ParsedPadFile $pad, string $padId, string $accessMode, string $padUrl): ParsedPadFile => (new PadFileService(new FixedClock()))->namingPad($pad, $padId, $accessMode, $padUrl), + ); $secureRandom = $this->createMock(ISecureRandom::class); $secureRandom->method('generate')->willReturn('abc123def456'); diff --git a/tests/phpunit/unit/SettleOnOpenTest.php b/tests/phpunit/unit/SettleOnOpenTest.php index f2547f05..0d9afab8 100644 --- a/tests/phpunit/unit/SettleOnOpenTest.php +++ b/tests/phpunit/unit/SettleOnOpenTest.php @@ -9,6 +9,7 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; use OCA\EtherpadNextcloud\Exception\BindingException; +use OCA\EtherpadNextcloud\Exception\BindingMismatchException; use OCA\EtherpadNextcloud\Exception\LifecycleException; use OCA\EtherpadNextcloud\Exception\MissingBindingException; use OCA\EtherpadNextcloud\Exception\WaitingBindingException; @@ -20,6 +21,7 @@ use OCA\EtherpadNextcloud\Service\SettleLock; use OCA\EtherpadNextcloud\Service\SettleOnOpen; use OCA\EtherpadNextcloud\Service\SettleOutcome; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCA\EtherpadNextcloud\Tests\Support\InMemoryLockingProvider; use OCA\EtherpadNextcloud\Tests\Support\SettlesOnOpen; @@ -30,6 +32,7 @@ use Psr\Log\LoggerInterface; class SettleOnOpenTest extends TestCase { + use BuildsBoundPads; // For waitingRow(); settleOnOpen() below is this test's own, over its own locks and logger. use SettlesOnOpen; @@ -45,22 +48,27 @@ protected function setUp(): void { $this->pad = new ParsedPadFile(frontmatter: [], body: '', padId: 'pad-a', accessMode: BindingService::ACCESS_PUBLIC, padUrl: '', isExternal: false, snapshotRev: 3); } - /** A row that does not wait answers as it is, and nothing is decided. */ + /** + * A row that does not wait answers as it is, and nothing is decided. Nor + * is a row that waits for another pad than the file names: it is not + * this file's to settle. + */ public function testARowThatDoesNotWaitAnswersAsItIs(): void { - foreach (['active' => null, 'another pad' => new BindingException('Binding pad ID mismatch.'), 'none' => new MissingBindingException('No binding exists for this file.')] as $case => $answer) { - $bindings = $this->createMock(BindingService::class); - $check = $bindings->expects($this->once())->method('assertConsistentMapping')->with(42, 'pad-a', BindingService::ACCESS_PUBLIC); - if ($answer !== null) { - $check->willThrowException($answer); - } + foreach ([ + 'active' => [new Binding(42, 'pad-a', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE), null], + 'another pad' => [new Binding(42, 'pad-b', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE), BindingMismatchException::class], + 'waiting for another pad' => [self::waitingRow(42, 'pad-b', BindingService::ACCESS_PUBLIC), BindingMismatchException::class], + 'none' => [null, MissingBindingException::class], + ] as $case => [$row, $answer]) { $restores = $this->createMock(RestoreService::class); $restores->expects($this->never())->method('settleOpenedFile'); try { - $this->settleOnOpen($bindings, $restores)->settleThenAssert($this->file, 42, $this->pad); + $bound = $this->settleOnOpen($this->rowsInTurn($row), $restores)->settleThenResolve($this->file, 42, $this->pad); $this->assertNull($answer, $case); + $this->assertSame($this->pad, $bound, $case); } catch (BindingException $e) { - $this->assertSame($answer, $e, $case); + $this->assertSame($answer, $e::class, $case); } } } @@ -72,7 +80,11 @@ public function testARowThatDoesNotWaitAnswersAsItIs(): void { * opens, gone and the file offers its recovery, or still waiting. */ public function testARowThatWaitsIsDecidedOnceAndAnswersAsItIsThen(): void { - foreach (['taken back' => null, 'let go' => new MissingBindingException('No binding exists for this file.'), 'still waiting' => new WaitingBindingException('Pad binding is not active.')] as $case => $then) { + foreach ([ + 'taken back' => [new Binding(42, 'pad-a', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE), null], + 'let go' => [null, MissingBindingException::class], + 'still waiting' => [self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC), WaitingBindingException::class], + ] as $case => [$then, $answer]) { // Noted here and asserted after: a failed assertion thrown in the // callback would be caught as a decision that failed. $seen = []; @@ -86,10 +98,10 @@ public function testARowThatWaitsIsDecidedOnceAndAnswersAsItIsThen(): void { }); try { - $this->settleOnOpen($this->waitingThen($then), $restores)->settleThenAssert($this->file, 42, $this->pad); - $this->assertNull($then, $case); + $this->settleOnOpen($this->waitingThen($then), $restores)->settleThenResolve($this->file, 42, $this->pad); + $this->assertNull($answer, $case); } catch (BindingException $e) { - $this->assertSame($then, $e, $case); + $this->assertSame($answer, $e::class, $case); } // Under the row's lock, with a few seconds for Etherpad. $this->assertSame([true, 5], $seen, $case); @@ -104,7 +116,6 @@ public function testARowThatWaitsIsDecidedOnceAndAnswersAsItIsThen(): void { */ public function testARowJustTouchedIsNotDecidedAgain(): void { foreach (['a minute ago' => [FixedClock::NOW - 60, true], 'within the minute' => [FixedClock::NOW - 59, false], 'gone meanwhile' => [null, false]] as $case => [$touched, $decided]) { - $waiting = new WaitingBindingException('Pad binding is not active.'); $restores = $this->createMock(RestoreService::class); $restores->expects($decided ? $this->once() : $this->never())->method('settleOpenedFile')->willReturn(SettleOutcome::Unanswered); // Left alone, not failed: nothing to report. @@ -112,10 +123,10 @@ public function testARowJustTouchedIsNotDecidedAgain(): void { $logger->expects($this->never())->method('warning'); try { - $this->settleOnOpen($this->waitingThen($waiting, touched: $touched), $restores, $logger)->settleThenAssert($this->file, 42, $this->pad); + $this->settleOnOpen($this->waitingThen(self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC), touched: $touched), $restores, $logger)->settleThenResolve($this->file, 42, $this->pad); $this->fail($case . ': the row still waits.'); - } catch (WaitingBindingException $e) { - $this->assertSame($waiting, $e, $case); + } catch (WaitingBindingException) { + $this->addToAssertionCount(1); } } } @@ -123,18 +134,16 @@ public function testARowJustTouchedIsNotDecidedAgain(): void { /** A row someone else is deciding is theirs: the open does not wait for it, and answers that it waits. */ public function testARowSomeoneElseIsDecidingIsLeftToThem(): void { $this->locks->acquireLock(self::LOCK, ILockingProvider::LOCK_EXCLUSIVE); - $waiting = new WaitingBindingException('Pad binding is not active.'); $restores = $this->createMock(RestoreService::class); $restores->expects($this->never())->method('settleOpenedFile'); - $this->expectExceptionObject($waiting); + $this->expectException(WaitingBindingException::class); - $this->settleOnOpen($this->waitingThen($waiting), $restores)->settleThenAssert($this->file, 42, $this->pad); + $this->settleOnOpen($this->waitingThen(self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC)), $restores)->settleThenResolve($this->file, 42, $this->pad); } /** Deciding that fails fails no open: it is logged, and the row answers as it is. */ public function testADecisionThatFailsIsLoggedAndTheRowAnswersAsItIs(): void { - $waiting = new WaitingBindingException('Pad binding is not active.'); $restores = $this->createMock(RestoreService::class); $restores->method('settleOpenedFile')->willThrowException(LifecycleException::failed('Restore', new \RuntimeException('database went away'))); $logger = $this->createMock(LoggerInterface::class); @@ -143,36 +152,34 @@ public function testADecisionThatFailsIsLoggedAndTheRowAnswersAsItIs(): void { $this->callback(static fn (array $context): bool => $context['fileId'] === 42), ); - $this->expectExceptionObject($waiting); + $this->expectException(WaitingBindingException::class); - $this->settleOnOpen($this->waitingThen($waiting), $restores, $logger)->settleThenAssert($this->file, 42, $this->pad); + $this->settleOnOpen($this->waitingThen(self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC)), $restores, $logger)->settleThenResolve($this->file, 42, $this->pad); } - /** - * Waiting at first, then $then: null for a check that passes. The row - * was last touched at $touched, an hour ago unless said otherwise; null - * for a row gone by the time it is read. - */ - private function waitingThen(?BindingException $then, ?int $touched = FixedClock::NOW - 3600): BindingService&MockObject { + /** File 42's row as each read finds it, in turn. */ + private function rowsInTurn(?Binding ...$rows): BindingService&MockObject { $bindings = $this->createMock(BindingService::class); - $calls = 0; - $bindings->expects($this->exactly(2)) - ->method('assertConsistentMapping') - ->with(42, 'pad-a', BindingService::ACCESS_PUBLIC) - ->willReturnCallback(static function () use (&$calls, $then): void { - if (++$calls === 1) { - throw new WaitingBindingException('Pad binding is not active.'); - } - if ($then !== null) { - throw $then; - } - }); - $bindings->method('findByFileId')->with(42)->willReturn($touched === null ? null : self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC, FixedClock::NOW - $touched)); + $bindings->expects($this->exactly(count($rows)))->method('findByFileId')->with(42)->willReturnOnConsecutiveCalls(...$rows); return $bindings; } + /** + * Waiting at the first look, then $then when the open looks again: null + * for a row gone by then. In between, the decision finds the row last + * touched at $touched, an hour ago unless said otherwise; null for a row + * gone by the time it is read. + */ + private function waitingThen(?Binding $then, ?int $touched = FixedClock::NOW - 3600): BindingService&MockObject { + return $this->rowsInTurn( + self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC), + $touched === null ? null : self::waitingRow(42, 'pad-a', BindingService::ACCESS_PUBLIC, FixedClock::NOW - $touched), + $then, + ); + } + private function settleOnOpen(BindingService $bindings, RestoreService $restores, ?LoggerInterface $logger = null): SettleOnOpen { $logger ??= $this->createMock(LoggerInterface::class); - return new SettleOnOpen($bindings, $restores, new SettleLock($this->locks, $logger), new FixedClock(), $logger); + return new SettleOnOpen($bindings, $restores, new SettleLock($this->locks, $logger), new FixedClock(), $logger, $this->boundPads($bindings, $logger)); } } From 45a1f9873e425a82d17c20880eb040f8e5cde877 Mon Sep 17 00:00:00 2001 From: Jaggob <37583151+Jaggob@users.noreply.github.com> Date: Sat, 26 Sep 2026 13:54:43 +0200 Subject: [PATCH 2/5] Read a trashed file as its row has it in one place The trash and the sweep each read the file through TrashSnapshotWriter and then applied the rule for a file that names the pad its row replaced. TrashSnapshotWriters::for() takes the row now, and read() applies it, so no caller of the writer can forget it. The restore keeps its own line: an open hands it the file as the open read it. A test holds that a row remembers only the pad its latest change replaced, and architecture.md says what happens to a file that names any other pad: its revision is kept, since such a file need not be the row's at all. --- docs/architecture.md | 2 +- lib/Service/LifecycleService.php | 8 +---- lib/Service/OwedDeletions.php | 6 +--- lib/Service/TrashSnapshotWriter.php | 28 ++++++++++------- lib/Service/TrashSnapshotWriters.php | 7 +++-- tests/phpunit/Support/WiresTheLifecycle.php | 3 +- tests/phpunit/unit/BoundPadResolverTest.php | 17 +++++++++++ tests/phpunit/unit/OwedDeletionsTest.php | 3 +- .../phpunit/unit/TrashSnapshotWriterTest.php | 30 ++++++++++++++++--- 9 files changed, 69 insertions(+), 35 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 4e9bab36..b09a97c1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -109,7 +109,7 @@ A `.pad` file names a pad, and whoever may write the file can make it name any p A row remembers one pad, the one its latest change of pad replaced. A file that names a pad from before that is refused like any other. -A file that names the pad its row replaced holds that pad's `snapshot_rev`, not one of the row's pad. Wherever the file's revision is held against the row's pad - sync and sync status, the trash, the sweep's rest of a trash, a restore - it counts as none: a higher revision of the old pad would make the row's pad look behind, and a trash would leave the old pad's text in the file and the row's pad over. Only then; a file that names the row's pad keeps its revision. +A file that names the pad its row replaced holds that pad's `snapshot_rev`, not one of the row's pad. Wherever the file's revision is held against the row's pad - sync and sync status, the trash, the sweep's rest of a trash, a restore - it counts as none: a higher revision of the old pad would make the row's pad look behind, and a trash would leave the old pad's text in the file and the row's pad over. Only then. A file that names the row's pad keeps its revision, and so does one that names any other pad: such a file need not be the row's at all - one overwritten with another `.pad`'s content, or a file id the file cache gave out again - so its revision is not given up for the row's pad. Trash, sweep and restore hold the row's pad to it as before: a pad behind it is left in place and logged with its id, as any pad behind is. Pads on another server have no row, and none of this applies to them: their frontmatter decides. The legacy migration is the only way a pad goes from a file into a row (`docs/legacy-ownpad-migration.md`); a restore and a recovery always make a new pad. diff --git a/lib/Service/LifecycleService.php b/lib/Service/LifecycleService.php index cf1de461..95fb4a5c 100644 --- a/lib/Service/LifecycleService.php +++ b/lib/Service/LifecycleService.php @@ -36,7 +36,6 @@ public function __construct( private ITimeFactory $timeFactory, private TrashSnapshotWriters $snapshotWriters, private RestoreService $restoreService, - private BoundPadResolver $boundPads, ) { } @@ -120,13 +119,8 @@ public function handleTrash(File $file): array { $deletedAt = $this->timeFactory->getTime(); try { - $snapshots = $this->snapshotWriters->for($file, $padId); + $snapshots = $this->snapshotWriters->for($file, $binding); $pad = $snapshots->read(); - if (!$pad instanceof TrashSnapshotMiss) { - // A file that names the pad its row replaced holds no revision - // of the row's pad, and takes the row's pad's content and name. - $pad = $this->boundPads->followingRow($pad, $binding); - } if ($pad instanceof TrashSnapshotMiss || !$snapshots->writeAtTrash($pad)) { // Without a fresh snapshot the pad may hold what the file lacks, // so it stays as it is and its deletion is owed. The sweep takes diff --git a/lib/Service/OwedDeletions.php b/lib/Service/OwedDeletions.php index 4cfe777d..7f1bc01c 100644 --- a/lib/Service/OwedDeletions.php +++ b/lib/Service/OwedDeletions.php @@ -42,7 +42,6 @@ public function __construct( private UserNodeResolver $userNodeResolver, private LoggerInterface $logger, private ITimeFactory $timeFactory, - private BoundPadResolver $boundPads, ) { } @@ -70,14 +69,11 @@ public function finishTrash(File $file, RunBudget $budget): SettleOutcome { return SettleOutcome::Left; } $padId = $binding->padId; - $snapshots = $this->snapshotWriters->for($file, $padId, news: $binding->untouchedSinceOwed()); + $snapshots = $this->snapshotWriters->for($file, $binding, news: $binding->untouchedSinceOwed()); $pad = $snapshots->read(); if ($pad instanceof TrashSnapshotMiss) { return $this->waitAgain($fileId, $padId, $pad); } - // A file that names the pad its row replaced holds no revision of the - // row's pad, and takes the row's pad's content and name. - $pad = $this->boundPads->followingRow($pad, $binding); $timeout = $budget->nextCallTimeout(); if ($timeout === null) { diff --git a/lib/Service/TrashSnapshotWriter.php b/lib/Service/TrashSnapshotWriter.php index bd36d0e9..c969282c 100644 --- a/lib/Service/TrashSnapshotWriter.php +++ b/lib/Service/TrashSnapshotWriter.php @@ -22,9 +22,11 @@ * then writes into it at trash time or, when the trash could not, in the * sweep. * - * Knows nothing about bindings. What a miss means for the row and the pad - * is the caller's to decide; here a miss is only logged, in one line an - * admin can filter by `reason`, and handed back. + * Of the file's row it knows which pad that is, and which one the file may + * still name (BoundPadResolver::followingRow()): a file that names the pad + * the row replaced holds no revision of the row's pad. What a miss means + * for the row and the pad is the caller's to decide; here a miss is only + * logged, in one line an admin can filter by `reason`, and handed back. * * $news: whether the file's trouble would be news (warning) or a repeat * (debug); a sweep reads it off the row. @@ -39,7 +41,8 @@ public function __construct( private LoggerInterface $logger, private TestFaults $testFaults, private File $file, - private string $padId, + private Binding $binding, + private BoundPadResolver $boundPads, private bool $news = true, ) { $this->context = ['app' => 'etherpad_nextcloud', 'fileId' => $file->getId()]; @@ -50,7 +53,9 @@ public function __construct( * snapshot into yet. A delete through WebDAV holds the file's lock while * the trash is decided, so a locked file is the ordinary case here. A * file that cannot be read for another reason is a miss of its own, - * reported as the file's trouble. + * reported as the file's trouble. A file that names the pad its row + * replaced reads as naming the row's, with no revision, so it takes the + * row's pad's content and name. */ public function read(): ParsedPadFile|TrashSnapshotMiss { try { @@ -67,10 +72,11 @@ public function read(): ParsedPadFile|TrashSnapshotMiss { return $this->missed(TrashSnapshotMiss::FileEmpty); } try { - return $this->padFileService->readPad($content); + $pad = $this->padFileService->readPad($content); } catch (\Throwable $parseError) { return $this->missed(TrashSnapshotMiss::FileUnparsable, SafeError::context($parseError)); } + return $this->boundPads->followingRow($pad, $this->binding); } /** @@ -142,7 +148,7 @@ private function take(ParsedPadFile $pad, ?int $revisions, ?RunBudget $budget, ? * @throws RunBudgetSpentException */ private function fresh(ParsedPadFile $pad, ?int $revisions, ?RunBudget $budget): PadSnapshot|TrashSnapshotMiss|bool { - $revisions ??= $this->etherpadClient->getRevisionsCount($this->padId, RunBudget::timeoutOf($budget)); + $revisions ??= $this->etherpadClient->getRevisionsCount($this->binding->padId, RunBudget::timeoutOf($budget)); if ($revisions === $pad->snapshotRev) { return true; } @@ -177,7 +183,7 @@ private function writeAndRecount(ParsedPadFile $pad, PadSnapshot $snapshot, ?Run return $written; } try { - $unchanged = $this->etherpadClient->getRevisionsCount($this->padId, RunBudget::timeoutOf($budget)) === $snapshot->revision; + $unchanged = $this->etherpadClient->getRevisionsCount($this->binding->padId, RunBudget::timeoutOf($budget)) === $snapshot->revision; } catch (EtherpadClientException $countError) { return $this->movedWhileWritten($moved) ?? $this->missed(TrashSnapshotMiss::PadNotRecounted, SafeError::context($countError)); } catch (RunBudgetSpentException $spent) { @@ -215,9 +221,9 @@ private function missed(TrashSnapshotMiss $miss, array $cause = []): TrashSnapsh * to hold after. */ private function fetchStable(int $before, ?RunBudget $budget): ?PadSnapshot { - $text = $this->etherpadClient->getText($this->padId, RunBudget::timeoutOf($budget)); - $html = $this->etherpadClient->getHTML($this->padId, RunBudget::timeoutOf($budget)); - $after = $this->etherpadClient->getRevisionsCount($this->padId, RunBudget::timeoutOf($budget)); + $text = $this->etherpadClient->getText($this->binding->padId, RunBudget::timeoutOf($budget)); + $html = $this->etherpadClient->getHTML($this->binding->padId, RunBudget::timeoutOf($budget)); + $after = $this->etherpadClient->getRevisionsCount($this->binding->padId, RunBudget::timeoutOf($budget)); return $before === $after ? new PadSnapshot($text, $html, $after) : null; } diff --git a/lib/Service/TrashSnapshotWriters.php b/lib/Service/TrashSnapshotWriters.php index b69d62fa..37de9b75 100644 --- a/lib/Service/TrashSnapshotWriters.php +++ b/lib/Service/TrashSnapshotWriters.php @@ -22,11 +22,12 @@ public function __construct( private PadFileService $padFileService, private LoggerInterface $logger, private TestFaults $testFaults, + private BoundPadResolver $boundPads, ) { } - /** $news as TrashSnapshotWriter takes it. */ - public function for(File $file, string $padId, bool $news = true): TrashSnapshotWriter { - return new TrashSnapshotWriter($this->etherpadClient, $this->padFileService, $this->logger, $this->testFaults, $file, $padId, $news); + /** For $file and the pad its row ($binding) names; $news as TrashSnapshotWriter takes it. */ + public function for(File $file, Binding $binding, bool $news = true): TrashSnapshotWriter { + return new TrashSnapshotWriter($this->etherpadClient, $this->padFileService, $this->logger, $this->testFaults, $file, $binding, $this->boundPads, $news); } } diff --git a/tests/phpunit/Support/WiresTheLifecycle.php b/tests/phpunit/Support/WiresTheLifecycle.php index 8c5f4e9e..27773fcd 100644 --- a/tests/phpunit/Support/WiresTheLifecycle.php +++ b/tests/phpunit/Support/WiresTheLifecycle.php @@ -76,9 +76,8 @@ private function lifecycleService( $nodes ?? $this->createMock(UserNodeResolver::class), $paths ?? $this->createMock(PathNormalizer::class), new FixedClock(), - new TrashSnapshotWriters($etherpad, $padFiles, $logger, $testFaults), + new TrashSnapshotWriters($etherpad, $padFiles, $logger, $testFaults, new BoundPadResolver($bindings, $padFiles, $etherpad, $logger)), $restores ?? $this->wireRestoreService($bindings, $etherpad, $padFiles, $padLifecycle, $appConfig, $logger, $padLifecycleLogger, $secureRandom, $testFaults), - new BoundPadResolver($bindings, $padFiles, $etherpad, $logger), ); } diff --git a/tests/phpunit/unit/BoundPadResolverTest.php b/tests/phpunit/unit/BoundPadResolverTest.php index f26f9138..7c077a8a 100644 --- a/tests/phpunit/unit/BoundPadResolverTest.php +++ b/tests/phpunit/unit/BoundPadResolverTest.php @@ -59,6 +59,23 @@ public function testTheFilesRowDecidesWhichPadItReaches(): void { } } + /** + * A row remembers one pad: the one its latest change of pad replaced. + * Moved from A to B and then to C, it takes a file that names B for C, + * and refuses one that still names A like any other. + */ + public function testARowRemembersOnlyThePadItsLatestChangeReplaced(): void { + $bindings = new BindingService(new InMemoryBindingTable([self::row(10, 'pad-a')]), new FixedClock()); + $this->assertTrue($bindings->rebind(10, 'pad-a', BindingService::STATE_ACTIVE, 'pad-b', BindingService::STATE_ACTIVE)); + $this->assertTrue($bindings->rebind(10, 'pad-b', BindingService::STATE_ACTIVE, 'pad-c', BindingService::STATE_ACTIVE)); + $resolver = $this->boundPads($bindings); + + $this->assertSame('pad-c', $resolver->resolve(10, self::pad('pad-c', BindingService::ACCESS_PUBLIC))->padId); + $this->assertSame('pad-c', $resolver->resolve(10, self::pad('pad-b', BindingService::ACCESS_PUBLIC))->padId); + $this->expectExceptionObject(new BindingMismatchException('Binding pad ID mismatch.')); + $resolver->resolve(10, self::pad('pad-a', BindingService::ACCESS_PUBLIC)); + } + /** * A file that names the pad its row replaced is read as naming the * row's: its id, mode and link, and no revision of its own, since the diff --git a/tests/phpunit/unit/OwedDeletionsTest.php b/tests/phpunit/unit/OwedDeletionsTest.php index a368763b..63a8ce2a 100644 --- a/tests/phpunit/unit/OwedDeletionsTest.php +++ b/tests/phpunit/unit/OwedDeletionsTest.php @@ -602,11 +602,10 @@ private function owed(BindingService $bindings, EtherpadClient $etherpad, bool $ $bindings, $appConfig, new ManagedPadLifecycle($etherpad, $this->createMock(LoggerInterface::class)), - new TrashSnapshotWriters($etherpad, $this->buildSnapshotWritingPadFileService(), $logger, new TestFaults($this->createMock(IConfig::class), $appConfig)), + new TrashSnapshotWriters($etherpad, $this->buildSnapshotWritingPadFileService(), $logger, new TestFaults($this->createMock(IConfig::class), $appConfig), $this->boundPads($bindings, $logger)), $nodes ?? $this->createMock(UserNodeResolver::class), $logger, new FixedClock(), - $this->boundPads($bindings, $logger), ); } } diff --git a/tests/phpunit/unit/TrashSnapshotWriterTest.php b/tests/phpunit/unit/TrashSnapshotWriterTest.php index 8383024e..74072e84 100644 --- a/tests/phpunit/unit/TrashSnapshotWriterTest.php +++ b/tests/phpunit/unit/TrashSnapshotWriterTest.php @@ -10,6 +10,7 @@ use OCA\EtherpadNextcloud\Exception\EtherpadClientException; use OCA\EtherpadNextcloud\Exception\RunBudgetSpentException; +use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\PadFileService; @@ -19,6 +20,7 @@ use OCA\EtherpadNextcloud\Service\TestFaults; use OCA\EtherpadNextcloud\Service\TrashSnapshotMiss; use OCA\EtherpadNextcloud\Service\TrashSnapshotWriter; +use OCA\EtherpadNextcloud\Tests\Support\BuildsBoundPads; use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCP\Files\File; use OCP\Lock\LockedException; @@ -27,6 +29,8 @@ use Psr\Log\LoggerInterface; class TrashSnapshotWriterTest extends TestCase { + use BuildsBoundPads; + private EtherpadClient&MockObject $etherpad; private PadFileService&MockObject $padFiles; private LoggerInterface&MockObject $logger; @@ -96,6 +100,22 @@ public function testReadHandsBackThePadAsTheFileRecordsIt(): void { $this->assertSame([], $this->logged); } + /** + * A file that names the pad its row replaced holds no revision of the + * row's pad, however high the one it has: it reads as naming the row's + * pad, with none. A file that names the row's pad reads as it is. + */ + public function testReadTakesAFileNamingThePadItsRowReplacedForTheRows(): void { + $this->file->method('getContent')->willReturn('doc-before'); + $this->padFiles->method('readPad')->willReturn($this->pad(snapshotRev: 50, padId: 'pad-before')); + + $pad = $this->writer(replaced: 'pad-before')->read(); + + $this->assertInstanceOf(ParsedPadFile::class, $pad); + $this->assertSame(['pad-a', -1, self::PAD_BASE . 'pad-a', 'body'], [$pad->padId, $pad->snapshotRev, $pad->padUrl, $pad->body]); + $this->assertSame([], $this->logged); + } + /** The injected read lock of a debug instance reads as the lock of a WebDAV delete. */ public function testTheReadLockFaultReadsAsALock(): void { $this->file->expects($this->never())->method('getContent'); @@ -416,7 +436,8 @@ public function testAtTrashAMissKeepsThePad(): void { $this->assertSame([['debug', 'pad_changed']], $this->logged); } - private function writer(bool $news = true): TrashSnapshotWriter { + /** For the row of file 7 naming 'pad-a', which replaced $replaced. */ + private function writer(bool $news = true, ?string $replaced = null): TrashSnapshotWriter { $testFaults = $this->createMock(TestFaults::class); $testFaults->method('isActive')->willReturnCallback(fn (string $fault): bool => $fault === $this->fault); return new TrashSnapshotWriter( @@ -425,7 +446,8 @@ private function writer(bool $news = true): TrashSnapshotWriter { $this->logger, $testFaults, $this->file, - 'pad-a', + new Binding(7, 'pad-a', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: $replaced), + $this->boundPads($this->createMock(BindingService::class)), $news, ); } @@ -440,11 +462,11 @@ private function notMoved(): \Closure { return static fn (): bool => false; } - private function pad(int $snapshotRev): ParsedPadFile { + private function pad(int $snapshotRev, string $padId = 'pad-a'): ParsedPadFile { return new ParsedPadFile( frontmatter: [], body: 'body', - padId: 'pad-a', + padId: $padId, accessMode: BindingService::ACCESS_PUBLIC, padUrl: '', isExternal: false, From 8942af6d6ed1697b1d914c844b26ac5ddaf9a33d Mon Sep 17 00:00:00 2001 From: Jaggob <37583151+Jaggob@users.noreply.github.com> Date: Sat, 26 Sep 2026 14:18:33 +0200 Subject: [PATCH 3/5] Say why a replaced pad's revision does not count in one place The reason stands at BoundPadResolver::followingRow() and in architecture.md; the writer, the restore, the sync and the tests point there. open-by-id's behaviour reads "as open". The tests build the resolver one way, BuildsBoundPads::boundPads(), which SettlesOnOpen and WiresTheLifecycle use too; its address is a method, since a trait has constants only from PHP 8.2 and the unit tests also run on 8.1. The sweep's own test for the rule goes: the writer's read() applies it, and the trash test drives it through real files. Four unused imports go from the test files at hand. --- docs/api-reference.md | 3 +-- docs/architecture.md | 2 +- lib/Service/BoundPadResolver.php | 6 +++-- lib/Service/PadSyncService.php | 6 ++--- lib/Service/RestoreService.php | 4 +-- lib/Service/TrashSnapshotWriter.php | 13 +++++----- tests/phpunit/Support/BuildsBoundPads.php | 10 +++++--- tests/phpunit/Support/SettlesOnOpen.php | 9 +++---- tests/phpunit/Support/WiresTheLifecycle.php | 17 +++++++++---- tests/phpunit/unit/BoundPadResolverTest.php | 4 +-- tests/phpunit/unit/LifecycleServiceTest.php | 5 ++-- tests/phpunit/unit/LivePadHtmlFetcherTest.php | 1 - tests/phpunit/unit/OwedDeletionsTest.php | 25 ++----------------- .../unit/PadLifecycleControllerTest.php | 1 - .../phpunit/unit/PadSessionControllerTest.php | 1 - tests/phpunit/unit/PadSyncServiceTest.php | 6 ++--- .../unit/PublicViewerControllerTest.php | 1 - tests/phpunit/unit/RestoreServiceTest.php | 11 +++----- .../phpunit/unit/TrashSnapshotWriterTest.php | 8 ++---- 19 files changed, 50 insertions(+), 83 deletions(-) diff --git a/docs/api-reference.md b/docs/api-reference.md index 81d30dc5..4b3eab1d 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -182,8 +182,7 @@ solely by the separate external-pad policy, not by these two settings. - Controller: `PadSessionController::openById` - Params: `fileId=` - Result: secure open URL via stable Nextcloud `fileId`. - - Behavior: CSRF-protected. The file's row decides which pad opens. The `.pad` file is written only when it names the pad its row replaced and the user may write it: it then names the row's pad (see "Which pad a file reaches" in `docs/architecture.md`). - - Protected mode: response includes one Etherpad session `Set-Cookie` header. + - Behavior and protected mode: as `open`. - `GET /api/v1/pads/content/{fileId}` - Controller: `PadSessionController::contentById` diff --git a/docs/architecture.md b/docs/architecture.md index b09a97c1..3c2276cf 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -256,7 +256,7 @@ Primary flow (native viewer): - A file that cannot be read, for any reason, is one more way to have no fresh snapshot: the user's delete goes through. - No fresh snapshot, or the delete fails: the pad stays as it is and the row becomes `pending_delete`; Nextcloud's trash succeeds either way. A protected pad whose group Etherpad cannot list counts as a delete that failed, so the sweep asks once more; past the row it has taken by then, a group it still cannot list is given up and the pad goes alone. A delete through WebDAV (Files UI, clients) holds the file's lock while the trash is decided, so there it is always this path. The sweep finishes the trash on its next run, usually within five minutes: the pad's content goes into the trashed file, then row and pad go (see below). Until then a public pad stays reachable by its URL, a restore takes the pad back, and the admin page counts it as a pending delete. - Restore without a binding row: provision a new pad from `.pad` frontmatter/snapshot. -- Restore of a waiting row (`pending_delete` or `restore_pending`), whatever `delete_on_trash` says now: read the file's `snapshot_rev`, then ask Etherpad about the row's pad. The pad id comes from the row, never from the file; a file that names the pad the row replaced holds no revision of the row's pad (see "Which pad a file reaches"). +- Restore of a waiting row (`pending_delete` or `restore_pending`), whatever `delete_on_trash` says now: read the file's `snapshot_rev`, then ask Etherpad about the row's pad. The pad id comes from the row, never from the file, and the file's revision counts as "Which pad a file reaches" says. - It exists with at least that many revisions: the row becomes `active` again on that same pad, which may hold edits the snapshot missed. If the sweep took row and pad in the meantime, finishing the trash, the restore finds no row when it reads again and makes a new pad from the file, which holds the snapshot the sweep wrote first. - Etherpad answers that it does not exist, or it has fewer revisions (created again since, or back from an older backup): a new pad from the file's snapshot, and the file records the new pad's revision count. A pad with fewer revisions is left in place and logged before anything else is tried; whoever wrote into it has only that copy. Of a pad that does not exist, what is left - an empty group - goes. - Of two restores that race for one file only one writes it, and a restore that fails takes back what it made (`RestoreService::restoreOntoNewPad`). diff --git a/lib/Service/BoundPadResolver.php b/lib/Service/BoundPadResolver.php index 54af70ad..8aa8e522 100644 --- a/lib/Service/BoundPadResolver.php +++ b/lib/Service/BoundPadResolver.php @@ -78,8 +78,10 @@ public function resolve(int $fileId, ParsedPadFile $pad): ParsedPadFile { /** * For a caller that holds the row already, a trash, a sweep or a * restore: $pad as namingPad() makes it when it names the pad $binding's - * replaced, so the revision it has is not held against the row's pad; - * otherwise $pad as it is. + * replaced, otherwise $pad as it is. Such a file holds a revision of the + * pad before, not of the row's: held against the row's pad, a higher + * one would make it look behind (docs/architecture.md, "Which pad a + * file reaches"). */ public function followingRow(ParsedPadFile $pad, Binding $binding): ParsedPadFile { if ($pad->padId !== $binding->replacedPadId || $pad->padId === $binding->padId) { diff --git a/lib/Service/PadSyncService.php b/lib/Service/PadSyncService.php index 846a4de8..baf2486b 100644 --- a/lib/Service/PadSyncService.php +++ b/lib/Service/PadSyncService.php @@ -62,8 +62,8 @@ public function syncById(string $uid, int $fileId, bool $force): PadSyncResult { return $this->syncExternalPad($node, $fileId, $pad, $force); } - // A file that named the pad its row replaced has no revision of - // the row's pad, so this always writes the pad's content and name. + // A file that named the pad its row replaced comes without a + // revision (BoundPadResolver::followingRow()): always written. $result = $this->syncInternalPad($node, $fileId, $synced, $force); if ($synced !== $pad) { $this->boundPads->repaired($fileId); @@ -92,8 +92,6 @@ public function syncStatusById(string $uid, int $fileId): PadSyncStatus { reason: 'external_no_revision', ); } - // A file that named the pad its row replaced is out of sync with the - // row's pad, whatever its revision says. $pad = $this->boundPads->resolve($fileId, $pad); $currentRev = $this->etherpadClient->getRevisionsCount($pad->padId); diff --git a/lib/Service/RestoreService.php b/lib/Service/RestoreService.php index 0627917c..b089e57a 100644 --- a/lib/Service/RestoreService.php +++ b/lib/Service/RestoreService.php @@ -177,9 +177,7 @@ private function settleWaitingBinding(File $file, Binding $binding, bool $mayRep // No revision to hold the pad to, so no decision either. return $this->deferRestore($fileId, $padId, $state, $readError); } - // A file that names the pad its row replaced holds no revision of - // the row's pad: a higher one of the pad before would make the - // row's pad look behind. + // As its row has it; an open hands the file over as it read it. $pad = $this->boundPads->followingRow($pad, $binding); $timeout = $budget?->nextCallTimeout(); if ($budget !== null && $timeout === null) { diff --git a/lib/Service/TrashSnapshotWriter.php b/lib/Service/TrashSnapshotWriter.php index c969282c..74e1b5e1 100644 --- a/lib/Service/TrashSnapshotWriter.php +++ b/lib/Service/TrashSnapshotWriter.php @@ -23,10 +23,10 @@ * sweep. * * Of the file's row it knows which pad that is, and which one the file may - * still name (BoundPadResolver::followingRow()): a file that names the pad - * the row replaced holds no revision of the row's pad. What a miss means - * for the row and the pad is the caller's to decide; here a miss is only - * logged, in one line an admin can filter by `reason`, and handed back. + * still name: read() hands the file over as the row has it + * (BoundPadResolver::followingRow()). What a miss means for the row and the + * pad is the caller's to decide; here a miss is only logged, in one line an + * admin can filter by `reason`, and handed back. * * $news: whether the file's trouble would be news (warning) or a repeat * (debug); a sweep reads it off the row. @@ -53,9 +53,8 @@ public function __construct( * snapshot into yet. A delete through WebDAV holds the file's lock while * the trash is decided, so a locked file is the ordinary case here. A * file that cannot be read for another reason is a miss of its own, - * reported as the file's trouble. A file that names the pad its row - * replaced reads as naming the row's, with no revision, so it takes the - * row's pad's content and name. + * reported as the file's trouble. The file comes as its row has it + * (BoundPadResolver::followingRow()). */ public function read(): ParsedPadFile|TrashSnapshotMiss { try { diff --git a/tests/phpunit/Support/BuildsBoundPads.php b/tests/phpunit/Support/BuildsBoundPads.php index 7fb5a1e0..3f286ae1 100644 --- a/tests/phpunit/Support/BuildsBoundPads.php +++ b/tests/phpunit/Support/BuildsBoundPads.php @@ -16,15 +16,17 @@ /** * The real BoundPadResolver over whatever rows a test gives it, so a test - * holds the service to the rule rather than to a stubbed answer. Pads live - * at PAD_BASE. + * holds the service to the rule rather than to a stubbed answer. */ trait BuildsBoundPads { - private const PAD_BASE = 'https://pad.example.test/p/'; + /** Where the resolver's Etherpad serves $padId. A method: a trait has constants only from PHP 8.2. */ + private static function padUrlOf(string $padId): string { + return 'https://pad.example.test/p/' . $padId; + } private function boundPads(BindingService $bindings, ?LoggerInterface $logger = null, ?PadFileService $padFiles = null): BoundPadResolver { $etherpad = $this->createMock(EtherpadClient::class); - $etherpad->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::PAD_BASE . $padId); + $etherpad->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::padUrlOf($padId)); return new BoundPadResolver( $bindings, $padFiles ?? new PadFileService(new FixedClock()), diff --git a/tests/phpunit/Support/SettlesOnOpen.php b/tests/phpunit/Support/SettlesOnOpen.php index adafacbd..5ca1611e 100644 --- a/tests/phpunit/Support/SettlesOnOpen.php +++ b/tests/phpunit/Support/SettlesOnOpen.php @@ -10,9 +10,6 @@ use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; -use OCA\EtherpadNextcloud\Service\BoundPadResolver; -use OCA\EtherpadNextcloud\Service\EtherpadClient; -use OCA\EtherpadNextcloud\Service\PadFileService; use OCA\EtherpadNextcloud\Service\RestoreService; use OCA\EtherpadNextcloud\Service\SettleLock; use OCA\EtherpadNextcloud\Service\SettleOnOpen; @@ -26,17 +23,17 @@ * as an enum cannot be doubled. */ trait SettlesOnOpen { + use BuildsBoundPads; + private function settleOnOpen(BindingService $bindings, ?RestoreService $restores = null, ?LoggerInterface $logger = null): SettleOnOpen { $logger ??= $this->createMock(LoggerInterface::class); - $etherpad = $this->createMock(EtherpadClient::class); - $etherpad->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId); return new SettleOnOpen( $bindings, $restores ?? $this->silentRestores(), new SettleLock(new InMemoryLockingProvider(), $logger), new FixedClock(), $logger, - new BoundPadResolver($bindings, new PadFileService(new FixedClock()), $etherpad, $logger), + $this->boundPads($bindings, $logger), ); } diff --git a/tests/phpunit/Support/WiresTheLifecycle.php b/tests/phpunit/Support/WiresTheLifecycle.php index 27773fcd..5908918c 100644 --- a/tests/phpunit/Support/WiresTheLifecycle.php +++ b/tests/phpunit/Support/WiresTheLifecycle.php @@ -36,6 +36,8 @@ * does not reproduce the graph proves less than it looks. */ trait WiresTheLifecycle { + use BuildsBoundPads; + /** * A LifecycleService with a mock for every collaborator the test does * not name, over a real RestoreService built from the same ones. The pad @@ -66,6 +68,7 @@ private function lifecycleService( $padLifecycle = new ManagedPadLifecycle($etherpad, $padLifecycleLogger); $appConfig = $this->appConfigDeletingOnTrash($deleteOnTrash); $testFaults ??= new TestFaults($this->createMock(IConfig::class), $appConfig); + $boundPads = $this->boundPads($bindings, $logger); return new LifecycleService( $bindings, @@ -76,8 +79,8 @@ private function lifecycleService( $nodes ?? $this->createMock(UserNodeResolver::class), $paths ?? $this->createMock(PathNormalizer::class), new FixedClock(), - new TrashSnapshotWriters($etherpad, $padFiles, $logger, $testFaults, new BoundPadResolver($bindings, $padFiles, $etherpad, $logger)), - $restores ?? $this->wireRestoreService($bindings, $etherpad, $padFiles, $padLifecycle, $appConfig, $logger, $padLifecycleLogger, $secureRandom, $testFaults), + new TrashSnapshotWriters($etherpad, $padFiles, $logger, $testFaults, $boundPads), + $restores ?? $this->wireRestoreService($bindings, $etherpad, $padFiles, $padLifecycle, $appConfig, $logger, $padLifecycleLogger, $secureRandom, $testFaults, $boundPads), ); } @@ -101,16 +104,19 @@ private function restoreService( $etherpad ??= $this->createMock(EtherpadClient::class); $padLifecycleLogger ??= $this->createMock(LoggerInterface::class); $appConfig = $this->appConfigDeletingOnTrash($deleteOnTrash); + $bindings ??= $this->createMock(BindingService::class); + $logger ??= $this->createMock(LoggerInterface::class); return $this->wireRestoreService( - $bindings ?? $this->createMock(BindingService::class), + $bindings, $etherpad, $padFiles ?? $this->createMock(PadFileService::class), new ManagedPadLifecycle($etherpad, $padLifecycleLogger), $appConfig, - $logger ?? $this->createMock(LoggerInterface::class), + $logger, $padLifecycleLogger, $secureRandom, $testFaults ?? new TestFaults($this->createMock(IConfig::class), $appConfig), + $this->boundPads($bindings, $logger), $nodes, ); } @@ -125,6 +131,7 @@ private function wireRestoreService( LoggerInterface $padLifecycleLogger, ?ISecureRandom $secureRandom, TestFaults $testFaults, + BoundPadResolver $boundPads, ?UserNodeResolver $nodes = null, ): RestoreService { return new RestoreService( @@ -139,7 +146,7 @@ private function wireRestoreService( $testFaults, // Unless a test says otherwise, a file stays where it was read. $nodes ?? $this->createMock(UserNodeResolver::class), - new BoundPadResolver($bindings, $padFiles, $etherpad, $logger), + $boundPads, ); } diff --git a/tests/phpunit/unit/BoundPadResolverTest.php b/tests/phpunit/unit/BoundPadResolverTest.php index 7c077a8a..6d05f6a1 100644 --- a/tests/phpunit/unit/BoundPadResolverTest.php +++ b/tests/phpunit/unit/BoundPadResolverTest.php @@ -96,7 +96,7 @@ public function testAFileNamingThePadItsRowReplacedIsReadAsNamingTheRows(): void $bound = $this->boundPads($bindings, $logger, new PadFileService(new FixedClock(2000000000)))->resolve(10, $pad); $this->assertSame( - ['g.new', BindingService::ACCESS_PROTECTED, self::PAD_BASE . 'g.new', -1, false, 'Text of the old pad'], + ['g.new', BindingService::ACCESS_PROTECTED, self::padUrlOf('g.new'), -1, false, 'Text of the old pad'], [$bound->padId, $bound->accessMode, $bound->padUrl, $bound->snapshotRev, $bound->isExternal, $bound->body], ); $this->assertSame([ @@ -105,7 +105,7 @@ public function testAFileNamingThePadItsRowReplacedIsReadAsNamingTheRows(): void 'access_mode' => BindingService::ACCESS_PROTECTED, 'snapshot_rev' => -1, 'updated_at' => '2033-05-18T03:33:20+00:00', - 'pad_url' => self::PAD_BASE . 'g.new', + 'pad_url' => self::padUrlOf('g.new'), ], array_intersect_key($bound->frontmatter, array_flip(['file_id', 'pad_id', 'access_mode', 'pad_url', 'snapshot_rev', 'updated_at']))); $this->assertSame(PadFileService::FORMAT_V1, $bound->frontmatter['format']); diff --git a/tests/phpunit/unit/LifecycleServiceTest.php b/tests/phpunit/unit/LifecycleServiceTest.php index 166fd9f0..4dd1e6b8 100644 --- a/tests/phpunit/unit/LifecycleServiceTest.php +++ b/tests/phpunit/unit/LifecycleServiceTest.php @@ -242,7 +242,6 @@ public function testATrashOfAFileNamingThePadItsRowReplacedTakesTheRowsPad(): vo ]]); $padFiles = new PadFileService(new FixedClock()); $etherpadClient = $this->createMock(EtherpadClient::class); - $etherpadClient->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => 'https://pad.example.test/p/' . $padId); $etherpadClient->method('getRevisionsCount')->with('pad-new')->willReturn(3); $etherpadClient->method('getText')->with('pad-new')->willReturn('Text of the new pad'); $etherpadClient->method('getHTML')->with('pad-new')->willReturn('

Text of the new pad

'); @@ -251,7 +250,7 @@ public function testATrashOfAFileNamingThePadItsRowReplacedTakesTheRowsPad(): vo $file = $this->createMock(File::class); $file->method('getId')->willReturn(110); $file->method('getName')->willReturn('Old.pad'); - $file->method('getContent')->willReturn($padFiles->buildInitialDocument(110, 'pad-old', BindingService::ACCESS_PUBLIC, new PadSnapshot('Text of the old pad', '

Text of the old pad

', 50), 'https://pad.example.test/p/pad-old')); + $file->method('getContent')->willReturn($padFiles->buildInitialDocument(110, 'pad-old', BindingService::ACCESS_PUBLIC, new PadSnapshot('Text of the old pad', '

Text of the old pad

', 50), self::padUrlOf('pad-old'))); $file->expects($this->once())->method('putContent')->willReturnCallback(static function (string $content) use (&$written): void { $written = $content; }); @@ -261,7 +260,7 @@ public function testATrashOfAFileNamingThePadItsRowReplacedTakesTheRowsPad(): vo $this->assertSame([LifecycleResult::TRASHED, true, false], [$result['status'], $result['snapshot_persisted'], $result['delete_pending']]); $this->assertSame([], $table->rows); $pad = $padFiles->readPad((string)$written); - $this->assertSame(['pad-new', 3, 'https://pad.example.test/p/pad-new'], [$pad->padId, $pad->snapshotRev, $pad->padUrl]); + $this->assertSame(['pad-new', 3, self::padUrlOf('pad-new')], [$pad->padId, $pad->snapshotRev, $pad->padUrl]); $this->assertStringContainsString('Text of the new pad', $pad->body); $this->assertStringNotContainsString('Text of the old pad', $pad->body); } diff --git a/tests/phpunit/unit/LivePadHtmlFetcherTest.php b/tests/phpunit/unit/LivePadHtmlFetcherTest.php index 9ab6a00b..fe4d810b 100644 --- a/tests/phpunit/unit/LivePadHtmlFetcherTest.php +++ b/tests/phpunit/unit/LivePadHtmlFetcherTest.php @@ -8,7 +8,6 @@ use OCA\EtherpadNextcloud\Exception\ExternalPadException; use OCA\EtherpadNextcloud\Exception\MissingBindingException; use OCA\EtherpadNextcloud\Exception\WaitingBindingException; -use OCA\EtherpadNextcloud\Exception\EtherpadClientException; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; use OCA\EtherpadNextcloud\Service\ExternalPadExportFetcher; diff --git a/tests/phpunit/unit/OwedDeletionsTest.php b/tests/phpunit/unit/OwedDeletionsTest.php index 63a8ce2a..69bc11a5 100644 --- a/tests/phpunit/unit/OwedDeletionsTest.php +++ b/tests/phpunit/unit/OwedDeletionsTest.php @@ -92,27 +92,6 @@ public function testFinishTrashWritesTheSnapshotThenTakesTheRowBeforeThePad(): v $this->assertSame(['snapshot', 'row', 'pad'], $order); } - /** - * A trashed file that still names the pad its row replaced holds no - * revision of the row's pad, however high the one it has: the row's pad - * is written into it and goes as any other. Held against the old pad's - * revision, the row's pad looked behind, and the row was let go with the - * pad left over and the file holding the old pad's text. - */ - public function testATrashedFileNamingThePadItsRowReplacedGetsTheRowsPad(): void { - // The formatter reads the file as naming 'pad'. - $bindingService = $this->pendingTrashRow(111, 'pad-new', replaced: 'pad'); - $bindingService->expects($this->once())->method('deleteInState')->with(111, 'pad-new', BindingService::STATE_PENDING_DELETE)->willReturn(true); - $etherpadClient = $this->createMock(EtherpadClient::class); - $etherpadClient->method('getRevisionsCount')->with('pad-new')->willReturn(5); - $etherpadClient->expects($this->once())->method('getText')->with('pad-new')->willReturn('the row\'s pad'); - $etherpadClient->expects($this->once())->method('deletePad')->with('pad-new'); - $file = $this->trashedFile(111, snapshotRev: 50); - $file->expects($this->once())->method('putContent')->with('doc-after'); - - $this->assertSame(SettleOutcome::Settled, $this->owed($bindingService, $etherpadClient)->finishTrash($file, new RunBudget(new FixedClock(), 20.0))); - } - /** * A restore that took the row between the sweep's read and its delete * has the pad: the sweep's delete finds no row in pending_delete, and @@ -564,9 +543,9 @@ public function testNoCallIsStartedOnceTheReadTookTheRunsTime(): void { } /** Owed since $deletedAt; $updatedAt past that once a run moved it back. */ - private function pendingTrashRow(int $fileId, string $padId, int $updatedAt = 100, ?int $deletedAt = 100, ?string $replaced = null): BindingService&MockObject { + private function pendingTrashRow(int $fileId, string $padId, int $updatedAt = 100, ?int $deletedAt = 100): BindingService&MockObject { $bindingService = $this->createMock(BindingService::class); - $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $padId, accessMode: BindingService::ACCESS_PUBLIC, state: BindingService::STATE_PENDING_DELETE, deletedAt: $deletedAt, updatedAt: $updatedAt, replacedPadId: $replaced)); + $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding(fileId: $fileId, padId: $padId, accessMode: BindingService::ACCESS_PUBLIC, state: BindingService::STATE_PENDING_DELETE, deletedAt: $deletedAt, updatedAt: $updatedAt)); return $bindingService; } diff --git a/tests/phpunit/unit/PadLifecycleControllerTest.php b/tests/phpunit/unit/PadLifecycleControllerTest.php index f3058b76..722376ba 100644 --- a/tests/phpunit/unit/PadLifecycleControllerTest.php +++ b/tests/phpunit/unit/PadLifecycleControllerTest.php @@ -4,7 +4,6 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; -use OCA\EtherpadNextcloud\Controller\PadControllerErrorMapper; use OCA\EtherpadNextcloud\Controller\PadLifecycleController; use OCA\EtherpadNextcloud\Exception\PadAlreadyHasBindingException; use OCA\EtherpadNextcloud\Service\AppConfigService; diff --git a/tests/phpunit/unit/PadSessionControllerTest.php b/tests/phpunit/unit/PadSessionControllerTest.php index 0968f153..78daa74a 100644 --- a/tests/phpunit/unit/PadSessionControllerTest.php +++ b/tests/phpunit/unit/PadSessionControllerTest.php @@ -4,7 +4,6 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; -use OCA\EtherpadNextcloud\Controller\PadControllerErrorMapper; use OCA\EtherpadNextcloud\Controller\PadSessionController; use OCA\EtherpadNextcloud\Service\AppConfigService; use OCA\EtherpadNextcloud\Service\Binding; diff --git a/tests/phpunit/unit/PadSyncServiceTest.php b/tests/phpunit/unit/PadSyncServiceTest.php index 7ca916b7..e848fdee 100644 --- a/tests/phpunit/unit/PadSyncServiceTest.php +++ b/tests/phpunit/unit/PadSyncServiceTest.php @@ -350,9 +350,9 @@ public function testTheFirstSyncAfterARestoreFromTheSnapshotFetchesTheNewPad(): } /** - * A file that still names the pad its row replaced has no revision of - * the row's pad, however high its own: the sync writes the row's pad - * into it, name and all, and says so once. Its status is out of sync. + * A file that still names the pad its row replaced, at a higher + * revision: the sync writes the row's pad into it, name and all, and + * says so once. Its status is out of sync. */ public function testAFileNamingThePadItsRowReplacedIsSyncedFromTheRowsPad(): void { $formatter = new PadFileService(new FixedClock()); diff --git a/tests/phpunit/unit/PublicViewerControllerTest.php b/tests/phpunit/unit/PublicViewerControllerTest.php index d1b5d204..7796bf9d 100644 --- a/tests/phpunit/unit/PublicViewerControllerTest.php +++ b/tests/phpunit/unit/PublicViewerControllerTest.php @@ -5,7 +5,6 @@ namespace OCA\EtherpadNextcloud\Tests\Unit; use OCA\EtherpadNextcloud\Controller\PublicViewerController; -use OCA\EtherpadNextcloud\Controller\PublicViewerControllerErrorMapper; use OCA\EtherpadNextcloud\Service\Binding; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\EtherpadClient; diff --git a/tests/phpunit/unit/RestoreServiceTest.php b/tests/phpunit/unit/RestoreServiceTest.php index 3b01a789..10e4aa37 100644 --- a/tests/phpunit/unit/RestoreServiceTest.php +++ b/tests/phpunit/unit/RestoreServiceTest.php @@ -650,11 +650,9 @@ public function testRestoreReplacesAPadThatIsBehindTheSnapshot(): void { } /** - * A file that still names the pad its row replaced holds no revision of - * the row's pad, however high the one it has: while Etherpad has the - * row's pad, the file gets it back as it is. Held against the old pad's - * revision, the row's pad looked behind and was replaced by a new pad - * made from the old pad's text. + * A file that still names the pad its row replaced, at a higher + * revision: while Etherpad has the row's pad, the file gets it back as + * it is, not a new pad made from the old pad's text. */ public function testARestoreDoesNotHoldTheRowsPadToThePadItReplaced(): void { $fileId = 98; @@ -1489,9 +1487,6 @@ function (ParsedPadFile $pad, string $text, string $html, string $padId, string return 'doc-after'; }, ); - $padFileService->method('namingPad')->willReturnCallback( - static fn (ParsedPadFile $pad, string $padId, string $accessMode, string $padUrl): ParsedPadFile => (new PadFileService(new FixedClock()))->namingPad($pad, $padId, $accessMode, $padUrl), - ); $secureRandom = $this->createMock(ISecureRandom::class); $secureRandom->method('generate')->willReturn('abc123def456'); diff --git a/tests/phpunit/unit/TrashSnapshotWriterTest.php b/tests/phpunit/unit/TrashSnapshotWriterTest.php index 74072e84..2cca0169 100644 --- a/tests/phpunit/unit/TrashSnapshotWriterTest.php +++ b/tests/phpunit/unit/TrashSnapshotWriterTest.php @@ -100,11 +100,7 @@ public function testReadHandsBackThePadAsTheFileRecordsIt(): void { $this->assertSame([], $this->logged); } - /** - * A file that names the pad its row replaced holds no revision of the - * row's pad, however high the one it has: it reads as naming the row's - * pad, with none. A file that names the row's pad reads as it is. - */ + /** A file that names the pad its row replaced, at a higher revision, reads as naming the row's pad, with none. */ public function testReadTakesAFileNamingThePadItsRowReplacedForTheRows(): void { $this->file->method('getContent')->willReturn('doc-before'); $this->padFiles->method('readPad')->willReturn($this->pad(snapshotRev: 50, padId: 'pad-before')); @@ -112,7 +108,7 @@ public function testReadTakesAFileNamingThePadItsRowReplacedForTheRows(): void { $pad = $this->writer(replaced: 'pad-before')->read(); $this->assertInstanceOf(ParsedPadFile::class, $pad); - $this->assertSame(['pad-a', -1, self::PAD_BASE . 'pad-a', 'body'], [$pad->padId, $pad->snapshotRev, $pad->padUrl, $pad->body]); + $this->assertSame(['pad-a', -1, self::padUrlOf('pad-a'), 'body'], [$pad->padId, $pad->snapshotRev, $pad->padUrl, $pad->body]); $this->assertSame([], $this->logged); } From bb7bdb0bc1642ade779b85407d4fc9dd89db8e76 Mon Sep 17 00:00:00 2001 From: Jaggob <37583151+Jaggob@users.noreply.github.com> Date: Sat, 26 Sep 2026 15:01:29 +0200 Subject: [PATCH 4/5] Rewrite a followed file where it is now, and name a new pad one way The open's rewrite finds the file again by its id, as the initialise does, so a file moved or deleted since the read is not written as a new file where it was; a rewrite skipped for a changed file is logged at debug too. Whether a file followed its row is told by its pad id, not by object identity, and the resolver builds the followed pad only after the row's state is checked. withRestoredSnapshot() is namingPad() with the snapshot, so a file that comes to name another pad is active and undeleted either way, and a restore writes the mode it made the new pad in rather than the file's. A recovery does not remember a pad id longer than the column, which only an edited file can name; the row is written all the same. sync-app.sh upgrades the app only when a migration is outstanding. The controller tests expect one read of the row per open again, and tests cover a restore that replaces the row's pad for a followed file and for one in another mode, the rewrite after an open decided a waiting row, and the rewrite of a moved or deleted file. architecture.md says that rows from before the column remember no pad. --- docs/architecture.md | 6 +- lib/Service/BindingService.php | 8 +- lib/Service/BoundPadResolver.php | 31 ++++--- lib/Service/PadFileService.php | 29 ++++--- lib/Service/PadOpenService.php | 18 +++- lib/Service/PadSyncService.php | 2 +- lib/Service/RestoreService.php | 1 + tests/e2e/docker/sync-app.sh | 12 ++- tests/phpunit/unit/BindingServiceTest.php | 8 +- tests/phpunit/unit/PadFileServiceTest.php | 33 +++++++ tests/phpunit/unit/PadOpenServiceTest.php | 86 ++++++++++++++++++- .../phpunit/unit/PadSessionControllerTest.php | 2 +- tests/phpunit/unit/PadSyncServiceTest.php | 2 +- .../unit/PublicViewerControllerTest.php | 2 +- tests/phpunit/unit/RestoreServiceTest.php | 57 +++++++++++- 15 files changed, 248 insertions(+), 49 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 3c2276cf..64e7cfcd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -85,7 +85,7 @@ checked-in runtime assets in `js/`. - `deleted_at` - `created_at` - `updated_at` - - `replaced_pad_id` (migration `Version000004Date20260926120000`): the pad the row's pad replaced, when a restore or a recovery put a new one in its place (see "Which pad a file reaches"). Nextcloud runs the migration when the app's version rises; `tests/e2e/docker/sync-app.sh` runs it for a working tree synced into the test stack. + - `replaced_pad_id` (migration `Version000004Date20260926120000`): the pad the row's pad replaced, when a restore or a recovery put a new one in its place (see "Which pad a file reaches"). Rows from before it remember no pad. Nextcloud runs the migration when the app's version rises; `tests/e2e/docker/sync-app.sh` runs it for a working tree synced into the test stack. - stores internal managed pads only; external `ext.*` rows from earlier development versions are removed by `Version000003Date20260512230000` - `.pad` file - Frontmatter: format, binding metadata, state, export metadata. @@ -102,12 +102,12 @@ A `.pad` file names a pad, and whoever may write the file can make it name any p - The file names the row's pad, in the row's access mode: that pad. - The file names the pad the row replaced (`replaced_pad_id`): the row's pad. A restore that put a new pad in place of one that was gone or behind remembers the old one, and so does the recovery of a file without a row, which makes a new pad. A file that still names the old pad reaches the new one: an older version of it restored through Nextcloud's versions, or one whose rewrite failed. - - An open that may write the file rewrites it to name the row's pad, with the row's access mode and link and no snapshot revision, if the file still holds what the open read. It logs `A .pad file named the pad its row replaced; it now names the row's.` as a warning. A rewrite that fails is logged at debug, and the row's pad opens all the same. + - An open that may write the file rewrites it to name the row's pad, with the row's access mode and link, active, and no snapshot revision. It finds the file again by its id, as the initialise does, and writes only while the file holds what the open read; a file moved meanwhile is written where it is now, a deleted one not at all. It logs `A .pad file named the pad its row replaced; it now names the row's.` as a warning. A rewrite skipped for a changed file, or one that fails, is logged at debug, and the row's pad opens all the same. There is no lock between the comparison and the write: a sync that writes in between is written over with the older text and no revision, and the next sync writes it again. - A sync writes the row's pad's snapshot into the file, under the row's pad, with the same line. - Whatever only reads - an open without write permission, a public share, the read-only content view, sync status - leaves the file as it is, and says so at debug. - The file names any other pad, or the row's pad in another access mode: refused, `Binding pad ID mismatch.` or `Binding access mode mismatch.` (see "Errors of the API"). This comes before the row's state is looked at, so such a file neither decides nor waits for a row that is not its own. -A row remembers one pad, the one its latest change of pad replaced. A file that names a pad from before that is refused like any other. +A row remembers one pad, the one its latest change of pad replaced. A file that names a pad from before that is refused like any other, and so is one a restore or recovery left behind before the column existed: such rows remember no pad, and nothing can tell afterwards which one they replaced. A file that names the pad its row replaced holds that pad's `snapshot_rev`, not one of the row's pad. Wherever the file's revision is held against the row's pad - sync and sync status, the trash, the sweep's rest of a trash, a restore - it counts as none: a higher revision of the old pad would make the row's pad look behind, and a trash would leave the old pad's text in the file and the row's pad over. Only then. A file that names the row's pad keeps its revision, and so does one that names any other pad: such a file need not be the row's at all - one overwritten with another `.pad`'s content, or a file id the file cache gave out again - so its revision is not given up for the row's pad. Trash, sweep and restore hold the row's pad to it as before: a pad behind it is left in place and logged with its id, as any pad behind is. diff --git a/lib/Service/BindingService.php b/lib/Service/BindingService.php index d0bea14b..b41b9309 100644 --- a/lib/Service/BindingService.php +++ b/lib/Service/BindingService.php @@ -30,6 +30,8 @@ class BindingService { * be gone. */ public const STATE_RESTORE_PENDING = 'restore_pending'; + /** What pad_id and replaced_pad_id hold at most; bytes, so it holds on every database. */ + private const PAD_ID_MAX_BYTES = 255; /** * Where trashes keep files, as file cache paths relative to their @@ -256,10 +258,14 @@ private function findWaitingByAge(string $state, string $ageColumn, int $minAgeS /** * $replacedPadId: the pad the file named, when the new pad takes its - * place (a recovery); see rebind(). + * place (a recovery); see rebind(). It comes from the file, so one longer + * than the column is no pad of this app's, and is not remembered. */ public function createBinding(int $fileId, string $padId, string $accessMode, ?string $replacedPadId = null): void { $this->assertAccessMode($accessMode); + if ($replacedPadId !== null && strlen($replacedPadId) > self::PAD_ID_MAX_BYTES) { + $replacedPadId = null; + } $now = $this->timeFactory->getTime(); $qb = $this->db->getQueryBuilder(); diff --git a/lib/Service/BoundPadResolver.php b/lib/Service/BoundPadResolver.php index 8aa8e522..43e0b6bc 100644 --- a/lib/Service/BoundPadResolver.php +++ b/lib/Service/BoundPadResolver.php @@ -36,7 +36,8 @@ public function __construct( /** * $pad as its active row has it: $pad itself when it names the row's * pad, the file as namingPad() makes it when it names the pad the row's - * replaced. + * replaced. Only then does the pad returned differ from the file's by + * its id, which is how a caller tells the file is out of date. * * @throws MissingBindingException the file has no row * @throws BindingMismatchException the file names neither pad @@ -50,8 +51,8 @@ public function resolve(int $fileId, ParsedPadFile $pad): ParsedPadFile { } // Before the state: a file that names neither pad is not this row's // to settle or wait for. - $bound = $this->followingRow($pad, $binding); - if ($bound === $pad) { + $namesReplaced = $this->namesReplacedPad($pad, $binding); + if (!$namesReplaced) { if ($binding->padId !== $pad->padId) { throw new BindingMismatchException('Binding pad ID mismatch.'); } @@ -66,13 +67,14 @@ public function resolve(int $fileId, ParsedPadFile $pad): ParsedPadFile { // A state the sweep never takes up, so nothing to wait for either. throw new BindingException('Pad binding is not active.'); } - if ($bound !== $pad) { - $this->logger->debug('A .pad file names the pad its row replaced; the row\'s pad is used.', [ - 'app' => 'etherpad_nextcloud', - 'fileId' => $fileId, - ]); + if (!$namesReplaced) { + return $pad; } - return $bound; + $this->logger->debug('A .pad file names the pad its row replaced; the row\'s pad is used.', [ + 'app' => 'etherpad_nextcloud', + 'fileId' => $fileId, + ]); + return $this->namingRowsPad($pad, $binding); } /** @@ -84,9 +86,14 @@ public function resolve(int $fileId, ParsedPadFile $pad): ParsedPadFile { * file reaches"). */ public function followingRow(ParsedPadFile $pad, Binding $binding): ParsedPadFile { - if ($pad->padId !== $binding->replacedPadId || $pad->padId === $binding->padId) { - return $pad; - } + return $this->namesReplacedPad($pad, $binding) ? $this->namingRowsPad($pad, $binding) : $pad; + } + + private function namesReplacedPad(ParsedPadFile $pad, Binding $binding): bool { + return $pad->padId === $binding->replacedPadId && $pad->padId !== $binding->padId; + } + + private function namingRowsPad(ParsedPadFile $pad, Binding $binding): ParsedPadFile { return $this->padFileService->namingPad($pad, $binding->padId, $binding->accessMode, $this->etherpadClient->buildPadUrl($binding->padId)); } diff --git a/lib/Service/PadFileService.php b/lib/Service/PadFileService.php index 09b0a82a..adf6b3a9 100644 --- a/lib/Service/PadFileService.php +++ b/lib/Service/PadFileService.php @@ -203,8 +203,8 @@ public function isExternalFrontmatter(array $frontmatter, string $padId): bool { } /** - * The document a restore writes: active again, no deletion timestamp, and - * pointed at the pad that was provisioned to replace the old one. + * The document a restore writes: the file as namingPad() makes it for + * the pad provisioned to replace the old one, holding the snapshot. * * The new pad counts revisions from zero, so the old pad's count cannot * carry over: the sync would take every revision below it as already @@ -220,33 +220,34 @@ public function withRestoredSnapshot( string $text, string $html, string $padId, + string $accessMode, string $padUrl, int $revision = -1, ): string { - $frontmatter = $pad->frontmatter; - $frontmatter['state'] = BindingService::STATE_ACTIVE; - $frontmatter['updated_at'] = $this->nowIso(); - $frontmatter['deleted_at'] = null; - $frontmatter['pad_id'] = $padId; + $frontmatter = $this->namingPad($pad, $padId, $accessMode, $padUrl)->frontmatter; $frontmatter['snapshot_rev'] = max(-1, $revision); - if ($padUrl !== '') { - $frontmatter['pad_url'] = $padUrl; - } return $this->serialize($frontmatter, $this->buildSnapshotBody($text, $html)); } /** * $pad as it reads once it names another pad: that pad's id, mode and - * address, and no snapshot revision, since the one it has counts the - * pad it named before. Its text stays until a sync or a trash writes the - * new pad's into it. Nothing is written here. + * address (none when $padUrl is empty), active, and no snapshot + * revision, since the one it has counts the pad it named before. Its + * text stays until a sync, a trash or a restore writes the new pad's + * into it. Nothing is written here. */ public function namingPad(ParsedPadFile $pad, string $padId, string $accessMode, string $padUrl): ParsedPadFile { $frontmatter = $pad->frontmatter; $frontmatter['pad_id'] = $padId; $frontmatter['access_mode'] = $accessMode; - $frontmatter['pad_url'] = $padUrl; + if ($padUrl === '') { + unset($frontmatter['pad_url']); + } else { + $frontmatter['pad_url'] = $padUrl; + } + $frontmatter['state'] = BindingService::STATE_ACTIVE; + $frontmatter['deleted_at'] = null; $frontmatter['snapshot_rev'] = -1; $frontmatter['updated_at'] = $this->nowIso(); return new ParsedPadFile( diff --git a/lib/Service/PadOpenService.php b/lib/Service/PadOpenService.php index deb61a04..52072dcb 100644 --- a/lib/Service/PadOpenService.php +++ b/lib/Service/PadOpenService.php @@ -84,8 +84,8 @@ private function openNode(string $uid, string $displayName, File $node, string $ $mayWrite = $node->isUpdateable(); if (!$pad->isExternal) { $bound = $this->settleOnOpen->settleThenResolve($node, $fileId, $pad); - if ($bound !== $pad && $mayWrite) { - $this->followRow($node, $fileId, $content, $bound); + if ($bound->padId !== $pad->padId && $mayWrite) { + $this->followRow($uid, $fileId, $content, $bound); } $pad = $bound; } @@ -98,10 +98,22 @@ private function openNode(string $uid, string $displayName, File $node, string $ * write it: it is written to name the row's pad, once, over what this * open read and nothing newer. Should that not work, the row's pad * opens all the same, and the next open or sync tries again. + * + * The file is found again by its id, as the initialise finds it: one + * moved or deleted since the read is written where it is now, or not at + * all, never as a new file where it was. Compared and written without a + * lock, as there: a sync that writes between the two is written over + * with the older text and no revision, and the next sync writes it + * again; the pad holds the text either way. */ - private function followRow(File $node, int $fileId, string $read, ParsedPadFile $bound): void { + private function followRow(string $uid, int $fileId, string $read, ParsedPadFile $bound): void { try { + $node = $this->userNodeResolver->resolveUserFileNodeById($uid, $fileId); if ($node->getContent() !== $read) { + $this->logger->debug('A .pad file changed since it was opened; it is not rewritten to its row\'s pad.', [ + 'app' => 'etherpad_nextcloud', + 'fileId' => $fileId, + ]); return; } $node->putContent($this->padFileService->serialize($bound->frontmatter, $bound->body)); diff --git a/lib/Service/PadSyncService.php b/lib/Service/PadSyncService.php index baf2486b..1728d82a 100644 --- a/lib/Service/PadSyncService.php +++ b/lib/Service/PadSyncService.php @@ -65,7 +65,7 @@ public function syncById(string $uid, int $fileId, bool $force): PadSyncResult { // A file that named the pad its row replaced comes without a // revision (BoundPadResolver::followingRow()): always written. $result = $this->syncInternalPad($node, $fileId, $synced, $force); - if ($synced !== $pad) { + if ($synced->padId !== $pad->padId) { $this->boundPads->repaired($fileId); } return $result; diff --git a/lib/Service/RestoreService.php b/lib/Service/RestoreService.php index b089e57a..5c06a9e2 100644 --- a/lib/Service/RestoreService.php +++ b/lib/Service/RestoreService.php @@ -451,6 +451,7 @@ private function seedFromSnapshot(int $fileId, ParsedPadFile $pad, string $acces $snapshot['text'], $snapshot['html'], $newPadId, + $accessMode, $this->etherpadClient->buildPadUrl($newPadId), $this->revisionsOfSeededPad($newPadId), ); diff --git a/tests/e2e/docker/sync-app.sh b/tests/e2e/docker/sync-app.sh index f6a430a8..e04553a8 100755 --- a/tests/e2e/docker/sync-app.sh +++ b/tests/e2e/docker/sync-app.sh @@ -34,8 +34,12 @@ compose restart nextcloud >/dev/null compose exec -T -u www-data nextcloud sh -c 'for _ in $(seq 1 60); do php occ status 2>/dev/null | grep -q "installed: true" && exit 0; sleep 1; done; exit 1' # Nextcloud runs an app's migrations when info.xml's version is above the -# one it has installed, and a branch leaves the version as it is. Marked -# older, the app is upgraded as a release upgrades it, and gets the -# migrations it has not run yet. On a new stack up.sh enables the app +# one it has installed, and a branch leaves the version as it is. An +# enabled app with a migration it has not run yet is marked older and +# upgraded, as a release upgrades it. On a new stack up.sh enables the app # after this, which runs them all. -compose exec -T -u www-data nextcloud sh -c 'if [ "$(php occ config:app:get etherpad_nextcloud enabled 2>/dev/null)" = yes ]; then php occ config:app:set etherpad_nextcloud installed_version --value 0.0.0 && php occ upgrade; fi' >/dev/null +as_www() { compose exec -T -u www-data nextcloud "$@"; } +if [ "$(as_www php occ config:app:get etherpad_nextcloud enabled 2>/dev/null)" = yes ] && + [ -n "$(as_www php -r 'require "lib/base.php"; echo (new \OC\DB\MigrationService("etherpad_nextcloud", \OCP\Server::get(\OC\DB\Connection::class)))->getMigration("next");')" ]; then + as_www sh -c 'php occ config:app:set etherpad_nextcloud installed_version --value 0.0.0 && php occ upgrade' >/dev/null +fi diff --git a/tests/phpunit/unit/BindingServiceTest.php b/tests/phpunit/unit/BindingServiceTest.php index a882248b..edfc9d7f 100644 --- a/tests/phpunit/unit/BindingServiceTest.php +++ b/tests/phpunit/unit/BindingServiceTest.php @@ -225,9 +225,13 @@ public function testAChangeOfStateKeepsThePadTheRowReplaced(): void { self::assertSame('before', $table->rows[0]['replaced_pad_id']); } - /** A recovery's row says which pad the file named; any other new row replaced none. */ + /** + * A recovery's row says which pad the file named; any other new row + * replaced none. A pad id longer than the column, which only an edited + * file can name, is not remembered, and the row is written all the same. + */ public function testANewRowSaysWhichPadItReplaced(): void { - foreach (['a recovery' => ['old', 'old'], 'a create' => [null, null]] as $case => [$replaced, $written]) { + foreach (['a recovery' => ['old', 'old'], 'a create' => [null, null], 'a pad id no column holds' => [str_repeat('p', 256), null], 'the longest one it holds' => [str_repeat('p', 255), str_repeat('p', 255)]] as $case => [$replaced, $written]) { $qb = new class implements IQueryBuilder { /** @var array */ public array $values = []; diff --git a/tests/phpunit/unit/PadFileServiceTest.php b/tests/phpunit/unit/PadFileServiceTest.php index 292ce7cd..d597c034 100644 --- a/tests/phpunit/unit/PadFileServiceTest.php +++ b/tests/phpunit/unit/PadFileServiceTest.php @@ -8,6 +8,7 @@ use OCA\EtherpadNextcloud\Exception\PadFileFormatException; use OCA\EtherpadNextcloud\Service\BindingService; use OCA\EtherpadNextcloud\Service\PadFileService; +use OCA\EtherpadNextcloud\Service\ParsedPadFile; use OCA\EtherpadNextcloud\Service\PadSnapshot; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -615,6 +616,34 @@ public function testSnapshotTextMayContainTheOpeningMarker(): void { $this->assertSame('

HTML

', $parts['html']); } + /** + * A file read as naming another pad: that pad's id, mode and address, + * active and undeleted like a restored one, whatever the file said, and + * no snapshot revision. Its text and everything else stay. Without an + * address the file gives none rather than the old pad's. + */ + public function testNamingPadMakesTheFileNameAnotherPad(): void { + $service = new PadFileService(new FixedClock()); + $old = new ParsedPadFile( + frontmatter: ['format' => PadFileService::FORMAT_V1, 'file_id' => 1, 'pad_id' => 'old-pad', 'access_mode' => BindingService::ACCESS_PUBLIC, 'state' => 'trashed', 'deleted_at' => 1700000000, 'snapshot_rev' => 9, 'pad_url' => 'https://pad.example.test/p/old-pad', 'created_at' => '2026-01-01T00:00:00+00:00'], + body: 'the text', + padId: 'old-pad', + accessMode: BindingService::ACCESS_PUBLIC, + padUrl: 'https://pad.example.test/p/old-pad', + isExternal: false, + snapshotRev: 9, + ); + + $named = $service->namingPad($old, 'new-pad', BindingService::ACCESS_PROTECTED, 'https://pad.example.test/p/new-pad'); + + $this->assertSame(['new-pad', BindingService::ACCESS_PROTECTED, 'https://pad.example.test/p/new-pad', -1, 'the text'], [$named->padId, $named->accessMode, $named->padUrl, $named->snapshotRev, $named->body]); + $this->assertSame( + ['pad_id' => 'new-pad', 'access_mode' => BindingService::ACCESS_PROTECTED, 'state' => BindingService::STATE_ACTIVE, 'deleted_at' => null, 'snapshot_rev' => -1, 'pad_url' => 'https://pad.example.test/p/new-pad', 'created_at' => '2026-01-01T00:00:00+00:00'], + array_intersect_key($named->frontmatter, array_flip(['pad_id', 'access_mode', 'state', 'deleted_at', 'snapshot_rev', 'pad_url', 'created_at'])), + ); + $this->assertArrayNotHasKey('pad_url', $service->namingPad($old, 'new-pad', BindingService::ACCESS_PROTECTED, '')->frontmatter); + } + public function testWithRestoredSnapshotWritesTheRestoreInvariant(): void { $service = new PadFileService(new FixedClock()); $trashed = $service->readPad($service->withExportSnapshot( @@ -627,6 +656,7 @@ public function testWithRestoredSnapshotWritesTheRestoreInvariant(): void { 'replaced text', '

replaced html

', 'new-pad', + BindingService::ACCESS_PROTECTED, 'https://pad.example.test/p/new-pad', )); @@ -634,6 +664,8 @@ public function testWithRestoredSnapshotWritesTheRestoreInvariant(): void { $this->assertSame(BindingService::STATE_ACTIVE, $restored->frontmatter['state']); $this->assertNull($restored->frontmatter['deleted_at']); $this->assertSame('new-pad', $restored->padId); + // The mode the new pad was made in, not the one the file had. + $this->assertSame(BindingService::ACCESS_PROTECTED, $restored->accessMode); $this->assertSame('https://pad.example.test/p/new-pad', $restored->padUrl); // The old pad's revision 4 means nothing to the new one: without the // new pad's own count, the document has not been synced yet. @@ -643,6 +675,7 @@ public function testWithRestoredSnapshotWritesTheRestoreInvariant(): void { 'replaced text', '

replaced html

', 'new-pad', + BindingService::ACCESS_PUBLIC, 'https://pad.example.test/p/new-pad', 2, ))->snapshotRev); diff --git a/tests/phpunit/unit/PadOpenServiceTest.php b/tests/phpunit/unit/PadOpenServiceTest.php index e9c1f8c4..f13e276a 100644 --- a/tests/phpunit/unit/PadOpenServiceTest.php +++ b/tests/phpunit/unit/PadOpenServiceTest.php @@ -26,6 +26,7 @@ use OCA\EtherpadNextcloud\Tests\Support\FixedClock; use OCA\EtherpadNextcloud\Tests\Support\SettlesOnOpen; use OCP\Files\File; +use OCP\Files\NotFoundException; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -261,6 +262,7 @@ public function testAFileNamingThePadItsRowReplacedOpensTheRowsPad(): void { $this->assertSame('r-new-pad', $target->padId, $case); $this->assertStringStartsWith('https://pad.example.test/p/r-new-pad', $target->url, $case); $this->assertSame($writeFails, in_array('Could not rewrite a .pad file to its row\'s pad; the row\'s pad opens all the same.', $debugs, true), $case); + $this->assertSame($changedMeanwhile, in_array('A .pad file changed since it was opened; it is not rewritten to its row\'s pad.', $debugs, true), $case); if ($rewritten) { $after = $formatter->readPad((string)$written); $this->assertSame(['r-new-pad', -1, 'old text'], [$after->padId, $after->snapshotRev, $formatter->getSnapshotPartsFromBody($after->body)['text']], $case); @@ -270,6 +272,80 @@ public function testAFileNamingThePadItsRowReplacedOpensTheRowsPad(): void { } } + /** + * A row that waits is decided first, then the file follows it: a file + * that names the pad the row replaced is rewritten once the open has + * taken the row back. + */ + public function testAFileNamingThePadItsRowReplacedIsRewrittenAfterItsRowIsDecided(): void { + $formatter = new PadFileService(new FixedClock()); + $stale = $formatter->buildInitialDocument(138, 'old-pad', BindingService::ACCESS_PUBLIC); + $file = $this->createMock(File::class); + $file->method('getId')->willReturn(138); + $file->method('isUpdateable')->willReturn(true); + $file->method('getContent')->willReturn($stale); + $written = null; + $file->expects($this->once())->method('putContent')->willReturnCallback(static function (string $content) use (&$written): void { + $written = $content; + }); + $waiting = new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_RESTORE_PENDING, updatedAt: FixedClock::NOW - 3600, replacedPadId: 'old-pad'); + $bindings = $this->createMock(BindingService::class); + $bindings->method('findByFileId')->willReturnOnConsecutiveCalls( + $waiting, + $waiting, + new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad'), + ); + $restores = $this->createMock(RestoreService::class); + $restores->expects($this->once())->method('settleOpenedFile')->willReturn(SettleOutcome::Settled); + $client = $this->createMock(EtherpadClient::class); + $client->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::padUrlOf($padId)); + + $target = $this->openService($formatter, $file, $bindings, $client, $this->createMock(LoggerInterface::class), $restores)->openById('alice', 'Alice', 138); + + $this->assertSame('r-new-pad', $target->padId); + $this->assertSame('r-new-pad', $formatter->readPad((string)$written)->padId); + } + + /** + * The rewrite finds the file by its id again: one moved since the open + * read it is written where it is now, and one deleted is not written at + * all, never made anew where it was. The row's pad opens either way. + */ + public function testTheRewriteGoesToTheFileWhereItIsNow(): void { + foreach (['moved' => true, 'deleted' => false] as $case => $stillThere) { + $formatter = new PadFileService(new FixedClock()); + $stale = $formatter->buildInitialDocument(138, 'old-pad', BindingService::ACCESS_PUBLIC); + $opened = $this->createMock(File::class); + $opened->method('getId')->willReturn(138); + $opened->method('isUpdateable')->willReturn(true); + $opened->method('getContent')->willReturn($stale); + $opened->expects($this->never())->method('putContent'); + $now = $this->createMock(File::class); + $now->method('getContent')->willReturn($stale); + $now->expects($stillThere ? $this->once() : $this->never())->method('putContent'); + // The open finds the file it reads; the rewrite finds it again. + $calls = 0; + $nodes = $this->createMock(UserNodeResolver::class); + $nodes->method('resolveUserFileNodeById')->with('alice', 138)->willReturnCallback(static function () use ($opened, $now, $stillThere, &$calls): File { + if (++$calls === 1) { + return $opened; + } + if (!$stillThere) { + throw new NotFoundException('gone'); + } + return $now; + }); + $bindings = $this->createMock(BindingService::class); + $bindings->method('findByFileId')->willReturn(new Binding(138, 'r-new-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE, replacedPadId: 'old-pad')); + $client = $this->createMock(EtherpadClient::class); + $client->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::padUrlOf($padId)); + + $target = $this->openService($formatter, $opened, $bindings, $client, $this->createMock(LoggerInterface::class), userNodeResolver: $nodes)->openById('alice', 'Alice', 138); + + $this->assertSame('r-new-pad', $target->padId, $case); + } + } + /** A file that names any other pad than its row's, or the one that replaced, opens nothing. */ public function testAFileNamingAPadItsRowDoesNotGiveOpensNothing(): void { $bindings = $this->createMock(BindingService::class); @@ -341,16 +417,18 @@ private function openWith( } /** Over a real PadFileService, a file of its own and rows of its own. */ - private function openService(PadFileService $formatter, File $file, BindingService $bindings, EtherpadClient $client, LoggerInterface $logger): PadOpenService { - $userNodeResolver = $this->createMock(UserNodeResolver::class); - $userNodeResolver->method('resolveUserFileNodeById')->willReturn($file); + private function openService(PadFileService $formatter, File $file, BindingService $bindings, EtherpadClient $client, LoggerInterface $logger, ?RestoreService $restores = null, ?UserNodeResolver $userNodeResolver = null): PadOpenService { + if ($userNodeResolver === null) { + $userNodeResolver = $this->createMock(UserNodeResolver::class); + $userNodeResolver->method('resolveUserFileNodeById')->willReturn($file); + } return new PadOpenService( $formatter, $this->createMock(PathNormalizer::class), $userNodeResolver, new PadFileLockRetryService(static function (int $delay): void { }), - $this->settleOnOpen($bindings, logger: $logger), + $this->settleOnOpen($bindings, $restores, $logger), $client, $this->createMock(ExternalPadExportFetcher::class), $this->createMock(PadSessionService::class), diff --git a/tests/phpunit/unit/PadSessionControllerTest.php b/tests/phpunit/unit/PadSessionControllerTest.php index 78daa74a..1b1066a8 100644 --- a/tests/phpunit/unit/PadSessionControllerTest.php +++ b/tests/phpunit/unit/PadSessionControllerTest.php @@ -91,7 +91,7 @@ public function testOpenByIdRetriesLockedReadAndEventuallySucceeds(): void { )); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->atLeastOnce()) + $bindingService->expects($this->once()) ->method('findByFileId') ->with(138) ->willReturn(new Binding(138, 'g.ABCDEFGHIJKLMNOP$pad-1', BindingService::ACCESS_PUBLIC, BindingService::STATE_ACTIVE)); diff --git a/tests/phpunit/unit/PadSyncServiceTest.php b/tests/phpunit/unit/PadSyncServiceTest.php index e848fdee..d7f5c88a 100644 --- a/tests/phpunit/unit/PadSyncServiceTest.php +++ b/tests/phpunit/unit/PadSyncServiceTest.php @@ -317,7 +317,7 @@ public function testTheFirstSyncAfterARestoreFromTheSnapshotFetchesTheNewPad(): $formatter->readPad($formatter->buildInitialDocument(138, 'old-pad', BindingService::ACCESS_PUBLIC)), new PadSnapshot('old text', '', 500), )); - $restored = $formatter->withRestoredSnapshot($trashed, 'old text', '', 'r-new-pad', 'https://pad.example.test/p/r-new-pad'); + $restored = $formatter->withRestoredSnapshot($trashed, 'old text', '', 'r-new-pad', BindingService::ACCESS_PUBLIC, 'https://pad.example.test/p/r-new-pad'); $file = $this->createMock(File::class); $file->method('getName')->willReturn('Notes.pad'); diff --git a/tests/phpunit/unit/PublicViewerControllerTest.php b/tests/phpunit/unit/PublicViewerControllerTest.php index 7796bf9d..30a35491 100644 --- a/tests/phpunit/unit/PublicViewerControllerTest.php +++ b/tests/phpunit/unit/PublicViewerControllerTest.php @@ -77,7 +77,7 @@ public function testProtectedReadOnlyPublicShareReturnsSnapshotWithoutEtherpadSe $padFileService->expects($this->never())->method('getSnapshotPartsFromBody'); $bindingService = $this->createMock(BindingService::class); - $bindingService->expects($this->atLeastOnce()) + $bindingService->expects($this->once()) ->method('findByFileId') ->with(42) ->willReturn(new Binding(42, 'g.abcdefghijklmnop$Shared', BindingService::ACCESS_PROTECTED, BindingService::STATE_ACTIVE)); diff --git a/tests/phpunit/unit/RestoreServiceTest.php b/tests/phpunit/unit/RestoreServiceTest.php index 10e4aa37..020e7f42 100644 --- a/tests/phpunit/unit/RestoreServiceTest.php +++ b/tests/phpunit/unit/RestoreServiceTest.php @@ -13,6 +13,7 @@ use OCA\EtherpadNextcloud\Service\LifecycleResult; use OCA\EtherpadNextcloud\Service\RestoreService; use OCA\EtherpadNextcloud\Service\PadFileService; +use OCA\EtherpadNextcloud\Service\PadSnapshot; use OCA\EtherpadNextcloud\Service\RunBudget; use OCA\EtherpadNextcloud\Service\SettleOutcome; use OCA\EtherpadNextcloud\Service\TestFaults; @@ -674,6 +675,58 @@ public function testARestoreDoesNotHoldTheRowsPadToThePadItReplaced(): void { $this->assertSame(['status' => LifecycleResult::RESTORED, 'old_pad_id' => 'old-pad', 'new_pad_id' => 'old-pad'], array_intersect_key($result, array_flip(['status', 'old_pad_id', 'new_pad_id']))); } + /** + * The row's pad is gone, and the file names the pad the row replaced, or + * the row's pad in another access mode: the new pad is made in the row's + * mode from the file's snapshot, and the file names it, in that mode, + * active and at the new pad's count, whatever the file said before. + */ + public function testARestoreReplacesTheRowsPadInTheRowsMode(): void { + foreach (['names the pad it replaced' => 'pad-before', 'names the row\'s pad in another mode' => 'old-pad'] as $case => $filePadId) { + $fileId = 99; + $newPadId = 'r-old-pad-abc123def456'; + $formatter = new PadFileService(new FixedClock()); + $bindingService = $this->createMock(BindingService::class); + $bindingService->method('findByFileId')->with($fileId)->willReturn(new Binding($fileId, 'old-pad', BindingService::ACCESS_PUBLIC, BindingService::STATE_PENDING_DELETE, replacedPadId: 'pad-before')); + $bindingService->expects($this->once()) + ->method('rebind') + ->with($fileId, 'old-pad', BindingService::STATE_PENDING_DELETE, $newPadId, BindingService::STATE_ACTIVE) + ->willReturn(true); + $etherpadClient = $this->createMock(EtherpadClient::class); + $etherpadClient->method('getRevisionsCount')->willReturnCallback(static function (string $padId): int { + if ($padId === 'old-pad') { + throw new \RuntimeException('padID does not exist'); + } + return 1; + }); + $etherpadClient->method('buildPadUrl')->willReturnCallback(static fn (string $padId): string => self::padUrlOf($padId)); + // A public pad: the row's mode, not the file's. + $etherpadClient->expects($this->once())->method('createPad')->with($newPadId); + $etherpadClient->expects($this->never())->method('createGroupPad'); + $etherpadClient->expects($this->once())->method('setText')->with($newPadId, 'Text of the old pad'); + $secureRandom = $this->createMock(ISecureRandom::class); + $secureRandom->method('generate')->willReturn('abc123def456'); + $written = null; + $file = $this->createMock(File::class); + $file->method('getId')->willReturn($fileId); + $file->method('getName')->willReturn('Restored.pad'); + $file->method('getContent')->willReturn($formatter->buildInitialDocument($fileId, $filePadId, BindingService::ACCESS_PROTECTED, new PadSnapshot('Text of the old pad', '', 500))); + $file->expects($this->once())->method('putContent')->willReturnCallback(static function (string $content) use (&$written): void { + $written = $content; + }); + + $result = $this->restoreService(bindings: $bindingService, etherpad: $etherpadClient, padFiles: $formatter, secureRandom: $secureRandom)->restore($file); + + $this->assertSame([LifecycleResult::RESTORED, $newPadId], [$result['status'], $result['new_pad_id']], $case); + $after = $formatter->readPad((string)$written); + $this->assertSame( + [$newPadId, BindingService::ACCESS_PUBLIC, self::padUrlOf($newPadId), 1, BindingService::STATE_ACTIVE, 'Text of the old pad'], + [$after->padId, $after->accessMode, $after->padUrl, $after->snapshotRev, $after->frontmatter['state'], $formatter->getSnapshotPartsFromBody($after->body)['text']], + $case, + ); + } + } + /** * Without the file there is no revision to hold the pad to, so the row * waits for a later check rather than being settled blind. @@ -1182,7 +1235,7 @@ public function testRestoreWithoutBindingRecreatesManagedPublicPad(): void { ]); $padFileService->expects($this->once()) ->method('withRestoredSnapshot') - ->with($this->identicalTo($parsedPad), 'plain text', '', $newPadId, $newPadUrl) + ->with($this->identicalTo($parsedPad), 'plain text', '', $newPadId, BindingService::ACCESS_PUBLIC, $newPadUrl) ->willReturn('doc-after'); $etherpadClient = $this->createMock(EtherpadClient::class); @@ -1482,7 +1535,7 @@ private function buildPendingDeleteRestoreService( $padFileService->method('readPad')->with('doc-before')->willReturn($parsedPad); $padFileService->method('getSnapshotPartsFromBody')->with($parsedPad->body)->willReturn(['text' => 'plain text', 'html' => $html]); $padFileService->method('withRestoredSnapshot')->willReturnCallback( - function (ParsedPadFile $pad, string $text, string $html, string $padId, string $padUrl, int $revision = -1): string { + function (ParsedPadFile $pad, string $text, string $html, string $padId, string $accessMode, string $padUrl, int $revision = -1): string { $this->restoredRevision = $revision; return 'doc-after'; }, From 7f67515d52405d5104702d4b799fd29007b30713 Mon Sep 17 00:00:00 2001 From: Jaggob <37583151+Jaggob@users.noreply.github.com> Date: Sat, 26 Sep 2026 15:10:23 +0200 Subject: [PATCH 5/5] Say each part of the rewrite and the restore once architecture.md describes the open's rewrite; followRow() keeps only why it finds the file by id and takes no lock. withRestoredSnapshot() says what it adds to namingPad(), the writer's read() and the restore no longer repeat followingRow(), and the column's note points at the section on older rows. The new migration takes the column's length from BindingService, and the restore test keeps only what namingPad()'s does not hold: the body and the revision. --- docs/architecture.md | 2 +- .../Version000004Date20260926120000.php | 2 +- lib/Service/BindingService.php | 6 +++--- lib/Service/PadFileService.php | 9 +++------ lib/Service/PadOpenService.php | 16 +++++----------- lib/Service/RestoreService.php | 3 ++- lib/Service/TrashSnapshotWriter.php | 3 +-- tests/phpunit/unit/PadFileServiceTest.php | 8 +------- 8 files changed, 17 insertions(+), 32 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 64e7cfcd..61cf11fd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -85,7 +85,7 @@ checked-in runtime assets in `js/`. - `deleted_at` - `created_at` - `updated_at` - - `replaced_pad_id` (migration `Version000004Date20260926120000`): the pad the row's pad replaced, when a restore or a recovery put a new one in its place (see "Which pad a file reaches"). Rows from before it remember no pad. Nextcloud runs the migration when the app's version rises; `tests/e2e/docker/sync-app.sh` runs it for a working tree synced into the test stack. + - `replaced_pad_id` (migration `Version000004Date20260926120000`): the pad the row's pad replaced, when a restore or a recovery put a new one in its place (see "Which pad a file reaches", also for rows from before it). Nextcloud runs the migration when the app's version rises; `tests/e2e/docker/sync-app.sh` runs it for a working tree synced into the test stack. - stores internal managed pads only; external `ext.*` rows from earlier development versions are removed by `Version000003Date20260512230000` - `.pad` file - Frontmatter: format, binding metadata, state, export metadata. diff --git a/lib/Migration/Version000004Date20260926120000.php b/lib/Migration/Version000004Date20260926120000.php index 7d85cf3b..635364a8 100644 --- a/lib/Migration/Version000004Date20260926120000.php +++ b/lib/Migration/Version000004Date20260926120000.php @@ -36,7 +36,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt return null; } $table->addColumn('replaced_pad_id', 'string', [ - 'length' => 255, + 'length' => BindingService::PAD_ID_MAX_LENGTH, 'notnull' => false, ]); return $schema; diff --git a/lib/Service/BindingService.php b/lib/Service/BindingService.php index b41b9309..96bdfc1a 100644 --- a/lib/Service/BindingService.php +++ b/lib/Service/BindingService.php @@ -30,8 +30,8 @@ class BindingService { * be gone. */ public const STATE_RESTORE_PENDING = 'restore_pending'; - /** What pad_id and replaced_pad_id hold at most; bytes, so it holds on every database. */ - private const PAD_ID_MAX_BYTES = 255; + /** The length of replaced_pad_id; a pad id is measured against it in bytes, so it fits on every database. */ + public const PAD_ID_MAX_LENGTH = 255; /** * Where trashes keep files, as file cache paths relative to their @@ -263,7 +263,7 @@ private function findWaitingByAge(string $state, string $ageColumn, int $minAgeS */ public function createBinding(int $fileId, string $padId, string $accessMode, ?string $replacedPadId = null): void { $this->assertAccessMode($accessMode); - if ($replacedPadId !== null && strlen($replacedPadId) > self::PAD_ID_MAX_BYTES) { + if ($replacedPadId !== null && strlen($replacedPadId) > self::PAD_ID_MAX_LENGTH) { $replacedPadId = null; } $now = $this->timeFactory->getTime(); diff --git a/lib/Service/PadFileService.php b/lib/Service/PadFileService.php index adf6b3a9..a538e41c 100644 --- a/lib/Service/PadFileService.php +++ b/lib/Service/PadFileService.php @@ -204,12 +204,9 @@ public function isExternalFrontmatter(array $frontmatter, string $padId): bool { /** * The document a restore writes: the file as namingPad() makes it for - * the pad provisioned to replace the old one, holding the snapshot. - * - * The new pad counts revisions from zero, so the old pad's count cannot - * carry over: the sync would take every revision below it as already - * held. The file records the new pad's count once the snapshot is in it, - * or -1 (not synced yet) when that is not known. + * the pad provisioned to replace the old one, with the snapshot as its + * body and $revision, the new pad's count once the snapshot is in it, + * or -1 when that is not known. * * The caller has already split the snapshot it is restoring, so both * halves arrive here rather than the body being taken apart a second time diff --git a/lib/Service/PadOpenService.php b/lib/Service/PadOpenService.php index 52072dcb..27927417 100644 --- a/lib/Service/PadOpenService.php +++ b/lib/Service/PadOpenService.php @@ -94,17 +94,11 @@ private function openNode(string $uid, string $displayName, File $node, string $ } /** - * The file named the pad its row replaced, and whoever opens it may - * write it: it is written to name the row's pad, once, over what this - * open read and nothing newer. Should that not work, the row's pad - * opens all the same, and the next open or sync tries again. - * - * The file is found again by its id, as the initialise finds it: one - * moved or deleted since the read is written where it is now, or not at - * all, never as a new file where it was. Compared and written without a - * lock, as there: a sync that writes between the two is written over - * with the older text and no revision, and the next sync writes it - * again; the pad holds the text either way. + * The rewrite of a file that named the pad its row replaced + * (docs/architecture.md, "Which pad a file reaches"). Found again by its + * id, so the old node never writes a new file where the file was; no + * lock between comparing and writing, as for the initialise, since what + * a sync in between loses the next sync writes again. */ private function followRow(string $uid, int $fileId, string $read, ParsedPadFile $bound): void { try { diff --git a/lib/Service/RestoreService.php b/lib/Service/RestoreService.php index 5c06a9e2..6416620d 100644 --- a/lib/Service/RestoreService.php +++ b/lib/Service/RestoreService.php @@ -177,7 +177,8 @@ private function settleWaitingBinding(File $file, Binding $binding, bool $mayRep // No revision to hold the pad to, so no decision either. return $this->deferRestore($fileId, $padId, $state, $readError); } - // As its row has it; an open hands the file over as it read it. + // Here rather than in readRestoredPad(): an open hands the file + // over as it read it. $pad = $this->boundPads->followingRow($pad, $binding); $timeout = $budget?->nextCallTimeout(); if ($budget !== null && $timeout === null) { diff --git a/lib/Service/TrashSnapshotWriter.php b/lib/Service/TrashSnapshotWriter.php index 74e1b5e1..6ed2ebc9 100644 --- a/lib/Service/TrashSnapshotWriter.php +++ b/lib/Service/TrashSnapshotWriter.php @@ -53,8 +53,7 @@ public function __construct( * snapshot into yet. A delete through WebDAV holds the file's lock while * the trash is decided, so a locked file is the ordinary case here. A * file that cannot be read for another reason is a miss of its own, - * reported as the file's trouble. The file comes as its row has it - * (BoundPadResolver::followingRow()). + * reported as the file's trouble. */ public function read(): ParsedPadFile|TrashSnapshotMiss { try { diff --git a/tests/phpunit/unit/PadFileServiceTest.php b/tests/phpunit/unit/PadFileServiceTest.php index d597c034..5a9f338e 100644 --- a/tests/phpunit/unit/PadFileServiceTest.php +++ b/tests/phpunit/unit/PadFileServiceTest.php @@ -660,13 +660,7 @@ public function testWithRestoredSnapshotWritesTheRestoreInvariant(): void { 'https://pad.example.test/p/new-pad', )); - // Active and undeleted are not the caller's to get wrong any more. - $this->assertSame(BindingService::STATE_ACTIVE, $restored->frontmatter['state']); - $this->assertNull($restored->frontmatter['deleted_at']); - $this->assertSame('new-pad', $restored->padId); - // The mode the new pad was made in, not the one the file had. - $this->assertSame(BindingService::ACCESS_PROTECTED, $restored->accessMode); - $this->assertSame('https://pad.example.test/p/new-pad', $restored->padUrl); + // The rest is namingPad()'s (testNamingPadMakesTheFileNameAnotherPad). // The old pad's revision 4 means nothing to the new one: without the // new pad's own count, the document has not been synced yet. $this->assertSame(-1, $restored->snapshotRev);