Merge branch 'MDL-58272-master-2' of https://github.com/snake/moodle
[moodle.git] / files / tests / converter_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * PHPUnit tests for fileconverter API.
20  *
21  * @package    core_files
22  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 use core_files\conversion;
30 use core_files\converter;
32 /**
33  * PHPUnit tests for fileconverter API.
34  *
35  * @package    core_files
36  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class core_files_converter_testcase extends advanced_testcase {
41     /**
42      * Get a testable mock of the abstract files_converter class.
43      *
44      * @param   array   $mockedmethods A list of methods you intend to override
45      *                  If no methods are specified, only abstract functions are mocked.
46      * @return  \core_files\converter
47      */
48     protected function get_testable_mock($mockedmethods = []) {
49         $converter = $this->getMockBuilder(\core_files\converter::class)
50             ->setMethods($mockedmethods)
51             ->getMockForAbstractClass();
53         return $converter;
54     }
56     /**
57      * Get a testable mock of the conversion.
58      *
59      * @param   array   $mockedmethods A list of methods you intend to override
60      * @return  \core_files\conversion
61      */
62     protected function get_testable_conversion($mockedmethods = []) {
63         $conversion = $this->getMockBuilder(\core_files\conversion::class)
64             ->setMethods($mockedmethods)
65             ->setConstructorArgs([0, (object) []])
66             ->getMock();
68         return $conversion;
69     }
71     /**
72      * Get a testable mock of the abstract files_converter class.
73      *
74      * @param   array   $mockedmethods A list of methods you intend to override
75      *                  If no methods are specified, only abstract functions are mocked.
76      * @return  \core_files\converter_interface
77      */
78     protected function get_mocked_converter($mockedmethods = []) {
79         $converter = $this->getMockBuilder(\core_files\converter_interface::class)
80             ->setMethods($mockedmethods)
81             ->getMockForAbstractClass();
83         return $converter;
84     }
86     /**
87      * Helper to create a stored file objectw with the given supplied content.
88      *
89      * @param   string  $filecontent The content of the mocked file
90      * @param   string  $filename The file name to use in the stored_file
91      * @param   array   $mockedmethods A list of methods you intend to override
92      *                  If no methods are specified, only abstract functions are mocked.
93      * @return  stored_file
94      */
95     protected function get_stored_file($filecontent = 'content', $filename = null, $filerecord = [], $mockedmethods = null) {
96         global $CFG;
98         $contenthash = sha1($filecontent);
99         if (empty($filename)) {
100             $filename = $contenthash;
101         }
103         $filerecord['contenthash'] = $contenthash;
104         $filerecord['filesize'] = strlen($filecontent);
105         $filerecord['filename'] = $filename;
106         $filerecord['id'] = 42;
108         $file = $this->getMockBuilder(stored_file::class)
109             ->setMethods($mockedmethods)
110             ->setConstructorArgs([get_file_storage(), (object) $filerecord])
111             ->getMock();
113         return $file;
114     }
116     /**
117      * Helper to create a stored file object with the given supplied content.
118      *
119      * @param   string $filecontent The content of the mocked file
120      * @param   string $filename The file name to use in the stored_file
121      * @param   string $filerecord Any overrides to the filerecord
122      * @return  stored_file
123      */
124     protected function create_stored_file($filecontent = 'content', $filename = 'testfile.txt', $filerecord = []) {
125         $filerecord = array_merge([
126                 'contextid' => context_system::instance()->id,
127                 'component' => 'core',
128                 'filearea'  => 'unittest',
129                 'itemid'    => 0,
130                 'filepath'  => '/',
131                 'filename'  => $filename,
132             ], $filerecord);
134         $fs = get_file_storage();
135         $file = $fs->create_file_from_string($filerecord, $filecontent);
137         return $file;
138     }
140     /**
141      * Get a mock of the file_storage API.
142      *
143      * @param   array   $mockedmethods A list of methods you intend to override
144      * @return  file_storage
145      */
146     protected function get_file_storage_mock($mockedmethods = []) {
147         $fs = $this->getMockBuilder(\file_storage::class)
148             ->setMethods($mockedmethods)
149             ->disableOriginalConstructor()
150             ->getMock();
152         return $fs;
153     }
155     /**
156      * Test the start_conversion function.
157      */
158     public function test_start_conversion_existing_single() {
159         $this->resetAfterTest();
161         $sourcefile = $this->create_stored_file();
163         $first = new conversion(0, (object) [
164                 'sourcefileid' => $sourcefile->get_id(),
165                 'targetformat' => 'pdf',
166             ]);
167         $first->create();
169         $converter = $this->get_testable_mock(['poll_conversion']);
170         $conversion = $converter->start_conversion($sourcefile, 'pdf', false);
172         // The old conversions should still be present and match the one returned.
173         $this->assertEquals($first->get('id'), $conversion->get('id'));
174     }
176     /**
177      * Test the start_conversion function.
178      */
179     public function test_start_conversion_existing_multiple() {
180         $this->resetAfterTest();
182         $sourcefile = $this->create_stored_file();
184         $first = new conversion(0, (object) [
185                 'sourcefileid' => $sourcefile->get_id(),
186                 'targetformat' => 'pdf',
187             ]);
188         $first->create();
190         $second = new conversion(0, (object) [
191                 'sourcefileid' => $sourcefile->get_id(),
192                 'targetformat' => 'pdf',
193             ]);
194         $second->create();
196         $converter = $this->get_testable_mock(['poll_conversion']);
197         $conversion = $converter->start_conversion($sourcefile, 'pdf', false);
199         // The old conversions should have been removed.
200         $this->assertFalse(conversion::get_record(['id' => $first->get('id')]));
201         $this->assertFalse(conversion::get_record(['id' => $second->get('id')]));
202     }
204     /**
205      * Test the start_conversion function.
206      */
207     public function test_start_conversion_no_existing() {
208         $this->resetAfterTest();
210         $sourcefile = $this->create_stored_file();
212         $converter = $this->get_testable_mock(['poll_conversion']);
213         $conversion = $converter->start_conversion($sourcefile, 'pdf', false);
215         $this->assertInstanceOf(\core_files\conversion::class, $conversion);
216     }
218     /**
219      * Test the get_document_converter_classes function with no enabled plugins.
220      */
221     public function test_get_document_converter_classes_no_plugins() {
222         $converter = $this->get_testable_mock(['get_enabled_plugins']);
223         $converter->method('get_enabled_plugins')->willReturn([]);
225         $method = new ReflectionMethod(\core_files\converter::class, 'get_document_converter_classes');
226         $method->setAccessible(true);
227         $result = $method->invokeArgs($converter, ['docx', 'pdf']);
228         $this->assertEmpty($result);
229     }
231     /**
232      * Test the get_document_converter_classes function when no class was found.
233      */
234     public function test_get_document_converter_classes_plugin_class_not_found() {
235         $converter = $this->get_testable_mock(['get_enabled_plugins']);
236         $converter->method('get_enabled_plugins')->willReturn([
237                 'noplugin' => '\not\a\real\plugin',
238             ]);
240         $method = new ReflectionMethod(\core_files\converter::class, 'get_document_converter_classes');
241         $method->setAccessible(true);
242         $result = $method->invokeArgs($converter, ['docx', 'pdf']);
243         $this->assertEmpty($result);
244     }
246     /**
247      * Test the get_document_converter_classes function when the returned classes do not meet requirements.
248      */
249     public function test_get_document_converter_classes_plugin_class_requirements_not_met() {
250         $plugin = $this->getMockBuilder(\core_file_converter_requirements_not_met_test::class)
251             ->setMethods()
252             ->getMock();
254         $converter = $this->get_testable_mock(['get_enabled_plugins']);
255         $converter->method('get_enabled_plugins')->willReturn([
256                 'test_plugin' => get_class($plugin),
257             ]);
259         $method = new ReflectionMethod(\core_files\converter::class, 'get_document_converter_classes');
260         $method->setAccessible(true);
261         $result = $method->invokeArgs($converter, ['docx', 'pdf']);
262         $this->assertEmpty($result);
263     }
265     /**
266      * Test the get_document_converter_classes function when the returned classes do not meet requirements.
267      */
268     public function test_get_document_converter_classes_plugin_class_met_not_supported() {
269         $plugin = $this->getMockBuilder(\core_file_converter_type_not_supported_test::class)
270             ->setMethods()
271             ->getMock();
273         $converter = $this->get_testable_mock(['get_enabled_plugins']);
274         $converter->method('get_enabled_plugins')->willReturn([
275                 'test_plugin' => get_class($plugin),
276             ]);
278         $method = new ReflectionMethod(\core_files\converter::class, 'get_document_converter_classes');
279         $method->setAccessible(true);
280         $result = $method->invokeArgs($converter, ['docx', 'pdf']);
281         $this->assertEmpty($result);
282     }
284     /**
285      * Test the get_document_converter_classes function when the returned classes do not meet requirements.
286      */
287     public function test_get_document_converter_classes_plugin_class_met_and_supported() {
288         $plugin = $this->getMockBuilder(\core_file_converter_type_supported_test::class)
289             ->setMethods()
290             ->getMock();
291         $classname = get_class($plugin);
293         $converter = $this->get_testable_mock(['get_enabled_plugins']);
294         $converter->method('get_enabled_plugins')->willReturn([
295                 'test_plugin' => $classname,
296             ]);
298         $method = new ReflectionMethod(\core_files\converter::class, 'get_document_converter_classes');
299         $method->setAccessible(true);
300         $result = $method->invokeArgs($converter, ['docx', 'pdf']);
301         $this->assertCount(1, $result);
302         $this->assertNotFalse(array_search($classname, $result));
303     }
305     /**
306      * Test the can_convert_storedfile_to function with a directory.
307      */
308     public function test_can_convert_storedfile_to_directory() {
309         $converter = $this->get_testable_mock();
311         // A file with filename '.' is a directory.
312         $file = $this->get_stored_file('', '.');
314         $this->assertFalse($converter->can_convert_storedfile_to($file, 'target'));
315     }
317     /**
318      * Test the can_convert_storedfile_to function with an empty file.
319      */
320     public function test_can_convert_storedfile_to_emptyfile() {
321         $converter = $this->get_testable_mock();
323         // A file with filename '.' is a directory.
324         $file = $this->get_stored_file('');
326         $this->assertFalse($converter->can_convert_storedfile_to($file, 'target'));
327     }
329     /**
330      * Test the can_convert_storedfile_to function with a file with indistinguished mimetype.
331      */
332     public function test_can_convert_storedfile_to_no_mimetype() {
333         $converter = $this->get_testable_mock();
335         // A file with filename '.' is a directory.
336         $file = $this->get_stored_file('example content', 'example', [
337                 'mimetype' => null,
338             ]);
340         $this->assertFalse($converter->can_convert_storedfile_to($file, 'target'));
341     }
343     /**
344      * Test the can_convert_storedfile_to function with a file with a known mimetype and extension.
345      */
346     public function test_can_convert_storedfile_to_docx() {
347         $returnvalue = (object) [];
349         $converter = $this->get_testable_mock([
350                 'can_convert_format_to'
351             ]);
353         $types = \core_filetypes::get_types();
355         $file = $this->get_stored_file('example content', 'example.docx', [
356                 'mimetype' => $types['docx']['type'],
357             ]);
359         $converter->expects($this->once())
360             ->method('can_convert_format_to')
361             ->willReturn($returnvalue);
363         $result = $converter->can_convert_storedfile_to($file, 'target');
364         $this->assertEquals($returnvalue, $result);
365     }
368     /**
369      * Test the can_convert_format_to function.
370      */
371     public function test_can_convert_format_to_found() {
372         $converter = $this->get_testable_mock(['get_document_converter_classes']);
374         $mock = $this->get_mocked_converter();
376         $converter->method('get_document_converter_classes')
377             ->willReturn([$mock]);
379         $result = $converter->can_convert_format_to('from', 'to');
380         $this->assertTrue($result);
381     }
383     /**
384      * Test the can_convert_format_to function.
385      */
386     public function test_can_convert_format_to_not_found() {
387         $converter = $this->get_testable_mock(['get_document_converter_classes']);
389         $converter->method('get_document_converter_classes')
390             ->willReturn([]);
392         $result = $converter->can_convert_format_to('from', 'to');
393         $this->assertFalse($result);
394     }
396     /**
397      * Test the can_convert_storedfile_to function with an empty file.
398      */
399     public function test_poll_conversion_in_progress() {
400         $this->resetAfterTest();
402         $converter = $this->get_testable_mock([
403                 'get_document_converter_classes',
404                 'get_next_converter',
405             ]);
407         $converter->method('get_document_converter_classes')->willReturn([]);
408         $converter->method('get_next_converter')->willReturn(false);
409         $file = $this->create_stored_file('example content', 'example', [
410                 'mimetype' => null,
411             ]);
413         $conversion = $this->get_testable_conversion([
414                 'get_converter_instance',
415             ]);
416         $conversion->set_sourcefile($file);
417         $conversion->set('targetformat', 'target');
418         $conversion->set('status', conversion::STATUS_IN_PROGRESS);
419         $conversion->create();
421         $converterinstance = $this->get_mocked_converter([
422                 'poll_conversion_status',
423             ]);
424         $converterinstance->expects($this->once())
425             ->method('poll_conversion_status');
426         $conversion->method('get_converter_instance')->willReturn($converterinstance);
428         $converter->poll_conversion($conversion);
430         $this->assertEquals(conversion::STATUS_IN_PROGRESS, $conversion->get('status'));
431     }
433     /**
434      * Test poll_conversion with an in-progress conversion where we are
435      * unable to instantiate the converter instance.
436      */
437     public function test_poll_conversion_in_progress_fail() {
438         $this->resetAfterTest();
440         $converter = $this->get_testable_mock([
441                 'get_document_converter_classes',
442                 'get_next_converter',
443             ]);
445         $converter->method('get_document_converter_classes')->willReturn([]);
446         $converter->method('get_next_converter')->willReturn(false);
447         $file = $this->create_stored_file('example content', 'example', [
448                 'mimetype' => null,
449             ]);
451         $conversion = $this->get_testable_conversion([
452                 'get_converter_instance',
453             ]);
454         $conversion->set_sourcefile($file);
455         $conversion->set('targetformat', 'target');
456         $conversion->set('status', conversion::STATUS_IN_PROGRESS);
457         $conversion->create();
459         $conversion->method('get_converter_instance')->willReturn(false);
461         $converter->poll_conversion($conversion);
463         $this->assertEquals(conversion::STATUS_FAILED, $conversion->get('status'));
464     }
466     /**
467      * Test the can_convert_storedfile_to function with an empty file.
468      */
469     public function test_poll_conversion_none_supported() {
470         $this->resetAfterTest();
472         $converter = $this->get_testable_mock([
473                 'get_document_converter_classes',
474                 'get_next_converter',
475             ]);
477         $converter->method('get_document_converter_classes')->willReturn([]);
478         $converter->method('get_next_converter')->willReturn(false);
479         $file = $this->create_stored_file('example content', 'example', [
480                 'mimetype' => null,
481             ]);
483         $conversion = new conversion(0, (object) [
484             'sourcefileid' => $file->get_id(),
485             'targetformat' => 'target',
486         ]);
487         $conversion->create();
489         $converter->poll_conversion($conversion);
491         $this->assertEquals(conversion::STATUS_FAILED, $conversion->get('status'));
492     }
494     /**
495      * Test the can_convert_storedfile_to function with an empty file.
496      */
497     public function test_poll_conversion_pick_first() {
498         $this->resetAfterTest();
500         $converterinstance = $this->get_mocked_converter([
501                 'start_document_conversion',
502                 'poll_conversion_status',
503             ]);
504         $converter = $this->get_testable_mock([
505                 'get_document_converter_classes',
506                 'get_next_converter',
507             ]);
509         $converter->method('get_document_converter_classes')->willReturn([]);
510         $converter->method('get_next_converter')->willReturn(get_class($converterinstance));
511         $file = $this->create_stored_file('example content', 'example', [
512                 'mimetype' => null,
513             ]);
515         $conversion = $this->get_testable_conversion([
516                 'get_converter_instance',
517             ]);
518         $conversion->set_sourcefile($file);
519         $conversion->set('targetformat', 'target');
520         $conversion->set('status', conversion::STATUS_PENDING);
521         $conversion->create();
523         $conversion->method('get_converter_instance')->willReturn($converterinstance);
525         $converterinstance->expects($this->once())
526             ->method('start_document_conversion');
527         $converterinstance->expects($this->never())
528             ->method('poll_conversion_status');
530         $converter->poll_conversion($conversion);
532         $this->assertEquals(conversion::STATUS_IN_PROGRESS, $conversion->get('status'));
533     }
535     /**
536      * Test the can_convert_storedfile_to function with an empty file.
537      */
538     public function test_poll_conversion_pick_subsequent() {
539         $this->resetAfterTest();
541         $converterinstance = $this->get_mocked_converter([
542                 'start_document_conversion',
543                 'poll_conversion_status',
544             ]);
545         $converterinstance2 = $this->get_mocked_converter([
546                 'start_document_conversion',
547                 'poll_conversion_status',
548             ]);
549         $converter = $this->get_testable_mock([
550                 'get_document_converter_classes',
551                 'get_next_converter',
552             ]);
554         $converter->method('get_document_converter_classes')->willReturn([]);
555         $converter->method('get_next_converter')
556             ->will($this->onConsecutiveCalls(
557                 get_class($converterinstance),
558                 get_class($converterinstance2)
559             ));
561         $file = $this->create_stored_file('example content', 'example', [
562                 'mimetype' => null,
563             ]);
565         $conversion = $this->get_testable_conversion([
566                 'get_converter_instance',
567                 'get_status',
568             ]);
569         $conversion->set_sourcefile($file);
570         $conversion->set('targetformat', 'target');
571         $conversion->set('status', conversion::STATUS_PENDING);
572         $conversion->create();
574         $conversion->method('get_status')
575             ->will($this->onConsecutiveCalls(
576                 // Initial status check.
577                 conversion::STATUS_PENDING,
578                 // Second check to make sure it's still pending after polling.
579                 conversion::STATUS_PENDING,
580                 // First one fails.
581                 conversion::STATUS_FAILED,
582                 // Second one succeeds.
583                 conversion::STATUS_COMPLETE,
584                 // And the final result checked in this unit test.
585                 conversion::STATUS_COMPLETE
586             ));
588         $conversion->method('get_converter_instance')
589             ->will($this->onConsecutiveCalls(
590                 $converterinstance,
591                 $converterinstance2
592             ));
594         $converterinstance->expects($this->once())
595             ->method('start_document_conversion');
596         $converterinstance->expects($this->never())
597             ->method('poll_conversion_status');
598         $converterinstance2->expects($this->once())
599             ->method('start_document_conversion');
600         $converterinstance2->expects($this->never())
601             ->method('poll_conversion_status');
603         $converter->poll_conversion($conversion);
605         $this->assertEquals(conversion::STATUS_COMPLETE, $conversion->get('status'));
606     }
608     /**
609      * Test the start_conversion with a single converter which succeeds.
610      */
611     public function test_start_conversion_one_supported_success() {
612         $this->resetAfterTest();
614         $converter = $this->get_testable_mock([
615                 'get_document_converter_classes',
616             ]);
618         $converter->method('get_document_converter_classes')
619             ->willReturn([\core_file_converter_type_successful::class]);
621         $file = $this->create_stored_file('example content', 'example', [
622                 'mimetype' => null,
623             ]);
625         $conversion = $converter->start_conversion($file, 'target');
627         $this->assertEquals(conversion::STATUS_COMPLETE, $conversion->get('status'));
628     }
630     /**
631      * Test the start_conversion with a single converter which failes.
632      */
633     public function test_start_conversion_one_supported_failure() {
634         $this->resetAfterTest();
636         $converter = $this->get_testable_mock([
637                 'get_document_converter_classes',
638             ]);
640         $mock = $this->get_mocked_converter(['start_document_conversion']);
641         $converter->method('get_document_converter_classes')
642             ->willReturn([\core_file_converter_type_failed::class]);
644         $file = $this->create_stored_file('example content', 'example', [
645                 'mimetype' => null,
646             ]);
648         $conversion = $converter->start_conversion($file, 'target');
650         $this->assertEquals(conversion::STATUS_FAILED, $conversion->get('status'));
651     }
653     /**
654      * Test the start_conversion with two converters - fail, then succeed.
655      */
656     public function test_start_conversion_two_supported() {
657         $this->resetAfterTest();
659         $converter = $this->get_testable_mock([
660                 'get_document_converter_classes',
661             ]);
663         $mock = $this->get_mocked_converter(['start_document_conversion']);
664         $converter->method('get_document_converter_classes')
665             ->willReturn([
666                 \core_file_converter_type_failed::class,
667                 \core_file_converter_type_successful::class,
668             ]);
670         $file = $this->create_stored_file('example content', 'example', [
671                 'mimetype' => null,
672             ]);
674         $conversion = $converter->start_conversion($file, 'target');
676         $this->assertEquals(conversion::STATUS_COMPLETE, $conversion->get('status'));
677     }
679     /**
680      * Ensure that get_next_converter returns false when no converters are available.
681      */
682     public function test_get_next_converter_no_converters() {
683         $rcm = new \ReflectionMethod(converter::class, 'get_next_converter');
684         $rcm->setAccessible(true);
686         $converter = new \core_files\converter();
687         $result = $rcm->invoke($converter, [], null);
688         $this->assertFalse($result);
689     }
691     /**
692      * Ensure that get_next_converter returns false when already on the
693      * only converter.
694      */
695     public function test_get_next_converter_only_converters() {
696         $rcm = new \ReflectionMethod(converter::class, 'get_next_converter');
697         $rcm->setAccessible(true);
699         $converter = new converter();
700         $result = $rcm->invoke($converter, ['example'], 'example');
701         $this->assertFalse($result);
702     }
704     /**
705      * Ensure that get_next_converter returns false when already on the
706      * last converter.
707      */
708     public function test_get_next_converter_last_converters() {
709         $rcm = new \ReflectionMethod(converter::class, 'get_next_converter');
710         $rcm->setAccessible(true);
712         $converter = new converter();
713         $result = $rcm->invoke($converter, ['foo', 'example'], 'example');
714         $this->assertFalse($result);
715     }
717     /**
718      * Ensure that get_next_converter returns the next vlaue when in a
719      * current converter.
720      */
721     public function test_get_next_converter_middle_converters() {
722         $rcm = new \ReflectionMethod(converter::class, 'get_next_converter');
723         $rcm->setAccessible(true);
725         $converter = new converter();
726         $result = $rcm->invoke($converter, ['foo', 'bar', 'baz', 'example'], 'bar');
727         $this->assertEquals('baz', $result);
728     }
729     /**
730      *
731      * Ensure that get_next_converter returns the next vlaue when in a
732      * current converter.
733      */
734     public function test_get_next_converter_first() {
735         $rcm = new \ReflectionMethod(converter::class, 'get_next_converter');
736         $rcm->setAccessible(true);
738         $converter = new converter();
739         $result = $rcm->invoke($converter, ['foo', 'bar', 'baz', 'example']);
740         $this->assertEquals('foo', $result);
741     }
744 class core_file_converter_requirements_test_base implements \core_files\converter_interface {
746     /**
747      * Whether the plugin is configured and requirements are met.
748      *
749      * @return  bool
750      */
751     public static function are_requirements_met() {
752         return false;
753     }
755     /**
756      * Convert a document to a new format and return a conversion object relating to the conversion in progress.
757      *
758      * @param   conversion $conversion The file to be converted
759      * @return  conversion
760      */
761     public function start_document_conversion(conversion $conversion) {
762     }
764     /**
765      * Poll an existing conversion for status update.
766      *
767      * @param   conversion $conversion The file to be converted
768      * @return  conversion
769      */
770     public function poll_conversion_status(conversion $conversion) {
771     }
773     /**
774      * Whether a file conversion can be completed using this converter.
775      *
776      * @param   string $from The source type
777      * @param   string $to The destination type
778      * @return  bool
779      */
780     public static function supports($from, $to) {
781         return false;
782     }
784     /**
785      * A list of the supported conversions.
786      *
787      * @return  string
788      */
789     public function  get_supported_conversions() {
790         return [];
791     }
795 /**
796  * Test class for converter support with requirements are not met.
797  *
798  * @package    core_files
799  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
800  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
801  */
802 class core_file_converter_requirements_not_met_test extends core_file_converter_requirements_test_base {
805 /**
806  * Test class for converter support with requirements met and conversion not supported.
807  *
808  * @package    core_files
809  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
810  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
811  */
812 class core_file_converter_type_not_supported_test extends core_file_converter_requirements_test_base {
814     /**
815      * Whether the plugin is configured and requirements are met.
816      *
817      * @return  bool
818      */
819     public static function are_requirements_met() {
820         return true;
821     }
824 /**
825  * Test class for converter support with requirements met and conversion supported.
826  *
827  * @package    core_files
828  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
829  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
830  */
831 class core_file_converter_type_supported_test extends core_file_converter_requirements_test_base {
833     /**
834      * Whether the plugin is configured and requirements are met.
835      *
836      * @return  bool
837      */
838     public static function are_requirements_met() {
839         return true;
840     }
842     /**
843      * Whether a file conversion can be completed using this converter.
844      *
845      * @param   string $from The source type
846      * @param   string $to The destination type
847      * @return  bool
848      */
849     public static function supports($from, $to) {
850         return true;
851     }
854 /**
855  * Test class for converter support with requirements met and successful conversion.
856  *
857  * @package    core_files
858  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
859  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
860  */
861 class core_file_converter_type_successful extends core_file_converter_requirements_test_base {
863     /**
864      * Convert a document to a new format and return a conversion object relating to the conversion in progress.
865      *
866      * @param   conversion $conversion The file to be converted
867      * @return  conversion
868      */
869     public function start_document_conversion(conversion $conversion) {
870         $conversion->set('status', conversion::STATUS_COMPLETE);
872         return $conversion;
873     }
875     /**
876      * Whether a file conversion can be completed using this converter.
877      *
878      * @param   string $from The source type
879      * @param   string $to The destination type
880      * @return  bool
881      */
882     public static function supports($from, $to) {
883         return true;
884     }
887 /**
888  * Test class for converter support with requirements met and failed conversion.
889  *
890  * @package    core_files
891  * @copyright  2017 Andrew nicols <andrew@nicols.co.uk>
892  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
893  */
894 class core_file_converter_type_failed extends core_file_converter_requirements_test_base {
896     /**
897      * Whether the plugin is configured and requirements are met.
898      *
899      * @return  bool
900      */
901     public static function are_requirements_met() {
902         return true;
903     }
905     /**
906      * Convert a document to a new format and return a conversion object relating to the conversion in progress.
907      *
908      * @param   conversion $conversion The file to be converted
909      * @return  conversion
910      */
911     public function start_document_conversion(conversion $conversion) {
912         $conversion->set('status', conversion::STATUS_FAILED);
914         return $conversion;
915     }
917     /**
918      * Whether a file conversion can be completed using this converter.
919      *
920      * @param   string $from The source type
921      * @param   string $to The destination type
922      * @return  bool
923      */
924     public static function supports($from, $to) {
925         return true;
926     }