From 2f51f441c9b8ae5a75dfc25a80d8679f33cc62a1 Mon Sep 17 00:00:00 2001 From: xjm Date: Thu, 28 May 2020 03:52:41 -0500 Subject: [PATCH] Issue #3002820 by daffie, pavnish, dww, sokru, mmjvb: PHP Warning in template_preprocess_update_report(): Invalid argument supplied for foreach() --- .../tests/src/Kernel/UpdateReportTest.php | 57 +++++++++++++++++++ core/modules/update/update.report.inc | 2 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 core/modules/update/tests/src/Kernel/UpdateReportTest.php diff --git a/core/modules/update/tests/src/Kernel/UpdateReportTest.php b/core/modules/update/tests/src/Kernel/UpdateReportTest.php new file mode 100644 index 00000000000..c7719c14cc1 --- /dev/null +++ b/core/modules/update/tests/src/Kernel/UpdateReportTest.php @@ -0,0 +1,57 @@ +loadInclude('update', 'inc', 'update.report'); + + // The function should run without an exception being thrown when the value + // of $variables['data'] is not set or is not an array. + template_preprocess_update_report($variables); + + // Test that the key "no_updates_message" has been set. + $this->assertArrayHasKey('no_updates_message', $variables); + } + + /** + * Provides data for testTemplatePreprocessUpdateReport(). + * + * @return array + * Array of $variables for template_preprocess_update_report(). + */ + public function providerTemplatePreprocessUpdateReport() { + return [ + '$variables with data not set' => [ + [], + ], + '$variables with data as an interger' => [ + ['data' => 4], + ], + '$variables with data as a string' => [ + ['data' => 'I am a string'], + ], + ]; + } + +} diff --git a/core/modules/update/update.report.inc b/core/modules/update/update.report.inc index a60c0d86d37..847f3c28a03 100644 --- a/core/modules/update/update.report.inc +++ b/core/modules/update/update.report.inc @@ -20,7 +20,7 @@ use Drupal\update\UpdateManagerInterface; * - data: An array of data about each project's status. */ function template_preprocess_update_report(&$variables) { - $data = $variables['data']; + $data = isset($variables['data']) && is_array($variables['data']) ? $variables['data'] : []; $last = \Drupal::state()->get('update.last_check', 0);