Proyectos de Subversion Moodle

Rev

Rev 11 | | 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 mod_forum;
18
 
19
use mod_forum\local\entities\discussion as discussion_entity;
20
use mod_forum\local\exporters\discussion as discussion_exporter;
21
 
22
/**
23
 * The discussion exporter tests.
24
 *
25
 * @package    mod_forum
26
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
final class exporters_discussion_test extends \advanced_testcase {
1 efrain 30
 
31
    /** @var \mod_forum\local\builders\exported_posts */
32
    private $builder;
33
 
34
    /**
35
     * Test set up function.
36
     */
37
    public function setUp(): void {
1441 ariadna 38
        parent::setUp();
1 efrain 39
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
40
        // tests using these functions.
41
        \mod_forum\subscriptions::reset_forum_cache();
42
 
43
        $builderfactory = \mod_forum\local\container::get_builder_factory();
44
        $this->builder = $builderfactory->get_exported_posts_builder();
45
    }
46
 
47
    /**
48
     * Test tear down function.
49
     */
50
    public function tearDown(): void {
51
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
52
        // tests using these functions.
53
        \mod_forum\subscriptions::reset_forum_cache();
1441 ariadna 54
        parent::tearDown();
1 efrain 55
    }
56
 
57
    /**
58
     * Test the export function returns expected values.
59
     */
11 efrain 60
    public function test_export(): void {
1 efrain 61
        global $PAGE;
62
        $this->resetAfterTest();
63
 
64
        $renderer = $PAGE->get_renderer('core');
65
        $datagenerator = $this->getDataGenerator();
66
        $user = $datagenerator->create_user();
67
        $course = $datagenerator->create_course();
68
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
69
        $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
70
        $context = \context_module::instance($coursemodule->id);
71
        $entityfactory = \mod_forum\local\container::get_entity_factory();
72
        $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
73
        $group = $datagenerator->create_group(['courseid' => $course->id]);
74
        $now = time();
75
        $discussion = new discussion_entity(
76
            1,
77
            $course->id,
78
            $forum->get_id(),
79
            'test discussion',
80
            1,
81
            $user->id,
82
            $group->id,
83
            false,
84
            $now,
85
            $now,
86
            0,
87
            0,
88
            false,
89
 
90
        );
91
 
92
        $exporter = new discussion_exporter($discussion, [
93
            'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(),
94
            'urlfactory' => \mod_forum\local\container::get_url_factory(),
95
            'capabilitymanager' => (\mod_forum\local\container::get_manager_factory())->get_capability_manager($forum),
96
            'context' => $context,
97
            'forum' => $forum,
98
            'user' => $user,
99
            'groupsbyid' => [$group->id => $group],
100
            'latestpostid' => 7
101
        ]);
102
 
103
        $exporteddiscussion = $exporter->export($renderer);
104
 
105
        $this->assertEquals(1, $exporteddiscussion->id);
106
        $this->assertEquals($forum->get_id(), $exporteddiscussion->forumid);
107
        $this->assertEquals(false, $exporteddiscussion->pinned);
108
        $this->assertEquals('test discussion', $exporteddiscussion->name);
109
        $this->assertEquals($now, $exporteddiscussion->times['modified']);
110
        $this->assertEquals(0, $exporteddiscussion->times['start']);
111
        $this->assertEquals(0, $exporteddiscussion->times['end']);
112
        $this->assertEquals($group->name, $exporteddiscussion->group['name']);
113
    }
114
}