Skip to content

Commit

Permalink
new phpunit test test_booking_cancellation_wiht_multiple_dates_and_co…
Browse files Browse the repository at this point in the history
…nsumption_enabled() (Wunderbyte-GmbH#777)
  • Loading branch information
semteacher committed Jan 5, 2025
1 parent fab5d0e commit dac905f
Showing 1 changed file with 207 additions and 0 deletions.
207 changes: 207 additions & 0 deletions tests/shopping_cart/shopping_cart_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,195 @@ public function test_booking_cancellation_wiht_consumption_enabled(array $bdata)
}
}

/**
* Test of purchase of few booking options with price and cancellation all by cashier with consumption has been enabled.
*
* @covers \condition\priceset::is_available
* @covers \booking_option::get_consumed_quota
* @covers \shopping_cart\service_provider::quota_consumed
*
* @param array $bdata
* @throws \coding_exception
* @throws \dml_exception
*
* @dataProvider booking_common_settings_provider
*
*/
public function test_booking_cancellation_wiht_multiple_dates_and_consumption_enabled(array $bdata): void {
global $DB, $CFG;

// Set parems requred for cancellation.
$bdata['booking']['cancancelbook'] = 1;
set_config('cancelationfee', 4, 'local_shopping_cart');
set_config('calculateconsumation', 1, 'local_shopping_cart');
// Ensure no fixed consumption set.
set_config('calculateconsumationfixedpercentage', -1, 'local_shopping_cart');
unset_config('fixedpercentageafterserviceperiodstart', 'local_shopping_cart');

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

// Create user profile custom fields.
$this->getDataGenerator()->create_custom_profile_field([
'datatype' => 'text', 'shortname' => 'pricecat', 'name' => 'pricecat',
]);
set_config('pricecategoryfield', 'pricecat', 'booking');

// Create users.
$students[0] = $this->getDataGenerator()->create_user(['profile_field_pricecat' => 'default']);
$students[1] = $this->getDataGenerator()->create_user(['profile_field_pricecat' => 'discount1']);
$students[2] = $this->getDataGenerator()->create_user(['profile_field_pricecat' => 'discount2']);
$teacher = $this->getDataGenerator()->create_user();
$bookingmanager = $this->getDataGenerator()->create_user(); // Booking manager.

$bdata['booking']['course'] = $course1->id;
$bdata['booking']['bookingmanager'] = $bookingmanager->username;
$booking1 = $this->getDataGenerator()->create_module('booking', $bdata['booking']);

$this->setAdminUser();

foreach ($students as $student) {
$this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student');
}
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id, 'editingteacher');
$this->getDataGenerator()->enrol_user($bookingmanager->id, $course1->id, 'editingteacher');

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

// Create set of price categories.
$plugingenerator->create_pricecategory($bdata['pricecategories'][0]);
$plugingenerator->create_pricecategory($bdata['pricecategories'][1]);
$plugingenerator->create_pricecategory($bdata['pricecategories'][2]);

// Create a booking option - setup only properties different form given in data provider.
$record = (object)$bdata['options'][3];
$record->bookingid = $booking1->id;
$record->chooseorcreatecourse = 1; // Reqiured.
$record->courseid = $course2->id;
$record->useprice = 1; // Use price from the default category.
$record->teachersforoption = $teacher->username;
$option1 = $plugingenerator->create_option($record);

singleton_service::destroy_booking_option_singleton($option1->id); // Require to avoid caching issues.
$settings1 = singleton_service::get_instance_of_booking_option_settings($option1->id);
$boinfo = new bo_info($settings1);

// Create users' purchases in background.
foreach ($students as $student) {
$userpurchase = ['optionid' => $option1->id, 'userid' => $student->id];
$plugingenerator->create_user_purchase($userpurchase);
}

// Validate that option already booked.
foreach ($students as $student) {
list($id, $isavailable, $description) = $boinfo->is_available($settings1->id, $student->id);
$this->assertEquals(MOD_BOOKING_BO_COND_ALREADYBOOKED, $id);
}

singleton_service::destroy_booking_option_singleton($option1->id); // Require to avoid caching issues.
$settings1 = singleton_service::get_instance_of_booking_option_settings($option1->id);

