MDL-55528 fileconverter_unoconv: Unit test correction
[moodle.git] / files / converter / unoconv / tests / converter_test.php
CommitLineData
15681687
AN
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/>.
16
17/**
18 * Test unoconv functionality.
19 *
20 * @package core
21 * @copyright 2016 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27
28/**
29 * A set of tests for some of the unoconv functionality within Moodle.
30 *
31 * @package core
32 * @copyright 2016 Damyon Wiese
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class fileconverter_unoconv_converter_testcase extends advanced_testcase {
36
37 /**
38 * Helper to skip tests which _require_ unoconv.
39 */
40 protected function require_unoconv() {
41 global $CFG;
42
43 if (empty($CFG->pathtounoconv) || !file_is_executable(trim($CFG->pathtounoconv))) {
44 // No conversions are possible, sorry.
45 $this->markTestSkipped();
46 }
47 }
48
49 /**
50 * Get a testable mock of the fileconverter_unoconv class.
51 *
52 * @param array $mockedmethods A list of methods you intend to override
53 * If no methods are specified, only abstract functions are mocked.
54 * @return \fileconverter_unoconv\converter
55 */
56 protected function get_testable_mock($mockedmethods = null) {
57 $converter = $this->getMockBuilder(\fileconverter_unoconv\converter::class)
58 ->setMethods($mockedmethods)
59 ->getMock();
60
61 return $converter;
62 }
63
64 /**
65 * Tests for the start_document_conversion function.
66 */
67 public function test_start_document_conversion() {
68 $this->resetAfterTest();
69
70 $this->require_unoconv();
71
72 // Mock the file to be converted.
73 $filerecord = [
74 'contextid' => context_system::instance()->id,
75 'component' => 'test',
76 'filearea' => 'unittest',
77 'itemid' => 0,
78 'filepath' => '/',
79 'filename' => 'test.docx',
80 ];
81 $fs = get_file_storage();
82 $source = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'unoconv-source.docx';
83 $testfile = $fs->create_file_from_pathname($filerecord, $source);
84
85 $converter = $this->get_testable_mock();
ac1c66a0
AN
86 $conversion = new \core_files\conversion(0, (object) [
87 'targetformat' => 'pdf',
88 ]);
89 $conversion->set_sourcefile($testfile);
90 $conversion->create();
15681687
AN
91
92 // Convert the document.
ac1c66a0
AN
93 $converter->start_document_conversion($conversion);
94 $result = $conversion->get_destfile();
15681687
AN
95 $this->assertNotFalse($result);
96 $this->assertSame('application/pdf', $result->get_mimetype());
97 $this->assertGreaterThan(0, $result->get_filesize());
15681687
AN
98 }
99
100 /**
101 * Tests for the test_unoconv_path function.
102 *
103 * @dataProvider provider_test_unoconv_path
104 * @param string $path The path to test
105 * @param int $status The expected status
106 */
107 public function test_test_unoconv_path($path, $status) {
108 global $CFG;
109
110 $this->resetAfterTest();
111
112 // Set the current path.
113 $CFG->pathtounoconv = $path;
114
115 // Run the tests.
116 $result = \fileconverter_unoconv\converter::test_unoconv_path();
117
118 $this->assertEquals($status, $result->status);
119 }
120
121 /**
122 * Provider for test_unoconv_path.
123 *
124 * @return array
125 */
126 public function provider_test_unoconv_path() {
127 return [
128 'Empty path' => [
129 'path' => null,
130 'status' => \fileconverter_unoconv\converter::UNOCONVPATH_EMPTY,
131 ],
132 'Invalid file' => [
133 'path' => '/path/to/nonexistent/file',
134 'status' => \fileconverter_unoconv\converter::UNOCONVPATH_DOESNOTEXIST,
135 ],
136 'Directory' => [
137 'path' => __DIR__,
138 'status' => \fileconverter_unoconv\converter::UNOCONVPATH_ISDIR,
139 ],
140 ];
141 }
142}