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_tests_generator_trait;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
require_once(__DIR__ . '/generator_trait.php');
24
 
25
/**
26
 * The discussion vault tests.
27
 *
28
 * @package    mod_forum
29
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class vaults_discussion_test extends \advanced_testcase {
1 efrain 33
    // Make use of the test generator trait.
34
    use mod_forum_tests_generator_trait;
35
 
36
    /** @var \mod_forum\local\vaults\discussion */
37
    private $vault;
38
 
39
    /**
40
     * Set up function for tests.
41
     */
42
    public function setUp(): void {
1441 ariadna 43
        parent::setUp();
1 efrain 44
        $vaultfactory = \mod_forum\local\container::get_vault_factory();
45
        $this->vault = $vaultfactory->get_discussion_vault();
46
    }
47
 
48
    /**
49
     * Test get_from_id.
50
     */
11 efrain 51
    public function test_get_from_id(): void {
1 efrain 52
        $this->resetAfterTest();
53
 
54
        $vault = $this->vault;
55
        $datagenerator = $this->getDataGenerator();
56
        $user = $datagenerator->create_user();
57
        $course = $datagenerator->create_course();
58
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
59
        [$discussionrecord, $post] = $this->helper_post_to_forum($forum, $user);
60
 
61
        $discussion = $vault->get_from_id($discussionrecord->id);
62
 
63
        $this->assertEquals($discussionrecord->id, $discussion->get_id());
64
    }
65
 
66
    /**
67
     * Test get_first_discussion_in_forum.
68
     */
11 efrain 69
    public function test_get_first_discussion_in_forum(): void {
1 efrain 70
        $this->resetAfterTest();
71
 
72
        $vault = $this->vault;
73
        $entityfactory = \mod_forum\local\container::get_entity_factory();
74
        $datagenerator = $this->getDataGenerator();
75
        $user = $datagenerator->create_user();
76
        $course = $datagenerator->create_course();
77
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
78
        $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
79
        $context = \context_module::instance($coursemodule->id);
80
        $forumentity = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
81
 
82
        $this->assertEquals(null, $vault->get_first_discussion_in_forum($forumentity));
83
 
84
        [$discussion1, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 2]);
85
        [$discussion2, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 1]);
86
        [$discussion3, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 3]);
87
 
88
        $discussionentity = $vault->get_first_discussion_in_forum($forumentity);
89
        $this->assertEquals($discussion2->id, $discussionentity->get_id());
90
    }
91
 
92
    /**
93
     * Test get_all_discussions_in_forum
94
     */
11 efrain 95
    public function test_get_all_discussions_in_forum(): void {
1 efrain 96
        $this->resetAfterTest();
97
 
98
        $vault = $this->vault;
99
        $entityfactory = \mod_forum\local\container::get_entity_factory();
100
        $datagenerator = $this->getDataGenerator();
101
        $user = $datagenerator->create_user();
102
        $course = $datagenerator->create_course();
103
        $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
104
        $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
105
        $context = \context_module::instance($coursemodule->id);
106
        $forumentity = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
107
 
108
        $this->assertEquals([], $vault->get_all_discussions_in_forum($forumentity));
109
 
110
        [$discussion1, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 2]);
111
        [$discussion2, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 1]);
112
        [$discussion3, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 3]);
113
 
114
        $discussionentity = $vault->get_all_discussions_in_forum($forumentity);
115
        $this->assertArrayHasKey($discussion1->id, $discussionentity); // Order is not guaranteed, so just verify element existence.
116
        $this->assertArrayHasKey($discussion2->id, $discussionentity);
117
        $this->assertArrayHasKey($discussion3->id, $discussionentity);
118
    }
119
}