From 1b0b2154b4892429e8ecd380d93d80fb9dfb58ca Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 17:07:59 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- code/BatchActions/CMSBatchAction_Restore.php | 2 +- code/Controllers/CMSMain.php | 66 +++++++-------- code/Controllers/CMSPageAddController.php | 4 +- code/Controllers/CMSPagesController.php | 2 +- code/Controllers/CMSSiteTreeFilter.php | 4 +- code/Controllers/ContentController.php | 10 +-- code/Controllers/ModelAsController.php | 6 +- code/Controllers/OldPageRedirector.php | 10 +-- code/Controllers/RootURLController.php | 2 +- code/Controllers/SilverStripeNavigator.php | 2 +- .../SilverStripeNavigatorItem_ArchiveLink.php | 4 +- code/Forms/SiteTreeURLSegmentField.php | 4 +- code/Model/RedirectorPage.php | 4 +- code/Model/SiteTree.php | 82 +++++++++---------- code/Model/SiteTreeFolderExtension.php | 4 +- code/Model/SiteTreeLinkTracking.php | 4 +- code/Model/SiteTreeLinkTracking_Parser.php | 10 +-- code/Model/VirtualPage.php | 8 +- code/Reports/BrokenFilesReport.php | 2 +- code/Reports/BrokenLinksReport.php | 4 +- code/Search/SearchForm.php | 30 +++---- tests/behat/src/LoginContext.php | 2 +- tests/behat/src/ThemeContext.php | 26 +++--- tests/bootstrap/app.php | 4 +- tests/php/Controllers/CMSBatchActionsTest.php | 4 +- tests/php/Controllers/CMSMainTest.php | 14 ++-- .../CMSPageHistoryControllerTest.php | 4 +- .../php/Controllers/CMSSiteTreeFilterTest.php | 12 +-- tests/php/Controllers/CMSTreeTest.php | 12 +-- .../php/Controllers/ContentControllerTest.php | 8 +- .../Controllers/SilverStripeNavigatorTest.php | 8 +- tests/php/GraphQL/LinkablePluginTest.php | 4 +- tests/php/Model/SiteTreeBacklinksTest.php | 4 +- .../php/Model/SiteTreeHTMLEditorFieldTest.php | 4 +- tests/php/Model/SiteTreeLinkTrackingTest.php | 10 +-- tests/php/Model/SiteTreeTest.php | 10 +-- .../php/Model/VirtualPageTest_TestDBField.php | 2 +- tests/php/Reports/CmsReportsTest.php | 8 +- tests/php/Search/CMSMainSearchFormTest.php | 4 +- .../Tasks/MigrateSiteTreeLinkingTaskTest.php | 2 +- 40 files changed, 203 insertions(+), 203 deletions(-) diff --git a/code/BatchActions/CMSBatchAction_Restore.php b/code/BatchActions/CMSBatchAction_Restore.php index 276b4a83..340d9db0 100644 --- a/code/BatchActions/CMSBatchAction_Restore.php +++ b/code/BatchActions/CMSBatchAction_Restore.php @@ -57,6 +57,6 @@ class CMSBatchAction_Restore extends CMSBatchAction // Get pages that exist in stage and remove them from the restore-able set $stageIDs = Versioned::get_by_stage($this->managedClass, Versioned::DRAFT)->column('ID'); - return array_values(array_diff($ids, $stageIDs)); + return array_values(array_diff($ids ?? [], $stageIDs)); } } diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index 8b75e3a9..f88d01e0 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -184,7 +184,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr { // set reading lang if (SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { - Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages(SiteTree::class))); + Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages(SiteTree::class) ?? [])); } parent::init(); @@ -454,7 +454,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ]; $link = Controller::join_links( $link, - array_filter(array_values($params)) ? '?' . http_build_query($params) : null + array_filter(array_values($params ?? [])) ? '?' . http_build_query($params) : null ); $this->extend('updateLinkWithSearch', $link); return $link; @@ -470,7 +470,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr } if ($placeholders) { - $link .= (strpos($link, '?') === false ? "?$placeholders" : "&$placeholders"); + $link .= (strpos($link ?? '', '?') === false ? "?$placeholders" : "&$placeholders"); } return $link; @@ -627,7 +627,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Get status flag classes $flags = $node->getStatusFlags(); if ($flags) { - $statuses = array_keys($flags); + $statuses = array_keys($flags ?? []); foreach ($statuses as $s) { $classes .= ' status-' . $s; } @@ -642,7 +642,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $classes .= ' ' . $filterClasses; } - return trim($classes); + return trim($classes ?? ''); } /** @@ -664,8 +664,8 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ); // Trim off the outer tag - $html = preg_replace('/^[\s\t\r\n]*]*>/', '', $html); - $html = preg_replace('/<\/ul[^>]*>[\s\t\r\n]*$/', '', $html); + $html = preg_replace('/^[\s\t\r\n]*]*>/', '', $html ?? ''); + $html = preg_replace('/<\/ul[^>]*>[\s\t\r\n]*$/', '', $html ?? ''); return $html; } @@ -682,7 +682,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr public function updatetreenodes($request) { $data = []; - $ids = explode(',', $request->getVar('ids')); + $ids = explode(',', $request->getVar('ids') ?? ''); foreach ($ids as $id) { if ($id === "") { continue; // $id may be a blank string, which is invalid and should be skipped over @@ -824,7 +824,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $this->getResponse()->addHeader( 'X-Status', - rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.')) + rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.') ?? '') ); } @@ -852,7 +852,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $this->getResponse()->addHeader( 'X-Status', - rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.')) + rawurlencode(_t(__CLASS__.'.REORGANISATIONSUCCESSFUL', 'Reorganised the site tree successfully.') ?? '') ); } @@ -923,7 +923,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $searchParams = array_combine(array_map(function ($key) { return 'Search__' . $key; - }, array_keys($searchParams)), $searchParams); + }, array_keys($searchParams ?? [])), $searchParams ?? []); $schema = [ 'formSchemaUrl' => $schemaUrl, @@ -1082,7 +1082,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Add all ancestors $ancestors = $record->getAncestors(); - $ancestors = new ArrayList(array_reverse($ancestors->toArray())); + $ancestors = new ArrayList(array_reverse($ancestors->toArray() ?? [])); $ancestors->push($record); /** @var SiteTree $ancestor */ foreach ($ancestors as $ancestor) { @@ -1144,7 +1144,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Check if can be created at the root $needsPerm = $obj->config()->get('need_permission'); if (!$obj->config()->get('can_be_root') - || (!array_key_exists($class, $canCreate) || !$canCreate[$class]) + || (!array_key_exists($class, $canCreate ?? []) || !$canCreate[$class]) || ($needsPerm && !$this->can($needsPerm)) ) { $def['Root']['disallowedChildren'][] = $class; @@ -1226,7 +1226,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if ($id instanceof $treeClass) { return $id; } - if (substr($id, 0, 3) == 'new') { + if (substr($id ?? '', 0, 3) == 'new') { return $this->getNewItem($id); } if (!is_numeric($id)) { @@ -1459,18 +1459,18 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Count number of affected change set $affectedChangeSetCount = 0; - if (count($inChangeSetIDs) > 0) { + if (count($inChangeSetIDs ?? []) > 0) { $affectedChangeSetCount = ChangeSet::get() ->filter(['ID' => $inChangeSetIDs, 'State' => ChangeSet::STATE_OPEN]) ->count(); } $numCampaigns = ChangeSet::singleton()->i18n_pluralise($affectedChangeSetCount); - $numCampaigns = mb_strtolower($numCampaigns); + $numCampaigns = mb_strtolower($numCampaigns ?? ''); - if (count($descendants) > 0 && $affectedChangeSetCount > 0) { + if (count($descendants ?? []) > 0 && $affectedChangeSetCount > 0) { $archiveWarningMsg = _t('SilverStripe\\CMS\\Controllers\\CMSMain.ArchiveWarningWithChildrenAndCampaigns', 'Warning: This page and all of its child pages will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]); - } elseif (count($descendants) > 0) { + } elseif (count($descendants ?? []) > 0) { $archiveWarningMsg = $defaultMessage; } elseif ($affectedChangeSetCount > 0) { $archiveWarningMsg = _t('SilverStripe\\CMS\\Controllers\\CMSMain.ArchiveWarningWithCampaigns', 'Warning: This page will be unpublished and automatically removed from their associated {NumCampaigns} before being sent to the archive.\n\nAre you sure you want to proceed?', [ 'NumCampaigns' => $numCampaigns ]); @@ -1693,9 +1693,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ); $breadcrumbs = $item->Breadcrumbs(20, true, false, true, '/'); // Remove item's tile - $breadcrumbs = preg_replace('/[^\/]+$/', '', trim($breadcrumbs)); + $breadcrumbs = preg_replace('/[^\/]+$/', '', trim($breadcrumbs ?? '')); // Trim spaces around delimiters - $breadcrumbs = preg_replace('/\s?\/\s?/', '/', trim($breadcrumbs)); + $breadcrumbs = preg_replace('/\s?\/\s?/', '/', trim($breadcrumbs ?? '')); return $title . sprintf('

