Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1441 ariadna 39
final class rsslib_test extends \advanced_testcase {
1 efrain 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 {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        \moodle_simplepie::reset_cache();
47
    }
48
 
11 efrain 49
    public function test_getfeed(): void {
1 efrain 50
        $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'), self::TIMEOUT);
51
 
52
        $this->assertInstanceOf('\moodle_simplepie', $feed);
53
 
54
        $this->assertNull($feed->error(), "Failed to load the sample RSS file. Please check your proxy settings in Moodle. %s");
55
 
56
        $this->assertSame('Moodle News', $feed->get_title());
57
 
58
        $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
59
        $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.",
60
            $feed->get_description());
61
 
62
        $this->assertSame('&amp;#169; 2007 moodle', $feed->get_copyright());
63
        $this->assertSame('http://moodle.org/pix/i/rsssitelogo.gif', $feed->get_image_url());
64
        $this->assertSame('moodle', $feed->get_image_title());
65
        $this->assertSame('http://moodle.org/', $feed->get_image_link());
66
        $this->assertEquals('140', $feed->get_image_width());
67
        $this->assertEquals('35', $feed->get_image_height());
68
 
69
        $this->assertNotEmpty($items = $feed->get_items());
70
        $this->assertCount(15, $items);
71
 
72
        $this->assertNotEmpty($itemone = $feed->get_item(0));
73
 
74
        $this->assertSame('Google HOP contest encourages pre-University students to work on Moodle', $itemone->get_title());
75
        $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_link());
76
        $this->assertSame('http://moodle.org/mod/forum/discuss.php?d=85629', $itemone->get_id());
77
        $description = <<<EOD
78
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 />
79
<br />
80
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 />
81
<br />
82
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 />
83
<br />
84
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 />
85
<br />
86
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>
87
EOD;
88
        $description = purify_html($description);
89
        $this->assertSame($description, $itemone->get_description());
90
 
91
        // TODO fix this so it uses $CFG by default.
92
        $this->assertSame(1196412453, $itemone->get_date('U'));
93
 
94
        // Last item.
95
        $this->assertNotEmpty($feed->get_item(14));
96
        // Past last item.
97
        $this->assertEmpty($feed->get_item(15));
98
    }
99
 
100
    /*
101
     * Test retrieving a url which doesn't exist.
102
     */
11 efrain 103
    public function test_failurl(): void {
1 efrain 104
        global $CFG;
105
 
106
        // We do not want this in php error log.
107
        $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
108
        $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest-which-doesnt-exist.xml'), self::TIMEOUT);
109
        error_reporting($errorlevel);
110
 
111
        $this->assertNotEmpty($feed->error());
112
    }
113
 
114
    /*
115
     * Test retrieving a url with broken proxy configuration.
116
     */
11 efrain 117
    public function test_failproxy(): void {
1 efrain 118
        global $CFG;
119
 
120
        $oldproxy = $CFG->proxyhost;
121
        $CFG->proxyhost = 'xxxxxxxxxxxxxxx.moodle.org';
122
 
123
        $oldproxybypass = $CFG->proxybypass; // Ensure we don't get locally served extests bypassing the proxy.
124
        $CFG->proxybypass = '';
125
 
126
        $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rsstest.xml'));
127
 
128
        $this->assertNotEmpty($feed->error());
129
        $this->assertEmpty($feed->get_title());
130
        $CFG->proxyhost = $oldproxy;
131
        $CFG->proxybypass = $oldproxybypass;
132
    }
133
 
134
    /*
135
     * Test retrieving a url which sends a redirect to another valid feed.
136
     */
11 efrain 137
    public function test_redirect(): void {
1 efrain 138
        $feed = new \moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT);
139
 
140
        $this->assertNull($feed->error());
141
        $this->assertSame('Moodle News', $feed->get_title());
142
        $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link());
143
    }
144
 
145
    /**
146
     * Test that we can get the right user ID based on the provided private key (token).
147
     *
148
     * @covers ::rss_get_userid_from_token
149
     */
11 efrain 150
    public function test_rss_get_userid_from_token(): void {
1 efrain 151
        global $USER;
152
 
153
        $this->resetAfterTest();
154
        $this->setGuestUser();
155
 
156
        $key = rss_get_token($USER->id);
157
        $this->assertSame(rss_get_userid_from_token($key), $USER->id);
158
    }
159
}