Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace block_rss_client;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  require_once (__DIR__ . '/../../moodleblock.class.php');
  21  require_once (__DIR__ . '/../block_rss_client.php');
  22  
  23  /**
  24   * PHPunit tests for rss client cron.
  25   *
  26   * @package    block_rss_client
  27   * @copyright  2015 Universit of Nottingham
  28   * @author     Neill Magill <neill.magill@nottingham.ac.uk>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class cron_test extends \advanced_testcase {
  32      /**
  33       * Test that when a record has a skipuntil time that is greater
  34       * than the current time the attempt is skipped.
  35       */
  36      public function test_skip() {
  37          global $DB, $CFG;
  38          $this->resetAfterTest();
  39          // Create a RSS feed record with a skip until time set to the future.
  40          $record = (object) array(
  41              'userid' => 1,
  42              'title' => 'Skip test feed',
  43              'preferredtitle' => '',
  44              'description' => 'A feed to test the skip time.',
  45              'shared' => 0,
  46              'url' => 'http://example.com/rss',
  47              'skiptime' => 330,
  48              'skipuntil' => time() + 300,
  49          );
  50          $DB->insert_record('block_rss_client', $record);
  51  
  52          $task = new \block_rss_client\task\refreshfeeds();
  53          ob_start();
  54  
  55          // Silence SimplePie php notices.
  56          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
  57          $task->execute();
  58          error_reporting($errorlevel);
  59  
  60          $cronoutput = ob_get_clean();
  61          $this->assertStringContainsString('skipping until ' . userdate($record->skipuntil), $cronoutput);
  62          $this->assertStringContainsString('0 feeds refreshed (took ', $cronoutput);
  63      }
  64  
  65      /**
  66       * Data provider for skip time tests.
  67       *
  68       * @return  array
  69       */
  70      public function skip_time_increase_provider() : array {
  71          return [
  72              'Never failed' => [
  73                  'skiptime' => 0,
  74                  'skipuntil' => 0,
  75                  'newvalue' => MINSECS * 5,
  76              ],
  77              'Failed before' => [
  78                  // This should just double the time.
  79                  'skiptime' => 330,
  80                  'skipuntil' => time(),
  81                  'newvalue' => 660,
  82              ],
  83              'Near max' => [
  84                  'skiptime' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME - 5,
  85                  'skipuntil' => time(),
  86                  'newvalue' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME,
  87              ],
  88          ];
  89      }
  90  
  91      /**
  92       * Test that when a feed has an error the skip time is increased correctly.
  93       *
  94       * @dataProvider    skip_time_increase_provider
  95       */
  96      public function test_error($skiptime, $skipuntil, $newvalue) {
  97          global $DB, $CFG;
  98          $this->resetAfterTest();
  99  
 100          require_once("{$CFG->libdir}/simplepie/moodle_simplepie.php");
 101  
 102          $time = time();
 103          // A record that has failed before.
 104          $record = (object) [
 105              'userid' => 1,
 106              'title' => 'Skip test feed',
 107              'preferredtitle' => '',
 108              'description' => 'A feed to test the skip time.',
 109              'shared' => 0,
 110              'url' => 'http://example.com/rss',
 111              'skiptime' => $skiptime,
 112              'skipuntil' => $skipuntil,
 113          ];
 114          $record->id = $DB->insert_record('block_rss_client', $record);
 115  
 116          // Run the scheduled task and have it fail.
 117          $task = $this->getMockBuilder(\block_rss_client\task\refreshfeeds::class)
 118              ->onlyMethods(['fetch_feed'])
 119              ->getMock();
 120  
 121          $piemock = $this->getMockBuilder(\moodle_simplepie::class)
 122              ->onlyMethods(['error'])
 123              ->getMock();
 124  
 125          $piemock->method('error')
 126              ->willReturn(true);
 127  
 128          $task->method('fetch_feed')
 129              ->willReturn($piemock);
 130  
 131          // Run the cron and capture its output.
 132          $this->expectOutputRegex("/.*Error: could not load\/find the RSS feed - skipping for {$newvalue} seconds.*/");
 133          $task->execute();
 134      }
 135  }