MDL-74910 core_files: Additional unit tests for get_conversions_for_file

This commit is contained in:
Kateryna Degtyariova 2022-06-21 14:48:00 +10:00 committed by Jun Pataleta
parent c9da36d731
commit 70bd5a2161

View file

@ -75,6 +75,53 @@ class conversion_test extends \advanced_testcase {
$this->assertFalse($conversion->get_destfile());
}
/**
* Ensure that get_conversions_for_file returns an existing conversion
* record with matching sourcefileid and targetformat when a file with the same
* contenthash is uploaded several times.
*
* @covers \core_files\conversion::get_conversions_for_file
*/
public function test_get_conversions_for_multiple_files_existing_conversion_incomplete() {
$this->resetAfterTest();
// Create a bunch of files with the same content.
for ($i = 0; $i < 5; $i++) {
$sourcefiles[] = $this->create_stored_file('test content', 'testfile' . $i . '.txt');
}
// Use only one file for the conversion.
// Pick some file in the middle.
$sourcefile = $sourcefiles[count($sourcefiles) - 2];
$existing = new conversion(0, (object) [
'sourcefileid' => $sourcefile->get_id(),
'targetformat' => 'pdf',
]);
$existing->create();
$conversions = conversion::get_conversions_for_file($sourcefile, 'pdf');
$this->assertCount(1, $conversions);
$conversion = array_shift($conversions);
$conversionfile = $conversion->get_sourcefile();
$this->assertEquals($sourcefile->get_id(), $conversionfile->get_id());
$this->assertFalse($conversion->get_destfile());
// Check that getting the conversion for a different file record with the same contenthash
// returns the same conversion as above.
$conversions = conversion::get_conversions_for_file($sourcefiles[count($sourcefiles) - 1], 'pdf');
$this->assertCount(1, $conversions);
$conversion = array_shift($conversions);
$conversionfile = $conversion->get_sourcefile();
$this->assertEquals($existing->get('id'), $conversion->get('id'));
$this->assertEquals($sourcefile->get_id(), $conversionfile->get_id());
$this->assertFalse($conversion->get_destfile());
}
/**
* Ensure that get_conversions_for_file returns an existing conversion
* record with matching sourcefileid and targetformat when a second
@ -139,6 +186,7 @@ class conversion_test extends \advanced_testcase {
$conversion = array_shift($conversions);
$this->assertEquals($existing->get('id'), $conversion->get('id'));
$this->assertEquals($sourcefile->get_id(), $conversion->get_sourcefile()->get_id());
$this->assertEquals($destfile->get_id(), $conversion->get_destfile()->get_id());
}