Skip to content

Commit cac514b

Browse files
committed
test cases completed for ratings.php
1 parent 97f2258 commit cac514b

File tree

1 file changed

+269
-24
lines changed

1 file changed

+269
-24
lines changed

tests/ratings_test.php

+269-24
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ public function tearDown(): void {
103103
// Begin of test functions
104104

105105
/**
106-
* Test, if rating can sort after ever group with ratingpreferences on helpful first.
106+
* Tests function ratings::moodleoverflow_sort_answer_by_ratings
107+
* Test case: Every group of rating exists (helful and solved posts, only helpful/solved and none)
107108
*/
108-
public function test_sort_answer_by_ratings() {
109+
public function test_answersorting_everygroup() {
109110
// Create helpful, solved, up and downvotes ratings.
110111
$this->create_everygroup();
111112

@@ -125,6 +126,15 @@ public function test_sort_answer_by_ratings() {
125126
$rightorderposts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer6, $this->answer5);
126127
$result = $this->postsorderequal($sortedposts, $rightorderposts);
127128
$this->assertEquals(1, $result);
129+
}
130+
131+
/**
132+
* Tests function ratings::moodleoverflow_sort_answer_by_ratings
133+
* Test case: One group of rating does not exist
134+
*/
135+
public function test_answersorting_threegroups() {
136+
// Create helpful, solved, up and downvotes ratings.
137+
$this->create_everygroup();
128138

129139
// Test without posts that are only marked as solved
130140
$posts = array($this->post, $this->answer1, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
@@ -167,8 +177,134 @@ public function test_sort_answer_by_ratings() {
167177
$rightorderposts = array($this->post, $this->answer2, $this->answer3, $this->answer4, $this->answer6, $this->answer5);
168178
$result = $this->postsorderequal($sortedposts, $rightorderposts);
169179
$this->assertEquals(1, $result);
180+
}
181+
182+
/**
183+
* Tests function ratings::moodleoverflow_sort_answer_by_ratings
184+
* Test case: two groups of rating do not exist
185+
*/
186+
public function test_answersorting_twogroups() {
187+
$this->set_ratingpreferences(0);
188+
189+
// Test case 1: helpful and solved post, only solved posts.
190+
$group1 = 'sh';
191+
$group2 = 's';
192+
$this->create_twogroups($group1, $group2);
193+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
194+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
195+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
196+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
197+
$this->assertEquals(1, $result);
198+
199+
// Test case 2: helpful and solved post, only helpful posts.
200+
$group1 = 'sh';
201+
$group2 = 'h';
202+
$this->create_twogroups($group1, $group2);
203+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
204+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
205+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
206+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
207+
$this->assertEquals(1, $result);
208+
209+
// Test case 3: helpful and solved post, not-marked posts.
210+
$group1 = 'sh';
211+
$group2 = 'o';
212+
$this->create_twogroups($group1, $group2);
213+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
214+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
215+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
216+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
217+
$this->assertEquals(1, $result);
170218

171-
// Now test every rating group alone
219+
// Test case 4: only solved posts and only helpful posts with ratingpreferences = 0.
220+
$group1 = 's';
221+
$group2 = 'h';
222+
$this->set_ratingpreferences(0);
223+
$this->create_twogroups($group1, $group2);
224+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
225+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
226+
$rightorderposts = array($this->post, $this->answer6, $this->answer5, $this->answer4, $this->answer2, $this->answer1, $this->answer3);
227+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
228+
$this->assertEquals(1, $result);
229+
230+
// Test case 5: only solved posts and only helpful posts with ratingpreferences = 1.
231+
$group1 = 's';
232+
$group2 = 'h';
233+
$this->set_ratingpreferences(1);
234+
$this->create_twogroups($group1, $group2);
235+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
236+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
237+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
238+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
239+
$this->assertEquals(1, $result);
240+
241+
// Test case 6: only solved posts and not-marked posts.
242+
$group1 = 's';
243+
$group2 = 'o';
244+
$this->create_twogroups($group1, $group2);
245+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
246+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
247+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
248+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
249+
$this->assertEquals(1, $result);
250+
251+
// Test case 6: only helpful posts and not-marked posts.
252+
$group1 = 'h';
253+
$group2 = 'o';
254+
$this->create_twogroups($group1, $group2);
255+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
256+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
257+
$rightorderposts = array($this->post, $this->answer2, $this->answer1, $this->answer3, $this->answer6, $this->answer5, $this->answer4);
258+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
259+
$this->assertEquals(1, $result);
260+
}
261+
262+
/**
263+
* Tests function ratings::moodleoverflow_sort_answer_by_ratings
264+
* Test case: Only one group of rating exists, so only:
265+
* - helpful and solved posts, or
266+
* - helpful, or
267+
* - solved, or
268+
* - not marked
269+
*/
270+
public function test_answersorting_onegroup() {
271+
$this->set_ratingpreferences(0);
272+
273+
// Test case 1: only solved and helpful posts.
274+
$group = 'sh';
275+
$this->create_onegroup($group);
276+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
277+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
278+
$rightorderposts = array($this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5);
279+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
280+
$this->assertEquals(1, $result);
281+
282+
// Test case 1: only solvedposts.
283+
$group = 's';
284+
$this->create_onegroup($group);
285+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
286+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
287+
$rightorderposts = array($this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5);
288+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
289+
$this->assertEquals(1, $result);
290+
291+
// Test case 1: only helpful posts.
292+
$group = 'h';
293+
$this->create_onegroup($group);
294+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
295+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
296+
$rightorderposts = array($this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5);
297+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
298+
$this->assertEquals(1, $result);
299+
300+
// Test case 1: only not marked posts.
301+
$group = 'o';
302+
$this->create_onegroup($group);
303+
$posts = array($this->post, $this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
304+
$sortedposts = ratings::moodleoverflow_sort_answers_by_ratings($posts);
305+
$rightorderposts = array($this->post, $this->answer4, $this->answer6, $this->answer3, $this->answer1, $this->answer2, $this->answer5);
306+
$result = $this->postsorderequal($sortedposts, $rightorderposts);
307+
$this->assertEquals(1, $result);
172308
}
173309

174310
// Helper functions
@@ -317,7 +453,6 @@ private function create_everygroup() {
317453
* - no mark (o)
318454
*/
319455
private function create_onegroup($group) {
320-
321456
$answers = array($this->answer1, $this->answer2, $this->answer3, $this->answer4, $this->answer5, $this->answer6);
322457
foreach ($answers as $answer) {
323458
switch ($group) {
@@ -337,45 +472,155 @@ private function create_onegroup($group) {
337472
$answer->markedhelpful = 0;
338473
$answer->markedsolution = 0;
339474
break;
340-
default:
341-
$answer->markedhelpful = 0;
342-
$answer->markedsolution = 0;
343-
break;
344475
}
345476
}
346477

347478
// Votes for the answerposts
348479
// Answer1.
349480
$this->answer1->upvotes = 4;
350-
$this->answer1->downvotes = -4;
481+
$this->answer1->downvotes = 4;
351482
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = 0.
352483

353484
// Answer2.
354-
$this->answer1->upvotes = 1;
355-
$this->answer1->downvotes = 2;
356-
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = -1.
485+
$this->answer2->upvotes = 1;
486+
$this->answer2->downvotes = 2;
487+
$this->answer2->votesdifference = $this->answer2->upvotes - $this->answer2->downvotes; // Vd = -1.
357488

358489
// Answer3.
359-
$this->answer1->upvotes = 3;
360-
$this->answer1->downvotes = 2;
361-
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = 1.
490+
$this->answer3->upvotes = 3;
491+
$this->answer3->downvotes = 2;
492+
$this->answer3->votesdifference = $this->answer3->upvotes - $this->answer3->downvotes; // Vd = 1.
362493

363494
// Answer4.
364-
$this->answer1->upvotes = 5;
365-
$this->answer1->downvotes = 0;
366-
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = 5
495+
$this->answer4->upvotes = 5;
496+
$this->answer4->downvotes = 0;
497+
$this->answer4->votesdifference = $this->answer4->upvotes - $this->answer4->downvotes; // Vd = 5
367498

368499
// Answer5.
369-
$this->answer1->upvotes = 0;
370-
$this->answer1->downvotes = 2;
371-
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = -2.
500+
$this->answer5->upvotes = 0;
501+
$this->answer5->downvotes = 2;
502+
$this->answer5->votesdifference = $this->answer5->upvotes - $this->answer5->downvotes; // Vd = -2.
372503

373504
// Answer6.
374-
$this->answer1->upvotes = 4;
375-
$this->answer1->downvotes = 2;
376-
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = 2.
505+
$this->answer6->upvotes = 4;
506+
$this->answer6->downvotes = 2;
507+
$this->answer6->votesdifference = $this->answer6->upvotes - $this->answer6->downvotes; // Vd = 2.
377508

378509
// Rightorder = answer4 , answer6, answer3, answer1, answer2, answer5.
379510
}
380511

512+
private function create_twogroups($group1, $group2) {
513+
// Set the first 3 answers to the first group of rating.
514+
switch ($group1) {
515+
case 'sh':
516+
$this->answer1->markedhelpful = 1;
517+
$this->answer1->markedsolution = 1;
518+
$this->answer2->markedhelpful = 1;
519+
$this->answer2->markedsolution = 1;
520+
$this->answer3->markedhelpful = 1;
521+
$this->answer3->markedsolution = 1;
522+
break;
523+
case 's':
524+
$this->answer1->markedhelpful = 0;
525+
$this->answer1->markedsolution = 1;
526+
$this->answer2->markedhelpful = 0;
527+
$this->answer2->markedsolution = 1;
528+
$this->answer3->markedhelpful = 0;
529+
$this->answer3->markedsolution = 1;
530+
break;
531+
case 'h':
532+
$this->answer1->markedhelpful = 1;
533+
$this->answer1->markedsolution = 0;
534+
$this->answer2->markedhelpful = 1;
535+
$this->answer2->markedsolution = 0;
536+
$this->answer3->markedhelpful = 1;
537+
$this->answer3->markedsolution = 0;
538+
break;
539+
case 'o':
540+
$this->answer1->markedhelpful = 0;
541+
$this->answer1->markedsolution = 0;
542+
$this->answer2->markedhelpful = 0;
543+
$this->answer2->markedsolution = 0;
544+
$this->answer3->markedhelpful = 0;
545+
$this->answer3->markedsolution = 0;
546+
break;
547+
}
548+
549+
switch ($group2) {
550+
case 'sh':
551+
$this->answer4->markedhelpful = 1;
552+
$this->answer4->markedsolution = 1;
553+
$this->answer5->markedhelpful = 1;
554+
$this->answer5->markedsolution = 1;
555+
$this->answer6->markedhelpful = 1;
556+
$this->answer6->markedsolution = 1;
557+
break;
558+
case 's':
559+
$this->answer4->markedhelpful = 0;
560+
$this->answer4->markedsolution = 1;
561+
$this->answer5->markedhelpful = 0;
562+
$this->answer5->markedsolution = 1;
563+
$this->answer6->markedhelpful = 0;
564+
$this->answer6->markedsolution = 1;
565+
break;
566+
case 'h':
567+
$this->answer4->markedhelpful = 1;
568+
$this->answer4->markedsolution = 0;
569+
$this->answer5->markedhelpful = 1;
570+
$this->answer5->markedsolution = 0;
571+
$this->answer6->markedhelpful = 1;
572+
$this->answer6->markedsolution = 0;
573+
break;
574+
case 'o':
575+
$this->answer4->markedhelpful = 0;
576+
$this->answer4->markedsolution = 0;
577+
$this->answer5->markedhelpful = 0;
578+
$this->answer5->markedsolution = 0;
579+
$this->answer6->markedhelpful = 0;
580+
$this->answer6->markedsolution = 0;
581+
break;
582+
}
583+
584+
// Now set the up and downvotes for every answer.
585+
// Answer1.
586+
$this->answer1->upvotes = 3;
587+
$this->answer1->downvotes = 4;
588+
$this->answer1->votesdifference = $this->answer1->upvotes - $this->answer1->downvotes; // Vd = -1.
589+
590+
// Answer2.
591+
$this->answer2->upvotes = 4;
592+
$this->answer2->downvotes = 1;
593+
$this->answer2->votesdifference = $this->answer2->upvotes - $this->answer2->downvotes; // Vd = 3.
594+
595+
// Answer3.
596+
$this->answer3->upvotes = 0;
597+
$this->answer3->downvotes = 2;
598+
$this->answer3->votesdifference = $this->answer3->upvotes - $this->answer3->downvotes; // Vd = -2.
599+
600+
// Answer4.
601+
$this->answer4->upvotes = 5;
602+
$this->answer4->downvotes = 5;
603+
$this->answer4->votesdifference = $this->answer4->upvotes - $this->answer4->downvotes; // Vd = 0
604+
605+
// Answer5.
606+
$this->answer5->upvotes = 6;
607+
$this->answer5->downvotes = 5;
608+
$this->answer5->votesdifference = $this->answer5->upvotes - $this->answer5->downvotes; // Vd = 1.
609+
610+
// Answer6.
611+
$this->answer6->upvotes = 4;
612+
$this->answer6->downvotes = 2;
613+
$this->answer6->votesdifference = $this->answer6->upvotes - $this->answer6->downvotes; // Vd = 2.
614+
615+
616+
// The Rightorder depends now on the group parameter.
617+
// Rightorder (sh,s) = answer2, answer1, answer3, answer6, answer5, answer4.
618+
// Rightorder (sh,h) = answer2, answer1, answer3, answer6, answer5, answer4.
619+
// Rightorder (sh,o) = answer2, answer1, answer3, answer6, answer5, answer4.
620+
// Rightorder (s,h) = answer6, answer5, answer4, answer2, answer1, answer3. with ratingpreference = 0.
621+
// Rightorder (s,h) = answer2, answer1, answer3, answer6, answer5, answer4. with ratingpreference = 1
622+
// Rightorder (s,o) = answer2, answer1, answer3, answer6, answer5, answer4.
623+
// Rightorder (h,o) = answer2, answer1, answer3, answer6, answer5, answer4.
624+
625+
}
381626
}

0 commit comments

Comments
 (0)