Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Performance test for questionnaire module.
19
 * @package mod_questionnaire
20
 * @group mod_questionnaire
21
 * @author     Guy Thomas
22
 * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
class mod_questionnaire_csvexport_test extends advanced_testcase {
26
 
27
    public function setUp(): void {
28
        global $CFG;
29
 
30
        require_once($CFG->dirroot.'/lib/testing/generator/data_generator.php');
31
        require_once($CFG->dirroot.'/lib/testing/generator/component_generator_base.php');
32
        require_once($CFG->dirroot.'/lib/testing/generator/module_generator.php');
33
    }
34
 
35
    /**
36
     * Get csv text
37
     *
38
     * @param array $rows
39
     * @return string
40
     */
41
    private function get_csv_text(array $rows) {
42
        $lines = [];
43
        foreach ($rows as $row) {
44
            // Remove the id and date fields.
45
            unset($row[0]);
46
            unset($row[1]);
47
            unset($row[6]);
48
            $text = implode("\t", $row);
49
            $lines[] = $text;
50
        }
51
        return $lines;
52
    }
53
 
54
    public function test_csvexport() {
55
        $this->resetAfterTest();
56
        $dg = $this->getDataGenerator();
57
        $qdg = $dg->get_plugin_generator('mod_questionnaire');
58
        $qdg->create_and_fully_populate(1, 5, 1, 1);
59
 
60
        // The following line simply.
61
        $questionnaires = $qdg->questionnaires();
62
        foreach ($questionnaires as $questionnaire) {
63
            list ($course, $cm) = get_course_and_cm_from_instance($questionnaire->id, 'questionnaire', $questionnaire->course);
64
            $questionnaireinst = new questionnaire($course, $cm, 0, $questionnaire);
65
 
66
            // Test for only complete responses.
67
            $newoutput = $this->get_csv_text($questionnaireinst->generate_csv(0, '', '', 0, 0, 0));
68
            $this->assertEquals(count($newoutput), count($this->expected_complete_output()));
69
            foreach ($newoutput as $key => $output) {
70
                $this->assertEquals($this->expected_complete_output()[$key], $output);
71
            }
72
 
73
            // Test for all responses.
74
            $newoutput = $this->get_csv_text($questionnaireinst->generate_csv(0, '', '', 0, 0, 1));
75
            $this->assertEquals(count($newoutput), count($this->expected_incomplete_output()));
76
            foreach ($newoutput as $key => $output) {
77
                $this->assertEquals($this->expected_incomplete_output()[$key], $output);
78
            }
79
        }
80
    }
81
 
82
    /**
83
     * Return the expected output.
84
     * @return string[]
85
     */
86
    private function expected_complete_output() {
87
        return ["Institution	Department	Course	Group	Full name	Username	Q01_Text Box 1000	Q02_Essay Box 1002	" .
88
            "Q03_Numeric 1004	Q04_Date 1006	Q05_Radio Buttons 1008	Q06_Drop Down 1010	Q07_Check Boxes 1012->four	" .
89
            "Q07_Check Boxes 1012->five	Q07_Check Boxes 1012->six	Q07_Check Boxes 1012->seven	Q07_Check Boxes 1012->eight	" .
90
            "Q07_Check Boxes 1012->nine	Q07_Check Boxes 1012->ten	Q07_Check Boxes 1012->eleven	" .
91
            "Q07_Check Boxes 1012->twelve	Q07_Check Boxes 1012->thirteen	Q08_Rate Scale 1014->fourteen	" .
92
            "Q08_Rate Scale 1014->fifteen	Q08_Rate Scale 1014->sixteen	Q08_Rate Scale 1014->seventeen	" .
93
            "Q08_Rate Scale 1014->eighteen	Q08_Rate Scale 1014->nineteen	Q08_Rate Scale 1014->twenty	" .
94
            "Q08_Rate Scale 1014->happy	Q08_Rate Scale 1014->sad	Q08_Rate Scale 1014->jealous	Q09_Slider 1016",
95
            "		Test course 1		Testy Lastname1	username1	Test answer	Some header textSome paragraph text	83	" .
96
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
97
            "		Test course 1		Testy Lastname2	username2	Test answer	Some header textSome paragraph text	83	" .
98
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
99
            "		Test course 1		Testy Lastname3	username3	Test answer	Some header textSome paragraph text	83	" .
100
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
101
            "		Test course 1		Testy Lastname4	username4	Test answer	Some header textSome paragraph text	83	" .
102
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5"];
103
    }
104
 
105
    /**
106
     * Return the exepected incomplete output.
107
     * @return string[]
108
     */
109
    private function expected_incomplete_output() {
110
        return ["Institution	Department	Course	Group	Full name	Username	Complete	Q01_Text Box 1000	" .
111
            "Q02_Essay Box 1002	" .
112
            "Q03_Numeric 1004	Q04_Date 1006	Q05_Radio Buttons 1008	Q06_Drop Down 1010	Q07_Check Boxes 1012->four	" .
113
            "Q07_Check Boxes 1012->five	Q07_Check Boxes 1012->six	Q07_Check Boxes 1012->seven	Q07_Check Boxes 1012->eight	" .
114
            "Q07_Check Boxes 1012->nine	Q07_Check Boxes 1012->ten	Q07_Check Boxes 1012->eleven	" .
115
            "Q07_Check Boxes 1012->twelve	Q07_Check Boxes 1012->thirteen	Q08_Rate Scale 1014->fourteen	" .
116
            "Q08_Rate Scale 1014->fifteen	Q08_Rate Scale 1014->sixteen	Q08_Rate Scale 1014->seventeen	" .
117
            "Q08_Rate Scale 1014->eighteen	Q08_Rate Scale 1014->nineteen	Q08_Rate Scale 1014->twenty	" .
118
            "Q08_Rate Scale 1014->happy	Q08_Rate Scale 1014->sad	Q08_Rate Scale 1014->jealous	Q09_Slider 1016",
119
            "		Test course 1		Testy Lastname1	username1	y	Test answer	Some header textSome paragraph text	83	" .
120
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
121
            "		Test course 1		Testy Lastname2	username2	y	Test answer	Some header textSome paragraph text	83	" .
122
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
123
            "		Test course 1		Testy Lastname3	username3	y	Test answer	Some header textSome paragraph text	83	" .
124
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
125
            "		Test course 1		Testy Lastname4	username4	y	Test answer	Some header textSome paragraph text	83	" .
126
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5",
127
            "		Test course 1		Testy Lastname5	username5	n	Test answer	Some header textSome paragraph text	83	" .
128
            "27/12/2017	wind	three	0	0	0	0	0	0	0	0	0	1	1	2	3	4	5	1	2	3	4		5"];
129
    }
130
}