Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft CMS 5

## Unreleased

- Fixed a bug where entries could deadlock when saving their authors. ([#15768](https://github.com/craftcms/cms/issues/15768))

## 5.10.13.1 - 2026-08-04

- Fixed a bug where sanitized SVGs wouldn’t render. ([#19368](https://github.com/craftcms/cms/issues/19368))
Expand Down
2 changes: 1 addition & 1 deletion src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3147,7 +3147,7 @@ private function _saveAuthors(): void
$this->_oldAuthorIds = array_map(fn($id) => (int)$id, $oldAuthorIds);
}

Db::delete(Table::ENTRIES_AUTHORS, ['entryId' => $this->id]);
Db::deleteIfExists(Table::ENTRIES_AUTHORS, ['entryId' => $this->id]);

if (!empty($this->_authorIds)) {
$data = [];
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/services/EntriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
namespace crafttests\unit\services;

use Craft;
use craft\db\Command;
use craft\db\Query;
use craft\db\Table;
use craft\elements\Entry;
use craft\elements\User;
use craft\services\Entries;
use craft\test\TestCase;
use crafttests\fixtures\EntryFixture;
use crafttests\fixtures\UserFixture;
use UnitTester;
use yii\base\Event;

/**
* Unit tests for the Entries service.
Expand Down Expand Up @@ -86,6 +90,105 @@
self::assertSame($this->userB->id, $untouchedEntry->getAuthorId());
}

/**
* @throws \Throwable
*/
public function testSaveAuthorsAvoidsDeletingMissingRows(): void
{
$entry = new Entry([
'sectionId' => 1000,
'typeId' => 1000,
'title' => 'Save Authors Test',
'authorId' => $this->userA->id,
]);

$commands = $this->_captureAuthorWriteCommands(fn() => $this->tester->saveElement($entry));
$entryId = $entry->id;
self::assertNotNull($entryId);

self::assertSame(['INSERT'], $commands);
self::assertSame([
[$this->userA->id, 1],
], $this->_savedAuthors($entryId));

$entry->authorId = $this->userB->id;
$commands = $this->_captureAuthorWriteCommands(fn() => $this->tester->saveElement($entry));

self::assertSame(['DELETE', 'INSERT'], $commands);
self::assertSame([
[$this->userB->id, 1],
], $this->_savedAuthors($entryId));

$section = $entry->getSection();
self::assertNotNull($section);
$oldMinAuthors = $section->minAuthors;
$section->minAuthors = 0;
$authorlessEntry = new Entry([
'sectionId' => 1000,
'typeId' => 1000,
'title' => 'Save Without Authors Test',
'authorIds' => [],
]);

try {
$commands = $this->_captureAuthorWriteCommands(fn() => $this->tester->saveElement($authorlessEntry));
} finally {
$section->minAuthors = $oldMinAuthors;
}
$authorlessEntryId = $authorlessEntry->id;
self::assertNotNull($authorlessEntryId);

self::assertSame([], $commands);
self::assertSame([], $this->_savedAuthors($authorlessEntryId));
}

/**
* @param callable(): mixed $callback
* @return string[]
*/
private function _captureAuthorWriteCommands(callable $callback): array
{
$commands = [];
$handler = static function(Event $event) use (&$commands): void {
/** @var Command $command */
$command = $event->sender;
$sql = ltrim($command->getSql());
if (
str_contains($sql, 'entries_authors') &&
preg_match('/^(DELETE|INSERT)\b/i', $sql, $matches)
) {
$commands[] = strtoupper($matches[1]);
}
};

Event::on(Command::class, Command::EVENT_BEFORE_EXECUTE, $handler);

Check failure on line 164 in tests/unit/services/EntriesTest.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Access to undefined constant craft\db\Command::EVENT_BEFORE_EXECUTE.
try {
$callback();
} finally {
Event::off(Command::class, Command::EVENT_BEFORE_EXECUTE, $handler);

Check failure on line 168 in tests/unit/services/EntriesTest.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Access to undefined constant craft\db\Command::EVENT_BEFORE_EXECUTE.
}

return $commands;
}

/**
* @return int[][]
*/
private function _savedAuthors(int $entryId): array
{
$rows = (new Query())
->select(['authorId', 'sortOrder'])
->from(Table::ENTRIES_AUTHORS)
->where(['entryId' => $entryId])
->orderBy(['sortOrder' => SORT_ASC])
->all();

return array_map(
fn(array $row) => [(int)$row['authorId'], (int)$row['sortOrder']],
$rows,
);
}

/**
* @inheritdoc
*/
Expand Down
Loading