CREATE_SESSION_FROM_MODEL also links courses - refs BT#16388

pull/3032/head
Sébastien Ducoulombier 6 years ago
parent 0e239f88c6
commit e6f5894dc0
  1. 15
      main/inc/lib/webservices/Rest.php
  2. 27
      main/webservices/api/test_api.php

@ -1594,11 +1594,13 @@ class Rest extends WebService
}
/**
* @param $params
*
* @param $modelSessionId
* @param $sessionName
* @param $startDate
* @param $endDate
* @param array $extraFields
* @return array
* @throws Exception
*
* @return integer
*/
public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = [])
{
@ -1645,6 +1647,11 @@ class Rest extends WebService
throw new Exception(get_lang('SessionNotRegistered'));
}
$courseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId));
if (!SessionManager::add_courses_to_session($newSessionId, $courseList)) {
throw new Exception(get_lang('CoursesNotAddedToSession'));
}
return [$newSessionId];
}

@ -116,14 +116,22 @@ class V2Test extends TestCase
public function testCreateSessionFromModel($apiKey)
{
$modelSessionId = SessionManager::create_session(
'Model session',
'Model session'.time(),
'2019-01-01 00:00', '2019-08-31 00:00',
'2019-01-01 00:00', '2019-08-31 00:00',
'2019-01-01 00:00', '2019-08-31 00:00',
null, null
);
$this->assertIsInt($modelSessionId);
$courseCodes = ['course A'.time(), 'course B'.time(), 'course C'.time()];
$courseList = [];
foreach($courseCodes as $code) {
$course = CourseManager::create_course(['code' => $code, 'title' => $code, 'wanted_code' => $code ], 1/*FIXME*/);
$courseList[] = $course['real_id'];
}
try {
SessionManager::add_courses_to_session($modelSessionId, $courseList);
} catch (Exception $e) {
}
$response = $this->client->post(
'v2.php',
@ -133,7 +141,7 @@ class V2Test extends TestCase
'username' => self::WEBSERVICE_USERNAME,
'api_key' => $apiKey,
'modelSessionId' => $modelSessionId,
'sessionName' => 'Name of the new session',
'sessionName' => 'Name of the new session'.time(),
'startDate' => '2019-09-01 00:00',
'endDate' => '2019-12-31 00:00',
'extraFields' => [
@ -146,8 +154,6 @@ class V2Test extends TestCase
]
);
SessionManager::delete($modelSessionId);
$this->assertSame(200, $response->getStatusCode(), 'Entry denied with code : ' . $response->getStatusCode());
$jsonResponse = json_decode($response->getBody()->getContents());
@ -159,6 +165,15 @@ class V2Test extends TestCase
$this->assertIsInt($jsonResponse->data[0]);
$newSessionId = $jsonResponse->data[0];
$modelCourseList = array_keys(SessionManager::get_course_list_by_session_id($modelSessionId));
$newCourseList = array_keys(SessionManager::get_course_list_by_session_id($newSessionId));
$this->assertSame($modelCourseList, $newCourseList);
foreach($courseCodes as $code) {
CourseManager::delete_course($code);
}
SessionManager::delete($modelSessionId);
SessionManager::delete($newSessionId);
}

Loading…
Cancel
Save