cron_test.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * PHPunit tests for rss client cron.
  18. *
  19. * @package block_rss_client
  20. * @copyright 2015 University of Nottingham
  21. * @author Neill Magill <neill.magill@nottingham.ac.uk>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once(__DIR__ . '/../../moodleblock.class.php');
  26. require_once(__DIR__ . '/../block_rss_client.php');
  27. /**
  28. * Class for the PHPunit tests for rss client cron.
  29. *
  30. * @package block_rss_client
  31. * @copyright 2015 Universit of Nottingham
  32. * @author Neill Magill <neill.magill@nottingham.ac.uk>
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class block_rss_client_cron_testcase extends advanced_testcase {
  36. /**
  37. * Test that when a record has a skipuntil time that is greater
  38. * than the current time the attempt is skipped.
  39. */
  40. public function test_skip() {
  41. global $DB, $CFG;
  42. $this->resetAfterTest();
  43. // Create a RSS feed record with a skip until time set to the future.
  44. $record = (object) array(
  45. 'userid' => 1,
  46. 'title' => 'Skip test feed',
  47. 'preferredtitle' => '',
  48. 'description' => 'A feed to test the skip time.',
  49. 'shared' => 0,
  50. 'url' => 'http://example.com/rss',
  51. 'skiptime' => 330,
  52. 'skipuntil' => time() + 300,
  53. );
  54. $DB->insert_record('block_rss_client', $record);
  55. $block = new block_rss_client();
  56. ob_start();
  57. // Silence SimplePie php notices.
  58. $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
  59. $block->cron();
  60. error_reporting($errorlevel);
  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. * Test that when a feed has an error the skip time is increaed correctly.
  67. */
  68. public function test_error() {
  69. global $DB, $CFG;
  70. $this->resetAfterTest();
  71. $time = time();
  72. // A record that has failed before.
  73. $record = (object) array(
  74. 'userid' => 1,
  75. 'title' => 'Skip test feed',
  76. 'preferredtitle' => '',
  77. 'description' => 'A feed to test the skip time.',
  78. 'shared' => 0,
  79. 'url' => 'http://example.com/rss',
  80. 'skiptime' => 330,
  81. 'skipuntil' => $time - 300,
  82. );
  83. $record->id = $DB->insert_record('block_rss_client', $record);
  84. // A record that has not failed before.
  85. $record2 = (object) array(
  86. 'userid' => 1,
  87. 'title' => 'Skip test feed',
  88. 'preferredtitle' => '',
  89. 'description' => 'A feed to test the skip time.',
  90. 'shared' => 0,
  91. 'url' => 'http://example.com/rss2',
  92. 'skiptime' => 0,
  93. 'skipuntil' => 0,
  94. );
  95. $record2->id = $DB->insert_record('block_rss_client', $record2);
  96. // A record that is near the maximum wait time.
  97. $record3 = (object) array(
  98. 'userid' => 1,
  99. 'title' => 'Skip test feed',
  100. 'preferredtitle' => '',
  101. 'description' => 'A feed to test the skip time.',
  102. 'shared' => 0,
  103. 'url' => 'http://example.com/rss3',
  104. 'skiptime' => block_rss_client::CLIENT_MAX_SKIPTIME - 5,
  105. 'skipuntil' => $time - 1,
  106. );
  107. $record3->id = $DB->insert_record('block_rss_client', $record3);
  108. // Run the cron.
  109. $block = new block_rss_client();
  110. ob_start();
  111. // Silence SimplePie php notices.
  112. $errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
  113. $block->cron();
  114. error_reporting($errorlevel);
  115. $cronoutput = ob_get_clean();
  116. $skiptime1 = $record->skiptime * 2;
  117. $message1 = 'http://example.com/rss Error: could not load/find the RSS feed - skipping for ' . $skiptime1 . ' seconds.';
  118. $this->assertContains($message1, $cronoutput);
  119. $skiptime2 = 330; // Assumes that the cron time in the version file is 300.
  120. $message2 = 'http://example.com/rss2 Error: could not load/find the RSS feed - skipping for ' . $skiptime2 . ' seconds.';
  121. $this->assertContains($message2, $cronoutput);
  122. $skiptime3 = block_rss_client::CLIENT_MAX_SKIPTIME;
  123. $message3 = 'http://example.com/rss3 Error: could not load/find the RSS feed - skipping for ' . $skiptime3 . ' seconds.';
  124. $this->assertContains($message3, $cronoutput);
  125. $this->assertContains('0 feeds refreshed (took ', $cronoutput);
  126. // Test that the records have been correctly updated.
  127. $newrecord = $DB->get_record('block_rss_client', array('id' => $record->id));
  128. $this->assertAttributeEquals($skiptime1, 'skiptime', $newrecord);
  129. $this->assertAttributeGreaterThanOrEqual($time + $skiptime1, 'skipuntil', $newrecord);
  130. $newrecord2 = $DB->get_record('block_rss_client', array('id' => $record2->id));
  131. $this->assertAttributeEquals($skiptime2, 'skiptime', $newrecord2);
  132. $this->assertAttributeGreaterThanOrEqual($time + $skiptime2, 'skipuntil', $newrecord2);
  133. $newrecord3 = $DB->get_record('block_rss_client', array('id' => $record3->id));
  134. $this->assertAttributeEquals($skiptime3, 'skiptime', $newrecord3);
  135. $this->assertAttributeGreaterThanOrEqual($time + $skiptime3, 'skipuntil', $newrecord3);
  136. }
  137. }