MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / health / tests / healthlib_test.php
CommitLineData
8db188c8
LR
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/**
a1ceaf89 18 * Unit tests for tool_health.
8db188c8 19 *
a1ceaf89
DM
20 * @package tool_health
21 * @copyright 2013 Marko Vidberg
8db188c8
LR
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
a1ceaf89 28require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/health/locallib.php');
8db188c8 29
a1ceaf89
DM
30/**
31 * Health lib testcase.
32 *
33 * @package tool_health
34 * @copyright 2013 Marko Vidberg
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class healthlib_testcase extends advanced_testcase {
8db188c8
LR
38
39 /**
a1ceaf89 40 * Data provider for test_tool_health_category_find_loops.
8db188c8
LR
41 */
42 public static function provider_loop_categories() {
43 return array(
559d6c00
SH
44 // One item loop including root.
45 0 => array(
46 array(
47 '1' => (object) array('id' => 1, 'parent' => 1)
8db188c8 48 ),
559d6c00
SH
49 array(
50 '1' => (object) array('id' => 1, 'parent' => 1)
8db188c8 51 ),
559d6c00
SH
52 ),
53 // One item loop not including root.
54 1 => array(
55 array(
56 '1' => (object) array('id' => 1, 'parent' => 0),
57 '2' => (object) array('id' => 2, 'parent' => 2)
8db188c8 58 ),
559d6c00
SH
59 array(
60 '2' => (object) array('id' => 2, 'parent' => 2)
8db188c8 61 ),
559d6c00
SH
62 ),
63 // Two item loop including root.
64 2 => array(
65 array(
66 '1' => (object) array('id' => 1, 'parent' => 2),
67 '2' => (object) array('id' => 2, 'parent' => 1)
8db188c8 68 ),
559d6c00
SH
69 array(
70 '2' => (object) array('id' => 2, 'parent' => 1),
71 '1' => (object) array('id' => 1, 'parent' => 2),
72 )
73 ),
74 // Two item loop not including root.
75 3 => array(
76 array(
77 '1' => (object) array('id' => 1, 'parent' => 0),
78 '2' => (object) array('id' => 2, 'parent' => 3),
79 '3' => (object) array('id' => 3, 'parent' => 2),
80 ),
81 array(
82 '3' => (object) array('id' => 3, 'parent' => 2),
83 '2' => (object) array('id' => 2, 'parent' => 3),
84 )
85 ),
86 // Three item loop including root.
87 4 => array(
88 array(
89 '1' => (object) array('id' => 1, 'parent' => 2),
90 '2' => (object) array('id' => 2, 'parent' => 3),
91 '3' => (object) array('id' => 3, 'parent' => 1),
8db188c8 92 ),
559d6c00
SH
93 array(
94 '3' => (object) array('id' => 3, 'parent' => 1),
95 '1' => (object) array('id' => 1, 'parent' => 2),
96 '2' => (object) array('id' => 2, 'parent' => 3),
97 )
98 ),
99 // Three item loop not including root.
100 5 => array(
101 array(
102 '1' => (object) array('id' => 1, 'parent' => 0),
103 '2' => (object) array('id' => 2, 'parent' => 3),
104 '3' => (object) array('id' => 3, 'parent' => 4),
105 '4' => (object) array('id' => 4, 'parent' => 2)
106 ),
107 array(
108 '4' => (object) array('id' => 4, 'parent' => 2),
109 '2' => (object) array('id' => 2, 'parent' => 3),
110 '3' => (object) array('id' => 3, 'parent' => 4),
111 )
112 ),
113 // Multi-loop.
114 6 => array(
115 array(
116 '1' => (object) array('id' => 1, 'parent' => 2),
117 '2' => (object) array('id' => 2, 'parent' => 1),
118 '3' => (object) array('id' => 3, 'parent' => 4),
119 '4' => (object) array('id' => 4, 'parent' => 5),
120 '5' => (object) array('id' => 5, 'parent' => 3),
121 '6' => (object) array('id' => 6, 'parent' => 6),
122 '7' => (object) array('id' => 7, 'parent' => 1),
123 '8' => (object) array('id' => 8, 'parent' => 7),
a1ceaf89 124 ),
559d6c00
SH
125 array(
126 '1' => (object) array('id' => 1, 'parent' => 2),
127 '2' => (object) array('id' => 2, 'parent' => 1),
128 '8' => (object) array('id' => 8, 'parent' => 7),
129 '7' => (object) array('id' => 7, 'parent' => 1),
130 '6' => (object) array('id' => 6, 'parent' => 6),
131 '5' => (object) array('id' => 5, 'parent' => 3),
132 '3' => (object) array('id' => 3, 'parent' => 4),
133 '4' => (object) array('id' => 4, 'parent' => 5),
8db188c8 134 )
559d6c00
SH
135 ),
136 // Double-loop
137 7 => array(
138 array(
139 '1' => (object) array('id' => 1, 'parent' => 2),
140 '2' => (object) array('id' => 2, 'parent' => 1),
141 '3' => (object) array('id' => 3, 'parent' => 2),
142 '4' => (object) array('id' => 4, 'parent' => 2),
143 ),
144 array(
145 '4' => (object) array('id' => 4, 'parent' => 2),
146 '3' => (object) array('id' => 3, 'parent' => 2),
147 '2' => (object) array('id' => 2, 'parent' => 1),
148 '1' => (object) array('id' => 1, 'parent' => 2),
149 )
150 )
8db188c8
LR
151 );
152 }
153
154 /**
a1ceaf89 155 * Data provider for test_tool_health_category_find_missing_parents.
8db188c8
LR
156 */
157 public static function provider_missing_parent_categories() {
158 return array(
a1ceaf89 159 // Test for two items, both with direct ancestor (parent) missing.
559d6c00
SH
160 0 => array(
161 array(
162 '1' => (object) array('id' => 1, 'parent' => 0),
163 '2' => (object) array('id' => 2, 'parent' => 3),
164 '4' => (object) array('id' => 4, 'parent' => 5),
165 '6' => (object) array('id' => 6, 'parent' => 2)
166 ),
167 array(
168 '4' => (object) array('id' => 4, 'parent' => 5),
169 '2' => (object) array('id' => 2, 'parent' => 3)
170 ),
171 )
8db188c8
LR
172 );
173 }
174
175 /**
a1ceaf89
DM
176 * Test finding loops between two items referring to each other.
177 *
178 * @param array $categories
179 * @param array $expected
180 * @dataProvider provider_loop_categories
181 */
182 public function test_tool_health_category_find_loops($categories, $expected) {
183 $loops = tool_health_category_find_loops($categories);
184 $this->assertEquals($expected, $loops);
185 }
186
187 /**
188 * Test finding missing parent categories.
189 *
190 * @param array $categories
191 * @param array $expected
8db188c8
LR
192 * @dataProvider provider_missing_parent_categories
193 */
a1ceaf89
DM
194 public function test_tool_health_category_find_missing_parents($categories, $expected) {
195 $missingparent = tool_health_category_find_missing_parents($categories);
8db188c8
LR
196 $this->assertEquals($expected, $missingparent);
197 }
198
199 /**
a1ceaf89 200 * Test listing missing parent categories.
8db188c8 201 */
a1ceaf89
DM
202 public function test_tool_health_category_list_missing_parents() {
203 $missingparent = array((object) array('id' => 2, 'parent' => 3, 'name' => 'test'),
204 (object) array('id' => 4, 'parent' => 5, 'name' => 'test2'));
205 $result = tool_health_category_list_missing_parents($missingparent);
8db188c8
LR
206 $this->assertRegExp('/Category 2: test/', $result);
207 $this->assertRegExp('/Category 4: test2/', $result);
208 }
209
210 /**
a1ceaf89 211 * Test listing loop categories.
8db188c8 212 */
a1ceaf89
DM
213 public function test_tool_health_category_list_loops() {
214 $loops = array((object) array('id' => 2, 'parent' => 3, 'name' => 'test'));
215 $result = tool_health_category_list_loops($loops);
8db188c8
LR
216 $this->assertRegExp('/Category 2: test/', $result);
217 }
218}