MDL-69206 course: Tidy up unit test to avoid potential failures
authorMihail Geshoski <mihail@moodle.com>
Mon, 6 Jul 2020 09:33:57 +0000 (17:33 +0800)
committerMihail Geshoski <mihail@moodle.com>
Tue, 14 Jul 2020 04:11:50 +0000 (12:11 +0800)
course/tests/services_content_item_service_test.php

index 51d2183..f1924fb 100644 (file)
@@ -153,9 +153,12 @@ class services_content_item_service_testcase extends \advanced_testcase {
         $matchingcontentitems1 = $cis->get_content_items_by_name_pattern($user, $pattern1);
         $matchingcontentitems2 = $cis->get_content_items_by_name_pattern($user, $pattern2);
 
-        // The pattern "assign" should return 1 content item ("Assignment").
-        $this->assertCount(1, $matchingcontentitems1);
-        $this->assertEquals("Assignment", $matchingcontentitems1[0]->title);
+        // The pattern "assign" should return at least 1 content item (ex. "Assignment").
+        $this->assertGreaterThanOrEqual(1, count($matchingcontentitems1));
+        // Verify the pattern "assign" can be found in the title of each returned content item.
+        foreach ($matchingcontentitems1 as $contentitem) {
+            $this->assertEquals(1, preg_match("/$pattern1/i", $contentitem->title));
+        }
         // The pattern "random string" should not return any content items.
         $this->assertEmpty($matchingcontentitems2);
     }