/mod/forum/tests/cron_trait.php

https://github.com/markn86/moodle · PHP · 129 lines · 73 code · 11 blank · 45 comment · 16 complexity · 6934932dbe89f62352fcf8796cdcff11 MD5 · raw file

  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. * The forum module cron trait.
  18. *
  19. * @package mod_forum
  20. * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. trait mod_forum_tests_cron_trait {
  25. /**
  26. * Run the main cron task to queue all tasks, and ensure that posts
  27. * were sent to the correct users.
  28. *
  29. * @param \stdClass[] $expectations The list of users, along with their expected count of messages and digests.
  30. */
  31. protected function queue_tasks_and_assert($expectations = []) {
  32. global $DB;
  33. // Note, we cannot use expectOutputRegex because it only allows for a single RegExp.
  34. ob_start();
  35. cron_setup_user();
  36. $cron = new \mod_forum\task\cron_task();
  37. $cron->execute();
  38. $output = ob_get_contents();
  39. ob_end_clean();
  40. $uniqueusers = 0;
  41. foreach ($expectations as $expect) {
  42. $expect->digests = isset($expect->digests) ? $expect->digests : 0;
  43. $expect->messages = isset($expect->messages) ? $expect->messages : 0;
  44. $expect->mentioned = isset($expect->mentioned) ? $expect->mentioned : false;
  45. if ($expect->digests || $expect->messages) {
  46. $expect->mentioned = true;
  47. }
  48. if (!$expect->mentioned) {
  49. $this->assertDoesNotMatchRegularExpression("/Queued 0 for {$expect->userid}/", $output);
  50. } else {
  51. $uniqueusers++;
  52. $this->assertMatchesRegularExpression(
  53. "/Queued {$expect->digests} digests and {$expect->messages} messages for {$expect->userid}/",
  54. $output
  55. );
  56. }
  57. }
  58. if (empty($expectations)) {
  59. $this->assertMatchesRegularExpression("/No posts found./", $output);
  60. } else {
  61. $this->assertMatchesRegularExpression("/Unique users: {$uniqueusers}/", $output);
  62. }
  63. // Update the forum queue for digests.
  64. $DB->execute("UPDATE {forum_queue} SET timemodified = timemodified - 1");
  65. }
  66. /**
  67. * Run any send_user_notifications tasks for the specified user, and
  68. * ensure that the posts specified were sent.
  69. *
  70. * @param \stdClass $user
  71. * @param \stdClass[] $posts
  72. * @param bool $ignoreemptyposts
  73. */
  74. protected function send_notifications_and_assert($user, $posts = [], $ignoreemptyposts = false) {
  75. ob_start();
  76. $this->runAdhocTasks(\mod_forum\task\send_user_notifications::class, $user->id);
  77. $output = ob_get_contents();
  78. ob_end_clean();
  79. if (empty($posts) && !$ignoreemptyposts) {
  80. $this->assertEquals('', $output);
  81. } else {
  82. $this->assertMatchesRegularExpression("/Sending messages to {$user->username}/", $output);
  83. foreach ($posts as $post) {
  84. $this->assertMatchesRegularExpression("/Post {$post->id} sent/", $output);
  85. }
  86. $count = count($posts);
  87. $this->assertMatchesRegularExpression("/Sent {$count} messages with 0 failures/", $output);
  88. }
  89. }
  90. /**
  91. * Run any send_user_digests tasks for the specified user, and
  92. * ensure that the posts specified were sent.
  93. *
  94. * @param \stdClass $user
  95. * @param \stdClass[] $fullposts
  96. * @param \stdClass[] $shortposts
  97. */
  98. protected function send_digests_and_assert($user, $fullposts = [], $shortposts = []) {
  99. ob_start();
  100. $this->runAdhocTasks(\mod_forum\task\send_user_digests::class, $user->id);
  101. $output = ob_get_contents();
  102. ob_end_clean();
  103. if (empty($shortposts) && empty($fullposts)) {
  104. $this->assertEquals('', $output);
  105. $this->assertMatchesRegularExpression("/Digest sent with 0 messages./", $output);
  106. } else {
  107. $this->assertMatchesRegularExpression("/Sending forum digests for {$user->username}/", $output);
  108. foreach ($fullposts as $post) {
  109. $this->assertMatchesRegularExpression("/Adding post {$post->id} in format 1/", $output);
  110. }
  111. foreach ($shortposts as $post) {
  112. $this->assertMatchesRegularExpression("/Adding post {$post->id} in format 2/", $output);
  113. }
  114. $count = count($fullposts) + count($shortposts);
  115. $this->assertMatchesRegularExpression("/Digest sent with {$count} messages./", $output);
  116. }
  117. }
  118. }