Skip to content

Commit 4f9bde5

Browse files
committed
Improve readability and code precision
Replace qualifiers with imports and long if-chains with the php match-statement. Make array definitions more compact.
1 parent c97aefc commit 4f9bde5

File tree

5 files changed

+90
-142
lines changed

5 files changed

+90
-142
lines changed

tests/cron_test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private function run_cron() {
133133
/**
134134
* Assert, that the algorithm status is not_started and the algorithm has created no allocation.
135135
*/
136-
private function assert_not_started() {
136+
private function assert_not_started(): void {
137137
global $DB;
138138
$record = $DB->get_record(this_db\ratingallocate::TABLE, []);
139139
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $record, $this->teacher);
@@ -145,7 +145,7 @@ private function assert_not_started() {
145145
/**
146146
* Assert, that the algorithm status is running and the algorithm has created no allocation.
147147
*/
148-
private function assert_running() {
148+
private function assert_running(): void {
149149
global $DB;
150150
$record = $DB->get_record(this_db\ratingallocate::TABLE, []);
151151
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $record, $this->teacher);
@@ -157,7 +157,7 @@ private function assert_running() {
157157
/**
158158
* Assert, that the algorithm status is failure and the algorithm has created no allocation.
159159
*/
160-
private function assert_failure() {
160+
private function assert_failure(): void {
161161
global $DB;
162162
$record = $DB->get_record(this_db\ratingallocate::TABLE, []);
163163
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $record, $this->teacher);
@@ -169,7 +169,7 @@ private function assert_failure() {
169169
/**
170170
* Assert, that the algorithm status is finished and the algorithm has created 4 allocations.
171171
*/
172-
private function assert_finish() {
172+
private function assert_finish(): void {
173173
global $DB;
174174
$record = $DB->get_record(this_db\ratingallocate::TABLE, []);
175175
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $record, $this->teacher);
@@ -181,7 +181,7 @@ private function assert_finish() {
181181
/**
182182
* Assert, that the algorithm status is still finished and the algorithm has created no allocation.
183183
*/
184-
private function assert_already_finish() {
184+
private function assert_already_finish(): void {
185185
global $DB;
186186
$record = $DB->get_record(this_db\ratingallocate::TABLE, []);
187187
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $record, $this->teacher);

tests/locallib_test.php

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function test_simple(): void {
6060

6161
$course = $this->getDataGenerator()->create_course();
6262

63-
$teacher = \mod_ratingallocate_generator::create_user_and_enrol($this, $course, true);
63+
$teacher = mod_ratingallocate_generator::create_user_and_enrol($this, $course, true);
6464
$this->setUser($teacher);
6565

6666
// There should not be any module for that course first.
@@ -71,27 +71,27 @@ public function test_simple(): void {
7171
);
7272

7373
// Set default data for category.
74-
$moduledata = \mod_ratingallocate_generator::get_default_values();
74+
$moduledata = mod_ratingallocate_generator::get_default_values();
7575
$moduledata['course'] = $course;
7676

77-
$choicedata = \mod_ratingallocate_generator::get_default_choice_data();
77+
$choicedata = mod_ratingallocate_generator::get_default_choice_data();
7878
foreach ($choicedata as $id => $choice) {
7979
$choice['maxsize'] = 2;
8080
$choice['active'] = true;
8181
$choicedata[$id] = $choice;
8282
}
8383

8484
// Create activity.
85-
$mod = \mod_ratingallocate_generator::create_instance_with_choices($this, $moduledata, $choicedata);
85+
$mod = mod_ratingallocate_generator::create_instance_with_choices($this, $moduledata, $choicedata);
8686
$this->assertEquals(2, $DB->count_records(this_db\ratingallocate_choices::TABLE),
8787
"Failure, debug info: " . implode(",", [this_db\ratingallocate_choices::ID => $mod->id]));
8888

89-
$student1 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
90-
$student2 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
91-
$student3 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
92-
$student4 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
89+
$student1 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
90+
$student2 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
91+
$student3 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
92+
$student4 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
9393

94-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user($this, $mod, $teacher);
94+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $mod, $teacher);
9595
$choices = $ratingallocate->get_rateable_choices();
9696

9797
$choice1 = reset($choices);
@@ -110,12 +110,13 @@ public function test_simple(): void {
110110
$preferssecond[$choice2->{this_db\ratingallocate_choices::ID}][this_db\ratingallocate_ratings::RATING] = true;
111111

112112
// Assign preferences.
113-
\mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student1, $prefersfirst);
114-
\mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student2, $prefersfirst);
115-
\mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student3, $preferssecond);
116-
\mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student4, $preferssecond);
113+
mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student1, $prefersfirst);
114+
mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student2, $prefersfirst);
115+
mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student3, $preferssecond);
116+
mod_ratingallocate_generator::save_rating_for_user($this, $mod, $student4, $preferssecond);
117117

118118
// Allocate choices.
119+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $mod, $teacher);
119120
$timeneeded = $ratingallocate->distribute_choices();
120121
$this->assertGreaterThan(0, $timeneeded);
121122
$this->assertLessThan(0.1, $timeneeded, 'Allocation is very slow');
@@ -178,25 +179,29 @@ private static function filter_allocations_by_choice($allocations, $choiceid) {
178179
/**
179180
* Default data has two choices but only one is active.
180181
* Test if count of rateable choices is 1.
182+
*
183+
* @covers \mod_ratingallocate\ratingallocate::get_rateable_choices
181184
*/
182185
public function test_get_ratable_choices(): void {
183-
$record = \mod_ratingallocate_generator::get_default_values();
186+
$record = mod_ratingallocate_generator::get_default_values();
184187
$testmodule = new \mod_ratingallocate_generated_module($this, $record);
185188
$ratingallocate =
186-
\mod_ratingallocate_generator::get_ratingallocate_for_user($this, $testmodule->moddb, $testmodule->teacher);
189+
mod_ratingallocate_generator::get_ratingallocate_for_user($this, $testmodule->moddb, $testmodule->teacher);
187190
$this->assertCount(1, $ratingallocate->get_rateable_choices());
188191
}
189192

190193
/**
191194
* Test if option titles are returned according to the default values
195+
*
196+
* @covers \mod_ratingallocate\ratingallocate::get_options_titles
192197
*/
193198
public function test_get_option_titles_default(): void {
194199
$expectedresult = [1 => 'Accept', 0 => 'Deny']; // Depends on language file.
195200
$ratings = [0, 1, 1, 1, 0];
196201

197-
$record = \mod_ratingallocate_generator::get_default_values();
202+
$record = mod_ratingallocate_generator::get_default_values();
198203
$testmodule = new \mod_ratingallocate_generated_module($this, $record);
199-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user(
204+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user(
200205
$this, $testmodule->moddb, $testmodule->teacher);
201206

202207
$result = $ratingallocate->get_options_titles($ratings);
@@ -205,15 +210,17 @@ public function test_get_option_titles_default(): void {
205210

206211
/**
207212
* Test if option titles are returned according to defined custom values
213+
*
214+
* @covers \mod_ratingallocate\ratingallocate::get_options_titles
208215
*/
209216
public function test_get_option_titles_custom(): void {
210217
$expectedresult = [1 => 'Ja1234', 0 => 'Nein1234']; // Test data.
211218
$ratings = [1, 1, 1, 0, 1, 1];
212219

213-
$record = \mod_ratingallocate_generator::get_default_values();
220+
$record = mod_ratingallocate_generator::get_default_values();
214221
$record['strategyopt']['strategy_yesno'] = $expectedresult;
215222
$testmodule = new \mod_ratingallocate_generated_module($this, $record);
216-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user(
223+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user(
217224
$this, $testmodule->moddb, $testmodule->teacher);
218225

219226
$result = $ratingallocate->get_options_titles($ratings);
@@ -222,15 +229,17 @@ public function test_get_option_titles_custom(): void {
222229

223230
/**
224231
* Test if option titles are returned according to defined custom values, if ratings consist of just one rating
232+
*
233+
* @covers \mod_ratingallocate\ratingallocate::get_options_titles
225234
*/
226235
public function test_get_option_titles_custom1(): void {
227236
$expectedresult = [1 => 'Ja1234']; // Test data.
228237
$ratings = [1, 1, 1, 1, 1];
229238

230-
$record = \mod_ratingallocate_generator::get_default_values();
239+
$record = mod_ratingallocate_generator::get_default_values();
231240
$record['strategyopt']['strategy_yesno'] = $expectedresult;
232241
$testmodule = new \mod_ratingallocate_generated_module($this, $record);
233-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user(
242+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user(
234243
$this, $testmodule->moddb, $testmodule->teacher);
235244

236245
$result = $ratingallocate->get_options_titles($ratings);
@@ -248,10 +257,10 @@ public function test_get_option_titles_mixed(): void {
248257
$expectedresult = $settings;
249258
$expectedresult[0] = 'Deny'; // Depends on language file.
250259

251-
$record = \mod_ratingallocate_generator::get_default_values();
260+
$record = mod_ratingallocate_generator::get_default_values();
252261
$record['strategyopt']['strategy_yesno'] = $settings;
253262
$testmodule = new \mod_ratingallocate_generated_module($this, $record);
254-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user(
263+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user(
255264
$this, $testmodule->moddb, $testmodule->teacher);
256265

257266
$result = $ratingallocate->get_options_titles($ratings);
@@ -269,49 +278,20 @@ public function test_reset_userdata(): void {
269278
$this->resetAfterTest();
270279

271280
$course = $this->getDataGenerator()->create_course();
272-
$teacher = \mod_ratingallocate_generator::create_user_and_enrol($this, $course, true);
273-
$student1 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
274-
$student2 = \mod_ratingallocate_generator::create_user_and_enrol($this, $course);
281+
$teacher = mod_ratingallocate_generator::create_user_and_enrol($this, $course, true);
282+
$student1 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
283+
$student2 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
275284

276285
$this->setUser($teacher);
277286

278-
$choices = [
279-
[
280-
'title' => 'C1',
281-
'maxsize' => '1',
282-
'active' => '1',
283-
],
284-
[
285-
'title' => 'C2',
286-
'maxsize' => '1',
287-
'active' => '1',
288-
],
289-
];
287+
$choices = [['title' => 'C1', 'maxsize' => '1', 'active' => '1'], ['title' => 'C2', 'maxsize' => '1', 'active' => '1']];
290288
$ratings = [
291-
$student1->id => [
292-
[
293-
'choice' => 'C1',
294-
'rating' => 1,
295-
],
296-
[
297-
'choice' => 'C2',
298-
'rating' => 0,
299-
],
300-
],
301-
$student2->id => [
302-
[
303-
'choice' => 'C1',
304-
'rating' => 0,
305-
],
306-
[
307-
'choice' => 'C2',
308-
'rating' => 1,
309-
],
310-
],
289+
$student1->id => [['choice' => 'C1', 'rating' => 1], ['choice' => 'C2', 'rating' => 0]],
290+
$student2->id => [['choice' => 'C1', 'rating' => 0], ['choice' => 'C2', 'rating' => 1]],
311291
];
312292

313293
// Create ratingallocate instance.
314-
$ratingallocate = \mod_ratingallocate_generator::get_closed_ratingallocate_for_teacher($this, $choices,
294+
$ratingallocate = mod_ratingallocate_generator::get_closed_ratingallocate_for_teacher($this, $choices,
315295
$course, $ratings);
316296

317297
// Simulate Allocation.
@@ -343,7 +323,7 @@ public function test_reset_userdata(): void {
343323

344324
// Reload the instance data.
345325
$ra = $DB->get_record('ratingallocate', ['id' => $ratingallocate->get_ratingallocateid()]);
346-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate($ra);
326+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate($ra);
347327

348328
// There should be no ratings and allocations anymore.
349329
$this->assertEquals(0, count($ratingallocate->get_allocations()));

tests/mod_generator_test.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function test_create_instance(): void {
5858
$this->assertEquals(0, count($records));
5959

6060
// Create activity.
61-
$mod = \mod_ratingallocate_generator::create_instance_with_choices($this,
61+
$mod = mod_ratingallocate_generator::create_instance_with_choices($this,
6262
['course' => $course]);
6363
$records = $DB->get_records('ratingallocate', ['course' => $course->id,
6464
], 'id');
@@ -118,7 +118,7 @@ public function test_create_instance(): void {
118118
// Create an other mod_ratingallocate within the course.
119119
$params = ['course' => $course->id, 'name' => 'Another mod_ratingallocate',
120120
];
121-
$mod = \mod_ratingallocate_generator::create_instance_with_choices($this, $params);
121+
$mod = mod_ratingallocate_generator::create_instance_with_choices($this, $params);
122122
$records = $DB->get_records('ratingallocate', ['course' => $course->id,
123123
], 'id');
124124
// Are there 2 modules within the course?
@@ -136,20 +136,25 @@ public function test_create_instance(): void {
136136
$this->assertEquals(0, count($records));
137137
}
138138

139+
/**
140+
* Test the creation of a mod_ratingallocate instance with choices and students.
141+
*
142+
* @covers \mod_ratingallocate_generator::get_ratingallocate_for_user
143+
*/
139144
public function test_mod_ratingallocate_generated_module(): void {
140-
$choicedata = \mod_ratingallocate_generator::get_default_choice_data();
145+
$choicedata = mod_ratingallocate_generator::get_default_choice_data();
141146
foreach ($choicedata as $id => $choice) {
142147
$choice['maxsize'] = 10;
143148
$choice['active'] = true;
144149
$choicedata[$id] = $choice;
145150
}
146-
$moduledata = \mod_ratingallocate_generator::get_default_values();
151+
$moduledata = mod_ratingallocate_generator::get_default_values();
147152
$moduledata['num_students'] = 22;
148153
$testmodule = new \mod_ratingallocate_generated_module($this, $moduledata, $choicedata);
149154
$this->assertCount($moduledata['num_students'], $testmodule->students);
150155
$this->assertCount(20, $testmodule->allocations);
151156

152-
$ratingallocate = \mod_ratingallocate_generator::get_ratingallocate_for_user(
157+
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user(
153158
$this, $testmodule->moddb, $testmodule->teacher);
154159
foreach ($ratingallocate->get_choices_with_allocationcount() as $choice) {
155160
$this->assertEquals(10, $choice->{'usercount'});

0 commit comments

Comments
 (0)