MDL-52487 rss: PHP 7 compatible error supression
[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
a9d2f1b4
PS
38class core_rsslib_testcase extends advanced_testcase {
39
f1da1322
DP
40 // The number of seconds tests should wait for the server to respond (high to prevent false positives).
41 const TIMEOUT = 10;
a3d5830a 42
be14898f 43 protected function setUp() {
a3d5830a
PS
44 moodle_simplepie::reset_cache();
45 }
46
be14898f 47 public function test_getfeed() {
a9d2f1b4 48 $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'), self::TIMEOUT);
a3d5830a
PS
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");
a3d5830a 53
be14898f 54 $this->assertSame('Moodle News', $feed->get_title());
a3d5830a 55
be14898f
PS
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());
a3d5830a 59
be14898f
PS
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());
a3d5830a
PS
66
67 $this->assertNotEmpty($items = $feed->get_items());
be14898f 68 $this->assertCount(15, $items);
a3d5830a
PS
69
70 $this->assertNotEmpty($itemone = $feed->get_item(0));
a3d5830a 71
be14898f
PS
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());
a3d5830a
PS
75 $description = <<<EOD
76by 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 />
78I'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 />
80We 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 />
82Google 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 />
84You 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>
85EOD;
d958e6bd 86 $description = purify_html($description);
be14898f 87 $this->assertSame($description, $itemone->get_description());
a3d5830a 88
be14898f
PS
89 // TODO fix this so it uses $CFG by default.
90 $this->assertSame(1196412453, $itemone->get_date('U'));
a3d5830a 91
be14898f 92 // Last item.
a3d5830a 93 $this->assertNotEmpty($feed->get_item(14));
be14898f 94 // Past last item.
a3d5830a
PS
95 $this->assertEmpty($feed->get_item(15));
96 }
97
98 /*
be14898f 99 * Test retrieving a url which doesn't exist.
a3d5830a 100 */
be14898f 101 public function test_failurl() {
abc4e1ef
DM
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);
a3d5830a
PS
108
109 $this->assertNotEmpty($feed->error());
110 }
111
112 /*
be14898f 113 * Test retrieving a url with broken proxy configuration.
a3d5830a 114 */
be14898f 115 public function test_failproxy() {
a3d5830a
PS
116 global $CFG;
117
118 $oldproxy = $CFG->proxyhost;
119 $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
120
a9d2f1b4 121 $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'));
a3d5830a
PS
122
123 $this->assertNotEmpty($feed->error());
124 $this->assertEmpty($feed->get_title());
125 $CFG->proxyhost = $oldproxy;
126 }
127
128 /*
be14898f 129 * Test retrieving a url which sends a redirect to another valid feed.
a3d5830a 130 */
be14898f 131 public function test_redirect() {
a9d2f1b4 132 $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT);
a3d5830a
PS
133
134 $this->assertNull($feed->error());
be14898f
PS
135 $this->assertSame('Moodle News', $feed->get_title());
136 $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
a3d5830a 137 }
a3d5830a 138}