// Validation: consumend quota should be 0 beacuse option not started yet and fixedpercentageafterserviceperiodstart==1.
foreach ($students as $student) {
$userhistory = shopping_cart_history::get_most_recent_historyitem(
'mod_booking',
'option',
$settings1->id,
$student->id
);
$consumed = (object)shopping_cart::get_quota_consumed(
'mod_booking',
'option',
$settings1->id,
$student->id,
$userhistory->id
);
$this->assertEquals(0.67, $consumed->quota);
$this->assertEquals(4, $consumed->cancelationfee);
}

// Test dynamic form (partial).
$formdata = [
'cancelationfee' => $consumed->cancelationfee,
'componentname' => 'mod_booking',
'area' => 'option',
'itemid' => $settings1->id,
];
$form = new modal_cancel_all_addcredit(null, $formdata, 'post', '', [], true, $formdata, true);
$formdata1 = $form->mock_ajax_submit($formdata);
// phpcs:ignore
// $form->process_dynamic_submission();

// Validate code of modal_cancel_all_addcredit/process_dynamic_submission.
$data = (object)$formdata1;
$bookedusers = shopping_cart_history::get_user_list_for_option($data->itemid, $data->componentname, $data->area);

$cancelationfee = $data->cancelationfee ?? 0;

if ($data->cancelationfee < 0) {
$cancelationfee = 0;
}

$componentname = $data->componentname;
$area = $data->area;

foreach ($bookedusers as $buser) {
$credit = $buser->price - $cancelationfee;

// Negative credits are not allowed.
if ($credit < 0.0) {
$credit = 0.0;
}

shopping_cart::cancel_purchase(
$buser->itemid,
$data->area,
$buser->userid,
$componentname,
$buser->id,
$credit,
$cancelationfee,
1,
1
);
}

// For the booking component, we have a special treatment here.
if ($componentname === 'mod_booking' && $area === 'option') {
$pluginmanager = \core_plugin_manager::instance();
$plugins = $pluginmanager->get_plugins_of_type('mod');
if (isset($plugins['booking'])) {
booking_option::cancelbookingoption($data->itemid);
}
}

// Validate that option have been cancelled and users' credits.
foreach ($students as $key => $student) {
list($id, $isavailable, $description) = $boinfo->is_available($settings1->id, $student->id);
$this->assertEquals(MOD_BOOKING_BO_COND_ISCANCELLED, $id);

// Validate user credits.
$balance1 = shopping_cart_credits::get_balance($student->id);
// Check get_balance response.
$this->assertIsArray($balance1);
$this->assertEquals('EUR', $balance1[1]);
$this->assertArrayNotHasKey(2, $balance1);
switch ($key) {
case 0:
$this->assertEquals(29, $balance1[0]);
break;
case 1:
$this->assertEquals(25, $balance1[0]);
break;
case 2:
$this->assertEquals(22, $balance1[0]);
break;
}
}
}

/**
* Data provider for shopping_cart_test
*
Expand Down Expand Up @@ -1034,6 +1223,24 @@ public static function booking_common_settings_provider(): array {
'coursestarttime_1' => strtotime('now -48 hours'),
'courseendtime_1' => strtotime('now +72 hours'),
],
// Option 3 with 1 ongoing and 2 past non-overlaping sessions.
3 => [
'text' => 'Test Option 4',
'courseid' => 0,
'maxanswers' => 4,
'optiondateid_1' => "0",
'daystonotify_1' => "0",
'coursestarttime_1' => strtotime('now -6 day'),
'courseendtime_1' => strtotime('now -5 day'),
'optiondateid_2' => "0",
'daystonotify_2' => "0",
'coursestarttime_2' => strtotime('now -4 day'),
'courseendtime_2' => strtotime('now -3 day'),
'optiondateid_3' => "0",
'daystonotify_3' => "0",
'coursestarttime_3' => strtotime('now -48 hours'),
'courseendtime_3' => strtotime('now +72 hours'),
],
],
'pricecategories' => [
0 => (object)[
Expand Down

0 comments on commit dac905f

Please sign in to comment.