MDL-40207 simplepie: reduce false failures in unit tests
[moodle.git] / lib / tests / rsslib_test.php
CommitLineData
a3d5830a
PS
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
32defined('MOODLE_INTERNAL') || die();
33
34global $CFG;
35require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
36
37
38class moodlesimplepie_testcase extends basic_testcase {
39
40 # A url we know exists and is valid
41 const VALIDURL = 'http://download.moodle.org/unittest/rsstest.xml';
42 # A url which we know doesn't exist
43 const INVALIDURL = 'http://download.moodle.org/unittest/rsstest-which-doesnt-exist.xml';
44 # This tinyurl redirects to th rsstest.xml file
45 const REDIRECTURL = 'http://tinyurl.com/lvyslv';
5d447145
DP
46 # The number of seconds tests should wait for the server to respond (high to prevent false positives).
47 const TIMEOUT = 10;
a3d5830a
PS
48
49 function setUp() {
50 moodle_simplepie::reset_cache();
51 }
52
53 function test_getfeed() {
5d447145
DP
54 $feed = new moodle_simplepie();
55 $feed->set_timeout(self::TIMEOUT);
56 $feed->set_feed_url(self::VALIDURL);
57 $feed->init();
a3d5830a
PS
58 $this->assertInstanceOf('moodle_simplepie', $feed);
59
60 $this->assertNull($feed->error(), "Failed to load the sample RSS file. Please check your proxy settings in Moodle. %s");
61 if ($feed->error()) {
62 return;
63 }
64
65 $this->assertEquals($feed->get_title(), 'Moodle News');
66
67 $this->assertEquals($feed->get_link(), 'http://moodle.org/mod/forum/view.php?f=1');
68 $this->assertEquals($feed->get_description(), "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.");
69
70 $this->assertEquals($feed->get_copyright(), '&amp;#169; 2007 moodle');
71 $this->assertEquals($feed->get_image_url(), 'http://moodle.org/pix/i/rsssitelogo.gif');
72 $this->assertEquals($feed->get_image_title(), 'moodle');
73 $this->assertEquals($feed->get_image_link(), 'http://moodle.org');
74 $this->assertEquals($feed->get_image_width(), '140');
75 $this->assertEquals($feed->get_image_height(), '35');
76
77 $this->assertNotEmpty($items = $feed->get_items());
78 $this->assertEquals(count($items), 15);
79
80 $this->assertNotEmpty($itemone = $feed->get_item(0));
81 if (!$itemone) {
82 return;
83 }
84
85 $this->assertEquals($itemone->get_title(), 'Google HOP contest encourages pre-University students to work on Moodle');
86 $this->assertEquals($itemone->get_link(), 'http://moodle.org/mod/forum/discuss.php?d=85629');
87 $this->assertEquals($itemone->get_id(), 'http://moodle.org/mod/forum/discuss.php?d=85629');
88 $description = <<<EOD
89by 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 />
90<br />
91I'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 />
92<br />
93We 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 />
94<br />
95Google 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 />
96<br />
97You 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>
98EOD;
99 $this->assertEquals($itemone->get_description(), $description);
100
101
102 // TODO fix this so it uses $CFG by default
103 $this->assertEquals($itemone->get_date('U'), 1196412453);
104
105 // last item
106 $this->assertNotEmpty($feed->get_item(14));
107 // Past last item
108 $this->assertEmpty($feed->get_item(15));
109 }
110
111 /*
112 * Test retrieving a url which doesn't exist
113 */
114 function test_failurl() {
5d447145
DP
115 $feed = new moodle_simplepie();
116 $feed->set_timeout(self::TIMEOUT);
117 $feed->set_feed_url(self::INVALIDURL);
118 @$feed->init(); // We do not want this in php error log.
a3d5830a
PS
119
120 $this->assertNotEmpty($feed->error());
121 }
122
123 /*
124 * Test retrieving a url with broken proxy configuration
125 */
126 function test_failproxy() {
127 global $CFG;
128
129 $oldproxy = $CFG->proxyhost;
130 $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
131
132 $feed = new moodle_simplepie(self::VALIDURL);
133
134 $this->assertNotEmpty($feed->error());
135 $this->assertEmpty($feed->get_title());
136 $CFG->proxyhost = $oldproxy;
137 }
138
139 /*
140 * Test retrieving a url which sends a redirect to another valid feed
141 */
142 function test_redirect() {
143 global $CFG;
144
5d447145
DP
145 $feed = new moodle_simplepie();
146 $feed->set_timeout(self::TIMEOUT);
147 $feed->set_feed_url(self::REDIRECTURL);
148 $feed->init();
a3d5830a
PS
149
150 $this->assertNull($feed->error());
151 $this->assertEquals($feed->get_title(), 'Moodle News');
152 $this->assertEquals($feed->get_link(), 'http://moodle.org/mod/forum/view.php?f=1');
153 }
154
155}