MDL-67518 phpunit: avoid DB sorting randomness in test
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 20 Apr 2020 20:52:04 +0000 (22:52 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 29 May 2020 14:26:01 +0000 (16:26 +0200)
While reading from mdl_config, sometimes this test was failing
because an order was being presumed. Now we convert the records
array to an associative one to perform tests against it.

blocks/tests/externallib_test.php

index bd815e6..ee1b61b 100644 (file)
@@ -267,9 +267,11 @@ class core_block_externallib_testcase extends externallib_advanced_testcase {
             $returnedblocks[] = $block['name'];
             // Check the configuration returned for this default block.
             if ($block['name'] == 'recentlyaccessedcourses') {
-                $this->assertEquals('displaycategories', $block['configs'][0]['name']);
-                $this->assertEquals(json_encode('0'), $block['configs'][0]['value']);
-                $this->assertEquals('plugin', $block['configs'][0]['type']);
+                // Convert config to associative array to avoid DB sorting randomness.
+                $config = array_column($block['configs'], null, 'name');
+                $this->assertArrayHasKey('displaycategories', $config);
+                $this->assertEquals(json_encode('0'), $config['displaycategories']['value']);
+                $this->assertEquals('plugin', $config['displaycategories']['type']);
             }
         }
         // Remove lp block.