2012-03-31 23:51:02 +02:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
2022-02-05 10:30:50 +01:00
namespace core ;
defined ( 'MOODLE_INTERNAL' ) || die ();
global $CFG ;
require_once ( $CFG -> libdir . '/simplepie/moodle_simplepie.php' );
2023-02-27 18:26:41 +01:00
require_once ( $CFG -> libdir . '/rsslib.php' );
2022-02-05 10:30:50 +01:00
2012-03-31 23:51:02 +02:00
/**
* These tests rely on the rsstest . xml file on download . moodle . org ,
* from eloys listing :
* rsstest . xml : One valid rss feed .
* md5 : 8 fd047914863bf9b3a4b1514ec51c32c
* size : 32188
*
* If networking / proxy configuration is wrong these tests will fail ..
*
* @ package core
2022-02-05 10:30:50 +01:00
* @ category test
2012-03-31 23:51:02 +02:00
* @ copyright 2009 Dan Poltawski
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2022-02-05 10:30:50 +01:00
class rsslib_test extends \advanced_testcase {
2013-09-24 21:11:10 +02:00
2013-06-17 10:23:44 +08:00
// The number of seconds tests should wait for the server to respond (high to prevent false positives).
const TIMEOUT = 10 ;
2012-03-31 23:51:02 +02:00
2020-08-02 10:59:12 +02:00
protected function setUp () : void {
2022-02-05 10:30:50 +01:00
\moodle_simplepie :: reset_cache ();
2012-03-31 23:51:02 +02:00
}
2024-05-30 09:37:38 +02:00
public function test_getfeed () : void {
2022-02-05 10:30:50 +01:00
$feed = new \moodle_simplepie ( $this -> getExternalTestFileUrl ( '/rsstest.xml' ), self :: TIMEOUT );
2012-03-31 23:51:02 +02:00
2022-02-05 10:30:50 +01:00
$this -> assertInstanceOf ( '\moodle_simplepie' , $feed );
2012-03-31 23:51:02 +02:00
$this -> assertNull ( $feed -> error (), " Failed to load the sample RSS file. Please check your proxy settings in Moodle. %s " );
2013-07-28 14:44:43 +02:00
$this -> assertSame ( 'Moodle News' , $feed -> get_title ());
2012-03-31 23:51:02 +02:00
2013-07-28 14:44:43 +02:00
$this -> assertSame ( 'http://moodle.org/mod/forum/view.php?f=1' , $feed -> get_link ());
$this -> assertSame ( " General news about Moodle. \n \n Moodle 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. " ,
$feed -> get_description ());
2012-03-31 23:51:02 +02:00
2013-07-28 14:44:43 +02:00
$this -> assertSame ( '&#169; 2007 moodle' , $feed -> get_copyright ());
$this -> assertSame ( 'http://moodle.org/pix/i/rsssitelogo.gif' , $feed -> get_image_url ());
$this -> assertSame ( 'moodle' , $feed -> get_image_title ());
$this -> assertSame ( 'http://moodle.org/' , $feed -> get_image_link ());
$this -> assertEquals ( '140' , $feed -> get_image_width ());
$this -> assertEquals ( '35' , $feed -> get_image_height ());
2012-03-31 23:51:02 +02:00
$this -> assertNotEmpty ( $items = $feed -> get_items ());
2013-07-28 14:44:43 +02:00
$this -> assertCount ( 15 , $items );
2012-03-31 23:51:02 +02:00
$this -> assertNotEmpty ( $itemone = $feed -> get_item ( 0 ));
2013-07-28 14:44:43 +02:00
$this -> assertSame ( 'Google HOP contest encourages pre-University students to work on Moodle' , $itemone -> get_title ());
$this -> assertSame ( 'http://moodle.org/mod/forum/discuss.php?d=85629' , $itemone -> get_link ());
$this -> assertSame ( 'http://moodle.org/mod/forum/discuss.php?d=85629' , $itemone -> get_id ());
2012-03-31 23:51:02 +02:00
$description = <<< EOD
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 />
< br />
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 />
< br />
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 />
< br />
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 />
< br />
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 >
EOD ;
2013-04-06 19:57:28 +02:00
$description = purify_html ( $description );
2013-07-28 14:44:43 +02:00
$this -> assertSame ( $description , $itemone -> get_description ());
2012-03-31 23:51:02 +02:00
2013-07-28 14:44:43 +02:00
// TODO fix this so it uses $CFG by default.
$this -> assertSame ( 1196412453 , $itemone -> get_date ( 'U' ));
2012-03-31 23:51:02 +02:00
2013-07-28 14:44:43 +02:00
// Last item.
2012-03-31 23:51:02 +02:00
$this -> assertNotEmpty ( $feed -> get_item ( 14 ));
2013-07-28 14:44:43 +02:00
// Past last item.
2012-03-31 23:51:02 +02:00
$this -> assertEmpty ( $feed -> get_item ( 15 ));
}
/*
2013-07-28 14:44:43 +02:00
* Test retrieving a url which doesn ' t exist .
2012-03-31 23:51:02 +02:00
*/
2024-05-30 09:37:38 +02:00
public function test_failurl () : void {
2015-12-14 14:53:07 +08:00
global $CFG ;
// We do not want this in php error log.
$errorlevel = error_reporting ( $CFG -> debug & ~ E_USER_NOTICE );
2022-02-05 10:30:50 +01:00
$feed = new \moodle_simplepie ( $this -> getExternalTestFileUrl ( '/rsstest-which-doesnt-exist.xml' ), self :: TIMEOUT );
2015-12-14 14:53:07 +08:00
error_reporting ( $errorlevel );
2012-03-31 23:51:02 +02:00
$this -> assertNotEmpty ( $feed -> error ());
}
/*
2013-07-28 14:44:43 +02:00
* Test retrieving a url with broken proxy configuration .
2012-03-31 23:51:02 +02:00
*/
2024-05-30 09:37:38 +02:00
public function test_failproxy () : void {
2012-03-31 23:51:02 +02:00
global $CFG ;
$oldproxy = $CFG -> proxyhost ;
$CFG -> proxyhost = 'xxxxxxxxxxxxxxx.moodle.org' ;
2020-05-10 16:54:14 +02:00
$oldproxybypass = $CFG -> proxybypass ; // Ensure we don't get locally served extests bypassing the proxy.
$CFG -> proxybypass = '' ;
2022-02-05 10:30:50 +01:00
$feed = new \moodle_simplepie ( $this -> getExternalTestFileUrl ( '/rsstest.xml' ));
2012-03-31 23:51:02 +02:00
$this -> assertNotEmpty ( $feed -> error ());
$this -> assertEmpty ( $feed -> get_title ());
$CFG -> proxyhost = $oldproxy ;
2020-05-10 16:54:14 +02:00
$CFG -> proxybypass = $oldproxybypass ;
2012-03-31 23:51:02 +02:00
}
/*
2013-07-28 14:44:43 +02:00
* Test retrieving a url which sends a redirect to another valid feed .
2012-03-31 23:51:02 +02:00
*/
2024-05-30 09:37:38 +02:00
public function test_redirect () : void {
2022-02-05 10:30:50 +01:00
$feed = new \moodle_simplepie ( $this -> getExternalTestFileUrl ( '/rss_redir.php' ), self :: TIMEOUT );
2012-03-31 23:51:02 +02:00
$this -> assertNull ( $feed -> error ());
2013-07-28 14:44:43 +02:00
$this -> assertSame ( 'Moodle News' , $feed -> get_title ());
$this -> assertSame ( 'http://moodle.org/mod/forum/view.php?f=1' , $feed -> get_link ());
2012-03-31 23:51:02 +02:00
}
2023-02-27 18:26:41 +01:00
/**
* Test that we can get the right user ID based on the provided private key ( token ) .
*
* @ covers :: rss_get_userid_from_token
*/
2024-05-30 09:37:38 +02:00
public function test_rss_get_userid_from_token () : void {
2023-02-27 18:26:41 +01:00
global $USER ;
$this -> resetAfterTest ();
$this -> setGuestUser ();
$key = rss_get_token ( $USER -> id );
$this -> assertSame ( rss_get_userid_from_token ( $key ), $USER -> id );
}
2012-03-31 23:51:02 +02:00
}