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   * These tests rely on the rsstest.xml file on download.moodle.org,
  19   * from eloys listing:
  20   *   rsstest.xml: One valid rss feed.
  21   *   md5:  8fd047914863bf9b3a4b1514ec51c32c
  22   *   size: 32188
  23   *
  24   * If networking/proxy configuration is wrong these tests will fail..
  25   *
  26   * @package    core
  27   * @category   phpunit
  28   * @copyright  2009 Dan Poltawski
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  global $CFG;
  35  require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
  36  
  37  
  38  class core_rsslib_testcase extends advanced_testcase {
  39  
  40      // The number of seconds tests should wait for the server to respond (high to prevent false positives).
  41      const TIMEOUT = 10;
  42  
  43      protected function setUp(): void {
  44          moodle_simplepie::reset_cache();
  45      }
  46  
  47      public function test_getfeed() {
  48          $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'), self::TIMEOUT);
  49  
  50          $this->assertInstanceOf('moodle_simplepie', $feed);
  51  
  52          $this->assertNull($feed->error(), "Failed to load the sample RSS file. Please check your proxy settings in Moodle. %s");
  53  
  54          $this->assertSame('Moodle News', $feed->get_title());
  55  
  56          $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
  57          $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.",
  58              $feed->get_description());
  59  
  60          $this->assertSame('&amp;#169; 2007 moodle', $feed->get_copyright());
  61          $this->assertSame('http://moodle.org/pix/i/rsssitelogo.gif', $feed->get_image_url());
  62          $this->assertSame('moodle', $feed->get_image_title());
  63          $this->assertSame('http://moodle.org/', $feed->get_image_link());
  64          $this->assertEquals('140', $feed->get_image_width());
  65          $this->assertEquals('35', $feed->get_image_height());
  66  
  67          $this->assertNotEmpty($items = $feed->get_items());
  68          $this->assertCount(15, $items);
  69  
  70          $this->assertNotEmpty($itemone = $feed->get_item(0));
  71  
  72          $this->assertSame('Google HOP contest encourages pre-University students to work on Moodle', $itemone->get_title());
  73          $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_link());
  74          $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_id());
  75          $description = <<<EOD
  76  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 />
  77  <br />
  78  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 />
  79  <br />
  80  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 />
  81  <br />
  82  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 />
  83  <br />
  84  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>
  85  EOD;
  86          $description = purify_html($description);
  87          $this->assertSame($description, $itemone->get_description());
  88  
  89          // TODO fix this so it uses $CFG by default.
  90          $this->assertSame(1196412453, $itemone->get_date('U'));
  91  
  92          // Last item.
  93          $this->assertNotEmpty($feed->get_item(14));
  94          // Past last item.
  95          $this->assertEmpty($feed->get_item(15));
  96      }
  97  
  98      /*
  99       * Test retrieving a url which doesn't exist.
 100       */
 101      public function test_failurl() {
 102          global $CFG;
 103  
 104          // We do not want this in php error log.
 105          $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
 106          $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest-which-doesnt-exist.xml'), self::TIMEOUT);
 107          error_reporting($errorlevel);
 108  
 109          $this->assertNotEmpty($feed->error());
 110      }
 111  
 112      /*
 113       * Test retrieving a url with broken proxy configuration.
 114       */
 115      public function test_failproxy() {
 116          global $CFG;
 117  
 118          $oldproxy = $CFG->proxyhost;
 119          $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
 120  
 121          $oldproxybypass = $CFG->proxybypass; // Ensure we don't get locally served extests bypassing the proxy.
 122          $CFG->proxybypass = '';
 123  
 124          $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'));
 125  
 126          $this->assertNotEmpty($feed->error());
 127          $this->assertEmpty($feed->get_title());
 128          $CFG->proxyhost = $oldproxy;
 129          $CFG->proxybypass = $oldproxybypass;
 130      }
 131  
 132      /*
 133       * Test retrieving a url which sends a redirect to another valid feed.
 134       */
 135      public function test_redirect() {
 136          $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT);
 137  
 138          $this->assertNull($feed->error());
 139          $this->assertSame('Moodle News', $feed->get_title());
 140          $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
 141      }
 142  }