Proyectos de Subversion Moodle

Rev

| 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
declare(strict_types=1);
18
 
19
namespace core\content\export\exporters;
20
 
21
use advanced_testcase;
22
use context_course;
23
use context_module;
24
use ZipArchive;
25
use core\content\export\zipwriter;
26
 
27
/**
28
 * Unit tests for activity exporter.
29
 *
30
 * @package     core
31
 * @category    test
32
 * @copyright   2020 Simey Lameze <simey@moodle.com>
33
 * @license     http://www.gnu.org/copyleft/gpl.html GNU Public License
34
 * @covers      \core\content\export\exporters\course_exporter
35
 */
36
class course_exporter_test extends advanced_testcase {
37
 
38
    /**
39
     * The course_exporter should still export a module intro when no exportables are passed.
40
     */
41
    public function test_no_exportables_exported(): void {
42
        $this->resetAfterTest(true);
43
 
44
        $generator = $this->getDataGenerator();
45
 
46
        $course = $generator->create_course();
47
        $coursecontext = context_course::instance($course->id);
48
 
49
        $intro = 'XX Some introduction should go here XX';
50
        $content = 'YY Some content should go here YY';
51
        $module = $generator->create_module('page', [
52
            'course' => $course->id,
53
            'intro' => $intro,
54
            'content' => $content,
55
        ]);
56
        $modcontext = context_module::instance($module->cmid);
57
 
58
        $user = $generator->create_user();
59
        $generator->enrol_user($user->id, $course->id);
60
 
61
        // Only the module index should be added.
62
        $archive = $this->get_mocked_zipwriter(['add_file_from_string']);
63
        $archive->expects($this->once())
64
            ->method('add_file_from_string')
65
            ->with(
66
                $modcontext,
67
                'index.html',
68
                $this->callback(function($html) use ($intro, $content): bool {
69
                    if (strpos($html, $intro) === false) {
70
                        return false;
71
                    }
72
 
73
                    if (strpos($html, $content) !== false) {
74
                        // The content as not exported.
75
                        return false;
76
                    }
77
 
78
                    return true;
79
                })
80
            );
81
        $archive->set_root_context($coursecontext);
82
 
83
        $coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
84
        $coursecontroller->export_mod_content($modcontext, []);
85
    }
86
 
87
    /**
88
     * The course_exporter should still export exportables as well as module intro.
89
     */
90
    public function test_exportables_exported(): void {
91
        $this->resetAfterTest(true);
92
 
93
        $generator = $this->getDataGenerator();
94
 
95
        $course = $generator->create_course();
96
        $coursecontext = context_course::instance($course->id);
97
 
98
        $intro = 'XX Some introduction should go here XX';
99
        $content = 'YY Some content should go here YY';
100
        $module = $generator->create_module('page', [
101
            'course' => $course->id,
102
            'intro' => $intro,
103
            'content' => $content,
104
        ]);
105
        $modcontext = context_module::instance($module->cmid);
106
 
107
        $user = $generator->create_user();
108
        $generator->enrol_user($user->id, $course->id);
109
 
110
        // Only the module index should be added.
111
        $archive = $this->get_mocked_zipwriter(['add_file_from_string']);
112
        $archive->expects($this->once())
113
            ->method('add_file_from_string')
114
            ->with(
115
                $modcontext,
116
                'index.html',
117
                $this->callback(function($html) use ($intro, $content): bool {
118
                    if (strpos($html, $intro) === false) {
119
                        return false;
120
                    }
121
 
122
                    if (strpos($html, $content) === false) {
123
                        // Content was exported.
124
                        return false;
125
                    }
126
 
127
                    return true;
128
                })
129
            );
130
        $archive->set_root_context($coursecontext);
131
 
132
        $pagecontroller = new \mod_page\content\exporter($modcontext, "mod_page", $user, $archive);
133
 
134
        $coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
135
        $coursecontroller->export_mod_content($modcontext, $pagecontroller->get_exportables());
136
    }
137
 
138
    /**
139
     * Get a mocked zipwriter instance, stubbing the supplieid classes.
140
     *
141
     * @param   string[] $methods
142
     * @return  zipwriter
143
     */
144
    protected function get_mocked_zipwriter(?array $methods = []): zipwriter {
145
        return $this->getMockBuilder(zipwriter::class)
146
            ->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
147
            ->onlyMethods($methods)
148
            ->getMock();
149
    }
150
}