/notes/tests/events_test.php

https://github.com/markn86/moodle · PHP · 178 lines · 96 code · 23 blank · 59 comment · 1 complexity · 48c44d2c071cb79be75960225747ea4b 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. * Tests for notes events.
  18. *
  19. * @package core_notes
  20. * @copyright 2013 Ankit Agarwal
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Class core_notes_events_testcase
  26. *
  27. * Class for tests related to notes events.
  28. *
  29. * @package core_notes
  30. * @copyright 2013 Ankit Agarwal
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  32. */
  33. class core_notes_events_testcase extends advanced_testcase {
  34. /** @var stdClass A note object. */
  35. private $eventnote;
  36. /** @var stdClass A complete record from post table */
  37. private $noterecord;
  38. public function setUp(): void {
  39. global $DB;
  40. $this->resetAfterTest();
  41. $this->setAdminUser();
  42. $course = $this->getDataGenerator()->create_course();
  43. $user = $this->getDataGenerator()->create_user();
  44. $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
  45. $this->eventnote = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
  46. // Get the full record, note_load doesn't return everything.
  47. $this->noterecord = $DB->get_record('post', array('id' => $this->eventnote->id), '*', MUST_EXIST);
  48. }
  49. /**
  50. * Tests for event note_deleted.
  51. */
  52. public function test_note_deleted_event() {
  53. // Delete a note.
  54. $sink = $this->redirectEvents();
  55. note_delete($this->eventnote);
  56. $events = $sink->get_events();
  57. $event = array_pop($events); // Delete note event.
  58. $sink->close();
  59. // Validate event data.
  60. $this->assertInstanceOf('\core\event\note_deleted', $event);
  61. $this->assertEquals($this->eventnote->id, $event->objectid);
  62. $this->assertEquals($this->eventnote->usermodified, $event->userid);
  63. $this->assertEquals($this->eventnote->userid, $event->relateduserid);
  64. $this->assertEquals('post', $event->objecttable);
  65. $this->assertEquals(null, $event->get_url());
  66. $this->assertEquals($this->noterecord, $event->get_record_snapshot('post', $event->objectid));
  67. $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
  68. // Test legacy data.
  69. $logurl = new \moodle_url('index.php',
  70. array('course' => $this->eventnote->courseid, 'user' => $this->eventnote->userid));
  71. $logurl->set_anchor('note-' . $this->eventnote->id);
  72. $arr = array($this->eventnote->courseid, 'notes', 'delete', $logurl, 'delete note');
  73. $this->assertEventLegacyLogData($arr, $event);
  74. $this->assertEventContextNotUsed($event);
  75. }
  76. /**
  77. * Tests for event note_created.
  78. */
  79. public function test_note_created_event() {
  80. // Delete a note.
  81. $sink = $this->redirectEvents();
  82. $note = clone $this->eventnote;
  83. unset($note->id);
  84. note_save($note);
  85. $events = $sink->get_events();
  86. $event = array_pop($events); // Delete note event.
  87. $sink->close();
  88. // Validate event data.
  89. $this->assertInstanceOf('\core\event\note_created', $event);
  90. $this->assertEquals($note->id, $event->objectid);
  91. $this->assertEquals($note->usermodified, $event->userid);
  92. $this->assertEquals($note->userid, $event->relateduserid);
  93. $this->assertEquals('post', $event->objecttable);
  94. $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
  95. // Test legacy data.
  96. $logurl = new \moodle_url('index.php',
  97. array('course' => $note->courseid, 'user' => $note->userid));
  98. $logurl->set_anchor('note-' . $note->id);
  99. $arr = array($note->courseid, 'notes', 'add', $logurl, 'add note');
  100. $this->assertEventLegacyLogData($arr, $event);
  101. $this->assertEventContextNotUsed($event);
  102. }
  103. /**
  104. * Tests for event note_updated.
  105. */
  106. public function test_note_updated_event() {
  107. // Delete a note.
  108. $sink = $this->redirectEvents();
  109. $note = clone $this->eventnote;
  110. $note->publishstate = NOTES_STATE_DRAFT;
  111. note_save($note);
  112. $events = $sink->get_events();
  113. $event = array_pop($events); // Delete note event.
  114. $sink->close();
  115. // Validate event data.
  116. $this->assertInstanceOf('\core\event\note_updated', $event);
  117. $this->assertEquals($note->id, $event->objectid);
  118. $this->assertEquals($note->usermodified, $event->userid);
  119. $this->assertEquals($note->userid, $event->relateduserid);
  120. $this->assertEquals('post', $event->objecttable);
  121. $this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']);
  122. // Test legacy data.
  123. $logurl = new \moodle_url('index.php',
  124. array('course' => $note->courseid, 'user' => $note->userid));
  125. $logurl->set_anchor('note-' . $note->id);
  126. $arr = array($note->courseid, 'notes', 'update', $logurl, 'update note');
  127. $this->assertEventLegacyLogData($arr, $event);
  128. $this->assertEventContextNotUsed($event);
  129. }
  130. /**
  131. * Test the notes viewed event.
  132. *
  133. * It's not possible to use the moodle API to simulate the viewing of notes, so here we
  134. * simply create the event and trigger it.
  135. */
  136. public function test_notes_viewed() {
  137. $coursecontext = context_course::instance($this->eventnote->courseid);
  138. // Trigger event for notes viewed.
  139. $event = \core\event\notes_viewed::create(array(
  140. 'context' => $coursecontext,
  141. 'relateduserid' => $this->eventnote->userid
  142. ));
  143. // Trigger and capture the event.
  144. $sink = $this->redirectEvents();
  145. $event->trigger();
  146. $events = $sink->get_events();
  147. $event = reset($events);
  148. $this->assertInstanceOf('\core\event\notes_viewed', $event);
  149. $this->assertEquals($coursecontext, $event->get_context());
  150. $expected = array($this->eventnote->courseid, 'notes', 'view', 'index.php?course=' .
  151. $this->eventnote->courseid.'&amp;user=' . $this->eventnote->userid, 'view notes');
  152. $this->assertEventLegacyLogData($expected, $event);
  153. $this->assertEventContextNotUsed($event);
  154. }
  155. }