Skip to content

Commit

Permalink
attempt to create selectedusers
Browse files Browse the repository at this point in the history
  • Loading branch information
semteacher committed Apr 23, 2024
1 parent 6f327fb commit 62fa879
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/bo_availability/condition_bookingpolicy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,90 @@ public function test_booking_maxperuser(array $bdata) {
$this->assertEquals(MOD_BOOKING_BO_COND_MAX_NUMBER_OF_BOOKINGS, $id);
}

/**
* Test booking option availability: \condition\selectusers.
*
* @covers \condition\selectusers::is_available
*
* @param array $bdata
* @throws \coding_exception
* @throws \dml_exception
*
* @dataProvider booking_settings_provider
*/
public function test_booking_selectusers(array $bdata) {

// Set test objective setting(s).
//$bdata['maxperuser'] = 1;

// Setup test data.
$course1 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);

// Create users.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$bookingmanager = $this->getDataGenerator()->create_user(); // Booking manager.

$bdata['course'] = $course1->id;
$bdata['bookingmanager'] = $bookingmanager->username;

$booking1 = $this->getDataGenerator()->create_module('booking', $bdata);
$bookingsettings = singleton_service::get_instance_of_booking_settings_by_bookingid($booking1->id);
singleton_service::destroy_booking_singleton_by_cmid($bookingsettings->cmid);
$bookingsettings = singleton_service::get_instance_of_booking_settings_by_bookingid($booking1->id);

$this->setAdminUser();

$this->getDataGenerator()->enrol_user($student1->id, $course1->id);
$this->getDataGenerator()->enrol_user($student2->id, $course1->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id);
$this->getDataGenerator()->enrol_user($bookingmanager->id, $course1->id);

// Set test availability setting(s).
$availability = new stdClass();
$availability->id = 14;
$availability->name = 'selectusers';
$availability->class = 'mod_booking\bo_availability\conditions\selectusers';
$availability->userids = [$student2->id];

$record = new stdClass();
$record->bookingid = $booking1->id;
$record->text = 'Test option1';
$record->courseid = $course1->id;
//$record->availability = json_encode([$availability]);
$record->bo_cond_selectusers_restrict = 1;
$record->bo_cond_selectusers_userids = [$student2->id];

/** @var mod_booking_generator $plugingenerator */
$plugingenerator = self::getDataGenerator()->get_plugin_generator('mod_booking');
$option1 = $plugingenerator->create_option($record);

$settings = singleton_service::get_instance_of_booking_option_settings($option1->id);

// Book the first user without any problem.
$boinfo = new bo_info($settings);

// Book the student right away.
$this->setUser($student1);

// Student1 not allowed to book.
$result = booking_bookit::bookit('option', $settings->id, $student1->id);
list($id, $isavailable, $description) = $boinfo->is_available($settings->id, $student1->id, true);
$this->assertEquals(MOD_BOOKING_BO_COND_JSON_SELECTUSERS, $id);

$this->setUser($student2);
// Student2 is allowed to book.
$result = booking_bookit::bookit('option', $settings->id, $student2->id);
list($id, $isavailable, $description) = $boinfo->is_available($settings->id, $student1->id, true);
$this->assertEquals(MOD_BOOKING_BO_COND_CONFIRMBOOKIT, $id);

// Student2 is actually book.
$result = booking_bookit::bookit('option', $settings->id, $student2->id);
list($id, $isavailable, $description) = $boinfo->is_available($settings->id, $student1->id, true);
$this->assertEquals(MOD_BOOKING_BO_COND_ALREADYBOOKED, $id);
}

/**
* Data provider for condition_bookingpolicy_test
*
Expand Down
1 change: 1 addition & 0 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function create_option($record = null) {

$record->addtocalendar = !empty($record->addtocalendar) ? $record->addtocalendar : 0;
$record->maxanswers = !empty($record->maxanswers) ? $record->maxanswers : 0;
$record->availability = !empty($record->availability) ? $record->availability : "";

// Process option teachers.
if (!empty($record->teachersforoption)) {
Expand Down

0 comments on commit 62fa879

Please sign in to comment.