Skip to content

Commit

Permalink
MNT Fix broken unit test when subsites is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 5, 2023
1 parent 4e4cbcb commit ae496cb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,25 @@ public function testPreviewLink(string $class, string $elementIdentifier, ?strin
/** @var BaseElement $element */
$element = $this->objFromFixture($class, $elementIdentifier);
$previewLink = $element->PreviewLink();
// Remove SubsiteID from preview link in case subsites is installed
$rx = '/[?|&]SubsiteID=[0-9]+/';
if (preg_match($rx, $previewLink)) {
$previewLink = preg_replace($rx, '', $previewLink);
}

if ($link) {
$regex = '/^' . preg_quote($link, '/') . '[?&]' . preg_quote('ElementalPreview=', '/')
// Note that the preview link is suffixed with forward slash when subsites is installed
// because of BaseElementSubsites::updatePreviewLink() use of HTTP::setGetVar()
// which turns relative urls to absolute urls
$regex = '/^\/?' . preg_quote($link, '/') . '[?&]' . preg_quote('ElementalPreview=', '/')
.'\d*#' . $element->getAnchor() . '$/';
$this->assertMatchesRegularExpression($regex, $previewLink);
// Doesn't try to blindly append query string and anchor - but instead merges intelligently with
// whatever's already there
$this->assertSame(1, substr_count($previewLink, '?'));
$this->assertSame(1, substr_count($previewLink, '#'));
} else {
$this->assertSame($link, $previewLink);
$this->assertEmpty(rtrim($previewLink ?? '', '/'));
}
}
}

0 comments on commit ae496cb

Please sign in to comment.