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 EduSharingApiClient\CurlResult;
24
use EduSharingApiClient\EduSharingAuthHelper;
25
use EduSharingApiClient\EduSharingHelperBase;
26
use EduSharingApiClient\EduSharingNodeHelper;
27
use EduSharingApiClient\EduSharingNodeHelperConfig;
28
use EduSharingApiClient\UrlHandling;
29
use JsonException;
30
 
31
// phpcs:ignore -- No Moodle internal check needed.
32
global $CFG;
33
require_once($CFG->dirroot . '/mod/edusharing/eduSharingAutoloader.php');
34
 
35
/**
36
 * class PluginRegistrationTest
37
 *
38
 * @author Marian Ziegler <ziegler@edu-sharing.net>
39
 * @package mod_edusharing
40
 * @covers \mod_edusharing\PluginRegistration
41
 */
42
class plugin_registration_test extends advanced_testcase {
43
    /**
44
     * Function test_register_plugin_returns_content_from_service_call_on_success
45
     *
46
     * @return void
47
     * @throws EduSharingUserException
48
     * @throws JsonException
49
     */
50
    public function test_register_plugin_returns_content_from_service_call_on_success(): void {
51
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
52
        $authhelper  = new EduSharingAuthHelper($basehelper);
53
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
54
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
55
        $repourl     = 'http://test.de';
56
        $user        = 'uName';
57
        $password    = 'testPass';
58
        $data        = 'data';
59
        $servicemock = $this->getMockBuilder(EduSharingService::class)
60
            ->onlyMethods(['validate_session', 'register_plugin'])
61
            ->setConstructorArgs([$authhelper, $nodehelper])
62
            ->getMock();
63
        $servicemock->expects($this->once())
64
            ->method('validate_session')
65
            ->with($repourl, $user . ':' . $password)
66
            ->will($this->returnValue(new CurlResult('{"isAdmin": true}', 0, [])));
67
        $servicemock->expects($this->once())
68
            ->method('register_plugin')
69
            ->with($repourl, $this->anything(), $this->anything(), $user . ':' . $password)
70
            ->will($this->returnValue(new CurlResult('{"content": "expectedContent"}', 0, [])));
71
        $registrationlogic = new PluginRegistration($servicemock);
72
        $result = $registrationlogic->register_plugin($repourl, $user, $password, $data);
73
        $this->assertArrayHasKey('content', $result);
74
        $this->assertEquals('expectedContent', $result['content']);
75
    }
76
 
77
    /**
78
     * Function test_register_plugin_throws_api_connection_exception_when_validate_session_fails_with_error
79
     *
80
     * @return void
81
     * @throws EduSharingUserException
82
     * @throws JsonException
83
     */
84
    public function test_register_plugin_throws_api_connection_exception_when_validate_session_fails_with_error(): void {
85
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
86
        $authhelper  = new EduSharingAuthHelper($basehelper);
87
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
88
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
89
        $repourl     = 'http://test.de';
90
        $user        = 'uName';
91
        $password    = 'testPass';
92
        $data        = 'data';
93
        $servicemock = $this->getMockBuilder(EduSharingService::class)
94
            ->onlyMethods(['validate_session', 'register_plugin'])
95
            ->setConstructorArgs([$authhelper, $nodehelper])
96
            ->getMock();
97
        $servicemock->expects($this->once())
98
            ->method('validate_session')
99
            ->with($repourl, $user . ':' . $password)
100
            ->will($this->returnValue(new CurlResult('{"isAdmin": true}', 3, [])));
101
        $registrationlogic = new PluginRegistration($servicemock);
102
        $this->expectException(EduSharingUserException::class);
103
        $this->expectExceptionMessage('API connection error');
104
        $registrationlogic->register_plugin($repourl, $user, $password, $data);
105
    }
106
 
107
 
108
    /**
109
     * Function test_register_plugin_throws_invalid_credentials_exception_if_user_is_no_admin
110
     *
111
     * @return void
112
     * @throws EduSharingUserException
113
     * @throws JsonException
114
     */
115
    public function test_register_plugin_throws_invalid_credentials_exception_if_user_is_no_admin(): void {
116
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
117
        $authhelper  = new EduSharingAuthHelper($basehelper);
118
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
119
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
120
        $repourl     = 'http://test.de';
121
        $user        = 'uName';
122
        $password    = 'testPass';
123
        $data        = 'data';
124
        $servicemock = $this->getMockBuilder(EduSharingService::class)
125
            ->onlyMethods(['validate_session', 'register_plugin'])
126
            ->setConstructorArgs([$authhelper, $nodehelper])
127
            ->getMock();
128
        $servicemock->expects($this->once())
129
            ->method('validate_session')
130
            ->with($repourl, $user . ':' . $password)
131
            ->will($this->returnValue(new CurlResult('{"isAdmin": false}', 0, [])));
132
        $registrationlogic = new PluginRegistration($servicemock);
133
        $this->expectException(EduSharingUserException::class);
134
        $this->expectExceptionMessage('Given user / password was not accepted as admin');
135
        $registrationlogic->register_plugin($repourl, $user, $password, $data);
136
    }
137
 
138
    /**
139
     * Function test_register_plugin_throws_api_connection_exception_when_register_plugin_fails_with_error
140
     *
141
     * @return void
142
     * @throws EduSharingUserException
143
     * @throws JsonException
144
     */
145
    public function test_register_plugin_throws_api_connection_exception_when_register_plugin_fails_with_error(): void {
146
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
147
        $authhelper  = new EduSharingAuthHelper($basehelper);
148
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
149
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
150
        $repourl     = 'http://test.de';
151
        $user        = 'uName';
152
        $password    = 'testPass';
153
        $data        = 'data';
154
        $servicemock = $this->getMockBuilder(EduSharingService::class)
155
            ->onlyMethods(['validate_session', 'register_plugin'])
156
            ->setConstructorArgs([$authhelper, $nodehelper])
157
            ->getMock();
158
        $servicemock->expects($this->once())
159
            ->method('validate_session')
160
            ->with($repourl, $user . ':' . $password)
161
            ->will($this->returnValue(new CurlResult('{"isAdmin": true}', 0, [])));
162
        $servicemock->expects($this->once())
163
            ->method('register_plugin')
164
            ->with($repourl, $this->anything(), $this->anything(), $user . ':' . $password)
165
            ->will($this->returnValue(new CurlResult('{"content": "expectedContent"}', 1, [])));
166
        $registrationlogic = new PluginRegistration($servicemock);
167
        $this->expectException(EduSharingUserException::class);
168
        $this->expectExceptionMessage('API connection error');
169
        $registrationlogic->register_plugin($repourl, $user, $password, $data);
170
    }
171
 
172
    /**
173
     * Function test_register_plugin_throws_json_exception_with_invalid_json_returned_from_api
174
     *
175
     * @return void
176
     * @throws EduSharingUserException
177
     * @throws JsonException
178
     */
179
    public function test_register_plugin_throws_json_exception_with_invalid_json_returned_from_api(): void {
180
        $basehelper  = new EduSharingHelperBase('www.url.de', 'pkey123', 'appid123');
181
        $authhelper  = new EduSharingAuthHelper($basehelper);
182
        $nodeconfig  = new EduSharingNodeHelperConfig(new UrlHandling(true));
183
        $nodehelper  = new EduSharingNodeHelper($basehelper, $nodeconfig);
184
        $repourl     = 'http://test.de';
185
        $user        = 'uName';
186
        $password    = 'testPass';
187
        $data        = 'data';
188
        $servicemock = $this->getMockBuilder(EduSharingService::class)
189
            ->onlyMethods(['validate_session', 'register_plugin'])
190
            ->setConstructorArgs([$authhelper, $nodehelper])
191
            ->getMock();
192
        $servicemock->expects($this->once())
193
            ->method('validate_session')
194
            ->with($repourl, $user . ':' . $password)
195
            ->will($this->returnValue(new CurlResult('{"isAdmin: false}', 0, [])));
196
        $registrationlogic = new PluginRegistration($servicemock);
197
        $this->expectException(JsonException::class);
198
        $registrationlogic->register_plugin($repourl, $user, $password, $data);
199
    }
200
}