MDL-67795 h5p: move methods from player to helper
[moodle.git] / h5p / tests / helper_test.php
index c216a2c..734bd08 100644 (file)
@@ -290,4 +290,42 @@ class helper_testcase extends \advanced_testcase {
         $candeploy = helper::can_update_library($file);
         $this->assertTrue($candeploy);
     }
+
+    /**
+     * Test the behaviour of get_messages().
+     */
+    public function test_get_messages(): void {
+        $this->resetAfterTest();
+
+        $factory = new \core_h5p\factory();
+        $messages = new \stdClass();
+
+        helper::get_messages($messages, $factory);
+        $this->assertTrue(empty($messages->error));
+        $this->assertTrue(empty($messages->info));
+
+        // Add an some messages manually and check they are still there.
+        $messages->error['error1'] = 'Testing ERROR message';
+        $messages->info['info1'] = 'Testing INFO message';
+        $messages->info['info2'] = 'Testing INFO message';
+        helper::get_messages($messages, $factory);
+        $this->assertCount(1, $messages->error);
+        $this->assertCount(2, $messages->info);
+
+        // When saving an invalid .h5p file, 6 errors should be raised.
+        $path = __DIR__ . '/fixtures/h5ptest.zip';
+        $file = helper::create_fake_stored_file_from_path($path);
+        $factory->get_framework()->set_file($file);
+        $config = (object)[
+            'frame' => 1,
+            'export' => 1,
+            'embed' => 0,
+            'copyright' => 0,
+        ];
+        $h5pid = helper::save_h5p($factory, $file, $config);
+        $this->assertFalse($h5pid);
+        helper::get_messages($messages, $factory);
+        $this->assertCount(7, $messages->error);
+        $this->assertCount(2, $messages->info);
+    }
 }