Skip to content

Commit

Permalink
MDL-57730 core_calendar: fixed failing unit tests
Browse files Browse the repository at this point in the history
Part of MDL-55611 epic.
  • Loading branch information
mdjnelson authored and Damyon Wiese committed Apr 3, 2017
1 parent 294dce6 commit c91b4c0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 83 deletions.
110 changes: 73 additions & 37 deletions calendar/classes/local/event/core_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class core_container {
protected static $actioneventfactory;

/**
* @var event_mapper_interface $eventmapper Event mapper.
* @var \core_calendar\local\interfaces\event_mapper_interface $eventmapper Event mapper.
*/
protected static $eventmapper;

Expand All @@ -65,54 +65,36 @@ class core_container {
protected static $actionfactory;

/**
* @var event_vault $eventvault Event vault.
* @var \core_calendar\local\event\data_access\event_vault $eventvault Event vault.
*/
private static $eventvault;

/**
* @var array A list of callbacks to use.
*/
protected static $callbacks = array();

/**
* Initialises the dependency graph if it hasn't yet been.
*/
private static function init() {
if (empty(self::$eventfactory)) {
$identity = function(event_interface $event) {
return $event;
};
self::initcallbacks();
self::$actionfactory = new action_factory();
self::$actioneventfactory = new action_event_factory();
self::$eventmapper = new event_mapper(
new event_factory($identity, $identity)
);
self::$eventfactory = new event_factory(
function(event_interface $event) {
$mapper = self::$eventmapper;
$action = component_callback(
'mod_' . $event->get_course_module()->get('modname'),
'core_calendar_provide_event_action',
[
$mapper->from_event_to_legacy_event($event),
self::$actionfactory
]
);

return $action ? self::$actioneventfactory->create_instance($event, $action) : $event;
},
function(event_interface $event) {
$mapper = self::$eventmapper;
$eventvisible = component_callback(
'mod_' . $event->get_course_module()->get('modname'),
'core_calendar_is_event_visible',
[
$mapper->from_event_to_legacy_event($event)
]
);

// Module does not implement the callback, event should be visible.
if (is_null($eventvisible)) {
new event_factory(
function(event_interface $event) {
return $event;
}, function() {
return true;
}
)
);

return $eventvisible ? true : false;
}
self::$eventfactory = new event_factory(
self::$callbacks[PHPUNIT_TEST ? 'testing' : 'production']['action'],
self::$callbacks[PHPUNIT_TEST ? 'testing' : 'production']['visibility']
);
}

Expand All @@ -135,7 +117,7 @@ public static function get_event_factory() {
/**
* Gets the event mapper
*
* @return event_mapper_interface
* @return \core_calendar\local\interfaces\event_mapper_interface
*/
public static function get_event_mapper() {
self::init();
Expand All @@ -145,10 +127,64 @@ public static function get_event_mapper() {
/**
* Return an event vault.
*
* @return event_vault
* @return \core_calendar\local\event\data_access\event_vault
*/
public static function get_event_vault() {
self::init();
return self::$eventvault;
}

/**
* Initialises the callbacks.
*/
private static function initcallbacks() {
self::$callbacks = array(
'testing' => array(
'action' => function (event_interface $event) {
return self::$actioneventfactory->create_instance($event,
new \core_calendar\local\event\value_objects\action(
'test',
new \moodle_url('http://example.com'),
420,
true
));
},
'visibility' => function (event_interface $event) {
return true;
}
),
'production' => array(
'action' => function (event_interface $event) {
$mapper = self::$eventmapper;
$action = component_callback(
'mod_' . $event->get_course_module()->get('modname'),
'core_calendar_provide_event_action',
[
$mapper->from_event_to_legacy_event($event),
self::$actionfactory
]
);

return $action ? self::$actioneventfactory->create_instance($event, $action) : $event;
},
'visibility' => function (event_interface $event) {
$mapper = self::$eventmapper;
$eventvisible = component_callback(
'mod_' . $event->get_course_module()->get('modname'),
'core_calendar_is_event_visible',
[
$mapper->from_event_to_legacy_event($event)
]
);

// Module does not implement the callback, event should be visible.
if (is_null($eventvisible)) {
return true;
}

return $eventvisible ? true : false;
}
),
);
}
}
64 changes: 20 additions & 44 deletions calendar/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,14 @@ public function test_add_subscription() {
* be returned.
*/
function test_get_calendar_action_events_by_timesort_after_time() {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$moduleinstance = $generator->create_instance(['course' => $course->id]);

$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->resetAfterTest(true);
$this->setUser($user);
$this->setAdminUser();

$params = [
'type' => CALENDAR_EVENT_TYPE_ACTION,
'modulename' => 'assign',
'instance' => $moduleinstance->id,
'userid' => $user->id,
'courseid' => 1,
'modulename' => 'xyz',
'instance' => 1,
'userid' => 1,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => 1,
Expand Down Expand Up @@ -450,20 +444,14 @@ function test_get_calendar_action_events_by_timesort_after_time() {
* returned.
*/
function test_get_calendar_action_events_by_timesort_before_time() {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$moduleinstance = $generator->create_instance(['course' => $course->id]);

$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->resetAfterTest(true);
$this->setUser($user);
$this->setAdminUser();

$params = [
'type' => CALENDAR_EVENT_TYPE_ACTION,
'modulename' => 'assign',
'instance' => $moduleinstance->id,
'userid' => $user->id,
'courseid' => 1,
'modulename' => 'xyz',
'instance' => 1,
'userid' => 1,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => 1,
Expand Down Expand Up @@ -499,20 +487,14 @@ function test_get_calendar_action_events_by_timesort_before_time() {
* returned.
*/
function test_get_calendar_action_events_by_timesort_time_range() {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$moduleinstance = $generator->create_instance(['course' => $course->id]);

$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->resetAfterTest(true);
$this->setUser($user);
$this->setAdminUser();

$params = [
'type' => CALENDAR_EVENT_TYPE_ACTION,
'modulename' => 'assign',
'instance' => $moduleinstance->id,
'userid' => $user->id,
'courseid' => 1,
'modulename' => 'xyz',
'instance' => 1,
'userid' => 1,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => 1,
Expand Down Expand Up @@ -549,20 +531,14 @@ function test_get_calendar_action_events_by_timesort_time_range() {
* returned.
*/
function test_get_calendar_action_events_by_timesort_time_limit_offset() {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$moduleinstance = $generator->create_instance(['course' => $course->id]);

$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->resetAfterTest(true);
$this->setUser($user);
$this->setAdminUser();

$params = [
'type' => CALENDAR_EVENT_TYPE_ACTION,
'modulename' => 'assign',
'instance' => $moduleinstance->id,
'userid' => $user->id,
'courseid' => 1,
'modulename' => 'xyz',
'instance' => 1,
'userid' => 1,
'eventtype' => 'user',
'repeats' => 0,
'timestart' => 1,
Expand Down
7 changes: 5 additions & 2 deletions calendar/tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/../lib.php');
global $CFG;

require_once($CFG->dirroot . '/calendar/lib.php');

use core_calendar\local\data_access\event_vault;
use core_calendar\local\event\entities\action_event;
Expand Down Expand Up @@ -137,7 +139,8 @@ public function create_instance(\stdClass $record) {
$action = new action(
'Test action',
new \moodle_url('/'),
1
1,
true
);

$actionevent = new action_event($event, $action);
Expand Down

0 comments on commit c91b4c0

Please sign in to comment.