Passed
Push — master ( 482b77...45d699 )
by Julito
09:12
created

CreateSessionFromModelTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 181
c 1
b 0
f 0
dl 0
loc 335
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A action() 0 3 1
B testFromAModelWithExtraFields() 0 112 1
A testFromAModelWithCourses() 0 47 3
A testFromAModelWithAPromotion() 0 46 1
A testFromAModelWithADifferentURL() 0 43 2
A testFromASimpleModel() 0 49 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
require_once __DIR__.'/V2TestCase.php';
6
require_once __DIR__.'/../../../../vendor/autoload.php';
7
8
/**
9
 * Class CreateSessionFromModelTest
10
 *
11
 * CREATE_SESSION_FROM_MODEL webservice unit tests
12
 */
13
class CreateSessionFromModelTest extends V2TestCase
14
{
15
    public function action()
16
    {
17
        return 'create_session_from_model';
18
    }
19
20
    /**
21
     * creates a session from a simple model session
22
     * asserts that it was created with the supplied data
23
     */
24
    public function testFromASimpleModel()
25
    {
26
        // create a model session
27
        $modelSessionId = SessionManager::create_session(
28
            'Model session'.time(),
29
            '2019-01-01 00:00',
30
            '2019-08-31 00:00',
31
            '2019-01-01 00:00',
32
            '2019-08-31 00:00',
33
            '2019-01-01 00:00',
34
            '2019-08-31 00:00',
35
            null,
36
            null
37
        );
38
39
        // call the webservice to create the new session from the model session,
40
        // and assert it returns an integer
41
        $name = 'New session'.time();
42
        $startDate = '2019-09-01 00:00:00';
43
        $endDate = '2019-12-31 00:00:00';
44
45
        $newSessionId = $this->integer(
46
            [
47
                'modelSessionId' => $modelSessionId,
48
                'sessionName' => $name,
49
                'startDate' => $startDate,
50
                'endDate' => $endDate,
51
            ]
52
        );
53
        //var_dump($newSessionId);exit;
54
55
        // assert the session was created and given the returned session id
56
        $entityManager = Database::getManager();
57
        $repository = $entityManager->getRepository('ChamiloCoreBundle:Session');
58
        $newSession = $repository->find($newSessionId);
59
        $this->assertIsObject($newSession);
60
61
        // assert the new session got the right data
62
        $this->assertSame($name, $newSession->getName());
63
        // FIXME account for UTC / local timezone shift
64
        // $this->assertSame($endDate, $newSession->getDisplayEndDate());
65
        // $this->assertSame($startDate, $newSession->getAccessStartDate());
66
        // $this->assertSame($endDate, $newSession->getAccessEndDate());
67
        // $this->assertSame($startDate, $newSession->getCoachAccessStartDate());
68
        // $this->assertSame($endDate, $newSession->getCoachAccessEndDate());
69
70
        // clean up
71
        SessionManager::delete($modelSessionId);
72
        SessionManager::delete($newSessionId);
73
        //var_dump($modelSessionId);exit;
74
    }
75
76
    /**
77
     * creates a session from a model session subscribed to a promotion
78
     * asserts that the promotion is inherited by the new session
79
     */
80
    public function testFromAModelWithAPromotion()
81
    {
82
        // create a promotion
83
        $career = new Career();
84
        $careerId = $career->save(['name' => 'test career'.time()]);
85
        $promotion = new Promotion();
86
        $promotionId = $promotion->save(['career_id' => $careerId, 'name' => 'test promo'.time()]);
87
88
        // create a model session
89
        $modelSessionId = SessionManager::create_session(
90
            'Model session'.time(),
91
            '2019-01-01 00:00',
92
            '2019-08-31 00:00',
93
            '2019-01-01 00:00',
94
            '2019-08-31 00:00',
95
            '2019-01-01 00:00',
96
            '2019-08-31 00:00',
97
            null,
98
            null
99
        );
100
101
        // subscribe the model session to the promotion - the new session will inherit this too
102
        SessionManager::subscribe_sessions_to_promotion($promotionId, [$modelSessionId]);
103
104
        // call the webservice to create the new session from the model session,
105
        // and assert it returns an integer
106
        $newSessionId = $this->integer(
107
            [
108
                'modelSessionId' => $modelSessionId,
109
                'sessionName' => 'New session'.time(),
110
                'startDate' => '2019-09-01 00:00',
111
                'endDate' => '2019-12-31 00:00',
112
            ]
113
        );
114
115
        // assert both sessions are in the promotion
116
        $promotionSessions = SessionManager::get_all_sessions_by_promotion($promotionId);
117
        $this->assertSame(2, count($promotionSessions));
118
        $this->assertArrayHasKey($newSessionId, $promotionSessions);
119
        $this->assertArrayHasKey($modelSessionId, $promotionSessions);
120
121
        // clean up
122
        SessionManager::delete($modelSessionId);
123
        SessionManager::delete($newSessionId);
124
        $promotion->delete($promotionId);
125
        $career->delete($careerId);
126
    }
127
128
    /**
129
     * Creates a session from a model session with a different URL
130
     * asserts that the new session has the called web server URL
131
     */
132
    public function testFromAModelWithADifferentURL()
133
    {
134
        if (!api_is_multiple_url_enabled()) {
135
            $this->markTestSkipped('needs multiple URL enabled');
136
        }
137
138
        // create a model session
139
        $modelSessionId = SessionManager::create_session(
140
            'Model session'.time(),
141
            '2019-01-01 00:00',
142
            '2019-08-31 00:00',
143
            '2019-01-01 00:00',
144
            '2019-08-31 00:00',
145
            '2019-01-01 00:00',
146
            '2019-08-31 00:00',
147
            null,
148
            null
149
        );
150
151
        // move the session to a non-standard URL
152
        $urlId = UrlManager::add('https://www.url.org/chamilo-lms/'.time(), 'Non-default URL', 1);
153
        UrlManager::update_urls_rel_session([$modelSessionId], $urlId);
154
155
        // call the webservice to create the new session from the model session,
156
        // and assert it returns an integer
157
        $newSessionId = $this->integer(
158
            [
159
                'modelSessionId' => $modelSessionId,
160
                'sessionName' => 'Name of the new session'.time(),
161
                'startDate' => '2019-09-01 00:00',
162
                'endDate' => '2019-12-31 00:00',
163
            ]
164
        );
165
166
        // assert the current url was set on the new session
167
        $urls = UrlManager::get_access_url_from_session($newSessionId);
168
        $this->assertSame(1, count($urls));
169
        $this->assertSame(api_get_current_access_url_id(), intval($urls[0]['access_url_id']));
170
171
        // clean up
172
        SessionManager::delete($modelSessionId);
173
        SessionManager::delete($newSessionId);
174
        UrlManager::delete($urlId);
175
    }
176
177
    /**
178
     * creates a session from a model session with courses
179
     * asserts that the new session inherits the same courses
180
     * @throws Exception
181
     */
182
    public function testFromAModelWithCourses()
183
    {
184
        // create a model session
185
        $modelSessionId = SessionManager::create_session(
186
            'Model session'.time(),
187
            '2019-01-01 00:00',
188
            '2019-08-31 00:00',
189
            '2019-01-01 00:00',
190
            '2019-08-31 00:00',
191
            '2019-01-01 00:00',
192
            '2019-08-31 00:00',
193
            null,
194
            null
195
        );
196
197
        // create courses and add them the the model session
198
        $courseCodes = ['course A'.time(), 'course B'.time(), 'course C'.time()];
199
        $courseList = [];
200
        $authorId = UserManager::get_user_id_from_username(self::WEBSERVICE_USERNAME);
201
        foreach ($courseCodes as $code) {
202
            $course = CourseManager::create_course(['code' => $code, 'title' => $code], $authorId);
203
            $courseList[] = $course['real_id'];
204
        }
205
        SessionManager::add_courses_to_session($modelSessionId, $courseList);
206
207
        // call the webservice to create the new session from the model session,
208
        // and assert it returns an integer
209
        $newSessionId = $this->integer(
210
            [
211
                'modelSessionId' => $modelSessionId,
212
                'sessionName' => 'Name of the new session'.time(),
213
                'startDate' => '2019-09-01 00:00',
214
                'endDate' => '2019-12-31 00:00',
215
            ]
216
        );
217
218
        // assert the new session inherited the model session courses
219
        $modelCourseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId));
