From 1d99aa104fb0700833bd5b4507934941afa19dfa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stephan=20Gro=C3=9Fberndt?= <stephan@grossberndt.de>
Date: Sat, 16 Dec 2017 15:57:00 +0100
Subject: [PATCH] [BUGFIX] Do not try to fetch additionalFields for invalid
 tasks

A task may be invalid when editing so there is no instance of
AbstractTask in $task. With this patch the SchedulerModuleController no
longer tries to add the additionalFields for that as the
AdditionalFieldProviders expect a valid task object when being called on
edit.

Additionally some cleanup in the PHPDoc of AdditionalFieldProviders is
made to make it clear the given task may be NULL on create.

Resolves: #79141
Releases: master, 8.7
Change-Id: Ia7cf8173e65cfdede89a776cff7615678e4ffb21
Reviewed-on: https://review.typo3.org/55122
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Benjamin Kluge <b.kluge@neusta.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Michael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../extbase/Classes/Scheduler/FieldProvider.php  | 16 +++++++++-------
 .../ValidatorTaskAdditionalFieldProvider.php     |  2 +-
 ...temStatusUpdateTaskNotificationEmailField.php |  9 +++++----
 .../Controller/SchedulerModuleController.php     | 11 +++++------
 ...SchedulableCommandAdditionalFieldProvider.php |  2 +-
 ...eStorageExtractionAdditionalFieldProvider.php | 12 ++++++------
 6 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/typo3/sysext/extbase/Classes/Scheduler/FieldProvider.php b/typo3/sysext/extbase/Classes/Scheduler/FieldProvider.php
index 9f83cebb0c54..f3a639f4a638 100644
--- a/typo3/sysext/extbase/Classes/Scheduler/FieldProvider.php
+++ b/typo3/sysext/extbase/Classes/Scheduler/FieldProvider.php
@@ -15,6 +15,8 @@ namespace TYPO3\CMS\Extbase\Scheduler;
  */
 
 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
+use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
+use TYPO3\CMS\Scheduler\Task\AbstractTask;
 
 /**
  * Field provider for Extbase CommandController Scheduler task
@@ -59,12 +61,12 @@ class FieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInter
      * Render additional information fields within the scheduler backend.
      *
      * @param array &$taskInfo Array information of task to return
-     * @param mixed $task \TYPO3\CMS\Scheduler\Task\AbstractTask or \TYPO3\CMS\Scheduler\Execution instance
-     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the calling object (BE module of the Scheduler)
+     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
+     * @param SchedulerModuleController $schedulerModule Reference to the calling object (BE module of the Scheduler)
      * @return array Additional fields
      * @see \TYPO3\CMS\Scheduler\AdditionalFieldProvider#getAdditionalFields($taskInfo, $task, $schedulerModule)
      */
-    public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)
+    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
     {
         $this->task = $task;
         if ($this->task !== null) {
@@ -86,10 +88,10 @@ class FieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInter
      * Validates additional selected fields
      *
      * @param array &$submittedData
-     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule
+     * @param SchedulerModuleController $schedulerModule
      * @return bool
      */
-    public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)
+    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
     {
         return true;
     }
@@ -98,10 +100,10 @@ class FieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInter
      * Saves additional field values
      *
      * @param array $submittedData
-     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
+     * @param AbstractTask $task
      * @return bool
      */
-    public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
+    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
     {
         $task->setCommandIdentifier($submittedData['task_extbase']['action']);
         $task->setArguments((array)$submittedData['task_extbase']['arguments']);
diff --git a/typo3/sysext/linkvalidator/Classes/Task/ValidatorTaskAdditionalFieldProvider.php b/typo3/sysext/linkvalidator/Classes/Task/ValidatorTaskAdditionalFieldProvider.php
index 929bf62c03ef..66ac3abe097a 100644
--- a/typo3/sysext/linkvalidator/Classes/Task/ValidatorTaskAdditionalFieldProvider.php
+++ b/typo3/sysext/linkvalidator/Classes/Task/ValidatorTaskAdditionalFieldProvider.php
@@ -38,7 +38,7 @@ class ValidatorTaskAdditionalFieldProvider implements AdditionalFieldProviderInt
      * Render additional information fields within the scheduler backend.
      *
      * @param array $taskInfo Array information of task to return
-     * @param ValidatorTask $task Task object
+     * @param AbstractTask|null $task The task object being edited. Null when adding a task!
      * @param SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
      * @return array Additional fields
      * @see AdditionalFieldProviderInterface->getAdditionalFields($taskInfo, $task, $schedulerModule)
diff --git a/typo3/sysext/reports/Classes/Task/SystemStatusUpdateTaskNotificationEmailField.php b/typo3/sysext/reports/Classes/Task/SystemStatusUpdateTaskNotificationEmailField.php
index f7ed23bc5a6c..8ec7d86f3324 100644
--- a/typo3/sysext/reports/Classes/Task/SystemStatusUpdateTaskNotificationEmailField.php
+++ b/typo3/sysext/reports/Classes/Task/SystemStatusUpdateTaskNotificationEmailField.php
@@ -13,6 +13,7 @@ namespace TYPO3\CMS\Reports\Task;
  *
  * The TYPO3 project - inspiring people to share!
  */
+
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -44,8 +45,8 @@ class SystemStatusUpdateTaskNotificationEmailField implements AdditionalFieldPro
      * Gets additional fields to render in the form to add/edit a task
      *
      * @param array $taskInfo Values of the fields from the add/edit task form
-     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task The task object being edited. Null when adding a task!
-     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
+     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
+     * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
      * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => ''))
      */
     public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
