From 600a4b2cde65120f8960166c059274727d646842 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 13:53:29 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- src/Filters/RateLimitFilter.php | 4 ++-- src/VersionFeedController.php | 2 +- tests/VersionFeedFunctionalTest.php | 20 ++++++++++---------- tests/VersionFeedTest.php | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Filters/RateLimitFilter.php b/src/Filters/RateLimitFilter.php index b1d97ff..02b3f56 100644 --- a/src/Filters/RateLimitFilter.php +++ b/src/Filters/RateLimitFilter.php @@ -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; diff --git a/src/VersionFeedController.php b/src/VersionFeedController.php index 42d785d..47e8b5d 100644 --- a/src/VersionFeedController.php +++ b/src/VersionFeedController.php @@ -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(); diff --git a/tests/VersionFeedFunctionalTest.php b/tests/VersionFeedFunctionalTest.php index c3ede53..a1143d0 100644 --- a/tests/VersionFeedFunctionalTest.php +++ b/tests/VersionFeedFunctionalTest.php @@ -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); } diff --git a/tests/VersionFeedTest.php b/tests/VersionFeedTest.php index b9e4fcc..eb98dcc 100644 --- a/tests/VersionFeedTest.php +++ b/tests/VersionFeedTest.php @@ -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' ); }