weekly release 4.0dev
[moodle.git] / blocks / rss_client / tests / cron_test.php
CommitLineData
83f21585
NM
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 * PHPunit tests for rss client cron.
19 *
20 * @package block_rss_client
21 * @copyright 2015 University of Nottingham
22 * @author Neill Magill <neill.magill@nottingham.ac.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25defined('MOODLE_INTERNAL') || die();
1fcf0ca8
RS
26require_once(__DIR__ . '/../../moodleblock.class.php');
27require_once(__DIR__ . '/../block_rss_client.php');
83f21585
NM
28
29/**
30 * Class for the PHPunit tests for rss client cron.
31 *
32 * @package block_rss_client
33 * @copyright 2015 Universit of Nottingham
34 * @author Neill Magill <neill.magill@nottingham.ac.uk>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class block_rss_client_cron_testcase extends advanced_testcase {
38 /**
39 * Test that when a record has a skipuntil time that is greater
40 * than the current time the attempt is skipped.
41 */
42 public function test_skip() {
abc4e1ef 43 global $DB, $CFG;
83f21585
NM
44 $this->resetAfterTest();
45 // Create a RSS feed record with a skip until time set to the future.
46 $record = (object) array(
47 'userid' => 1,
48 'title' => 'Skip test feed',
49 'preferredtitle' => '',
50 'description' => 'A feed to test the skip time.',
51 'shared' => 0,
52 'url' => 'http://example.com/rss',
53 'skiptime' => 330,
54 'skipuntil' => time() + 300,
55 );
56 $DB->insert_record('block_rss_client', $record);
57
fad9dc92 58 $task = new \block_rss_client\task\refreshfeeds();
83f21585 59 ob_start();
abc4e1ef 60
6573ba3e 61 // Silence SimplePie php notices.
abc4e1ef 62 $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
fad9dc92 63 $task->execute();
abc4e1ef
DM
64 error_reporting($errorlevel);
65
83f21585 66 $cronoutput = ob_get_clean();
40de097e
EL
67 $this->assertStringContainsString('skipping until ' . userdate($record->skipuntil), $cronoutput);
68 $this->assertStringContainsString('0 feeds refreshed (took ', $cronoutput);
83f21585
NM
69 }
70
111c6a56
AN
71 /**
72 * Data provider for skip time tests.
73 *
74 * @return array
75 */
76 public function skip_time_increase_provider() : array {
77 return [
78 'Never failed' => [
79 'skiptime' => 0,
80 'skipuntil' => 0,
81 'newvalue' => MINSECS * 5,
82 ],
83 'Failed before' => [
84 // This should just double the time.
85 'skiptime' => 330,
86 'skipuntil' => time(),
87 'newvalue' => 660,
88 ],
89 'Near max' => [
90 'skiptime' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME - 5,
91 'skipuntil' => time(),
92 'newvalue' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME,
93 ],
94 ];
95 }
96
83f21585 97 /**
fad9dc92 98 * Test that when a feed has an error the skip time is increased correctly.
111c6a56
AN
99 *
100 * @dataProvider skip_time_increase_provider
83f21585 101 */
111c6a56 102 public function test_error($skiptime, $skipuntil, $newvalue) {
abc4e1ef 103 global $DB, $CFG;
83f21585 104 $this->resetAfterTest();
111c6a56
AN
105
106 require_once("{$CFG->libdir}/simplepie/moodle_simplepie.php");
107
83f21585
NM
108 $time = time();
109 // A record that has failed before.
111c6a56 110 $record = (object) [
83f21585
NM
111 'userid' => 1,
112 'title' => 'Skip test feed',
113 'preferredtitle' => '',
114 'description' => 'A feed to test the skip time.',
115 'shared' => 0,
116 'url' => 'http://example.com/rss',
111c6a56
AN
117 'skiptime' => $skiptime,
118 'skipuntil' => $skipuntil,
119 ];
83f21585
NM
120 $record->id = $DB->insert_record('block_rss_client', $record);
121
111c6a56
AN
122 // Run the scheduled task and have it fail.
123 $task = $this->getMockBuilder(\block_rss_client\task\refreshfeeds::class)
81407f18 124 ->onlyMethods(['fetch_feed'])
111c6a56 125 ->getMock();
83f21585 126
111c6a56 127 $piemock = $this->getMockBuilder(\moodle_simplepie::class)
81407f18 128 ->onlyMethods(['error'])
111c6a56 129 ->getMock();
83f21585 130
111c6a56
AN
131 $piemock->method('error')
132 ->willReturn(true);
abc4e1ef 133
111c6a56
AN
134 $task->method('fetch_feed')
135 ->willReturn($piemock);
abc4e1ef 136
111c6a56
AN
137 // Run the cron and capture its output.
138 $this->expectOutputRegex("/.*Error: could not load\/find the RSS feed - skipping for {$newvalue} seconds.*/");
139 $task->execute();
83f21585 140 }
6573ba3e 141}