diff --git a/lib/tests/fixtures/pandoc-source.docx b/lib/tests/fixtures/pandoc-source.docx new file mode 100644 index 00000000000..286c58a8926 Binary files /dev/null and b/lib/tests/fixtures/pandoc-source.docx differ diff --git a/lib/tests/fixtures/pandoc-source.html b/lib/tests/fixtures/pandoc-source.html new file mode 100644 index 00000000000..45ad289a658 --- /dev/null +++ b/lib/tests/fixtures/pandoc-source.html @@ -0,0 +1,12 @@ + + + + +

My First Heading

+ +

My first paragraph.

+ + + + + diff --git a/lib/tests/pandoc_test.php b/lib/tests/pandoc_test.php new file mode 100644 index 00000000000..68fcdcc1202 --- /dev/null +++ b/lib/tests/pandoc_test.php @@ -0,0 +1,104 @@ +. + +/** + * Test pandoc functionality. + * + * @package core + * @category phpunit + * @copyright 2016 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * A set of tests for some of the pandoc functionality within Moodle. + * + * @package core + * @category phpunit + * @copyright 2016 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_pandoc_testcase extends advanced_testcase { + + private $testfile1 = null; + private $testfile2 = null; + + public function setUp() { + $this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR; + + $fs = get_file_storage(); + $filerecord = array( + 'contextid' => context_system::instance()->id, + 'component' => 'test', + 'filearea' => 'unittest', + 'itemid' => 0, + 'filepath' => '/', + 'filename' => 'test.html' + ); + $teststring = file_get_contents($this->fixturepath . DIRECTORY_SEPARATOR . 'pandoc-source.html'); + $this->testfile1 = $fs->create_file_from_string($filerecord, $teststring); + + $filerecord = array( + 'contextid' => context_system::instance()->id, + 'component' => 'test', + 'filearea' => 'unittest', + 'itemid' => 0, + 'filepath' => '/', + 'filename' => 'test.docx' + ); + $teststring = file_get_contents($this->fixturepath . DIRECTORY_SEPARATOR . 'pandoc-source.docx'); + $this->testfile2 = $fs->create_file_from_string($filerecord, $teststring); + + $this->resetAfterTest(); + } + + public function test_generate_pdf() { + global $CFG; + + if (empty($CFG->pathtopandoc) || !is_executable(trim($CFG->pathtopandoc))) { + // No conversions are possible, sorry. + return $this->markTestSkipped(); + } + $fs = get_file_storage(); + + $result = $fs->get_converted_document($this->testfile1, 'pdf'); + $this->assertSame($result->get_mimetype(), 'application/pdf'); + $this->assertGreaterThan(0, $result->get_filesize()); + $result = $fs->get_converted_document($this->testfile2, 'pdf'); + $this->assertSame($result->get_mimetype(), 'application/pdf'); + $this->assertGreaterThan(0, $result->get_filesize()); + } + + public function test_generate_markdown() { + global $CFG; + + if (empty($CFG->pathtopandoc) || !is_executable(trim($CFG->pathtopandoc))) { + // No conversions are possible, sorry. + return $this->markTestSkipped(); + } + $fs = get_file_storage(); + + $result = $fs->get_converted_document($this->testfile1, 'md'); + $this->assertSame($result->get_mimetype(), 'text/plain'); + $this->assertGreaterThan(0, $result->get_filesize()); + $result = $fs->get_converted_document($this->testfile2, 'md'); + $this->assertSame($result->get_mimetype(), 'text/plain'); + $this->assertGreaterThan(0, $result->get_filesize()); + } +}