Skip to content

Commit

Permalink
Improvement: Implement unit test for overlapping condition (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
eynimeni committed Dec 5, 2024
1 parent 08244d0 commit 974b028
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
7 changes: 5 additions & 2 deletions classes/bo_availability/bo_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public function __construct(booking_option_settings $settings) {

$this->optionid = $settings->id;
$this->userid = $USER->id;

}

/**
Expand Down Expand Up @@ -511,7 +510,11 @@ public static function return_sql_from_conditions() {
}
foreach ($conditions as $class) {

$condition = new $class();
if (method_exists($class, 'instance')) {
$condition = $class::instance();
} else {
$condition = new $class();
}

list($select, $from, $filter, $params, $where) = $condition->return_sql();

Expand Down
7 changes: 7 additions & 0 deletions classes/bo_availability/conditions/nooverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class nooverlapping implements bo_condition {
*/
private array $overlappinganswers = [];

/**
* Customsettings.
*
* @var object
*/
public object $customsettings;

/**
* Singleton instance.
*
Expand Down
2 changes: 1 addition & 1 deletion classes/booking_answers.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function is_overlapping(int $userid): array {
// If the coursestarttime is bigger than the other courseendtime we can skip.
if (
!self::check_overlap(
$answer->coursestartime,
$answer->coursestarttime,
$answer->courseendtime,
$settings->coursestarttime,
$settings->courseendtime
Expand Down
81 changes: 81 additions & 0 deletions tests/bo_availability/condition_all_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,87 @@ public function test_booking_bookit_overbooking_with_price(array $bdata): void {
singleton_service::get_instance()->userpricecategory = [];
}

/**
* Test overlapping.
*
* @covers \condition\isbookable::is_available
* @covers \condition\bookitbutton::is_available
* @covers \condition\alreadybooked::is_available
* @param array $bdata
* @throws \coding_exception
* @throws \dml_exception
*
* @dataProvider booking_common_settings_provider
*/
public function test_booking_bookit_overlapping(array $bdata): void {
global $DB, $CFG;

// Setup test data.
$course = $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'] = $course->id;
$bdata['bookingmanager'] = $bookingmanager->username;

$booking1 = $this->getDataGenerator()->create_module('booking', $bdata);

$this->setAdminUser();

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

$record = new stdClass();
$record->bookingid = $booking1->id;
$record->text = 'Test option1';
$record->courseid = 0;
$record->maxanswers = 2;
$record->disablebookingusers = 0;
$record->coursestarttime = strtotime('now + 3 day');
$record->courseendtime = strtotime('now + 6 day');
// $record->optiondateid_0 = 1;
// $record->coursestarttime_0 = strtotime('now - 3 day');
// $record->courseendtime_0 = strtotime('now + 6 day');
// $record->optiondateid_1 = 1;
// $record->coursestarttime_1 = strtotime('now - 1 day');
// $record->courseendtime_1 = strtotime('now + 3 day');

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

// Add restriction
$record->coursestarttime = strtotime('now + 2 day');
$record->courseendtime = strtotime('now + 4 day');
$record->bo_cond_nooverlapping_restrict = 1;
$record->bo_cond_nooverlapping_handling = MOD_BOOKING_COND_OVERLAPPING_HANDLING_BLOCK;
$option2 = $plugingenerator->create_option($record);
// Enrol user in first option.
$settings1 = singleton_service::get_instance_of_booking_option_settings($option1->id);
$settings2 = singleton_service::get_instance_of_booking_option_settings($option2->id);
$boinfo1 = new bo_info($settings1);
$boinfo2 = new bo_info($settings2);

// Try to enrol user in second option.
$this->setUser($student1);
$result = booking_bookit::bookit('option', $settings1->id, $student1->id);
$result = booking_bookit::bookit('option', $settings1->id, $student1->id);

// 2. option to check if overlapping.
// Try to book the student1.


// Option 2 is bookable since we didn't define nooverlapping.
list($id, $isavailable, $description) = $boinfo2->is_available($settings2->id, $student1->id, true);
$this->assertEquals(MOD_BOOKING_BO_COND_JSON_NOOVERLAPPING, $id);
}

/**
* Data provider for condition_bookingpolicy_test
*
Expand Down

0 comments on commit 974b028

Please sign in to comment.