2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once("$CFG->libdir/graphlib.php");
27 * @coversDefaultClass \graph
29 * @copyright 2023 Meirza (meirza.arson@moodle.com)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class graphlib_test
extends \basic_testcase
{
35 * Data provider for test_graphlib.
39 public function create_data(): array {
44 'survey_name' => 'Survey 101',
46 'Relevance', 'Reflective thinking', 'Interactivity', 'Tutor support', 'Peer support', 'Interpretation'
49 0.75, 2.5, 1.5, 1.5, 2.5, 2.5
51 'stractual' => 'Actual',
53 -1, -1, -1, -1, -1, -1
55 'strpreferred' => 'Preferred',
57 0.82915619758885, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499, 1.1180339887499
63 'Almost never', 'Seldom', 'Sometimes', 'Often', 'Almost always'
75 * @dataProvider create_data
80 public function test_graphlib($mock) {
81 $graph = new \
graph(300, 200);
83 $graph->parameter
['title'] = strip_tags(format_string($mock['survey_name'], true));
84 $graph->x_data
= $mock['names'];
85 $graph->y_data
['answers1'] = $mock['buckets1'];
86 $graph->y_format
['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
87 'shadow_offset' => 4, 'legend' => $mock['stractual']);
88 $graph->y_data
['answers2'] = $mock['buckets2'];
89 $graph->y_format
['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
90 'shadow_offset' => 4, 'legend' => $mock['strpreferred']);
91 $graph->y_data
['stdev1'] = $mock['stdev1'];
92 $graph->y_format
['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
93 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
94 $graph->y_data
['stdev2'] = $mock['stdev2'];
95 $graph->y_format
['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
96 'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
97 $graph->offset_relation
['stdev1'] = 'answers1';
98 $graph->offset_relation
['stdev2'] = 'answers2';
99 $graph->parameter
['legend'] = 'outside-top';
100 $graph->parameter
['legend_border'] = 'black';
101 $graph->parameter
['legend_offset'] = 4;
102 $graph->y_tick_labels
= $mock['options'];
103 if (($mock['maxbuckets1'] > 0.0) && ($mock['maxbuckets2'] > 0.0)) {
104 $graph->y_order
= array('stdev1', 'answers1', 'stdev2', 'answers2');
105 } else if ($mock['maxbuckets1'] > 0.0) {
106 $graph->y_order
= array('stdev1', 'answers1');
108 $graph->y_order
= array('stdev2', 'answers2');
110 $graph->parameter
['y_max_left'] = 4;
111 $graph->parameter
['y_axis_gridlines'] = 5;
112 $graph->parameter
['y_resolution_left'] = 1;
113 $graph->parameter
['y_decimal_left'] = 1;
114 $graph->parameter
['x_axis_angle'] = 0;
115 $graph->parameter
['x_inner_padding'] = 6;
117 $res = ob_end_clean();
119 $this->assertTrue($res);