220
        $newCourseList = array_keys(SessionManager::get_course_list_by_session_id($newSessionId));
221
        $this->assertSame($modelCourseList, $newCourseList);
222
223
        // clean up
224
        foreach ($courseCodes as $code) {
225
            CourseManager::delete_course($code);
226
        }
227
        SessionManager::delete($modelSessionId);
228
        SessionManager::delete($newSessionId);
229
    }
230
231
    /**
232
     * creates a session from a model session with extra fields
233
     * asserts that the new session inherits the extra fields
234
     * and that specifying other extra field values works
235
     */
236
    public function testFromAModelWithExtraFields()
237
    {
238
        // create a model session
239
        $modelSessionId = SessionManager::create_session(
240
            'Model session'.time(),
241
            '2019-01-01 00:00',
242
            '2019-08-31 00:00',
243
            '2019-01-01 00:00',
244
            '2019-08-31 00:00',
245
            '2019-01-01 00:00',
246
            '2019-08-31 00:00',
247
            null,
248
            null
249
        );
250
251
        // create an extra field and set its value in the model session
252
        // the new session will be given a different value in this field
253
        $extraFieldModel = new ExtraField('session');
254
        $firstExtraFieldName = 'extraField'.time();
255
        $firstExtraFieldNameForModelSession = 'extra field value for model';
256
        $firstExtraFieldNameForNewSession = 'extra field value for new session';
257
        $firstExtraFieldId = $extraFieldModel->save(
258
            [
259
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
260
                'variable' => $firstExtraFieldName,
261
                'display_text' => $firstExtraFieldName,
262
                'visible_to_self' => 1,
263
                'visible_to_others' => 1,
264
                'changeable' => 1,
265
                'filter' => 1,
266
            ]
267
        );
268
        SessionManager::update_session_extra_field_value(
269
            $modelSessionId,
270
            $firstExtraFieldName,
271
            $firstExtraFieldNameForModelSession
272
        );
273
274
        // create a second extra field and set its value in the model session
275
        // the new session will inherit the same value in this field
276
        $secondExtraFieldName = 'secondExtraField'.time();
277
        $secondExtraFieldValue = 'second extra field value';
278
        $secondExtraFieldId = $extraFieldModel->save(
279
            [
280
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
281
                'variable' => $secondExtraFieldName,
282
                'display_text' => $secondExtraFieldName,
283
                'visible_to_self' => 1,
284
                'visible_to_others' => 1,
285
                'changeable' => 1,
286
                'filter' => 1,
287
            ]
288
        );
289
        SessionManager::update_session_extra_field_value(
290
            $modelSessionId,
291
            $secondExtraFieldName,
292
            $secondExtraFieldValue
293
        );
294
295
        // call the webservice to create the new session from the model session,
296
        // specifying a different value in the first extra field
297
        // and assert it returns an integer
298
        $newSessionId = $this->integer(
299
            [
300
                'modelSessionId' => $modelSessionId,
301
                'sessionName' => 'Name of the new session'.time(),
302
                'startDate' => '2019-09-01 00:00',
303
                'endDate' => '2019-12-31 00:00',
304
                'extraFields' => [
305
                    $firstExtraFieldName => $firstExtraFieldNameForNewSession,
306
                ],
307
            ]
308
        );
309
310
        // assert the new session has its own new value in the first extra field
311
        $extraFieldValueModel = new ExtraFieldValue('session');
312
        $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
313
            $newSessionId,
314
            $firstExtraFieldName
315
        );
