Пример #1
0
 public function test_category_created()
 {
     global $DB;
     // There is no proper API to call to trigger this event, so what we are
     // doing here is simply making sure that the events returns the right information.
     $this->setAdminUser();
     $course = $this->getDataGenerator()->create_course();
     $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
     $context = context_module::instance($glossary->cmid);
     // Create category and trigger event.
     $category = new stdClass();
     $category->name = 'New category';
     $category->usedynalink = 0;
     $category->id = $DB->insert_record('glossary_categories', $category);
     $event = \mod_glossary\event\category_created::create(array('context' => $context, 'objectid' => $category->id));
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_created', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
     $expected = array($course->id, "glossary", "add category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     // Update category and trigger event.
     $category->name = 'Updated category';
     $DB->update_record('glossary_categories', $category);
     $event = \mod_glossary\event\category_updated::create(array('context' => $context, 'objectid' => $category->id));
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_updated', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
     $expected = array($course->id, "glossary", "edit category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     // Delete category and trigger event.
     $category = $DB->get_record('glossary_categories', array('id' => $category->id));
     $DB->delete_records('glossary_categories', array('id' => $category->id));
     $event = \mod_glossary\event\category_deleted::create(array('context' => $context, 'objectid' => $category->id));
     $event->add_record_snapshot('glossary_categories', $category);
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_deleted', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
     $expected = array($course->id, "glossary", "delete category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
 }
         \mod_glossary\local\concept_cache::reset_glossary($glossary);
     } else {
         echo $OUTPUT->header();
         echo $OUTPUT->heading(format_string($glossary->name), 2);
         echo $OUTPUT->heading(format_string(get_string("edit") . " " . get_string("category", "glossary")), 3);
         $name = $category->name;
         $usedynalink = $category->usedynalink;
         require "editcategories.html";
         echo $OUTPUT->footer();
         die;
     }
 } elseif ($action == "delete") {
     if ($confirm) {
         $DB->delete_records("glossary_entries_categories", array("categoryid" => $hook));
         $DB->delete_records("glossary_categories", array("id" => $hook));
         $event = \mod_glossary\event\category_deleted::create(array('context' => $context, 'objectid' => $hook));
         $event->add_record_snapshot('glossary_categories', $category);
         $event->add_record_snapshot('glossary', $glossary);
         $event->trigger();
         // Reset caches.
         \mod_glossary\local\concept_cache::reset_glossary($glossary);
         redirect("editcategories.php?id={$cm->id}", get_string("categorydeleted", "glossary"), 2);
     } else {
         echo $OUTPUT->header();
         echo $OUTPUT->heading(format_string($glossary->name), 2);
         echo $OUTPUT->heading(format_string(get_string("delete") . " " . get_string("category", "glossary")), 3);
         echo $OUTPUT->box_start('generalbox boxaligncenter errorboxcontent boxwidthnarrow');
         echo "<div class=\"boxaligncenter deletecatconfirm\">" . format_string($category->name, true, $fmtoptions) . "<br/>";
         $num_entries = $DB->count_records("glossary_entries_categories", array("categoryid" => $category->id));
         if ($num_entries) {
             print_string("deletingnoneemptycategory", "glossary");