Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace mod_survey;
18
 
19
/**
20
 * Genarator tests class for mod_survey.
21
 *
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
26
 */
27
class generator_test extends \advanced_testcase {
28
 
29
    /**
30
     * Setup testcase.
31
     */
32
    public function setUp(): void {
33
        // Survey module is disabled by default, enable it for testing.
34
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
35
        $manager::enable_plugin('survey', 1);
36
    }
37
 
11 efrain 38
    public function test_create_instance(): void {
1 efrain 39
        global $DB;
40
        $this->resetAfterTest();
41
        $this->setAdminUser();
42
 
43
        $course = $this->getDataGenerator()->create_course();
44
 
45
        $this->assertFalse($DB->record_exists('survey', array('course' => $course->id)));
46
        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
47
        $records = $DB->get_records('survey', array('course' => $course->id), 'id');
48
        $this->assertEquals(1, count($records));
49
        $this->assertTrue(array_key_exists($survey->id, $records));
50
 
51
        $params = array('course' => $course->id, 'name' => 'Another survey');
52
        $survey = $this->getDataGenerator()->create_module('survey', $params);
53
        $records = $DB->get_records('survey', array('course' => $course->id), 'id');
54
        $this->assertEquals(2, count($records));
55
        $this->assertEquals('Another survey', $records[$survey->id]->name);
56
    }
57
 
11 efrain 58
    public function test_create_instance_with_template(): void {
1 efrain 59
        global $DB;
60
        $this->resetAfterTest();
61
        $this->setAdminUser();
62
 
63
        $course = $this->getDataGenerator()->create_course();
64
        $templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'id, name');
65
        $firsttemplateid = key($templates);
66
 
67
        // By default survey is created with the first available template.
68
        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
69
        $record = $DB->get_record('survey', array('id' => $survey->id));
70
        $this->assertEquals($firsttemplateid, $record->template);
71
 
72
        // Survey can be created specifying the template id.
73
        $tmplid = array_search('ciqname', $templates);
74
        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
75
            'template' => $tmplid));
76
        $record = $DB->get_record('survey', array('id' => $survey->id));
77
        $this->assertEquals($tmplid, $record->template);
78
 
79
        // Survey can be created specifying the template name instead of id.
80
        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
81
            'template' => 'collesaname'));
82
        $record = $DB->get_record('survey', array('id' => $survey->id));
83
        $this->assertEquals(array_search('collesaname', $templates), $record->template);
84
 
85
        // Survey can not be created specifying non-existing template id or name.
86
        try {
87
            $this->getDataGenerator()->create_module('survey', array('course' => $course,
88
                'template' => 87654));
89
            $this->fail('Exception about non-existing numeric template is expected');
90
        } catch (\Exception $e) {}
91
        try {
92
            $this->getDataGenerator()->create_module('survey', array('course' => $course,
93
                'template' => 'nonexistingcode'));
94
            $this->fail('Exception about non-existing string template is expected');
95
        } catch (\Exception $e) {}
96
    }
97
}