ENH PHP 8.1 compatibility

This commit is contained in:
Steve Boyd 2022-04-13 13:53:29 +12:00
parent 7195e2a833
commit 600a4b2cde
4 changed files with 16 additions and 16 deletions

View File

@ -68,13 +68,13 @@ class RateLimitFilter extends ContentFilter
// Add global identifier
if ($this->config()->get('lock_bypage')) {
$key .= '_' . md5($itemkey);
$key .= '_' . md5($itemkey ?? '');
}
// Add user-specific identifier
if ($this->config()->get('lock_byuserip') && Controller::has_curr()) {
$ip = Controller::curr()->getRequest()->getIP();
$key .= '_' . md5($ip);
$key .= '_' . md5($ip ?? '');
}
return $key;

View File

@ -119,7 +119,7 @@ class VersionFeedController extends Extension
if ($lastChange) {
// Cache the diffs to remove DOS possibility.
$key = 'allchanges'
. preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited'])
. preg_replace('#[^a-zA-Z0-9_]#', '', $lastChange['LastEdited'] ?? '')
. (Security::getCurrentUser() ? Security::getCurrentUser()->ID : 'public');
$changeList = $this->filterContent($key, function () use ($latestChanges) {
$changeList = new ArrayList();

View File

@ -86,7 +86,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$this->assertFalse(
(bool)$xml->channel->item,
'With Page\'s "PublicHistory" disabled, `allchanges` action should not have an item in the channel'
@ -99,7 +99,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$response = $this->get($page->RelativeLink('changes'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$this->assertTrue(
(bool)$xml->channel->item,
'With Page\'s "PublicHistory" enabled, `changes` action should have an item in the channel'
@ -107,7 +107,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$response = $this->get($page->RelativeLink('allchanges'));
$this->assertEquals(200, $response->getStatusCode());
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$this->assertTrue(
(bool)$xml->channel->item,
'With "PublicHistory" enabled, `allchanges` action should have an item in the channel'
@ -141,7 +141,7 @@ class VersionFeedFunctionalTest extends FunctionalTest
$page1->ID,
Versioned::get_versionnumber_by_stage(SiteTree::class, 'Live', $page1->ID, false)
]);
$key = RateLimitFilter::CACHE_PREFIX . '_' . md5($key);
$key = RateLimitFilter::CACHE_PREFIX . '_' . md5($key ?? '');
$this->cache->set($key, time() + 10);
$response = $this->get($page1->RelativeLink('changes'));
$this->assertEquals(429, $response->getStatusCode());
@ -171,19 +171,19 @@ class VersionFeedFunctionalTest extends FunctionalTest
$page2 = $this->createPageWithChanges(['Title' => 'Page2']);
$response = $this->get($page1->RelativeLink('changes'));
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$titles = array_map(function ($item) {
return (string)$item->title;
}, $xml->xpath('//item'));
}, $xml->xpath('//item') ?? []);
// TODO Unclear if this should contain the original version
$this->assertContains('Changed: Page1', $titles);
$this->assertNotContains('Changed: Page2', $titles);
$response = $this->get($page2->RelativeLink('changes'));
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$titles = array_map(function ($item) {
return (string)$item->title;
}, $xml->xpath('//item'));
}, $xml->xpath('//item') ?? []);
// TODO Unclear if this should contain the original version
$this->assertNotContains('Changed: Page1', $titles);
$this->assertContains('Changed: Page2', $titles);
@ -195,10 +195,10 @@ class VersionFeedFunctionalTest extends FunctionalTest
$page2 = $this->createPageWithChanges(['Title' => 'Page2']);
$response = $this->get($page1->RelativeLink('allchanges'));
$xml = simplexml_load_string($response->getBody());
$xml = simplexml_load_string($response->getBody() ?? '');
$titles = array_map(function ($item) {
return str_replace('Changed: ', '', (string) $item->title);
}, $xml->xpath('//item'));
}, $xml->xpath('//item') ?? []);
$this->assertContains('Page1', $titles);
$this->assertContains('Page2', $titles);
}

View File

@ -44,18 +44,18 @@ class VersionFeedTest extends SapphireTest
// Strip spaces from test output because they're not reliably maintained by the HTML Tidier
$cleanDiffOutput = function ($val) {
return str_replace(' ', '', strip_tags($val));
return str_replace(' ', '', strip_tags($val ?? ''));
};
$this->assertContains(
str_replace(' ', '', _t('RSSHistory.TITLECHANGED', 'Title has changed:') . 'My Changed Title'),
array_map($cleanDiffOutput, $page->getDiffList()->column('DiffTitle')),
array_map($cleanDiffOutput, $page->getDiffList()->column('DiffTitle') ?? []),
'Detects published title changes'
);
$this->assertNotContains(
str_replace(' ', '', _t('RSSHistory.TITLECHANGED', 'Title has changed:') . 'My Unpublished Changed Title'),
array_map($cleanDiffOutput, $page->getDiffList()->column('DiffTitle')),
array_map($cleanDiffOutput, $page->getDiffList()->column('DiffTitle') ?? []),
'Ignores unpublished title changes'
);
}