@@ -86,7 +87,7 @@ class SystemStatusUpdateTaskNotificationEmailField implements AdditionalFieldPro
      * Validates the additional fields' values
      *
      * @param array $submittedData An array containing the data submitted by the add/edit task form
-     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module
+     * @param SchedulerModuleController $schedulerModule Reference to the scheduler backend module
      * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
      */
     public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
@@ -110,7 +111,7 @@ class SystemStatusUpdateTaskNotificationEmailField implements AdditionalFieldPro
      * Takes care of saving the additional fields' values in the task's object
      *
      * @param array $submittedData An array containing the data submitted by the add/edit task form
-     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the scheduler backend module
+     * @param AbstractTask $task Reference to the scheduler backend module
      */
     public function saveAdditionalFields(array $submittedData, AbstractTask $task)
     {
diff --git a/typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php b/typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php
index 99dcf6061a1f..a008f3e01466 100644
--- a/typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php
+++ b/typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php
@@ -592,12 +592,11 @@ class SchedulerModuleController
                     }
                 }
             }
-        } else {
-            if (!empty($registeredClasses[$taskInfo['class']]['provider'])) {
-                $providerObject = GeneralUtility::makeInstance($registeredClasses[$taskInfo['class']]['provider']);
-                if ($providerObject instanceof AdditionalFieldProviderInterface) {
-                    $allAdditionalFields[$taskInfo['class']] = $providerObject->getAdditionalFields($taskInfo, $task, $this);
-                }
+        } elseif ($task !== null && !empty($registeredClasses[$taskInfo['class']]['provider'])) {
+            // only try to fetch additionalFields if the task is valid
+            $providerObject = GeneralUtility::makeInstance($registeredClasses[$taskInfo['class']]['provider']);
+            if ($providerObject instanceof AdditionalFieldProviderInterface) {
+                $allAdditionalFields[$taskInfo['class']] = $providerObject->getAdditionalFields($taskInfo, $task, $this);
             }
         }
 
diff --git a/typo3/sysext/scheduler/Classes/Task/ExecuteSchedulableCommandAdditionalFieldProvider.php b/typo3/sysext/scheduler/Classes/Task/ExecuteSchedulableCommandAdditionalFieldProvider.php
index e2974504f768..0f63ed9269a3 100644
--- a/typo3/sysext/scheduler/Classes/Task/ExecuteSchedulableCommandAdditionalFieldProvider.php
+++ b/typo3/sysext/scheduler/Classes/Task/ExecuteSchedulableCommandAdditionalFieldProvider.php
@@ -87,7 +87,7 @@ class ExecuteSchedulableCommandAdditionalFieldProvider implements AdditionalFiel
      * Render additional information fields within the scheduler backend.
      *
      * @param array &$taskInfo Array information of task to return
-     * @param mixed $task \TYPO3\CMS\Scheduler\Task\AbstractTask or \TYPO3\CMS\Scheduler\Execution instance
+     * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
      * @param SchedulerModuleController $schedulerModule Reference to the calling object (BE module of the Scheduler)
      * @return array Additional fields
      * @see \TYPO3\CMS\Scheduler\AdditionalFieldProvider#getAdditionalFields($taskInfo, $task, $schedulerModule)
diff --git a/typo3/sysext/scheduler/Classes/Task/FileStorageExtractionAdditionalFieldProvider.php b/typo3/sysext/scheduler/Classes/Task/FileStorageExtractionAdditionalFieldProvider.php
index ead35f11a2c6..478812b61fb6 100644
--- a/typo3/sysext/scheduler/Classes/Task/FileStorageExtractionAdditionalFieldProvider.php
+++ b/typo3/sysext/scheduler/Classes/Task/FileStorageExtractionAdditionalFieldProvider.php
@@ -50,7 +50,7 @@ class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldPro
     /**
      * Returns a field configuration including a selectbox for available storages
      *
-     * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
+     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
      * @return array Array containing all the information pertaining to the additional fields
      */
     protected function getAllStoragesField(FileStorageExtractionTask $task = null)
@@ -82,7 +82,7 @@ class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldPro
     /**
      * Returns a field configuration including an input field for the file count
      *
-     * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
+     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
      * @return array Array containing all the information pertaining to the additional fields
      */
     protected function getFileCountField(FileStorageExtractionTask $task = null)
@@ -104,7 +104,7 @@ class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldPro
     /**
      * Returns a field configuration telling about the status of registered extractors.
      *
-     * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
+     * @param FileStorageExtractionTask|null $task When editing, reference to the current task object. NULL when adding.
      * @return array Array containing all the information pertaining to the additional fields
      */
     protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = null)
@@ -172,12 +172,12 @@ class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldPro
      * Save additional field in task
      *
      * @param array $submittedData Contains data submitted by the user
-     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
+     * @param AbstractTask $task Reference to the current task object
      * @throws \InvalidArgumentException
      */
-    public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
+    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
     {
-        if ($task !== null && !$task instanceof FileStorageExtractionTask) {
+        if (!$task instanceof FileStorageExtractionTask) {
             throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
         }
         $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
-- 
GitLab