%s

', $breadcrumbs); } ]); @@ -1752,7 +1752,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Existing or new record? $id = $data['ID']; - if (substr($id, 0, 3) != 'new') { + if (substr($id ?? '', 0, 3) != 'new') { /** @var SiteTree $record */ $record = DataObject::get_by_id($className, $id); // Check edit permissions @@ -1810,7 +1810,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ); } - $this->getResponse()->addHeader('X-Status', rawurlencode($message)); + $this->getResponse()->addHeader('X-Status', rawurlencode($message ?? '')); return $this->getResponseNegotiator()->respond($this->getRequest()); } @@ -1825,9 +1825,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr public function getNewItem($id, $setID = true) { $parentClass = $this->config()->get('tree_class'); - list(, $className, $parentID) = array_pad(explode('-', $id), 3, null); + list(, $className, $parentID) = array_pad(explode('-', $id ?? ''), 3, null); - if (!is_a($className, $parentClass, true)) { + if (!is_a($className, $parentClass ?? '', true)) { $response = Security::permissionFailure($this); if (!$response) { $response = $this->getResponse(); @@ -1931,7 +1931,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr "Restored '{title}' successfully", 'Param {title} is a title', ['title' => $record->Title] - )) + ) ?? '') ); return $this->getResponseNegotiator()->respond($this->getRequest()); @@ -1967,7 +1967,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.REMOVEDPAGEFROMDRAFT', "Removed '{title}' from the draft site", ['title' => $record->Title] - )) + ) ?? '') ); // Even if the record has been deleted from stage and live, it can be viewed in "archive mode" @@ -2003,7 +2003,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.ARCHIVEDPAGE', "Archived page '{title}'", ['title' => $record->Title] - )) + ) ?? '') ); // Even if the record has been deleted from stage and live, it can be viewed in "archive mode" @@ -2038,7 +2038,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.REMOVEDPAGE', "Removed '{title}' from the published site", ['title' => $record->Title] - )) + ) ?? '') ); return $this->getResponseNegotiator()->respond($this->getRequest()); @@ -2091,7 +2091,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ); } - $this->getResponse()->addHeader('X-Status', rawurlencode($message)); + $this->getResponse()->addHeader('X-Status', rawurlencode($message ?? '')); // Can be used in different contexts: In normal page edit view, in which case the redirect won't have any effect. // Or in history view, in which case a revert causes the CMS to re-load the edit view. @@ -2242,7 +2242,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.RESTORED', "Restored '{title}' successfully", ['title' => $restoredPage->Title] - )) + ) ?? '') ); return $this->getResponseNegotiator()->respond($this->getRequest()); @@ -2280,7 +2280,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.DUPLICATED', "Duplicated '{title}' successfully", ['title' => $newPage->Title] - )) + ) ?? '') ); $url = $newPage->CMSEditLink(); $this->getResponse()->addHeader('X-ControllerURL', $url); @@ -2318,7 +2318,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr __CLASS__ . '.DUPLICATEDWITHCHILDREN', "Duplicated '{title}' and children successfully", ['title' => $newPage->Title] - )) + ) ?? '') ); $url = $newPage->CMSEditLink(); $this->getResponse()->addHeader('X-ControllerURL', $url); @@ -2370,7 +2370,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $this->extend('updateHintsCacheKey', $baseKey); - return md5($baseKey); + return md5($baseKey ?? ''); } /** diff --git a/code/Controllers/CMSPageAddController.php b/code/Controllers/CMSPageAddController.php index a246826d..d198d41f 100644 --- a/code/Controllers/CMSPageAddController.php +++ b/code/Controllers/CMSPageAddController.php @@ -112,7 +112,7 @@ class CMSPageAddController extends CMSPageEditController "PageType", DBField::create_field( 'HTMLFragment', - sprintf($numericLabelTmpl, 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type')) + sprintf($numericLabelTmpl ?? '', 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type')) ), $pageTypes, 'Page' @@ -121,7 +121,7 @@ class CMSPageAddController extends CMSPageEditController $parentModeField->setTitle(DBField::create_field( 'HTMLFragment', - sprintf($numericLabelTmpl, 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page')) + sprintf($numericLabelTmpl ?? '', 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page')) )); $parentField->setSearchFunction(function ($sourceObject, $labelField, $search) { diff --git a/code/Controllers/CMSPagesController.php b/code/Controllers/CMSPagesController.php index 4c9a9e6e..5fc962a9 100644 --- a/code/Controllers/CMSPagesController.php +++ b/code/Controllers/CMSPagesController.php @@ -55,7 +55,7 @@ class CMSPagesController extends CMSMain $params['ParentID'] = $page->ID; $item = new stdClass(); $item->Title = $page->Title; - $item->Link = Controller::join_links($this->Link(), '?' . http_build_query($params)); + $item->Link = Controller::join_links($this->Link(), '?' . http_build_query($params ?? [])); $items->push(new ArrayData($item)); } } diff --git a/code/Controllers/CMSSiteTreeFilter.php b/code/Controllers/CMSSiteTreeFilter.php index 92e73c2a..d73fe964 100644 --- a/code/Controllers/CMSSiteTreeFilter.php +++ b/code/Controllers/CMSSiteTreeFilter.php @@ -90,7 +90,7 @@ abstract class CMSSiteTreeFilter implements LeftAndMain_SearchFilter uasort($filterMap, function ($a, $b) { return ($a === CMSSiteTreeFilter_Search::title()) ? -1 - : strcasecmp($a, $b); + : strcasecmp($a ?? '', $b ?? ''); }); return $filterMap; @@ -164,7 +164,7 @@ abstract class CMSSiteTreeFilter implements LeftAndMain_SearchFilter while (!empty($parents)) { $q = Versioned::get_including_deleted(SiteTree::class) - ->byIDs(array_keys($parents)); + ->byIDs(array_keys($parents ?? [])); $list = $q->map('ID', 'ParentID'); $parents = []; foreach ($list as $id => $parentID) { diff --git a/code/Controllers/ContentController.php b/code/Controllers/ContentController.php index 19e9eada..3fcb51f9 100644 --- a/code/Controllers/ContentController.php +++ b/code/Controllers/ContentController.php @@ -156,7 +156,7 @@ class ContentController extends Controller $getVars = $_GET; unset($getVars['url']); if ($getVars) { - $url = "?" . http_build_query($getVars); + $url = "?" . http_build_query($getVars ?? []); } else { $url = ""; } @@ -494,7 +494,7 @@ HTML; // TODO Allow this to work when allow_url_fopen=0 if (isset($_SESSION['StatsID']) && $_SESSION['StatsID']) { $url = 'http://ss2stat.silverstripe.com/Installation/installed?ID=' . $_SESSION['StatsID']; - @file_get_contents($url); + @file_get_contents($url ?? ''); } global $project; @@ -532,11 +532,11 @@ HTML; $unsuccessful = new ArrayList(); foreach ($installfiles as $installfile) { $installfilepath = PUBLIC_PATH . '/' . $installfile; - if (file_exists($installfilepath)) { - @unlink($installfilepath); + if (file_exists($installfilepath ?? '')) { + @unlink($installfilepath ?? ''); } - if (file_exists($installfilepath)) { + if (file_exists($installfilepath ?? '')) { $unsuccessful->push(new ArrayData(['File' => $installfile])); } } diff --git a/code/Controllers/ModelAsController.php b/code/Controllers/ModelAsController.php index c266b45c..8f9233ca 100644 --- a/code/Controllers/ModelAsController.php +++ b/code/Controllers/ModelAsController.php @@ -41,8 +41,8 @@ class ModelAsController extends Controller implements NestedController { $controller = $sitetree->getControllerName(); - if ($action && class_exists($controller . '_' . ucfirst($action))) { - $controller = $controller . '_' . ucfirst($action); + if ($action && class_exists($controller . '_' . ucfirst($action ?? ''))) { + $controller = $controller . '_' . ucfirst($action ?? ''); } return Injector::inst()->create($controller, $sitetree); @@ -131,7 +131,7 @@ class ModelAsController extends Controller implements NestedController // url encode unless it's multibyte (already pre-encoded in the database) $filter = URLSegmentFilter::create(); if (!$filter->getAllowMultibyte()) { - $URLSegment = rawurlencode($URLSegment); + $URLSegment = rawurlencode($URLSegment ?? ''); } // Select child page diff --git a/code/Controllers/OldPageRedirector.php b/code/Controllers/OldPageRedirector.php index 52f8ad73..5e526e11 100644 --- a/code/Controllers/OldPageRedirector.php +++ b/code/Controllers/OldPageRedirector.php @@ -23,8 +23,8 @@ class OldPageRedirector extends Extension public function onBeforeHTTPError404($request) { // We need to get the URL ourselves because $request->allParams() only has a max of 4 params - $params = preg_split('|/+|', $request->getURL()); - $cleanURL = trim(Director::makeRelative($request->getURL(false)), '/'); + $params = preg_split('|/+|', $request->getURL() ?? ''); + $cleanURL = trim(Director::makeRelative($request->getURL(false)) ?? '', '/'); $getvars = $request->getVars(); unset($getvars['url']); @@ -33,7 +33,7 @@ class OldPageRedirector extends Extension if (!$page) { $page = self::find_old_page($params); } - $cleanPage = trim(Director::makeRelative($page), '/'); + $cleanPage = trim(Director::makeRelative($page) ?? '', '/'); if (!$cleanPage) { $cleanPage = Director::makeRelative(RootURLController::get_homepage_link()); } @@ -64,7 +64,7 @@ class OldPageRedirector extends Extension { $parent = is_numeric($parent) && $parent > 0 ? SiteTree::get()->byID($parent) : $parent; $params = (array)$params; - $URL = rawurlencode(array_shift($params)); + $URL = rawurlencode(array_shift($params) ?? ''); if (empty($URL)) { return false; } @@ -93,7 +93,7 @@ class OldPageRedirector extends Extension } if ($page && $page->canView()) { - if (count($params)) { + if (count($params ?? [])) { // We have to go deeper! $ret = self::find_old_page($params, $page, $redirect); if ($ret) { diff --git a/code/Controllers/RootURLController.php b/code/Controllers/RootURLController.php index f7641d34..e8522989 100644 --- a/code/Controllers/RootURLController.php +++ b/code/Controllers/RootURLController.php @@ -56,7 +56,7 @@ class RootURLController extends Controller implements Resettable */ public static function should_be_on_root(SiteTree $page) { - return (!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')); + return (!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true) ?? '', '/')); } /** diff --git a/code/Controllers/SilverStripeNavigator.php b/code/Controllers/SilverStripeNavigator.php index 2f5db31b..dce05372 100644 --- a/code/Controllers/SilverStripeNavigator.php +++ b/code/Controllers/SilverStripeNavigator.php @@ -67,7 +67,7 @@ class SilverStripeNavigator extends ViewableData ksort($items); // Drop the keys and let the ArrayList handle the numbering, so $First, $Last and others work properly. - return new ArrayList(array_values($items)); + return new ArrayList(array_values($items ?? [])); } /** diff --git a/code/Controllers/SilverStripeNavigatorItem_ArchiveLink.php b/code/Controllers/SilverStripeNavigatorItem_ArchiveLink.php index b573b183..00dab137 100644 --- a/code/Controllers/SilverStripeNavigatorItem_ArchiveLink.php +++ b/code/Controllers/SilverStripeNavigatorItem_ArchiveLink.php @@ -20,7 +20,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem $linkTitle = _t('SilverStripe\\CMS\\Controllers\\ContentController.ARCHIVEDSITE', 'Preview version'); $recordLink = Convert::raw2att(Controller::join_links( $this->record->AbsoluteLink(), - '?archiveDate=' . urlencode($this->record->LastEdited) + '?archiveDate=' . urlencode($this->record->LastEdited ?? '') )); return "$linkTitle"; } @@ -48,7 +48,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem { return Controller::join_links( $this->record->PreviewLink(), - '?archiveDate=' . urlencode($this->record->LastEdited) + '?archiveDate=' . urlencode($this->record->LastEdited ?? '') ); } diff --git a/code/Forms/SiteTreeURLSegmentField.php b/code/Forms/SiteTreeURLSegmentField.php index 2e3a44b0..c4930182 100644 --- a/code/Forms/SiteTreeURLSegmentField.php +++ b/code/Forms/SiteTreeURLSegmentField.php @@ -44,7 +44,7 @@ class SiteTreeURLSegmentField extends TextField public function Value() { - return rawurldecode($this->value); + return rawurldecode($this->value ?? ''); } public function getAttributes() @@ -82,7 +82,7 @@ class SiteTreeURLSegmentField extends TextField $page->URLSegment = $page->generateURLSegment($request->getVar('value')); $count = 2; while (!$page->validURLSegment()) { - $page->URLSegment = preg_replace('/-[0-9]+$/', '', $page->URLSegment) . '-' . $count; + $page->URLSegment = preg_replace('/-[0-9]+$/', '', $page->URLSegment ?? '') . '-' . $count; $count++; } diff --git a/code/Model/RedirectorPage.php b/code/Model/RedirectorPage.php index 95b0dec3..97572142 100644 --- a/code/Model/RedirectorPage.php +++ b/code/Model/RedirectorPage.php @@ -173,8 +173,8 @@ class RedirectorPage extends Page { parent::onBeforeWrite(); - if ($this->ExternalURL && substr($this->ExternalURL, 0, 2) !== '//') { - $urlParts = parse_url($this->ExternalURL); + if ($this->ExternalURL && substr($this->ExternalURL ?? '', 0, 2) !== '//') { + $urlParts = parse_url($this->ExternalURL ?? ''); if ($urlParts) { if (empty($urlParts['scheme'])) { // no scheme, assume http diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 458ec9c5..e44dc247 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -430,16 +430,16 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $urlSegmentExpr = sprintf('"%s"."URLSegment"', $tableName); $parentIDExpr = sprintf('"%s"."ParentID"', $tableName); - $link = trim(Director::makeRelative($link), '/'); + $link = trim(Director::makeRelative($link) ?? '', '/'); if (!$link) { $link = RootURLController::get_homepage_link(); } - $parts = preg_split('|/+|', $link); + $parts = preg_split('|/+|', $link ?? ''); // Grab the initial root level page to traverse down from. $URLSegment = array_shift($parts); - $conditions = [$urlSegmentExpr => rawurlencode($URLSegment)]; + $conditions = [$urlSegmentExpr => rawurlencode($URLSegment ?? '')]; if (self::config()->get('nested_urls')) { $conditions[] = [$parentIDExpr => 0]; } @@ -474,7 +474,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi } // Check if we have any more URL parts to parse. - if (!self::config()->get('nested_urls') || !count($parts)) { + if (!self::config()->get('nested_urls') || !count($parts ?? [])) { return $sitetree; } @@ -521,7 +521,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi { $classes = ClassInfo::getValidSubClasses(); - $baseClassIndex = array_search(self::class, $classes); + $baseClassIndex = array_search(self::class, $classes ?? []); if ($baseClassIndex !== false) { unset($classes[$baseClassIndex]); } @@ -542,10 +542,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // If any of the descendents don't want any of the elders to show up, cruelly render the elders surplus to // requirements if ($kill_ancestors) { - $kill_ancestors = array_unique($kill_ancestors); + $kill_ancestors = array_unique($kill_ancestors ?? []); foreach ($kill_ancestors as $mark) { // unset from $classes - $idx = array_search($mark, $classes, true); + $idx = array_search($mark, $classes ?? [], true); if ($idx !== false) { unset($classes[$idx]); } @@ -733,7 +733,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi */ public function ElementName() { - return str_replace('/', '-', trim($this->RelativeLink(true), '/')); + return str_replace('/', '-', trim($this->RelativeLink(true) ?? '', '/')); } /** @@ -762,7 +762,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi public function isSection() { return $this->isCurrent() || ( - Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column()) + Director::get_current_page() instanceof SiteTree && in_array($this->ID, Director::get_current_page()->getAncestors()->column() ?? []) ); } @@ -931,7 +931,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi while ($page && $page->exists() - && (!$maxDepth || count($pages) < $maxDepth) + && (!$maxDepth || count($pages ?? []) < $maxDepth) && (!$stopAtPageType || $page->ClassName != $stopAtPageType) ) { if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) { @@ -941,7 +941,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $page = $page->Parent(); } - return new ArrayList(array_reverse($pages)); + return new ArrayList(array_reverse($pages ?? [])); } @@ -1013,7 +1013,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $item = $item->getParent(); $level--; } - return implode($separator, array_reverse($parts)); + return implode($separator ?? '', array_reverse($parts ?? [])); } /** @@ -1043,8 +1043,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi return true; } - if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { - $method = 'can' . ucfirst($perm); + if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm ?? ''))) { + $method = 'can' . ucfirst($perm ?? ''); return $this->$method($member); } @@ -1266,7 +1266,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // Block children not allowed for this parent type $parent = isset($context['Parent']) ? $context['Parent'] : null; $strictParentInstance = ($parent && $parent instanceof SiteTree); - if ($strictParentInstance && !in_array(static::class, $parent->allowedChildren())) { + if ($strictParentInstance && !in_array(static::class, $parent->allowedChildren() ?? [])) { return false; } @@ -1341,7 +1341,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi public function getSiteConfig() { $configs = $this->invokeWithExtensions('alternateSiteConfig'); - foreach (array_filter($configs) as $config) { + foreach (array_filter($configs ?? []) as $config) { return $config; } @@ -1472,7 +1472,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi */ private function getGenerator(): string { - $generator = trim(Config::inst()->get(self::class, 'meta_generator')); + $generator = trim(Config::inst()->get(self::class, 'meta_generator') ?? ''); if ($generator === '') { return ''; } @@ -1480,7 +1480,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $version = $this->getVersionProvider()->getModuleVersion('silverstripe/framework'); // Only include stable version numbers so as not to clutter any aggregate reports // with non-standard versions e.g. forks - if (preg_match('#^([0-9]+\.[0-9]+)\.[0-9]+$#', $version, $m)) { + if (preg_match('#^([0-9]+\.[0-9]+)\.[0-9]+$#', $version ?? '', $m)) { $generator .= ' ' . $m[1]; } } @@ -1517,7 +1517,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi { $tags = []; $tagsArray = $this->MetaComponents(); - if (!$includeTitle || strtolower($includeTitle) == 'false') { + if (!$includeTitle || strtolower($includeTitle ?? '') == 'false') { unset($tagsArray['title']); } @@ -1652,19 +1652,19 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // Ensure that this object has a non-conflicting URLSegment value. $count = 2; while (!$this->validURLSegment()) { - $this->URLSegment = preg_replace('/-[0-9]+$/', '', $this->URLSegment) . '-' . $count; + $this->URLSegment = preg_replace('/-[0-9]+$/', '', $this->URLSegment ?? '') . '-' . $count; $count++; } // Check to see if we've only altered fields that shouldn't affect versioning $fieldsIgnoredByVersioning = ['HasBrokenLink', 'Status', 'HasBrokenFile', 'ToDo', 'VersionID', 'SaveCount']; - $changedFields = array_keys($this->getChangedFields(true, 2)); + $changedFields = array_keys($this->getChangedFields(true, 2) ?? []); // This more rigorous check is inline with the test that write() does to decide whether or not to write to the // DB. We use that to avoid cluttering the system with a migrateVersion() call that doesn't get used - $oneChangedFields = array_keys($this->getChangedFields(true, 1)); + $oneChangedFields = array_keys($this->getChangedFields(true, 1) ?? []); - if ($oneChangedFields && !array_diff($changedFields, $fieldsIgnoredByVersioning)) { + if ($oneChangedFields && !array_diff($changedFields ?? [], $fieldsIgnoredByVersioning)) { $this->setNextWriteWithoutVersion(true); } @@ -1755,7 +1755,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $subject = ($this instanceof VirtualPage && $this->CopyContentFromID) ? $this->CopyContentFrom() : $this; - if (!in_array($subject->ClassName, $allowed)) { + if (!in_array($subject->ClassName, $allowed ?? [])) { $result->addError( _t( 'SilverStripe\\CMS\\Model\\SiteTree.PageTypeNotAllowed', @@ -1804,7 +1804,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi return false; } } - } elseif (in_array(strtolower($this->URLSegment), $this->getExcludedURLSegments())) { + } elseif (in_array(strtolower($this->URLSegment ?? ''), $this->getExcludedURLSegments() ?? [])) { // Guard against url segments for the base page // Default to '-2', onBeforeWrite takes care of further possible clashes return false; @@ -2276,7 +2276,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $viewerGroupsField->setDescription(_t( 'SilverStripe\\CMS\\Model\\SiteTree.VIEWER_GROUPS_FIELD_DESC', 'Groups with global view permissions: {groupList}', - ['groupList' => implode(', ', array_values($viewAllGroupsMap))] + ['groupList' => implode(', ', array_values($viewAllGroupsMap ?? []))] )); } @@ -2284,7 +2284,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $editorGroupsField->setDescription(_t( 'SilverStripe\\CMS\\Model\\SiteTree.EDITOR_GROUPS_FIELD_DESC', 'Groups with global edit permissions: {groupList}', - ['groupList' => implode(', ', array_values($editAllGroupsMap))] + ['groupList' => implode(', ', array_values($editAllGroupsMap ?? []))] )); } @@ -2622,7 +2622,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi return false; } - return stripos($this->ID, 'new') === 0; + return stripos($this->ID ?? '', 'new') === 0; } /** @@ -2673,9 +2673,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi if ($currentClass) { $currentPageTypeName = $result[$currentClass]; unset($result[$currentClass]); - $result = array_reverse($result); + $result = array_reverse($result ?? []); $result[$currentClass] = $currentPageTypeName; - $result = array_reverse($result); + $result = array_reverse($result ?? []); } return $result; @@ -2699,7 +2699,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $candidates = Config::inst()->get($class, 'allowed_children', Config::UNINHERITED); break; } - $class = get_parent_class($class); + $class = get_parent_class($class ?? ''); } if (!$candidates || $candidates === 'none' || $candidates === 'SiteTree_root') { return []; @@ -2710,8 +2710,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi foreach ((array)$candidates as $candidate) { // If a classname is prefixed by "*", such as "*Page", then only that class is allowed - no subclasses. // Otherwise, the class and all its subclasses are allowed. - if (substr($candidate, 0, 1) == '*') { - $allowedChildren[] = substr($candidate, 1); + if (substr($candidate ?? '', 0, 1) == '*') { + $allowedChildren[] = substr($candidate ?? '', 1); } elseif (($candidate !== 'SiteTree_root') && ($subclasses = ClassInfo::subclassesFor($candidate)) ) { @@ -2802,7 +2802,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $default = $this->config()->get('default_child'); $allowed = $this->allowedChildren(); if ($allowed) { - if (!$default || !in_array($default, $allowed)) { + if (!$default || !in_array($default, $allowed ?? [])) { $default = reset($allowed); } return $default; @@ -2929,7 +2929,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi Convert::raw2htmlid(static::class), $this->isHomePage() ? ' homepage' : '', Convert::raw2att(json_encode($children)), - Convert::raw2xml(str_replace(["\n","\r"], "", $this->MenuTitle)) + Convert::raw2xml(str_replace(["\n","\r"], "", $this->MenuTitle ?? '')) ); foreach ($flags as $class => $data) { if (is_string($data)) { @@ -3143,7 +3143,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi if (!$icon) { return null; } - if (strpos($icon, 'data:image/') !== false) { + if (strpos($icon ?? '', 'data:image/') !== false) { return $icon; } @@ -3284,15 +3284,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // Build from rules foreach (Director::config()->get('rules') as $pattern => $rule) { - $route = explode('/', $pattern); - if (!empty($route) && strpos($route[0], '$') === false) { - $excludes[] = strtolower($route[0]); + $route = explode('/', $pattern ?? ''); + if (!empty($route) && strpos($route[0] ?? '', '$') === false) { + $excludes[] = strtolower($route[0] ?? ''); } } // Build from base folders foreach (glob(Director::publicFolder() . '/*', GLOB_ONLYDIR) as $folder) { - $excludes[] = strtolower(basename($folder)); + $excludes[] = strtolower(basename($folder ?? '')); } $this->extend('updateExcludedURLSegments', $excludes); @@ -3306,7 +3306,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi { $parseSuccess = preg_match_all( "/\\s+(name|id)\\s*=\\s*([\"'])([^\\2\\s>]*?)\\2|\\s+(name|id)\\s*=\\s*([^\"']+)[\\s +>]/im", - $this->Content, + $this->Content ?? '', $matches ); diff --git a/code/Model/SiteTreeFolderExtension.php b/code/Model/SiteTreeFolderExtension.php index 05d827ee..164b6660 100644 --- a/code/Model/SiteTreeFolderExtension.php +++ b/code/Model/SiteTreeFolderExtension.php @@ -41,7 +41,7 @@ class SiteTreeFolderExtension extends DataExtension // Get all classes that aren't folder $fileClasses = array_diff_key( - ClassInfo::subclassesFor(File::class), + ClassInfo::subclassesFor(File::class) ?? [], ClassInfo::subclassesFor(Folder::class) ); @@ -58,7 +58,7 @@ class SiteTreeFolderExtension extends DataExtension $where = []; $columns = []; foreach ($hasOnes as $relName => $joinClass) { - if (in_array($joinClass, $fileClasses)) { + if (in_array($joinClass, $fileClasses ?? [])) { $column = $relName . 'ID'; $columns[] = $column; $quotedColumn = $schema->sqlColumnForField($className, $column); diff --git a/code/Model/SiteTreeLinkTracking.php b/code/Model/SiteTreeLinkTracking.php index c3c2111d..c9b53826 100644 --- a/code/Model/SiteTreeLinkTracking.php +++ b/code/Model/SiteTreeLinkTracking.php @@ -190,13 +190,13 @@ class SiteTreeLinkTracking extends DataExtension protected function toggleElementClass(DOMElement $domReference, $class, $toggle) { // Get all existing classes. - $classes = array_filter(explode(' ', trim($domReference->getAttribute('class')))); + $classes = array_filter(explode(' ', trim($domReference->getAttribute('class') ?? ''))); // Add or remove the broken class from the link, depending on the link status. if ($toggle) { $classes = array_unique(array_merge($classes, [$class])); } else { - $classes = array_diff($classes, [$class]); + $classes = array_diff($classes ?? [], [$class]); } if (!empty($classes)) { diff --git a/code/Model/SiteTreeLinkTracking_Parser.php b/code/Model/SiteTreeLinkTracking_Parser.php index 16ac823b..857c6c12 100644 --- a/code/Model/SiteTreeLinkTracking_Parser.php +++ b/code/Model/SiteTreeLinkTracking_Parser.php @@ -59,7 +59,7 @@ class SiteTreeLinkTracking_Parser // Link to a page on this site. $matches = []; - if (preg_match('/\[sitetree_link(?:\s*|%20|,)?id=(?[0-9]+)\](#(?.*))?/i', $href, $matches)) { + if (preg_match('/\[sitetree_link(?:\s*|%20|,)?id=(?[0-9]+)\](#(?.*))?/i', $href ?? '', $matches)) { // Check if page link is broken /** @var SiteTree $page */ $page = DataObject::get_by_id(SiteTree::class, $matches['id']); @@ -68,7 +68,7 @@ class SiteTreeLinkTracking_Parser $broken = true; } elseif (!empty($matches['anchor'])) { // Ensure anchor isn't broken on target page - $broken = !in_array($matches['anchor'], $page->getAnchorsOnPage()); + $broken = !in_array($matches['anchor'], $page->getAnchorsOnPage() ?? []); } else { $broken = false; } @@ -85,14 +85,14 @@ class SiteTreeLinkTracking_Parser } // Local anchor. - if (preg_match('/^#(.*)/i', $href, $matches)) { - $anchor = preg_quote($matches[1], '#'); + if (preg_match('/^#(.*)/i', $href ?? '', $matches)) { + $anchor = preg_quote($matches[1] ?? '', '#'); $results[] = [ 'Type' => 'localanchor', 'Target' => null, 'Anchor' => $matches[1], 'DOMReference' => $link, - 'Broken' => !preg_match("#(name|id)=\"{$anchor}\"#", $htmlValue->getContent()) + 'Broken' => !preg_match("#(name|id)=\"{$anchor}\"#", $htmlValue->getContent() ?? '') ]; continue; diff --git a/code/Model/VirtualPage.php b/code/Model/VirtualPage.php index 2219225b..dd4187cc 100644 --- a/code/Model/VirtualPage.php +++ b/code/Model/VirtualPage.php @@ -96,9 +96,9 @@ class VirtualPage extends Page } // Diff db with non-virtual fields - $fields = array_keys(static::getSchema()->fieldSpecs($record)); + $fields = array_keys(static::getSchema()->fieldSpecs($record) ?? []); $nonVirtualFields = $this->getNonVirtualisedFields(); - return array_diff($fields, $nonVirtualFields); + return array_diff($fields ?? [], $nonVirtualFields); } /** @@ -448,7 +448,7 @@ class VirtualPage extends Page { // Don't defer if field is non-virtualised $ignore = $this->getNonVirtualisedFields(); - if (in_array($field, $ignore)) { + if (in_array($field, $ignore ?? [])) { return false; } @@ -474,7 +474,7 @@ class VirtualPage extends Page if (parent::hasMethod($method)) { return parent::__call($method, $args); } else { - return call_user_func_array([$this->CopyContentFrom(), $method], $args); + return call_user_func_array([$this->CopyContentFrom(), $method], $args ?? []); } } diff --git a/code/Reports/BrokenFilesReport.php b/code/Reports/BrokenFilesReport.php index 3b1bfcc7..609428c3 100644 --- a/code/Reports/BrokenFilesReport.php +++ b/code/Reports/BrokenFilesReport.php @@ -29,7 +29,7 @@ class BrokenFilesReport extends Report { // Get class names for page types that are not virtual pages or redirector pages $classes = array_diff( - ClassInfo::subclassesFor(SiteTree::class), + ClassInfo::subclassesFor(SiteTree::class) ?? [], ClassInfo::subclassesFor(VirtualPage::class), ClassInfo::subclassesFor(RedirectorPage::class) ); diff --git a/code/Reports/BrokenLinksReport.php b/code/Reports/BrokenLinksReport.php index 796df874..4490658a 100644 --- a/code/Reports/BrokenLinksReport.php +++ b/code/Reports/BrokenLinksReport.php @@ -33,7 +33,7 @@ class BrokenLinksReport extends Report $join = ''; $sortBrokenReason = false; if ($sort) { - $parts = explode(' ', $sort); + $parts = explode(' ', $sort ?? ''); $field = $parts[0]; $direction = $parts[1]; @@ -93,7 +93,7 @@ class BrokenLinksReport extends Report } if ($reason) { - if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes)) { + if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes ?? [])) { continue; } $record->BrokenReason = $reason; diff --git a/code/Search/SearchForm.php b/code/Search/SearchForm.php index 12f81534..7bb6654e 100644 --- a/code/Search/SearchForm.php +++ b/code/Search/SearchForm.php @@ -98,14 +98,14 @@ class SearchForm extends Form public function classesToSearch($classes) { $supportedClasses = [SiteTree::class, File::class]; - $illegalClasses = array_diff($classes, $supportedClasses); + $illegalClasses = array_diff($classes ?? [], $supportedClasses); if ($illegalClasses) { throw new BadMethodCallException( "SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) . "'. At this stage, only File and SiteTree are allowed" ); } - $legalClasses = array_intersect($classes, $supportedClasses); + $legalClasses = array_intersect($classes ?? [], $supportedClasses); $this->classesToSearch = $legalClasses; } @@ -155,10 +155,10 @@ class SearchForm extends Form return ' -' . $matches[3]; }; - $keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); - $keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); - $keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); - $keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); + $keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords ?? ''); + $keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords ?? ''); + $keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords ?? ''); + $keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords ?? ''); $keywords = $this->addStarsToKeywords($keywords); @@ -166,10 +166,10 @@ class SearchForm extends Form $start = max(0, (int)$request->requestVar('start')); $booleanSearch = - strpos($keywords, '"') !== false || - strpos($keywords, '+') !== false || - strpos($keywords, '-') !== false || - strpos($keywords, '*') !== false; + strpos($keywords ?? '', '"') !== false || + strpos($keywords ?? '', '+') !== false || + strpos($keywords ?? '', '-') !== false || + strpos($keywords ?? '', '*') !== false; $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch); // filter by permission @@ -197,19 +197,19 @@ class SearchForm extends Form protected function addStarsToKeywords($keywords) { - if (!trim($keywords)) { + if (!trim($keywords ?? '')) { return ""; } // Add * to each keyword - $splitWords = preg_split("/ +/", trim($keywords)); + $splitWords = preg_split("/ +/", trim($keywords ?? '')); $newWords = []; - for ($i = 0; $i < count($splitWords); $i++) { + for ($i = 0; $i < count($splitWords ?? []); $i++) { $word = $splitWords[$i]; if ($word[0] == '"') { - while (++$i < count($splitWords)) { + while (++$i < count($splitWords ?? [])) { $subword = $splitWords[$i]; $word .= ' ' . $subword; - if (substr($subword, -1) == '"') { + if (substr($subword ?? '', -1) == '"') { break; } } diff --git a/tests/behat/src/LoginContext.php b/tests/behat/src/LoginContext.php index 93d982a8..44fb3c04 100644 --- a/tests/behat/src/LoginContext.php +++ b/tests/behat/src/LoginContext.php @@ -25,7 +25,7 @@ class LoginContext extends BehatLoginContext $email = "{$permCode}@example.org"; $password = 'Password!456'; $member = $this->generateMemberWithPermission($email, $password, $permCode); - $canEdit = strstr($negative, 'not') ? false : true; + $canEdit = strstr($negative ?? '', 'not') ? false : true; if ($canEdit) { Assert::assertTrue($page->canEdit($member), 'The member can edit this page'); diff --git a/tests/behat/src/ThemeContext.php b/tests/behat/src/ThemeContext.php index a7dc6a6d..ead1f826 100644 --- a/tests/behat/src/ThemeContext.php +++ b/tests/behat/src/ThemeContext.php @@ -23,7 +23,7 @@ class ThemeContext implements Context */ public function stepCreateTheme($theme) { - if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) { + if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme ?? '')) { throw new \InvalidArgumentException("Bad theme '$theme'"); } @@ -42,10 +42,10 @@ class ThemeContext implements Context */ public function stepCreateTemplate($template, $theme, $content) { - if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) { + if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme ?? '')) { throw new \InvalidArgumentException("Bad theme '$theme'"); } - if (!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template)) { + if (!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template ?? '')) { throw new \InvalidArgumentException("Bad template '$template'"); } @@ -56,16 +56,16 @@ class ThemeContext implements Context protected function requireFile($filename, $content) { // Already exists - if (file_exists($filename)) { + if (file_exists($filename ?? '')) { // If the content is different, remember old content for restoration - $origContent = file_get_contents($filename); + $origContent = file_get_contents($filename ?? ''); if ($origContent != $content) { - file_put_contents($filename, $content); + file_put_contents($filename ?? '', $content); $this->restoreFiles[$filename] = $origContent; } // Doesn't exist, mark it for deletion after test } else { - file_put_contents($filename, $content); + file_put_contents($filename ?? '', $content); $this->restoreFiles[$filename] = null; } } @@ -73,8 +73,8 @@ class ThemeContext implements Context protected function requireDir($dirname) { // Directory doesn't exist, create it and mark it for deletion - if (!file_exists($dirname)) { - mkdir($dirname); + if (!file_exists($dirname ?? '')) { + mkdir($dirname ?? ''); $this->restoreDirectories[] = $dirname; } } @@ -92,9 +92,9 @@ class ThemeContext implements Context if ($this->restoreFiles) { foreach ($this->restoreFiles as $file => $origContent) { if ($origContent === null) { - unlink($file); + unlink($file ?? ''); } else { - file_put_contents($file, $origContent); + file_put_contents($file ?? '', $origContent); } } @@ -104,9 +104,9 @@ class ThemeContext implements Context // Restore any created directories: that is, delete them if ($this->restoreDirectories) { // Flip the order so that nested direcotires are unlinked() first - $this->restoreDirectories = array_reverse($this->restoreDirectories); + $this->restoreDirectories = array_reverse($this->restoreDirectories ?? []); foreach ($this->restoreDirectories as $dir) { - rmdir($dir); + rmdir($dir ?? ''); } $this->restoreDirectories = []; diff --git a/tests/bootstrap/app.php b/tests/bootstrap/app.php index 41372cf8..e8412b0f 100644 --- a/tests/bootstrap/app.php +++ b/tests/bootstrap/app.php @@ -6,8 +6,8 @@ if (defined('BASE_PATH')) { } else { $projectPath = getcwd() . '/app'; } -if (!is_dir($projectPath)) { - mkdir($projectPath, 02775); +if (!is_dir($projectPath ?? '')) { + mkdir($projectPath ?? '', 02775); mkdir($projectPath . '/code', 02775); mkdir($projectPath . '/_config', 02775); copy(__DIR__ . '/fixtures/Page.php.fixture', $projectPath . '/code/Page.php'); diff --git a/tests/php/Controllers/CMSBatchActionsTest.php b/tests/php/Controllers/CMSBatchActionsTest.php index 441e18b2..9a079b80 100644 --- a/tests/php/Controllers/CMSBatchActionsTest.php +++ b/tests/php/Controllers/CMSBatchActionsTest.php @@ -142,7 +142,7 @@ class CMSBatchActionsTest extends SapphireTest $this->assertEquals($archivedID, $list->first()->ParentID); // Run restore - $result = json_decode($action->run($list), true); + $result = json_decode($action->run($list) ?? '', true); $this->assertEquals( [ $archivedxID => $archivedxID @@ -162,7 +162,7 @@ class CMSBatchActionsTest extends SapphireTest $this->assertEquals(0, $list->last()->ParentID); // archived (parent) // Run restore - $result = json_decode($action->run($list), true); + $result = json_decode($action->run($list) ?? '', true); $this->assertEquals( [ // Order of archived is opposite to order items are passed in, as diff --git a/tests/php/Controllers/CMSMainTest.php b/tests/php/Controllers/CMSMainTest.php index 6584f0b8..f61158f4 100644 --- a/tests/php/Controllers/CMSMainTest.php +++ b/tests/php/Controllers/CMSMainTest.php @@ -56,8 +56,8 @@ class CMSMainTest extends FunctionalTest $rawHints = singleton(CMSMain::class)->SiteTreeHints(); $this->assertNotNull($rawHints); - $rawHints = preg_replace('/^"(.*)"$/', '$1', Convert::xml2raw($rawHints)); - $hints = json_decode($rawHints, true); + $rawHints = preg_replace('/^"(.*)"$/', '$1', Convert::xml2raw($rawHints) ?? ''); + $hints = json_decode($rawHints ?? '', true); $this->assertArrayHasKey('Root', $hints); $this->assertArrayHasKey('Page', $hints); @@ -100,7 +100,7 @@ class CMSMainTest extends FunctionalTest // Check query $response = $this->get('admin/pages/childfilter?ParentID=' . $pageA->ID); - $children = json_decode($response->getBody()); + $children = json_decode($response->getBody() ?? ''); $this->assertFalse($response->isError()); // Page A can't have unrelated children @@ -137,7 +137,7 @@ class CMSMainTest extends FunctionalTest $actions = CMSBatchActionHandler::config()->batch_actions; if (isset($actions['publish'])) { $response = $this->get('admin/pages/batchactions/publish?ajax=1&csvIDs=' . implode(',', [$page1->ID, $page2->ID])); - $responseData = json_decode($response->getBody(), true); + $responseData = json_decode($response->getBody() ?? '', true); $this->assertArrayHasKey($page1->ID, $responseData['modified']); $this->assertArrayHasKey($page2->ID, $responseData['modified']); } @@ -335,7 +335,7 @@ class CMSMainTest extends FunctionalTest ] ); $this->assertFalse($response->isError()); - $ok = preg_match('/edit\/show\/(\d*)/', $response->getHeader('X-ControllerURL'), $matches); + $ok = preg_match('/edit\/show\/(\d*)/', $response->getHeader('X-ControllerURL') ?? '', $matches); $this->assertNotEmpty($ok); $newPageId = $matches[1]; @@ -360,7 +360,7 @@ class CMSMainTest extends FunctionalTest // Verify that the page was created and redirected to accurately $newerPage = SiteTree::get()->byID($newPageId)->AllChildren()->first(); $this->assertNotEmpty($newerPage); - $ok = preg_match('/edit\/show\/(\d*)/', $response->getHeader('X-ControllerURL'), $matches); + $ok = preg_match('/edit\/show\/(\d*)/', $response->getHeader('X-ControllerURL') ?? '', $matches); $this->assertNotEmpty($ok); $newerPageID = $matches[1]; $this->assertEquals($newerPage->ID, $newerPageID); @@ -398,7 +398,7 @@ class CMSMainTest extends FunctionalTest $crumbs = $parser->getBySelector('.breadcrumbs-wrapper .crumb'); $this->assertNotNull($crumbs); - $this->assertEquals(2, count($crumbs)); + $this->assertEquals(2, count($crumbs ?? [])); $this->assertEquals('Page 3', (string)$crumbs[0]); $this->assertEquals('Page 3.1', (string)$crumbs[1]); diff --git a/tests/php/Controllers/CMSPageHistoryControllerTest.php b/tests/php/Controllers/CMSPageHistoryControllerTest.php index 18ef6d7a..40d564b5 100755 --- a/tests/php/Controllers/CMSPageHistoryControllerTest.php +++ b/tests/php/Controllers/CMSPageHistoryControllerTest.php @@ -112,7 +112,7 @@ class CMSPageHistoryControllerTest extends FunctionalTest $form = $this->cssParser()->getBySelector('#Form_VersionsForm'); - $this->assertEquals(1, count($form)); + $this->assertEquals(1, count($form ?? [])); // check the page ID is present $hidden = $form[0]->xpath("fieldset/input[@type='hidden']"); @@ -122,7 +122,7 @@ class CMSPageHistoryControllerTest extends FunctionalTest // ensure that all the versions are present in the table and displayed $rows = $form[0]->xpath("fieldset/table/tbody/tr"); - $this->assertEquals(4, count($rows)); + $this->assertEquals(4, count($rows ?? [])); } public function testVersionsFormTableContainsInformation() diff --git a/tests/php/Controllers/CMSSiteTreeFilterTest.php b/tests/php/Controllers/CMSSiteTreeFilterTest.php index 9946894f..b76688f2 100644 --- a/tests/php/Controllers/CMSSiteTreeFilterTest.php +++ b/tests/php/Controllers/CMSSiteTreeFilterTest.php @@ -40,7 +40,7 @@ class CMSSiteTreeFilterTest extends SapphireTest $this->assertTrue($f->isPageIncluded($page1)); $this->assertFalse($f->isPageIncluded($page2)); - $this->assertEquals(1, count($results)); + $this->assertEquals(1, count($results ?? [])); $this->assertEquals( ['ID' => $page1->ID, 'ParentID' => 0], $results[0] @@ -68,7 +68,7 @@ class CMSSiteTreeFilterTest extends SapphireTest $this->assertTrue($f->isPageIncluded($parent)); $this->assertTrue($f->isPageIncluded($child)); - $this->assertEquals(1, count($results)); + $this->assertEquals(1, count($results ?? [])); $this->assertEquals( ['ID' => $child->ID, 'ParentID' => $parent->ID], $results[0] @@ -95,7 +95,7 @@ class CMSSiteTreeFilterTest extends SapphireTest $this->assertTrue($f->isPageIncluded($changedPage)); $this->assertFalse($f->isPageIncluded($unchangedPage)); - $this->assertEquals(1, count($results)); + $this->assertEquals(1, count($results ?? [])); $this->assertEquals( ['ID' => $changedPage->ID, 'ParentID' => 0], $results[0] @@ -104,7 +104,7 @@ class CMSSiteTreeFilterTest extends SapphireTest // Check that only changed pages are returned $f = new CMSSiteTreeFilter_ChangedPages(['Term' => 'No Matches']); $results = $f->pagesIncluded(); - $this->assertEquals(0, count($results)); + $this->assertEquals(0, count($results ?? [])); // If we roll back to an earlier version than what's on the published site, we should still show the changed $changedPage->Title = 'Changed 2'; @@ -115,7 +115,7 @@ class CMSSiteTreeFilterTest extends SapphireTest $f = new CMSSiteTreeFilter_ChangedPages(['Term' => 'Changed']); $results = $f->pagesIncluded(); - $this->assertEquals(1, count($results)); + $this->assertEquals(1, count($results ?? [])); $this->assertEquals(['ID' => $changedPage->ID, 'ParentID' => 0], $results[0]); } @@ -166,7 +166,7 @@ class CMSSiteTreeFilterTest extends SapphireTest { $draftPage = $this->objFromFixture('Page', 'page4'); // Grab the date - $date = substr($draftPage->LastEdited, 0, 10); + $date = substr($draftPage->LastEdited ?? '', 0, 10); // Filter with that date $filter = new CMSSiteTreeFilter_Search([ 'LastEditedFrom' => $date, diff --git a/tests/php/Controllers/CMSTreeTest.php b/tests/php/Controllers/CMSTreeTest.php index f4743c83..c17b75cd 100644 --- a/tests/php/Controllers/CMSTreeTest.php +++ b/tests/php/Controllers/CMSTreeTest.php @@ -98,7 +98,7 @@ class CMSTreeTest extends FunctionalTest $result = $this->get('admin/pages/edit/updatetreenodes?ids='.$page1->ID); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('application/json', $result->getHeader('Content-Type')); - $data = json_decode($result->getBody(), true); + $data = json_decode($result->getBody() ?? '', true); $pageData = $data[$page1->ID]; $this->assertEquals(0, $pageData['ParentID']); $this->assertEquals($page2->ID, $pageData['NextID']); @@ -108,7 +108,7 @@ class CMSTreeTest extends FunctionalTest $result = $this->get('admin/pages/edit/updatetreenodes?ids='.$page31->ID); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('application/json', $result->getHeader('Content-Type')); - $data = json_decode($result->getBody(), true); + $data = json_decode($result->getBody() ?? '', true); $pageData = $data[$page31->ID]; $this->assertEquals($page3->ID, $pageData['ParentID']); $this->assertEquals($page32->ID, $pageData['NextID']); @@ -118,14 +118,14 @@ class CMSTreeTest extends FunctionalTest $result = $this->get('admin/pages/edit/updatetreenodes?ids='.$page1->ID.','.$page2->ID); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('application/json', $result->getHeader('Content-Type')); - $data = json_decode($result->getBody(), true); - $this->assertEquals(2, count($data)); + $data = json_decode($result->getBody() ?? '', true); + $this->assertEquals(2, count($data ?? [])); // Invalid IDs $result = $this->get('admin/pages/edit/updatetreenodes?ids=-3'); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals('application/json', $result->getHeader('Content-Type')); - $data = json_decode($result->getBody(), true); - $this->assertEquals(0, count($data)); + $data = json_decode($result->getBody() ?? '', true); + $this->assertEquals(0, count($data ?? [])); } } diff --git a/tests/php/Controllers/ContentControllerTest.php b/tests/php/Controllers/ContentControllerTest.php index b094c9d9..e6acd9b5 100755 --- a/tests/php/Controllers/ContentControllerTest.php +++ b/tests/php/Controllers/ContentControllerTest.php @@ -172,7 +172,7 @@ class ContentControllerTest extends FunctionalTest $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $response = $this->get($page->RelativeLink()); - $this->assertEquals("ContentControllerTestPageWithoutController", trim($response->getBody())); + $this->assertEquals("ContentControllerTestPageWithoutController", trim($response->getBody() ?? '')); // // This should fall over to user Page.ss $page = new ContentControllerTestPage(); @@ -181,7 +181,7 @@ class ContentControllerTest extends FunctionalTest $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $response = $this->get($page->RelativeLink()); - $this->assertEquals("Page", trim($response->getBody())); + $this->assertEquals("Page", trim($response->getBody() ?? '')); // Test that the action template is rendered. @@ -191,12 +191,12 @@ class ContentControllerTest extends FunctionalTest $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $response = $this->get($page->RelativeLink("test")); - $this->assertEquals("ContentControllerTestPage_test", trim($response->getBody())); + $this->assertEquals("ContentControllerTestPage_test", trim($response->getBody() ?? '')); // Test that an action without a template will default to the index template, which is // to say the default Page.ss template $response = $this->get($page->RelativeLink("testwithouttemplate")); - $this->assertEquals("Page", trim($response->getBody())); + $this->assertEquals("Page", trim($response->getBody() ?? '')); // Test that an action with a template will render the both action template *and* the // correct parent template diff --git a/tests/php/Controllers/SilverStripeNavigatorTest.php b/tests/php/Controllers/SilverStripeNavigatorTest.php index bfb3f7ee..18fd5832 100644 --- a/tests/php/Controllers/SilverStripeNavigatorTest.php +++ b/tests/php/Controllers/SilverStripeNavigatorTest.php @@ -23,7 +23,7 @@ class SilverStripeNavigatorTest extends SapphireTest $navigator = new SilverStripeNavigator($page); $items = $navigator->getItems(); - $classes = array_map('get_class', $items->toArray()); + $classes = array_map('get_class', $items->toArray() ?? []); $this->assertContains( SilverStripeNavigatorItem_StageLink::class, $classes, @@ -48,18 +48,18 @@ class SilverStripeNavigatorTest extends SapphireTest // TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default $this->logInWithPermission('CMS_ACCESS_CMSMain'); $items = $navigator->getItems(); - $classes = array_map('get_class', $items->toArray()); + $classes = array_map('get_class', $items->toArray() ?? []); $this->assertNotContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes); $this->logInWithPermission('ADMIN'); $items = $navigator->getItems(); - $classes = array_map('get_class', $items->toArray()); + $classes = array_map('get_class', $items->toArray() ?? []); $this->assertContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes); // Unversioned record shouldn't be viewable in stage / live specific views $unversioned = new SilverStripeNavigatorTest\UnstagedRecord(); $navigator2 = new SilverStripeNavigator($unversioned); - $classes = array_map('get_class', $navigator2->getItems()->toArray()); + $classes = array_map('get_class', $navigator2->getItems()->toArray() ?? []); $this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes); $this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes); $this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes); diff --git a/tests/php/GraphQL/LinkablePluginTest.php b/tests/php/GraphQL/LinkablePluginTest.php index 6c832ec4..2687a393 100644 --- a/tests/php/GraphQL/LinkablePluginTest.php +++ b/tests/php/GraphQL/LinkablePluginTest.php @@ -78,8 +78,8 @@ class LinkablePluginTest extends SapphireTest $this->assertTrue($result->exists()); $this->assertCount(2, $result); $titles = $result->column('Title'); - $this->assertTrue(in_array('Test page', $titles)); - $this->assertTrue(in_array('Other test page', $titles)); + $this->assertTrue(in_array('Test page', $titles ?? [])); + $this->assertTrue(in_array('Other test page', $titles ?? [])); $result = LinkablePlugin::applyLinkFilter( 'test', diff --git a/tests/php/Model/SiteTreeBacklinksTest.php b/tests/php/Model/SiteTreeBacklinksTest.php index dc0a2851..927586db 100644 --- a/tests/php/Model/SiteTreeBacklinksTest.php +++ b/tests/php/Model/SiteTreeBacklinksTest.php @@ -37,8 +37,8 @@ class SiteTreeBacklinksTest extends SapphireTest $page3 = $this->objFromFixture('Page', 'page3'); $page3->Content = str_replace( '$page1.ID', - $this->objFromFixture('Page', 'page1')->ID, - $page3->Content + $this->objFromFixture('Page', 'page1')->ID ?? '', + $page3->Content ?? '' ); $page3->write(); } diff --git a/tests/php/Model/SiteTreeHTMLEditorFieldTest.php b/tests/php/Model/SiteTreeHTMLEditorFieldTest.php index a3daa927..d2324b45 100644 --- a/tests/php/Model/SiteTreeHTMLEditorFieldTest.php +++ b/tests/php/Model/SiteTreeHTMLEditorFieldTest.php @@ -26,8 +26,8 @@ class SiteTreeHTMLEditorFieldTest extends FunctionalTest $files = File::get()->exclude('ClassName', Folder::class); foreach ($files as $file) { $destPath = TestAssetStore::getLocalPath($file); - Filesystem::makeFolder(dirname($destPath)); - file_put_contents($destPath, str_repeat('x', 1000000)); + Filesystem::makeFolder(dirname($destPath ?? '')); + file_put_contents($destPath ?? '', str_repeat('x', 1000000)); } // Ensure all pages are published diff --git a/tests/php/Model/SiteTreeLinkTrackingTest.php b/tests/php/Model/SiteTreeLinkTrackingTest.php index 5d0c50c8..4247eee1 100644 --- a/tests/php/Model/SiteTreeLinkTrackingTest.php +++ b/tests/php/Model/SiteTreeLinkTrackingTest.php @@ -81,11 +81,11 @@ class SiteTreeLinkTrackingTest extends SapphireTest public function testHighlighter() { $content = $this->highlight('link'); - $this->assertEquals(substr_count($content, 'ss-broken'), 1, 'A ss-broken class is added to the broken link.'); - $this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.'); + $this->assertEquals(substr_count($content ?? '', 'ss-broken'), 1, 'A ss-broken class is added to the broken link.'); + $this->assertEquals(substr_count($content ?? '', 'existing-class'), 1, 'Existing class is not removed.'); $content = $this->highlight('link'); - $this->assertEquals(substr_count($content, 'ss-broken'), 1, 'ss-broken class is added to the broken link.'); + $this->assertEquals(substr_count($content ?? '', 'ss-broken'), 1, 'ss-broken class is added to the broken link.'); $otherPage = new Page(); $otherPage->Content = ''; @@ -94,7 +94,7 @@ class SiteTreeLinkTrackingTest extends SapphireTest $content = $this->highlight( "ID]\" class=\"existing-class ss-broken ss-broken\">link" ); - $this->assertEquals(substr_count($content, 'ss-broken'), 0, 'All ss-broken classes are removed from good link'); - $this->assertEquals(substr_count($content, 'existing-class'), 1, 'Existing class is not removed.'); + $this->assertEquals(substr_count($content ?? '', 'ss-broken'), 0, 'All ss-broken classes are removed from good link'); + $this->assertEquals(substr_count($content ?? '', 'existing-class'), 1, 'Existing class is not removed.'); } } diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index f801b7a5..0728ba4a 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -146,7 +146,7 @@ class SiteTreeTest extends SapphireTest public function testDisallowedChildURLGeneration($title, $urlSegment) { // Using the same dataprovider, strip out the -2 from the admin and dev segment - $urlSegment = str_replace('-2', '', $urlSegment); + $urlSegment = str_replace('-2', '', $urlSegment ?? ''); $page = Page::create(['Title' => $title, 'ParentID' => 1]); $id = $page->write(); $page = Page::get()->byID($id); @@ -821,9 +821,9 @@ class SiteTreeTest extends SapphireTest $diff = $page->compareVersions(1, 2); - $processedContent = trim($diff->Content); - $processedContent = preg_replace('/\s*\s*/', '>', $processedContent); + $processedContent = trim($diff->Content ?? ''); + $processedContent = preg_replace('/\s*\s*/', '>', $processedContent ?? ''); $this->assertEquals("This is a test", $processedContent); Diff::$html_cleaner_class = $oldCleanerClass; @@ -1485,7 +1485,7 @@ class SiteTreeTest extends SapphireTest // test the meta generator tag version can be configured off Config::modify()->set(SiteTree::class, 'show_meta_generator_version', false); $content = $expected['generator']['attributes']['content']; - $expected['generator']['attributes']['content'] = str_replace(' 4.50', '', $content); + $expected['generator']['attributes']['content'] = str_replace(' 4.50', '', $content ?? ''); $this->assertEquals($expected, $page->MetaComponents()); } diff --git a/tests/php/Model/VirtualPageTest_TestDBField.php b/tests/php/Model/VirtualPageTest_TestDBField.php index f9f60222..5554567f 100644 --- a/tests/php/Model/VirtualPageTest_TestDBField.php +++ b/tests/php/Model/VirtualPageTest_TestDBField.php @@ -9,6 +9,6 @@ class VirtualPageTest_TestDBField extends DBVarchar implements TestOnly { public function forTemplate() { - return strtoupper($this->XML()); + return strtoupper($this->XML() ?? ''); } } diff --git a/tests/php/Reports/CmsReportsTest.php b/tests/php/Reports/CmsReportsTest.php index 6132eb30..ffb8fdaa 100644 --- a/tests/php/Reports/CmsReportsTest.php +++ b/tests/php/Reports/CmsReportsTest.php @@ -52,12 +52,12 @@ class CmsReportsTest extends SapphireTest // ASSERT that the "draft" report is returning the correct results. $parameters = ['CheckSite' => 'Draft']; - $results = count($report->sourceRecords($parameters, null, null)) > 0; + $results = count($report->sourceRecords($parameters, null, null) ?? []) > 0; $isDraftBroken ? $this->assertTrue($results, "{$class} has NOT returned the correct DRAFT results, as NO pages were found.") : $this->assertFalse($results, "{$class} has NOT returned the correct DRAFT results, as pages were found."); // ASSERT that the "published" report is returning the correct results. $parameters = ['CheckSite' => 'Published', 'OnLive' => 1]; - $results = count($report->sourceRecords($parameters, null, null)) > 0; + $results = count($report->sourceRecords($parameters, null, null) ?? []) > 0; $isPublishedBroken ? $this->assertTrue($results, "{$class} has NOT returned the correct PUBLISHED results, as NO pages were found.") : $this->assertFalse($results, "{$class} has NOT returned the correct PUBLISHED results, as pages were found."); } @@ -124,7 +124,7 @@ class CmsReportsTest extends SapphireTest // Correct the "draft" broken link. - $page->Content = str_replace('987654321', $page->ID, $page->Content); + $page->Content = str_replace('987654321', $page->ID ?? '', $page->Content ?? ''); $page->writeToStage('Stage'); // ASSERT that the "draft" report has NOT detected the page having a broken link. @@ -191,7 +191,7 @@ class CmsReportsTest extends SapphireTest $file = File::create(); $file->Filename = 'name.pdf'; $file->write(); - $page->Content = str_replace('987654321', $file->ID, $page->Content); + $page->Content = str_replace('987654321', $file->ID ?? '', $page->Content ?? ''); $page->writeToStage('Stage'); // ASSERT that the "draft" report has NOT detected the page having a broken file. diff --git a/tests/php/Search/CMSMainSearchFormTest.php b/tests/php/Search/CMSMainSearchFormTest.php index a59ad26a..9835552a 100644 --- a/tests/php/Search/CMSMainSearchFormTest.php +++ b/tests/php/Search/CMSMainSearchFormTest.php @@ -25,7 +25,7 @@ class CMSMainSearchFormTest extends FunctionalTest ); $titles = $this->getPageTitles(); - $this->assertEquals(count($titles), 1); + $this->assertEquals(count($titles ?? []), 1); // For some reason the title gets split into two lines $this->assertStringContainsString('Page 1', $titles[0]); @@ -37,7 +37,7 @@ class CMSMainSearchFormTest extends FunctionalTest $links = $this->cssParser()->getBySelector('.col-getTreeTitle span.item'); if ($links) { foreach ($links as $link) { - $titles[] = preg_replace('/\n/', ' ', $link->asXML()); + $titles[] = preg_replace('/\n/', ' ', $link->asXML() ?? ''); } } return $titles; diff --git a/tests/php/Tasks/MigrateSiteTreeLinkingTaskTest.php b/tests/php/Tasks/MigrateSiteTreeLinkingTaskTest.php index 1181d268..12f92ccd 100644 --- a/tests/php/Tasks/MigrateSiteTreeLinkingTaskTest.php +++ b/tests/php/Tasks/MigrateSiteTreeLinkingTaskTest.php @@ -46,7 +46,7 @@ class MigrateSiteTreeLinkingTaskTest extends SapphireTest ]; foreach (DB::query('SELECT "ID", "Content" FROM "SiteTree"') as $row) { $id = (int)$row['ID']; - $content = str_replace(array_keys($replacements), array_values($replacements), $row['Content']); + $content = str_replace(array_keys($replacements ?? []), array_values($replacements ?? []), $row['Content'] ?? ''); DB::prepared_query('UPDATE "SiteTree" SET "Content" = ? WHERE "ID" = ?', [$content, $id]); } DataObject::reset();