MDL-44273 block_rss: Back-off strategy for RSS feeds
[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();
26require_once(dirname(dirname(__DIR__)) . '/moodleblock.class.php');
27require_once(dirname(__DIR__) . '/block_rss_client.php');
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() {
43 global $DB;
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
58 $block = new block_rss_client();
59 ob_start();
60 $block->cron();
61 $cronoutput = ob_get_clean();
62 $this->assertContains('skipping until ' . userdate($record->skipuntil), $cronoutput);
63 $this->assertContains('0 feeds refreshed (took ', $cronoutput);
64 }
65
66 /**
67 * Test that when a feed has an error the skip time is increaed correctly.
68 */
69 public function test_error() {
70 global $DB;
71 $this->resetAfterTest();
72 $time = time();
73 // A record that has failed before.
74 $record = (object) array(
75 'userid' => 1,
76 'title' => 'Skip test feed',
77 'preferredtitle' => '',
78 'description' => 'A feed to test the skip time.',
79 'shared' => 0,
80 'url' => 'http://example.com/rss',
81 'skiptime' => 330,
82 'skipuntil' => $time - 300,
83 );
84 $record->id = $DB->insert_record('block_rss_client', $record);
85
86 // A record that has not failed before.
87 $record2 = (object) array(
88 'userid' => 1,
89 'title' => 'Skip test feed',
90 'preferredtitle' => '',
91 'description' => 'A feed to test the skip time.',
92 'shared' => 0,
93 'url' => 'http://example.com/rss2',
94 'skiptime' => 0,
95 'skipuntil' => 0,
96 );
97 $record2->id = $DB->insert_record('block_rss_client', $record2);
98
99 // A record that is near the maximum wait time.
100 $record3 = (object) array(
101 'userid' => 1,
102 'title' => 'Skip test feed',
103 'preferredtitle' => '',
104 'description' => 'A feed to test the skip time.',
105 'shared' => 0,
106 'url' => 'http://example.com/rss3',
107 'skiptime' => block_rss_client::CLIENT_MAX_SKIPTIME - 5,
108 'skipuntil' => $time - 1,
109 );
110 $record3->id = $DB->insert_record('block_rss_client', $record3);
111
112 // Run the cron.
113 $block = new block_rss_client();
114 ob_start();
115 $block->cron();
116 $cronoutput = ob_get_clean();
117 $skiptime1 = $record->skiptime * 2;
118 $message1 = 'http://example.com/rss Error: could not load/find the RSS feed - skipping for ' . $skiptime1 . ' seconds.';
119 $this->assertContains($message1, $cronoutput);
120 $skiptime2 = 330; // Assumes that the cron time in the version file is 300.
121 $message2 = 'http://example.com/rss2 Error: could not load/find the RSS feed - skipping for ' . $skiptime2 . ' seconds.';
122 $this->assertContains($message2, $cronoutput);
123 $skiptime3 = block_rss_client::CLIENT_MAX_SKIPTIME;
124 $message3 = 'http://example.com/rss3 Error: could not load/find the RSS feed - skipping for ' . $skiptime3 . ' seconds.';
125 $this->assertContains($message3, $cronoutput);
126 $this->assertContains('0 feeds refreshed (took ', $cronoutput);
127
128 // Test that the records have been correctly updated.
129 $newrecord = $DB->get_record('block_rss_client', array('id' => $record->id));
130 $this->assertAttributeEquals($skiptime1, 'skiptime', $newrecord);
131 $this->assertAttributeGreaterThanOrEqual($time + $skiptime1, 'skipuntil', $newrecord);
132 $newrecord2 = $DB->get_record('block_rss_client', array('id' => $record2->id));
133 $this->assertAttributeEquals($skiptime2, 'skiptime', $newrecord2);
134 $this->assertAttributeGreaterThanOrEqual($time + $skiptime2, 'skipuntil', $newrecord2);
135 $newrecord3 = $DB->get_record('block_rss_client', array('id' => $record3->id));
136 $this->assertAttributeEquals($skiptime3, 'skiptime', $newrecord3);
137 $this->assertAttributeGreaterThanOrEqual($time + $skiptime3, 'skipuntil', $newrecord3);
138 }
139}