MDL-59393 calendar: add update_event_start_day to local api

This commit is contained in:
Ryan Wyllie
2017-08-23 00:56:26 +00:00
parent 0dc3aa1295
commit 55f5fcb9c9
2 changed files with 98 additions and 0 deletions
+28
View File
@@ -26,6 +26,8 @@ namespace core_calendar\local;
defined('MOODLE_INTERNAL') || die();
use core_calendar\local\event\container;
use core_calendar\local\event\entities\event_interface;
use core_calendar\local\event\exceptions\limit_invalid_parameter_exception;
/**
@@ -214,4 +216,30 @@ class api {
return $return;
}
/**
* Change the start day for an event. Only the date will be
* modified, the time of day for the event will be left as is.
*
* @param event_interface $event The existing event to modify
* @param DateTimeInterface $startDate The new date to use for the start day
* @return event_interface The new event with updated start date
*/
public static function update_event_start_day(
event_interface $event,
\DateTimeInterface $startDate
) {
$mapper = container::get_event_mapper();
$legacyevent = $mapper->from_event_to_legacy_event($event);
$starttime = $event->get_times()->get_start_time()->setDate(
$startDate->format('Y'),
$startDate->format('n'),
$startDate->format('j')
);
// This function does our capability checks.
$legacyevent->update((object) ['timestart' => $starttime->getTimestamp()]);
return $mapper->from_legacy_event_to_event($legacyevent);
}
}
+70
View File
@@ -26,6 +26,8 @@ defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/helpers.php');
use \core_calendar\local\event\container;
/**
* Class contaning unit tests for the calendar local API.
*
@@ -858,4 +860,72 @@ class core_calendar_local_api_testcase extends advanced_testcase {
$events = calendar_get_legacy_events($timestart, $timeend, true, true, true);
$this->assertCount(3, $events);
}
/**
* Setting the start date on the calendar event should update the date
* of the event but should leave the time of day unchanged.
*/
public function test_update_event_start_day_updates_date() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$roleid = $generator->create_role();
$context = \context_system::instance();
$originalStartTime = new DateTimeImmutable('2017-01-1T15:00:00+08:00');
$newStartDate = new DateTimeImmutable('2018-02-2T10:00:00+08:00');
$expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00');
$mapper = container::get_event_mapper();
$generator->role_assign($roleid, $user->id, $context->id);
assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
$this->setUser($user);
$this->resetAfterTest(true);
$event = create_event([
'name' => 'Test event',
'userid' => $user->id,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => $originalStartTime->getTimestamp(),
]);
$event = $mapper->from_legacy_event_to_event($event);
$newEvent = \core_calendar\local\api::update_event_start_day($event, $newStartDate);
$actual = $newEvent->get_times()->get_start_time();
$this->assertEquals($expected->getTimestamp(), $actual->getTimestamp());
}
/**
* A user should not be able to update the start date of the event
* that they don't have the capabilities to modify.
*/
public function test_update_event_start_day_no_permission() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$roleid = $generator->create_role();
$context = \context_system::instance();
$originalStartTime = new DateTimeImmutable('2017-01-1T15:00:00+08:00');
$newStartDate = new DateTimeImmutable('2018-02-2T10:00:00+08:00');
$expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00');
$mapper = container::get_event_mapper();
$generator->role_assign($roleid, $user->id, $context->id);
$this->setUser($user);
$this->resetAfterTest(true);
$event = create_event([
'name' => 'Test event',
'userid' => $user->id,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => $originalStartTime->getTimestamp(),
]);
$event = $mapper->from_legacy_event_to_event($event);
assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
$this->expectException('moodle_exception');
$newEvent = \core_calendar\local\api::update_event_start_day($event, $newStartDate);
}
}