mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-57730 core_calendar: Allow modules to control event visibility
By implementing the mod_{modname}_core_calendar_is_event_visible callback a module can decide whether or not a user should see an event. Part of MDL-55611 epic.
This commit is contained in:
parent
5df117d438
commit
2a5cce61ba
5 changed files with 185 additions and 45 deletions
|
@ -62,6 +62,10 @@ class core_calendar_container_testcase extends advanced_testcase {
|
|||
$dbrow->id = $legacyevent->id;
|
||||
$event = $factory->create_instance($dbrow);
|
||||
|
||||
if (is_null($event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertInstanceOf(event_interface::class, $event);
|
||||
$this->assertTrue($event instanceof event || $event instanceof action_event);
|
||||
|
||||
|
|
|
@ -38,22 +38,38 @@ class core_calendar_event_factory_testcase extends advanced_testcase {
|
|||
* Test event class getters.
|
||||
*
|
||||
* @dataProvider create_instance_testcases()
|
||||
* @param array $instanceparams Associative array of instance parameters.
|
||||
* @param \stdClass $dbrow Row from the event table.
|
||||
* @param callable $actioncallbackapplier Action callback applier.
|
||||
* @param callable $visibilitycallbackapplier Visibility callback applier.
|
||||
* @param string $expectedclass Class the factory is expected to produce.
|
||||
* @param mixed $expectedattributevalue Expected value of the modified attribute.
|
||||
*/
|
||||
public function test_create_instance(
|
||||
$dbrow,
|
||||
callable $callbackapplier,
|
||||
callable $actioncallbackapplier,
|
||||
callable $visibilitycallbackapplier,
|
||||
$expectedclass,
|
||||
$expectedattributevalue
|
||||
) {
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$event = $this->create_event();
|
||||
|
||||
$factory = new event_factory($callbackapplier);
|
||||
$factory = new event_factory($actioncallbackapplier, $visibilitycallbackapplier);
|
||||
$dbrow->id = $event->id;
|
||||
$instance = $factory->create_instance($dbrow);
|
||||
|
||||
$this->assertEquals($instance->testattribute, $expectedattributevalue);
|
||||
if ($expectedclass) {
|
||||
$this->assertInstanceOf($expectedclass, $instance);
|
||||
}
|
||||
|
||||
if (is_null($expectedclass)) {
|
||||
$this->assertNull($instance);
|
||||
}
|
||||
|
||||
if ($expectedattributevalue) {
|
||||
$this->assertEquals($instance->testattribute, $expectedattributevalue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,13 +77,59 @@ class core_calendar_event_factory_testcase extends advanced_testcase {
|
|||
*
|
||||
* @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
|
||||
*/
|
||||
public function test_invalid_callback() {
|
||||
public function test_invalid_action_callback() {
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$event = $this->create_event();
|
||||
$factory = new event_factory(function () {
|
||||
return 'hello';
|
||||
});
|
||||
$factory = new event_factory(
|
||||
function () {
|
||||
return 'hello';
|
||||
},
|
||||
function () {
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
$factory->create_instance(
|
||||
(object)[
|
||||
'id' => $event->id,
|
||||
'name' => 'test',
|
||||
'description' => 'Test description',
|
||||
'format' => 2,
|
||||
'courseid' => 1,
|
||||
'groupid' => 1,
|
||||
'userid' => 1,
|
||||
'repeatid' => 1,
|
||||
'modulename' => 'assign',
|
||||
'instance' => 1,
|
||||
'eventtype' => 'due',
|
||||
'timestart' => 123456789,
|
||||
'timeduration' => 12,
|
||||
'timemodified' => 123456789,
|
||||
'timesort' => 123456789,
|
||||
'visible' => 1,
|
||||
'subscriptionid' => 1
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test invalid callback exception.
|
||||
*
|
||||
* @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
|
||||
*/
|
||||
public function test_invalid_visibility_callback() {
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$event = $this->create_event();
|
||||
$factory = new event_factory(
|
||||
function ($event) {
|
||||
return $event;
|
||||
},
|
||||
function () {
|
||||
return 'asdf';
|
||||
}
|
||||
);
|
||||
|
||||
$factory->create_instance(
|
||||
(object)[
|
||||
|
@ -94,7 +156,7 @@ class core_calendar_event_factory_testcase extends advanced_testcase {
|
|||
|
||||
public function create_instance_testcases() {
|
||||
return [
|
||||
'Sample event record' => [
|
||||
'Sample event record with event exposed' => [
|
||||
'dbrow' => (object)[
|
||||
'name' => 'Test event',
|
||||
'description' => 'Hello',
|
||||
|
@ -113,11 +175,44 @@ class core_calendar_event_factory_testcase extends advanced_testcase {
|
|||
'visible' => true,
|
||||
'subscriptionid' => 1
|
||||
],
|
||||
'callbackapplier' => function(event_interface $event) {
|
||||
'actioncallbackapplier' => function(event_interface $event) {
|
||||
$event->testattribute = 'Hello';
|
||||
return $event;
|
||||
},
|
||||
'visibilitycallbackapplier' => function(event_interface $event) {
|
||||
return true;
|
||||
},
|
||||
event_interface::class,
|
||||
'Hello'
|
||||
],
|
||||
'Sample event record with event hidden' => [
|
||||
'dbrow' => (object)[
|
||||
'name' => 'Test event',
|
||||
'description' => 'Hello',
|
||||
'format' => 1,
|
||||
'courseid' => 1,
|
||||
'groupid' => 1,
|
||||
'userid' => 1,
|
||||
'repeatid' => null,
|
||||
'modulename' => 'Test module',
|
||||
'instance' => 1,
|
||||
'eventtype' => 'Due',
|
||||
'timestart' => 123456789,
|
||||
'timeduration' => 123456789,
|
||||
'timemodified' => 123456789,
|
||||
'timesort' => 123456789,
|
||||
'visible' => true,
|
||||
'subscriptionid' => 1
|
||||
],
|
||||
'actioncallbackapplier' => function(event_interface $event) {
|
||||
$event->testattribute = 'Hello';
|
||||
return $event;
|
||||
},
|
||||
'visibilitycallbackapplier' => function(event_interface $event) {
|
||||
return false;
|
||||
},
|
||||
null,
|
||||
null
|
||||
]
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue