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