Merge branch 'MDL-57604-32' of git://github.com/danpoltawski/moodle into MOODLE_32_STABLE
[moodle.git] / question / tests / events_test.php
blob357422e4e15fba8fc1a17e8fd3494619830e5c28
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.
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/>.
17 /**
18 * Events tests.
20 * @package core_question
21 * @copyright 2014 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/question/editlib.php');
30 require_once($CFG->dirroot . '/question/category_class.php');
32 class core_question_events_testcase extends advanced_testcase {
34 /**
35 * Tests set up.
37 public function setUp() {
38 $this->resetAfterTest();
41 /**
42 * Test the question category created event.
44 public function test_question_category_created() {
45 $this->setAdminUser();
46 $course = $this->getDataGenerator()->create_course();
47 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
49 $contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
51 $defaultcategoryobj = question_make_default_categories(array($contexts->lowest()));
52 $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
54 $qcobject = new question_category_object(
56 new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)),
57 $contexts->having_one_edit_tab_cap('categories'),
58 $defaultcategoryobj->id,
59 $defaultcategory,
60 null,
61 $contexts->having_cap('moodle/question:add'));
63 // Trigger and capture the event.
64 $sink = $this->redirectEvents();
65 $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true);
66 $events = $sink->get_events();
67 $event = reset($events);
69 // Check that the event data is valid.
70 $this->assertInstanceOf('\core\event\question_category_created', $event);
71 $this->assertEquals(context_module::instance($quiz->cmid), $event->get_context());
72 $expected = array($course->id, 'quiz', 'addcategory', 'view.php?id=' . $quiz->cmid , $categoryid, $quiz->cmid);
73 $this->assertEventLegacyLogData($expected, $event);
74 $this->assertEventContextNotUsed($event);