Esempio n. 1
0
 /**
  * Constructor
  *
  * @api
  * @param string $cronCommand The cron command can hold any combination documented as valid
  * @param bool|int $timestamp Optional start time, used in unit tests
  * @return \TYPO3\CMS\Scheduler\CronCommand\CronCommand
  */
 public function __construct($cronCommand, $timestamp = FALSE)
 {
     $cronCommand = \TYPO3\CMS\Scheduler\CronCommand\NormalizeCommand::normalize($cronCommand);
     // Explode cron command to sections
     $this->cronCommandSections = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $cronCommand);
     // Initialize the values with the starting time
     // This takes care that the calculated time is always in the future
     if ($timestamp === FALSE) {
         $timestamp = strtotime('+1 minute');
     } else {
         $timestamp += 60;
     }
     $this->timestamp = $this->roundTimestamp($timestamp);
 }
 /**
  * @test
  * @dataProvider normalizeValidDataProvider
  * @param string $expression Cron command to test
  * @param string $expected Expected result (normalized cron command syntax)
  */
 public function normalizeConvertsCronCommand($expression, $expected)
 {
     $result = \TYPO3\CMS\Scheduler\CronCommand\NormalizeCommand::normalize($expression);
     $this->assertEquals($expected, $result);
 }
 /**
  * Checks the submitted data and performs some pre-processing on it
  *
  * @return boolean TRUE if everything was ok, FALSE otherwise
  */
 protected function preprocessData()
 {
     $result = TRUE;
     // Validate id
     $this->submittedData['uid'] = empty($this->submittedData['uid']) ? 0 : intval($this->submittedData['uid']);
     // Validate selected task class
     if (!class_exists($this->submittedData['class'])) {
         $this->addMessage($GLOBALS['LANG']->getLL('msg.noTaskClassFound'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
     }
     // Check start date
     if (empty($this->submittedData['start'])) {
         $this->addMessage($GLOBALS['LANG']->getLL('msg.noStartDate'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
         $result = FALSE;
     } else {
         try {
             $timestamp = $this->checkDate($this->submittedData['start']);
             $this->submittedData['start'] = $timestamp;
         } catch (\Exception $e) {
             $this->addMessage($GLOBALS['LANG']->getLL('msg.invalidStartDate'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
             $result = FALSE;
         }
     }
     // Check end date, if recurring task
     if ($this->submittedData['type'] == 2 && !empty($this->submittedData['end'])) {
         try {
             $timestamp = $this->checkDate($this->submittedData['end']);
             $this->submittedData['end'] = $timestamp;
             if ($this->submittedData['end'] < $this->submittedData['start']) {
                 $this->addMessage($GLOBALS['LANG']->getLL('msg.endDateSmallerThanStartDate'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
                 $result = FALSE;
             }
         } catch (\Exception $e) {
             $this->addMessage($GLOBALS['LANG']->getLL('msg.invalidEndDate'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
             $result = FALSE;
         }
     }
     // Set default values for interval and cron command
     $this->submittedData['interval'] = 0;
     $this->submittedData['croncmd'] = '';
     // Check type and validity of frequency, if recurring
     if ($this->submittedData['type'] == 2) {
         $frequency = trim($this->submittedData['frequency']);
         if (empty($frequency)) {
             // Empty frequency, not valid
             $this->addMessage($GLOBALS['LANG']->getLL('msg.noFrequency'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
             $result = FALSE;
         } else {
             $cronErrorCode = 0;
             $cronErrorMessage = '';
             // Try interpreting the cron command
             try {
                 \TYPO3\CMS\Scheduler\CronCommand\NormalizeCommand::normalize($frequency);
                 $this->submittedData['croncmd'] = $frequency;
             } catch (\Exception $e) {
                 // Store the exception's result
                 $cronErrorMessage = $e->getMessage();
                 $cronErrorCode = $e->getCode();
                 // Check if the frequency is a valid number
                 // If yes, assume it is a frequency in seconds, and unset cron error code
                 if (is_numeric($frequency)) {
                     $this->submittedData['interval'] = intval($frequency);
                     unset($cronErrorCode);
                 }
             }
             // If there's a cron error code, issue validation error message
             if (!empty($cronErrorCode)) {
                 $this->addMessage(sprintf($GLOBALS['LANG']->getLL('msg.frequencyError'), $cronErrorMessage, $cronErrorCode), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
                 $result = FALSE;
             }
         }
     }
     // Validate additional input fields
     if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][$this->submittedData['class']]['additionalFields'])) {
         $providerObject = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][$this->submittedData['class']]['additionalFields']);
         if ($providerObject instanceof \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface) {
             // The validate method will return TRUE if all went well, but that must not
             // override previous FALSE values => AND the returned value with the existing one
             $result &= $providerObject->validateAdditionalFields($this->submittedData, $this);
         }
     }
     return $result;
 }
 public static function normalizeWeekday($weekday)
 {
     return parent::normalizeWeekday($weekday);
 }
 /**
  * @test
  * @dataProvider normalizeValidDataProvider
  * @param string $expression Cron command to test
  * @param string $expected Expected result (normalized cron command syntax)
  */
 public function normalizeConvertsCronCommand($expression, $expected)
 {
     $result = NormalizeCommand::normalize($expression);
     $this->assertEquals($expected, $result);
 }