316
        $this->assertNotFalse($extraFieldValue);
317
        $this->assertSame($firstExtraFieldNameForNewSession, $extraFieldValue['value']);
318
319
        // assert the model session still has its own original value in the first extra field
320
        $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
321
            $modelSessionId,
322
            $firstExtraFieldName
323
        );
324
        $this->assertNotFalse($extraFieldValue);
325
        $this->assertSame($firstExtraFieldNameForModelSession, $extraFieldValue['value']);
326
327
        // assert the new session has inherited the model session value in the second extra field
328
        $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
329
            $newSessionId,
330
            $secondExtraFieldName
331
        );
332
        $this->assertNotFalse($extraFieldValue);
333
        $this->assertSame($secondExtraFieldValue, $extraFieldValue['value']);
334
335
        // assert the model session still has the same value in the second extra field
336
        $extraFieldValue = $extraFieldValueModel->get_values_by_handler_and_field_variable(
337
            $modelSessionId,
338
            $secondExtraFieldName
339
        );
340
        $this->assertNotFalse($extraFieldValue);
341
        $this->assertSame($secondExtraFieldValue, $extraFieldValue['value']);
342
343
        // clean up
344
        SessionManager::delete($modelSessionId);
345
        SessionManager::delete($newSessionId);
346
        $extraFieldModel->delete($firstExtraFieldId);
347
        $extraFieldModel->delete($secondExtraFieldId);
348
    }
349
}
350