Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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 core;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
  23  require_once($CFG->libdir . '/rsslib.php');
  24  
  25  /**
  26   * These tests rely on the rsstest.xml file on download.moodle.org,
  27   * from eloys listing:
  28   *   rsstest.xml: One valid rss feed.
  29   *   md5:  8fd047914863bf9b3a4b1514ec51c32c
  30   *   size: 32188
  31   *
  32   * If networking/proxy configuration is wrong these tests will fail..
  33   *
  34   * @package    core
  35   * @category   test
  36   * @copyright  2009 Dan Poltawski
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class rsslib_test extends \advanced_testcase {
  40  
  41      // The number of seconds tests should wait for the server to respond (high to prevent false positives).
  42      const TIMEOUT = 10;
  43  
  44      protected function setUp(): void {
  45          \moodle_simplepie::reset_cache();
  46      }
  47  
  48      public function test_getfeed() {
  49          $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'), self::TIMEOUT);
  50  
  51          $this->assertInstanceOf('\moodle_simplepie', $feed);
  52  
  53          $this->assertNull($feed->error(), "Failed to load the sample RSS file. Please check your proxy settings in Moodle. %s");
  54  
  55          $this->assertSame('Moodle News', $feed->get_title());
  56  
  57          $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
  58          $this->assertSame("General news about Moodle.\n\nMoodle is a leading open-source course management system (CMS) - a software package designed to help educators create quality online courses. Such e-learning systems are sometimes also called Learning Management Systems (LMS) or Virtual Learning Environments (VLE). One of the main advantages of Moodle over other systems is a strong grounding in social constructionist pedagogy.",
  59              $feed->get_description());
  60  
  61          $this->assertSame('&amp;#169; 2007 moodle', $feed->get_copyright());
  62          $this->assertSame('http://moodle.org/pix/i/rsssitelogo.gif', $feed->get_image_url());
  63          $this->assertSame('moodle', $feed->get_image_title());
  64          $this->assertSame('http://moodle.org/', $feed->get_image_link());
  65          $this->assertEquals('140', $feed->get_image_width());
  66          $this->assertEquals('35', $feed->get_image_height());
  67  
  68          $this->assertNotEmpty($items = $feed->get_items());
  69          $this->assertCount(15, $items);
  70  
  71          $this->assertNotEmpty($itemone = $feed->get_item(0));
  72  
  73          $this->assertSame('Google HOP contest encourages pre-University students to work on Moodle', $itemone->get_title());
  74          $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_link());
  75          $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_id());
  76          $description = <<<EOD
  77  by Martin Dougiamas. &nbsp;<p><p><img src="http://code.google.com/opensource/ghop/2007-8/images/ghoplogosm.jpg" align="right" style="margin:10px" />After their very successful <a href="http://code.google.com/soc/2007/">Summer of Code</a> program for University students, Google just announced their new <a href="http://code.google.com/opensource/ghop/2007-8/">Highly Open Participation contest</a>, designed to encourage pre-University students to get involved with open source projects via much smaller and diverse contributions.<br />
  78  <br />
  79  I'm very proud that Moodle has been selected as one of only <a href="http://code.google.com/opensource/ghop/2007-8/projects.html">ten open source projects</a> to take part in the inaugural year of this new contest.<br />
  80  <br />
  81  We have a <a href="http://code.google.com/p/google-highly-open-participation-moodle/issues/list">long list of small tasks</a> prepared already for students, but we would definitely like to see the Moodle community come up with more - so if you have any ideas for things you want to see done, please <a href="http://code.google.com/p/google-highly-open-participation-moodle/">send them to us</a>!  Just remember they can't take more than five days.<br />
  82  <br />
  83  Google will pay students US$100 for every three tasks they successfully complete, plus send a cool T-shirt.  There are also grand prizes including an all-expenses-paid trip to Google HQ in Mountain View, California.  If you are (or know) a young student with an interest in Moodle then give it a go! <br />
  84  <br />
  85  You can find out all the details on the <a href="http://code.google.com/p/google-highly-open-participation-moodle/">Moodle/GHOP contest site</a>.</p></p>
  86  EOD;
  87          $description = purify_html($description);
  88          $this->assertSame($description, $itemone->get_description());
  89  
  90          // TODO fix this so it uses $CFG by default.
  91          $this->assertSame(1196412453, $itemone->get_date('U'));
  92  
  93          // Last item.
  94          $this->assertNotEmpty($feed->get_item(14));
  95          // Past last item.
  96          $this->assertEmpty($feed->get_item(15));
  97      }
  98  
  99      /*
 100       * Test retrieving a url which doesn't exist.
 101       */
 102      public function test_failurl() {
 103          global $CFG;
 104  
 105          // We do not want this in php error log.
 106          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
 107          $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest-which-doesnt-exist.xml'), self::TIMEOUT);
 108          error_reporting($errorlevel);
 109  
 110          $this->assertNotEmpty($feed->error());
 111      }
 112  
 113      /*
 114       * Test retrieving a url with broken proxy configuration.
 115       */
 116      public function test_failproxy() {
 117          global $CFG;
 118  
 119          $oldproxy = $CFG->proxyhost;
 120          $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
 121  
 122          $oldproxybypass = $CFG->proxybypass; // Ensure we don't get locally served extests bypassing the proxy.
 123          $CFG->proxybypass = '';
 124  
 125          $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'));
 126  
 127          $this->assertNotEmpty($feed->error());
 128          $this->assertEmpty($feed->get_title());
 129          $CFG->proxyhost = $oldproxy;
 130          $CFG->proxybypass = $oldproxybypass;
 131      }
 132  
 133      /*
 134       * Test retrieving a url which sends a redirect to another valid feed.
 135       */
 136      public function test_redirect() {
 137          $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT);
 138  
 139          $this->assertNull($feed->error());
 140          $this->assertSame('Moodle News', $feed->get_title());
 141          $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
 142      }
 143  
 144      /**
 145       * Test that we can get the right user ID based on the provided private key (token).
 146       *
 147       * @covers ::rss_get_userid_from_token
 148       */
 149      public function test_rss_get_userid_from_token() {
 150          global $USER;
 151  
 152          $this->resetAfterTest();
 153          $this->setGuestUser();
 154  
 155          $key = rss_get_token($USER->id);
 156          $this->assertSame(rss_get_userid_from_token($key), $USER->id);
 157      }
 158  }