Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * PHPunit tests for rss client cron.
  19   *
  20   * @package    block_rss_client
  21   * @copyright  2015 University of Nottingham
  22   * @author     Neill Magill <neill.magill@nottingham.ac.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  require_once (__DIR__ . '/../../moodleblock.class.php');
  27  require_once (__DIR__ . '/../block_rss_client.php');
  28  
  29  /**
  30   * Class for the PHPunit tests for rss client cron.
  31   *
  32   * @package    block_rss_client
  33   * @copyright  2015 Universit of Nottingham
  34   * @author     Neill Magill <neill.magill@nottingham.ac.uk>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class block_rss_client_cron_testcase extends advanced_testcase {
  38      /**
  39       * Test that when a record has a skipuntil time that is greater
  40       * than the current time the attempt is skipped.
  41       */
  42      public function test_skip() {
  43          global $DB, $CFG;
  44          $this->resetAfterTest();
  45          // Create a RSS feed record with a skip until time set to the future.
  46          $record = (object) array(
  47              'userid' => 1,
  48              'title' => 'Skip test feed',
  49              'preferredtitle' => '',
  50              'description' => 'A feed to test the skip time.',
  51              'shared' => 0,
  52              'url' => 'http://example.com/rss',
  53              'skiptime' => 330,
  54              'skipuntil' => time() + 300,
  55          );
  56          $DB->insert_record('block_rss_client', $record);
  57  
  58          $task = new \block_rss_client\task\refreshfeeds();
  59          ob_start();
  60  
  61          // Silence SimplePie php notices.
  62          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
  63          $task->execute();
  64          error_reporting($errorlevel);
  65  
  66          $cronoutput = ob_get_clean();
  67          $this->assertStringContainsString('skipping until ' . userdate($record->skipuntil), $cronoutput);
  68          $this->assertStringContainsString('0 feeds refreshed (took ', $cronoutput);
  69      }
  70  
  71      /**
  72       * Data provider for skip time tests.
  73       *
  74       * @return  array
  75       */
  76      public function skip_time_increase_provider() : array {
  77          return [
  78              'Never failed' => [
  79                  'skiptime' => 0,
  80                  'skipuntil' => 0,
  81                  'newvalue' => MINSECS * 5,
  82              ],
  83              'Failed before' => [
  84                  // This should just double the time.
  85                  'skiptime' => 330,
  86                  'skipuntil' => time(),
  87                  'newvalue' => 660,
  88              ],
  89              'Near max' => [
  90                  'skiptime' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME - 5,
  91                  'skipuntil' => time(),
  92                  'newvalue' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME,
  93              ],
  94          ];
  95      }
  96  
  97      /**
  98       * Test that when a feed has an error the skip time is increased correctly.
  99       *
 100       * @dataProvider    skip_time_increase_provider
 101       */
 102      public function test_error($skiptime, $skipuntil, $newvalue) {
 103          global $DB, $CFG;
 104          $this->resetAfterTest();
 105  
 106          require_once("{$CFG->libdir}/simplepie/moodle_simplepie.php");
 107  
 108          $time = time();
 109          // A record that has failed before.
 110          $record = (object) [
 111              'userid' => 1,
 112              'title' => 'Skip test feed',
 113              'preferredtitle' => '',
 114              'description' => 'A feed to test the skip time.',
 115              'shared' => 0,
 116              'url' => 'http://example.com/rss',
 117              'skiptime' => $skiptime,
 118              'skipuntil' => $skipuntil,
 119          ];
 120          $record->id = $DB->insert_record('block_rss_client', $record);
 121  
 122          // Run the scheduled task and have it fail.
 123          $task = $this->getMockBuilder(\block_rss_client\task\refreshfeeds::class)
 124              ->setMethods(['fetch_feed'])
 125              ->getMock();
 126  
 127          $piemock = $this->getMockBuilder(\moodle_simplepie::class)
 128              ->setMethods(['error'])
 129              ->getMock();
 130  
 131          $piemock->method('error')
 132              ->willReturn(true);
 133  
 134          $task->method('fetch_feed')
 135              ->willReturn($piemock);
 136  
 137          // Run the cron and capture its output.
 138          $this->expectOutputRegex("/.*Error: could not load\/find the RSS feed - skipping for {$newvalue} seconds.*/");
 139          $task->execute();
 140      }
 141  }