Proyectos de Subversion Moodle

Rev

Rev 11 | | 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 core;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once("$CFG->libdir/graphlib.php");
23
 
24
/**
25
 * Tests for Graphlib.
26
 *
27
 * @coversDefaultClass \graph
28
 * @package    core
29
 * @copyright  2023 Meirza (meirza.arson@moodle.com)
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class graphlib_test extends \basic_testcase {
1 efrain 33
 
34
    /**
35
     * Data provider for test_graphlib.
36
     *
37
     * @return array
38
     */
1441 ariadna 39
    public static function create_data(): array {
1 efrain 40
        return [
41
            'data' =>
42
            [
43
                'mock' => [
44
                    'survey_name' => 'Survey 101',
45
                    'names' => [
1441 ariadna 46
                        'Relevance',
47
                        'Reflective thinking',
48
                        'Interactivity',
49
                        'Tutor support',
50
                        'Peer support',
51
                        'Interpretation',
1 efrain 52
                    ],
53
                    'buckets1' => [
1441 ariadna 54
                        0.75,
55
                        2.5,
56
                        1.5,
57
                        1.5,
58
                        2.5,
59
                        2.5,
1 efrain 60
                    ],
61
                    'stractual' => 'Actual',
62
                    'buckets2' => [
1441 ariadna 63
                        -1,
64
                        -1,
65
                        -1,
66
                        -1,
67
                        -1,
68
                        -1,
1 efrain 69
                    ],
70
                    'strpreferred' => 'Preferred',
1441 ariadna 71
                    'strimagine' => 'Imagine',
72
                    'buckets3' => [
73
                        1,
74
                        2.75,
75
                        0.5,
76
                        3.5,
77
                        1.25,
78
                        3,
79
                    ],
1 efrain 80
                    'stdev1' => [
1441 ariadna 81
                        0.82915619758885,
82
                        1.1180339887499,
83
                        1.1180339887499,
84
                        1.1180339887499,
85
                        1.1180339887499,
86
                        1.1180339887499,
1 efrain 87
                    ],
88
                    'stdev2' => [
1441 ariadna 89
                        0,
90
                        0,
91
                        0,
92
                        0,
93
                        0,
94
                        0,
1 efrain 95
                    ],
1441 ariadna 96
                    'stdev3' => [
97
                        0.92915619758885,
98
                        2.1180339887499,
99
                        2.1180339887499,
100
                        2.1180339887499,
101
                        2.1180339887499,
102
                        2.1180339887499,
103
                    ],
1 efrain 104
                    'options' => [
1441 ariadna 105
                        'Almost never',
106
                        'Seldom',
107
                        'Sometimes',
108
                        'Often',
109
                        'Almost always',
1 efrain 110
                    ],
111
                    'maxbuckets1' => 2.5,
1441 ariadna 112
                    'maxbuckets2' => -1,
113
                    'maxbuckets3' => 3.5,
1 efrain 114
                ]
115
            ]
116
        ];
117
    }
118
 
119
    /**
120
     * Test graphlib.
121
     *
122
     * @dataProvider create_data
123
     * @covers ::output
124
     * @param array $mock
125
     * @return void
126
     */
11 efrain 127
    public function test_graphlib($mock): void {
1 efrain 128
        $graph = new \graph(300, 200);
129
        ob_start();
130
        $graph->parameter['title'] = strip_tags(format_string($mock['survey_name'], true));
1441 ariadna 131
        $graph->parameter['zero_axis'] = 'black';
1 efrain 132
        $graph->x_data = $mock['names'];
133
        $graph->y_data['answers1'] = $mock['buckets1'];
1441 ariadna 134
        $graph->y_format['answers1'] = [
135
            'colour' => 'ltblue',
136
            'line' => 'line',
137
            'point' => 'square',
138
            'shadow_offset' => 4,
139
            'legend' => $mock['stractual'],
140
        ];
1 efrain 141
        $graph->y_data['answers2'] = $mock['buckets2'];
1441 ariadna 142
        $graph->y_format['answers2'] = [
143
            'colour' => 'ltorange',
144
            'line' => 'line',
145
            'point' => 'square',
146
            'shadow_offset' => 4,
147
            'legend' => $mock['strpreferred'],
148
        ];
149
        $graph->y_data['answers3'] = $mock['buckets3'];
150
        $graph->y_format['answers3'] = [
151
            'colour' => 'ltred',
152
            'line' => 'brush',
153
            'point' => 'square',
154
            'shadow_offset' => 4,
155
            'legend' => $mock['stractual'],
156
        ];
1 efrain 157
        $graph->y_data['stdev1'] = $mock['stdev1'];
1441 ariadna 158
        $graph->y_format['stdev1'] = [
159
            'colour' => 'ltltblue',
160
            'bar' => 'fill',
161
            'shadow_offset' => '4',
162
            'legend' => 'none',
163
            'bar_size' => 0.3,
164
        ];
1 efrain 165
        $graph->y_data['stdev2'] = $mock['stdev2'];
1441 ariadna 166
        $graph->y_format['stdev2'] = [
167
            'colour' => 'ltltorange',
168
            'bar' => 'fill',
169
            'shadow_offset' => '4',
170
            'legend' => 'none',
171
            'bar_size' => 0.2,
172
        ];
173
        $graph->y_data['stdev3'] = $mock['stdev3'];
174
        $graph->y_format['stdev3'] = [
175
            'colour' => 'ltred',
176
            'bar' => 'fill',
177
            'shadow_offset' => '4',
178
            'legend' => 'none',
179
            'bar_size' => 0.2,
180
        ];
1 efrain 181
        $graph->offset_relation['stdev1'] = 'answers1';
182
        $graph->offset_relation['stdev2'] = 'answers2';
1441 ariadna 183
        $graph->offset_relation['stdev3'] = 'answers3';
1 efrain 184
        $graph->parameter['legend'] = 'outside-top';
185
        $graph->parameter['legend_border'] = 'black';
186
        $graph->parameter['legend_offset'] = 4;
187
        $graph->y_tick_labels = $mock['options'];
1441 ariadna 188
        if (($mock['maxbuckets1'] > 0.0) && ($mock['maxbuckets2'] > 0.0) && ($mock['maxbuckets3'] > 0.0)) {
189
            $graph->y_order = [
190
                'stdev1',
191
                'answers1',
192
                'stdev2',
193
                'answers2',
194
                'stdev3',
195
                'answers3',
196
            ];
1 efrain 197
        } else if ($mock['maxbuckets1'] > 0.0) {
1441 ariadna 198
            $graph->y_order = [
199
                'stdev1',
200
                'answers1',
201
            ];
202
        } else if ($mock['maxbuckets2'] > 0.0) {
203
            $graph->y_order = [
204
                'stdev2',
205
                'answers2',
206
            ];
1 efrain 207
        } else {
1441 ariadna 208
            $graph->y_order = [
209
                'stdev3',
210
                'answers3',
211
            ];
1 efrain 212
        }
213
        $graph->parameter['y_max_left'] = 4;
214
        $graph->parameter['y_axis_gridlines'] = 5;
215
        $graph->parameter['y_resolution_left'] = 1;
216
        $graph->parameter['y_decimal_left'] = 1;
217
        $graph->parameter['x_axis_angle'] = 0;
218
        $graph->parameter['x_inner_padding'] = 6;
219
        @$graph->draw();
220
        $res = ob_end_clean();
221
 
222
        $this->assertTrue($res);
223
    }
224
}