MDL-67795 h5p: move methods from player to helper

This commit is contained in:
Sara Arjona 2020-02-21 09:16:00 +01:00
parent 172d3ee7ab
commit 153c45625d
5 changed files with 568 additions and 292 deletions

View file

@ -273,4 +273,182 @@ class api_testcase extends \advanced_testcase {
],
];
}
/**
* Test the behaviour of get_content_from_pluginfile_url().
*/
public function test_get_content_from_pluginfile_url(): void {
$this->setRunTestInSeparateProcess(true);
$this->resetAfterTest();
$factory = new factory();
// Create the H5P data.
$filename = 'find-the-words.h5p';
$path = __DIR__ . '/fixtures/' . $filename;
$fakefile = helper::create_fake_stored_file_from_path($path);
$config = (object)[
'frame' => 1,
'export' => 1,
'embed' => 0,
'copyright' => 0,
];
// Get URL for this H5P content file.
$syscontext = \context_system::instance();
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
$filename
);
// Scenario 1: Get the H5P for this URL and check there isn't any existing H5P (because it hasn't been saved).
list($newfile, $h5p) = api::get_content_from_pluginfile_url($url->out());
$this->assertEquals($fakefile->get_pathnamehash(), $newfile->get_pathnamehash());
$this->assertEquals($fakefile->get_contenthash(), $newfile->get_contenthash());
$this->assertFalse($h5p);
// Scenario 2: Save the H5P and check now the H5P is exactly the same as the original one.
$h5pid = helper::save_h5p($factory, $fakefile, $config);
list($newfile, $h5p) = api::get_content_from_pluginfile_url($url->out());
$this->assertEquals($h5pid, $h5p->id);
$this->assertEquals($fakefile->get_pathnamehash(), $h5p->pathnamehash);
$this->assertEquals($fakefile->get_contenthash(), $h5p->contenthash);
// Scenario 3: Get the H5P for an unexisting H5P file.
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
'unexisting.h5p'
);
list($newfile, $h5p) = api::get_content_from_pluginfile_url($url->out());
$this->assertFalse($newfile);
$this->assertFalse($h5p);
}
/**
* Test the behaviour of create_content_from_pluginfile_url().
*/
public function test_create_content_from_pluginfile_url(): void {
global $DB;
$this->setRunTestInSeparateProcess(true);
$this->resetAfterTest();
$factory = new factory();
// Create the H5P data.
$filename = 'find-the-words.h5p';
$path = __DIR__ . '/fixtures/' . $filename;
$fakefile = helper::create_fake_stored_file_from_path($path);
$config = (object)[
'frame' => 1,
'export' => 1,
'embed' => 0,
'copyright' => 0,
];
// Get URL for this H5P content file.
$syscontext = \context_system::instance();
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
$filename
);
// Scenario 1: Create the H5P from this URL and check the content is exactly the same as the fake file.
$messages = new \stdClass();
list($newfile, $h5pid) = api::create_content_from_pluginfile_url($url->out(), $config, $factory, $messages);
$this->assertNotFalse($h5pid);
$h5p = $DB->get_record('h5p', ['id' => $h5pid]);
$this->assertEquals($fakefile->get_pathnamehash(), $h5p->pathnamehash);
$this->assertEquals($fakefile->get_contenthash(), $h5p->contenthash);
$this->assertTrue(empty($messages->error));
$this->assertTrue(empty($messages->info));
// Scenario 2: Create the H5P for an unexisting H5P file.
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
'unexisting.h5p'
);
list($newfile, $h5p) = api::create_content_from_pluginfile_url($url->out(), $config, $factory, $messages);
$this->assertFalse($newfile);
$this->assertFalse($h5p);
$this->assertTrue(empty($messages->error));
$this->assertTrue(empty($messages->info));
}
/**
* Test the behaviour of delete_content_from_pluginfile_url().
*/
public function test_delete_content_from_pluginfile_url(): void {
global $DB;
$this->setRunTestInSeparateProcess(true);
$this->resetAfterTest();
$factory = new factory();
// Create the H5P data.
$filename = 'find-the-words.h5p';
$path = __DIR__ . '/fixtures/' . $filename;
$fakefile = helper::create_fake_stored_file_from_path($path);
$config = (object)[
'frame' => 1,
'export' => 1,
'embed' => 0,
'copyright' => 0,
];
// Get URL for this H5P content file.
$syscontext = \context_system::instance();
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
$filename
);
// Scenario 1: Try to remove the H5P content for an undeployed file.
list($newfile, $h5p) = api::get_content_from_pluginfile_url($url->out());
$this->assertEquals(0, $DB->count_records('h5p'));
api::delete_content_from_pluginfile_url($url->out(), $factory);
$this->assertEquals(0, $DB->count_records('h5p'));
// Scenario 2: Deploy an H5P from this URL, check it's created, remove it and check it has been removed as expected.
$this->assertEquals(0, $DB->count_records('h5p'));
$messages = new \stdClass();
list($newfile, $h5pid) = api::create_content_from_pluginfile_url($url->out(), $config, $factory, $messages);
$this->assertEquals(1, $DB->count_records('h5p'));
api::delete_content_from_pluginfile_url($url->out(), $factory);
$this->assertEquals(0, $DB->count_records('h5p'));
// Scenario 3: Try to remove the H5P for an unexisting H5P URL.
$url = \moodle_url::make_pluginfile_url(
$syscontext->id,
\core_h5p\file_storage::COMPONENT,
'unittest',
$fakefile->get_itemid(),
'/',
'unexisting.h5p'
);
$this->assertEquals(0, $DB->count_records('h5p'));
api::delete_content_from_pluginfile_url($url->out(), $factory);
$this->assertEquals(0, $DB->count_records('h5p'));
}
}

View 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);
}
}