Skip to content

Commit

Permalink
Merge branch '4' into 5
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Aug 15, 2024
2 parents a88066b + ed84556 commit 5904235
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\HiddenField;
use SilverStripe\LinkField\Services\LinkTypeService;
use SilverStripe\ORM\DataList;
Expand Down Expand Up @@ -133,7 +134,8 @@ public function linkDelete(): HTTPResponse
}
$link = $this->linkFromRequest();
if ($link->hasExtension(Versioned::class)) {
if (!$link->canArchive()) {
$canArchive = Deprecation::withNoReplacement(fn() => $link->canArchive());
if (!$canArchive) {
$this->jsonError(403);
}
$link->doArchive();
Expand Down
57 changes: 30 additions & 27 deletions tests/php/Controllers/LinkFieldControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,38 +538,41 @@ public function testLinkArchive(
int $expectedCode
): void {
TestPhoneLink::$fail = $fail;
if ($fail == 'can-delete') {
if ($fail === 'can-delete') {
// Remove the Versioned extension because there's logic in LinkFieldController
// to use either canArchive() or canDelete() based on the presence of the Versioned extension
// Note that you must remove the extension on the base class rather than a TestOnly subclass
Link::remove_extension(Versioned::class);
}
$owner = $this->getFixtureLinkOwner();
$ownerID = $owner->ID;
$ownerClass = urlencode($owner->ClassName);
$ownerRelation = 'Link';
$ownerLinkID = $owner->LinkID;
$id = $this->getID($idType);
$fixtureID = $this->getFixtureLink()->ID;
if ($id === -1) {
$url = "/admin/linkfield/delete?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation";
} else {
$url = "/admin/linkfield/delete/$id?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation";
}
$headers = [];
if ($fail !== 'csrf-token') {
$headers = array_merge($headers, $this->csrfTokenheader());
}
$response = $this->mainSession->sendRequest('DELETE', $url, [], $headers);
$this->assertSame('application/json', $response->getHeader('Content-type'));
$this->assertSame($expectedCode, $response->getStatusCode());
if ($expectedCode >= 400) {
$this->assertNotNull(TestPhoneLink::get()->byID($fixtureID));
} else {
$this->assertNull(TestPhoneLink::get()->byID($fixtureID));
}
if ($fail == 'can-delete') {
Link::add_extension(Versioned::class);
try {
$owner = $this->getFixtureLinkOwner();
$ownerID = $owner->ID;
$ownerClass = urlencode($owner->ClassName);
$ownerRelation = 'Link';
$ownerLinkID = $owner->LinkID;
$id = $this->getID($idType);
$fixtureID = $this->getFixtureLink()->ID;
if ($id === -1) {
$url = "/admin/linkfield/delete?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation";
} else {
$url = "/admin/linkfield/delete/$id?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation";
}
$headers = [];
if ($fail !== 'csrf-token') {
$headers = array_merge($headers, $this->csrfTokenheader());
}
$response = $this->mainSession->sendRequest('DELETE', $url, [], $headers);
$this->assertSame('application/json', $response->getHeader('Content-type'));
$this->assertSame($expectedCode, $response->getStatusCode());
if ($expectedCode >= 400) {
$this->assertNotNull(TestPhoneLink::get()->byID($fixtureID));
} else {
$this->assertNull(TestPhoneLink::get()->byID($fixtureID));
}
} finally {
if ($fail === 'can-delete') {
Link::add_extension(Versioned::class);
}
}
}

Expand Down

0 comments on commit 5904235

Please sign in to comment.