Skip to content

Commit

Permalink
new phpunit method test_booking_customform_delete_data() (#636) (not …
Browse files Browse the repository at this point in the history
…fully working, core\cron disabled as not sypported in Moodle 4.1)
  • Loading branch information
semteacher committed Oct 24, 2024
1 parent 7e2a1b5 commit 05d4942
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions tests/bo_availability/condition_bookingpolicy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
use coding_exception;
use mod_booking_generator;
use mod_booking\bo_availability\bo_info;
use mod_booking\bo_availability\conditions\customform;
use mod_booking\local\mobile\customformstore;
// phpcs:ignore
//use core\cron;
use stdClass;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -215,6 +219,120 @@ public function test_booking_customform(array $bdata): void {
singleton_service::destroy_booking_option_singleton($option1->id);
}

/**
* Test booking option availability: \condition\customform with supporting of data deletion.
*
* @covers \condition\customform::is_available
*
* @param array $bdata
* @throws \coding_exception
* @throws \dml_exception
*
* @dataProvider booking_settings_provider
*/
public function test_booking_customform_delete_data(array $bdata): void {
// phpcs:ignore
//cron::setup_user();
// 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, 'student');
$this->getDataGenerator()->enrol_user($student2->id, $course1->id, 'student');
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id, 'student');
$this->getDataGenerator()->enrol_user($bookingmanager->id, $course1->id, 'editingteacher');

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

// Create booking rule - "ndays before".
$ruledata1 = [
'name' => '1daybefore',
'conditionname' => 'select_users',
'contextid' => 1,
'conditiondata' => '{"userids":["2"]}',
'actionname' => 'delete_conditions_from_bookinganswer',
'actiondata' => '{}',
'rulename' => 'rule_daysbefore',
'ruledata' => '{"days":"0","datefield":"bookingclosingtime","cancelrules":[]}',
];
$rule1 = $plugingenerator->create_rule($ruledata1);

$record = new stdClass();
$record->bookingid = $booking1->id;
$record->text = 'Test option1';
$record->chooseorcreatecourse = 1; // Reqiured.
$record->courseid = $course1->id;
// Set test objective setting(s).
$record->bo_cond_customform_restrict = 1;
$record->bo_cond_customform_select_1_1 = 'shorttext';
$record->bo_cond_customform_label_1_1 = 'Personal requirement:';
$record->bo_cond_customform_deleteinfoscheckboxadmin = 1; // Admin-level deletion.
// phpcs:ignore
//$record->bookingclosingtime = strtotime('24 February 2022 04:00');
$record->bookingclosingtime = strtotime('now - 1 min');
$option1 = $plugingenerator->create_option($record);
$settings1 = singleton_service::get_instance_of_booking_option_settings($option1->id);

// Booking options by the 1st student.
$result = $plugingenerator->create_answer(['optionid' => $option1->id, 'userid' => $student1->id]);
$this->assertEquals(MOD_BOOKING_BO_COND_ALREADYBOOKED, $result);
$answer1 = singleton_service::get_instance_of_booking_answers($settings1)->answers;
$this->assertIsArray($answer1);
$this->assertCount(1, $answer1);
$answer1 = array_shift($answer1);

// Create option's answer custom form data record.
$formrecord = new stdClass();
$formrecord->id = $option1->id;
$formrecord->userid = $student1->id;
$formrecord->customform_shorttext_1 = 'lactose-free milk';
$customformstore1 = new customformstore($student1->id, $settings1->id);
$customformstore1->set_customform_data($formrecord);
customform::add_json_to_booking_answer($answer1, $student1->id);

// Verify presence of json string in the answer.
singleton_service::destroy_booking_option_singleton($option1->id);
$settings1 = singleton_service::get_instance_of_booking_option_settings($option1->id);

$answer2 = singleton_service::get_instance_of_booking_answers($settings1)->answers;
$this->assertIsArray($answer2);
$this->assertCount(1, $answer2);
$answer2 = array_shift($answer2);
$this->assertStringContainsString($formrecord->customform_shorttext_1, $answer2->json);

// Trigger cron tasks.
ob_start();
// phpcs:ignore
//cron::run_scheduled_tasks(time());
$this->runAdhocTasks();

$res = ob_get_clean();

$answer3 = singleton_service::get_instance_of_booking_answers($settings1)->answers;
$answer3 = array_shift($answer3);
// phpcs:ignore
//$this->assertNull($answer3->json);

// Mandatory to solve potential cache issues.
singleton_service::destroy_booking_option_singleton($option1->id);
}

/**
* Test booking option availability: \condition\max_number_of_bookings.
*
Expand Down

0 comments on commit 05d4942

Please sign in to comment.