MDL-70288 qtype: Remove unnecessary empty label string
[moodle.git] / notes / tests / generator_test.php
blob6db5be66cea77b51ab21894df7c06a096006252a
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 * Generator tests.
20 * @package core_notes
21 * @copyright 2013 Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Generator tests class.
30 * @package core_notes
31 * @copyright 2013 Ankit Agarwal
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_notes_generator_testcase extends advanced_testcase {
36 /** Test create_instance method */
37 public function test_create_instance() {
38 global $DB;
39 $this->resetAfterTest();
40 $this->setAdminUser();
42 $course = $this->getDataGenerator()->create_course();
43 $user = $this->getDataGenerator()->create_user();
44 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
46 $this->assertFalse($DB->record_exists('post', array('courseid' => $course->id)));
47 $note = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
48 $this->assertEquals(1, $DB->count_records('post', array('courseid' => $course->id, 'userid' => $user->id)));
49 $this->assertTrue($DB->record_exists('post', array('id' => $note->id)));
51 $params = array('courseid' => $course->id, 'userid' => $user->id, 'publishstate' => NOTES_STATE_DRAFT);
52 $note = $gen->create_instance($params);
53 $this->assertEquals(2, $DB->count_records('post', array('courseid' => $course->id, 'userid' => $user->id)));
54 $this->assertEquals(NOTES_STATE_DRAFT, $DB->get_field_select('post', 'publishstate', 'id = :id',
55 array('id' => $note->id)));
58 /** Test Exceptions thrown by create_instance method */
59 public function test_create_instance_exceptions() {
60 $this->resetAfterTest();
62 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
64 // Test not setting userid.
65 try {
66 $gen->create_instance(array('courseid' => 2));
67 $this->fail('A note should not be allowed to be created without associcated userid');
68 } catch (coding_exception $e) {
69 $this->assertStringContainsString('Module generator requires $record->userid', $e->getMessage());
72 // Test not setting courseid.
73 try {
74 $gen->create_instance(array('userid' => 2));
75 $this->fail('A note should not be allowed to be created without associcated courseid');
76 } catch (coding_exception $e) {
77 $this->assertStringContainsString('Module generator requires $record->courseid', $e->getMessage());