[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blocks/rss_client/tests/ -> cron_test.php (source)

   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          $block = new block_rss_client();
  59          ob_start();
  60  
  61          // Silence SimplePie php notices.
  62          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
  63          $block->cron();
  64          error_reporting($errorlevel);
  65  
  66          $cronoutput = ob_get_clean();
  67          $this->assertContains('skipping until ' . userdate($record->skipuntil), $cronoutput);
  68          $this->assertContains('0 feeds refreshed (took ', $cronoutput);
  69      }
  70  
  71      /**
  72       * Test that when a feed has an error the skip time is increaed correctly.
  73       */
  74      public function test_error() {
  75          global $DB, $CFG;
  76          $this->resetAfterTest();
  77          $time = time();
  78          // A record that has failed before.
  79          $record = (object) array(
  80              'userid' => 1,
  81              'title' => 'Skip test feed',
  82              'preferredtitle' => '',
  83              'description' => 'A feed to test the skip time.',
  84              'shared' => 0,
  85              'url' => 'http://example.com/rss',
  86              'skiptime' => 330,
  87              'skipuntil' => $time - 300,
  88          );
  89          $record->id = $DB->insert_record('block_rss_client', $record);
  90  
  91          // A record that has not failed before.
  92          $record2 = (object) array(
  93              'userid' => 1,
  94              'title' => 'Skip test feed',
  95              'preferredtitle' => '',
  96              'description' => 'A feed to test the skip time.',
  97              'shared' => 0,
  98              'url' => 'http://example.com/rss2',
  99              'skiptime' => 0,
 100              'skipuntil' => 0,
 101          );
 102          $record2->id = $DB->insert_record('block_rss_client', $record2);
 103  
 104          // A record that is near the maximum wait time.
 105          $record3 = (object) array(
 106              'userid' => 1,
 107              'title' => 'Skip test feed',
 108              'preferredtitle' => '',
 109              'description' => 'A feed to test the skip time.',
 110              'shared' => 0,
 111              'url' => 'http://example.com/rss3',
 112              'skiptime' => block_rss_client::CLIENT_MAX_SKIPTIME - 5,
 113              'skipuntil' => $time - 1,
 114          );
 115          $record3->id = $DB->insert_record('block_rss_client', $record3);
 116  
 117          // Run the cron.
 118          $block = new block_rss_client();
 119          ob_start();
 120  
 121          // Silence SimplePie php notices.
 122          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
 123          $block->cron();
 124          error_reporting($errorlevel);
 125  
 126          $cronoutput = ob_get_clean();
 127          $skiptime1 = $record->skiptime * 2;
 128          $message1 = 'http://example.com/rss Error: could not load/find the RSS feed - skipping for ' . $skiptime1 . ' seconds.';
 129          $this->assertContains($message1, $cronoutput);
 130          $skiptime2 = 330; // Assumes that the cron time in the version file is 300.
 131          $message2 = 'http://example.com/rss2 Error: could not load/find the RSS feed - skipping for ' . $skiptime2 . ' seconds.';
 132          $this->assertContains($message2, $cronoutput);
 133          $skiptime3 = block_rss_client::CLIENT_MAX_SKIPTIME;
 134          $message3 = 'http://example.com/rss3 Error: could not load/find the RSS feed - skipping for ' . $skiptime3 . ' seconds.';
 135          $this->assertContains($message3, $cronoutput);
 136          $this->assertContains('0 feeds refreshed (took ', $cronoutput);
 137  
 138          // Test that the records have been correctly updated.
 139          $newrecord = $DB->get_record('block_rss_client', array('id' => $record->id));
 140          $this->assertAttributeEquals($skiptime1, 'skiptime', $newrecord);
 141          $this->assertAttributeGreaterThanOrEqual($time + $skiptime1, 'skipuntil', $newrecord);
 142          $newrecord2 = $DB->get_record('block_rss_client', array('id' => $record2->id));
 143          $this->assertAttributeEquals($skiptime2, 'skiptime', $newrecord2);
 144          $this->assertAttributeGreaterThanOrEqual($time + $skiptime2, 'skipuntil', $newrecord2);
 145          $newrecord3 = $DB->get_record('block_rss_client', array('id' => $record3->id));
 146          $this->assertAttributeEquals($skiptime3, 'skiptime', $newrecord3);
 147          $this->assertAttributeGreaterThanOrEqual($time + $skiptime3, 'skipuntil', $newrecord3);
 148      }
 149  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1