Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT Add extension in finally block #316

Merged
Merged
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
57 changes: 30 additions & 27 deletions tests/php/Controllers/LinkFieldControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,38 +537,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
Loading