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
// This program 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
// This program 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 does not match PSR. But Moodle likes it this way.
20
namespace mod_edusharing;
21
 
22
use advanced_testcase;
23
use dml_exception;
24
use EduSharingApiClient\EduSharingAuthHelper;
25
use EduSharingApiClient\EduSharingHelperBase;
26
use EduSharingApiClient\EduSharingNodeHelper;
27
use EduSharingApiClient\EduSharingNodeHelperConfig;
28
use EduSharingApiClient\UrlHandling;
29
use Exception;
30
use JsonException;
31
 
32
/**
33
 * Class InstallUpgradeLogicTest
34
 *
35
 * @author Marian Ziegler
36
 * @package mod_edusharing
37
 * @covers \mod_edusharing\InstallUpgradeLogic
38
 */
39
class install_upgrade_logic_test extends advanced_testcase {
40
 
41
    /**
42
     * Function test_parse_config_data_throws_exception_if_file_not_found
43
     *
44
     * @return void
45
     * @throws JsonException
46
     */
47
    public function test_parse_config_data_throws_exception_if_file_not_found(): void {
48
        $logic = new InstallUpgradeLogic(__DIR__ . '/../nothing/tests/installConfigTest.json');
49
        $this->expectException(Exception::class);
50
        $this->expectExceptionMessage('Missing installConfig');
51
        $logic->parse_config_data();
52
    }
53
 
54
    /**
55
     * Function test_parse_config_data_throws_json_exception_if_json_invalid
56
     *
57
     * @return void
58
     * @throws JsonException
59
     */
60
    public function test_parse_config_data_throws_json_exception_if_json_invalid(): void {
61
        $logic = new InstallUpgradeLogic(__DIR__ . '/../tests/installConfigTestInvalid.json');
62
        $this->expectException(JsonException::class);
63
        $logic->parse_config_data();
64
    }
65
 
66
    /**
67
     * Function test_perform_returns_void_if_all_goes_well
68
     *
69
     * @return void
70
     * @throws JsonException
71
     * @throws dml_exception
72
     */
73
    public function test_perform_returns_void_if_all_goes_well(): void {
74
        global $CFG;
75
        require_once($CFG->dirroot . '/mod/edusharing/eduSharingAutoloader.php');
76
        $basehelper        = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
77
        $authhelper        = new EduSharingAuthHelper($basehelper);
78
        $nodeconfig        = new EduSharingNodeHelperConfig(new UrlHandling(true));
79
        $nodehelper        = new EduSharingNodeHelper($basehelper, $nodeconfig);
80
        $service           = new EduSharingService($authhelper, $nodehelper);
81
        $metadatalogicmock = $this->getMockBuilder(MetadataLogic::class)
82
            ->setConstructorArgs([$service])
83
            ->getMock();
84
        $registrationlogicmock = $this->getMockBuilder(PluginRegistration::class)
85
            ->setConstructorArgs([$service])
86
            ->getMock();
87
        $metadatalogicmock->expects($this->once())
88
            ->method('import_metadata')
89
            ->with('http://localhost:8080/edu-sharing/metadata?format=lms&external=true');
90
        $metadatalogicmock->expects($this->once())
91
            ->method('create_xml_metadata')
92
            ->will($this->returnValue('superTestData'));
93
        $registrationlogicmock->expects($this->once())
94
            ->method('register_plugin')
95
            ->will($this->returnValue(['appid' => 'testId']));
96
        $logic = new InstallUpgradeLogic(__DIR__ . '/../tests/installConfigTest.json');
97
        $logic->set_registration_logic($registrationlogicmock);
98
        $logic->set_metadata_logic($metadatalogicmock);
99
        $logic->parse_config_data();
100
        $logic->perform();
101
    }
102
}