Merge branch 'MDL-72646-font-mimetype-311' of https://github.com/doctorlard/moodle...
[moodle.git] / mod / survey / tests / generator_test.php
blob841c497db3ecc7ef358d3e045cd861bc15793709
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 namespace mod_survey;
19 /**
20 * Genarator tests class for mod_survey.
22 * @package mod_survey
23 * @category test
24 * @copyright 2013 Marina Glancy
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 class generator_test extends \advanced_testcase {
29 public function test_create_instance() {
30 global $DB;
31 $this->resetAfterTest();
32 $this->setAdminUser();
34 $course = $this->getDataGenerator()->create_course();
36 $this->assertFalse($DB->record_exists('survey', array('course' => $course->id)));
37 $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
38 $records = $DB->get_records('survey', array('course' => $course->id), 'id');
39 $this->assertEquals(1, count($records));
40 $this->assertTrue(array_key_exists($survey->id, $records));
42 $params = array('course' => $course->id, 'name' => 'Another survey');
43 $survey = $this->getDataGenerator()->create_module('survey', $params);
44 $records = $DB->get_records('survey', array('course' => $course->id), 'id');
45 $this->assertEquals(2, count($records));
46 $this->assertEquals('Another survey', $records[$survey->id]->name);
49 public function test_create_instance_with_template() {
50 global $DB;
51 $this->resetAfterTest();
52 $this->setAdminUser();
54 $course = $this->getDataGenerator()->create_course();
55 $templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'id, name');
56 $firsttemplateid = key($templates);
58 // By default survey is created with the first available template.
59 $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
60 $record = $DB->get_record('survey', array('id' => $survey->id));
61 $this->assertEquals($firsttemplateid, $record->template);
63 // Survey can be created specifying the template id.
64 $tmplid = array_search('ciqname', $templates);
65 $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
66 'template' => $tmplid));
67 $record = $DB->get_record('survey', array('id' => $survey->id));
68 $this->assertEquals($tmplid, $record->template);
70 // Survey can be created specifying the template name instead of id.
71 $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
72 'template' => 'collesaname'));
73 $record = $DB->get_record('survey', array('id' => $survey->id));
74 $this->assertEquals(array_search('collesaname', $templates), $record->template);
76 // Survey can not be created specifying non-existing template id or name.
77 try {
78 $this->getDataGenerator()->create_module('survey', array('course' => $course,
79 'template' => 87654));
80 $this->fail('Exception about non-existing numeric template is expected');
81 } catch (\Exception $e) {}
82 try {
83 $this->getDataGenerator()->create_module('survey', array('course' => $course,
84 'template' => 'nonexistingcode'));
85 $this->fail('Exception about non-existing string template is expected');
86 } catch (\Exception $e) {}