Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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