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