Skip to content

Commit

Permalink
infection
Browse files Browse the repository at this point in the history
  • Loading branch information
rdebleu committed Jun 8, 2024
1 parent 4f5096d commit c55b0b8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
8 changes: 5 additions & 3 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function get_description($full, $not, info $info): string {
* @return string Text representation of parameters
*/
protected function get_debug_string() {
// TODO: remove concat.
$modname = '';
if ($this->relativestart === 7) {
$modname = ' ';
Expand Down Expand Up @@ -203,7 +204,7 @@ public static function options_start(int $i) {
* @param int $number
* @return array
*/
public static function options_dwm($number = 2) {
public static function options_dwm($number = 1) {
$s = $number === 1 ? '' : 's';
return [
0 => get_string('minute' . $s, 'availability_relativedate'),
Expand Down Expand Up @@ -384,7 +385,7 @@ public static function completion_value_used($course, $cmid): bool {
*/
public function update_dependency_id($table, $oldid, $newid) {
if ($this->relativestart === 7) {
if ($table === 'course_modules' || $table === 'course_sections') {
if (in_array($table, ['course_modules', 'course_sections'])) {
if ($this->relativecoursemodule === $oldid) {
$this->relativecoursemodule = $newid;
return true;
Expand All @@ -405,14 +406,15 @@ public function update_dependency_id($table, $oldid, $newid) {
*/
public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name): bool {
$rec = \restore_dbops::get_backup_ids_record($restoreid, 'course_module', $this->relativecoursemodule);
if (!$rec || !$rec->newitemid) {
if (!($rec && $rec->newitemid)) {
// If we are on the same course (e.g. duplicate) then we can just use the existing one.
if (!get_coursemodule_from_id('', $this->relativecoursemodule, $courseid)) {
$this->relativecoursemodule = 0;
$logger->process(
"Restored item ($name has availability condition on module that was not restored",
\backup::LOG_WARNING
);
return false;
}
} else {
$this->relativecoursemodule = $rec->newitemid;
Expand Down
7 changes: 3 additions & 4 deletions classes/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class frontend extends \core_availability\frontend {
*/
protected function get_javascript_init_params($course, ?cm_info $cm = null, ?section_info $section = null) {
global $DB;
$optionsdwm = self::convert_associative_array_for_js(condition::options_dwm(), 'field', 'display');
$optionsdwm = self::convert_associative_array_for_js(condition::options_dwm(2), 'field', 'display');
$optionsstart = [
['field' => 1, 'display' => condition::options_start(1)],
['field' => 6, 'display' => condition::options_start(6)],
Expand All @@ -65,7 +65,6 @@ protected function get_javascript_init_params($course, ?cm_info $cm = null, ?sec
}
$activitysel = [];
if ($course->enablecompletion) {
$currentcmid = $cm ? $cm->id : 0;
$modinfo = get_fast_modinfo($course);
$str = get_string('section');
$s = [];
Expand All @@ -76,7 +75,7 @@ protected function get_javascript_init_params($course, ?cm_info $cm = null, ?sec
$s['name'] = empty($name) ? "$str $sectionnum" : format_string($name);
$s['coursemodules'] = [];
foreach ($cursection as $cmid) {
if ($currentcmid === $cmid) {
if ($cm && $cm->id === $cmid) {
continue;
}
$module = $modinfo->get_cm($cmid);
Expand All @@ -88,7 +87,7 @@ protected function get_javascript_init_params($course, ?cm_info $cm = null, ?sec
'name' => format_string($module->name),
'completionenabled' => $compused,
];
$enabled = $enabled || $compused;
$enabled = $compused ? true : $enabled;
}
}
$activitysel[] = $s;
Expand Down
4 changes: 3 additions & 1 deletion tests/backup_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace availability_relativedate;

use availability_relativedate\condition;
use core_availability\info_module;

/**
* Unit tests for the relativedate condition.
Expand Down Expand Up @@ -121,6 +122,7 @@ public function test_backup(): void {
$rc->destroy();
$course = get_course($newid);
$modinfo = get_fast_modinfo($course);
$this->assertCount(12, $modinfo->get_instances_of('page'));
$pages = $modinfo->get_instances_of('page');
$this->assertCount(12, $pages);
}
}
4 changes: 2 additions & 2 deletions tests/behat/behat_availability_relativedate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class behat_availability_relativedate extends behat_base {
* @param string $date
*/
public function i_should_see_relativedate($date) {
global $USER;
$user = self::get_session_user();
$times = array_filter(explode('##', $date));
$time = reset($times);
$stime = userdate($time, get_string('strftimedate', 'langconfig'), $USER->timezone);
$stime = userdate($time, get_string('strftimedate', 'langconfig'), $user->timezone);
$this->execute("behat_general::assert_element_contains_text", [$stime, '.course-content', 'css_element']);
}

Expand Down
44 changes: 29 additions & 15 deletions tests/condition_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use availability_relativedate\condition;
use context_module;
use core\event\course_module_completion_updated;
use core_availability\{tree, mock_info, info_module};
use core_availability\{tree, mock_info, info_module, info_section};
use stdClass;

/**
Expand All @@ -54,6 +54,7 @@ public function setUp(): void {
parent::setUp();
require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info_module.php');
require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info_section.php');
require_once($CFG->libdir . '/completionlib.php');
$this->resetAfterTest();
$this->setAdminUser();
Expand All @@ -72,12 +73,12 @@ public function setUp(): void {
*/
public static function tree_provider(): array {
return [
'After start course' => [2, 1, 1, "+2 hour", "From"],
'Before end course' => [3, 2, 2, '-3 day', 'Until'],
'After end enrol' => [4, 3, 3, '+4 week', 'From'],
'After end method' => [4, 3, 4, '+4 week', 'From'],
'After end course' => [3, 2, 5, '+3 day', 'From'],
'Before start course' => [2, 1, 6, "-2 hour", "Until"],
'After start course' => [2, 1, 1, "+2 hour", "From", false, true],
'Before end course' => [3, 2, 2, '-3 day', 'Until', false, true],
'After end enrol' => [4, 3, 3, '+4 week', 'From', false, true],
'After end method' => [4, 3, 4, '+4 week', 'From', false, true],
'After end course' => [3, 2, 5, '+3 day', 'From', false, true],
'Before start course' => [2, 1, 6, '-2 hour', 'Until', true, false],
];
}

Expand All @@ -90,9 +91,11 @@ public static function tree_provider(): array {
* @param int $s relative to
* @param string $str
* @param string $result
* @param bool $availablefalse
* @param bool $availabletrue
* @covers \availability_relativedate\condition
*/
public function test_tree($n, $d, $s, $str, $result): void {
public function test_tree($n, $d, $s, $str, $result, $availablefalse, $availabletrue): void {
$arr = (object)['type' => 'relativedate', 'n' => $n, 'd' => $d, 's' => $s, 'm' => 9999999];
$stru = (object)['op' => '|', 'show' => true, 'c' => [$arr]];
$tree = new tree($stru);
Expand All @@ -103,7 +106,8 @@ public function test_tree($n, $d, $s, $str, $result): void {
$nau = 'Not available unless:';
$calc = userdate(strtotime($str, $this->get_reldate($s)), $strf, 0);
$this->assertEquals("$nau $result $calc", $tree->get_full_information($info));
$tree->check_available(false, $info, false, $this->user->id)->is_available();
$this->assertEquals($availablefalse, $tree->check_available(false, $info, false, $this->user->id)->is_available());
$this->assertEquals($availabletrue, $tree->check_available(true, $info, false, $this->user->id)->is_available());
}

/**
Expand Down Expand Up @@ -172,7 +176,10 @@ public function test_description($n, $d, $s, $str, $result1, $result2, $result3)

$this->setAdminUser();
$this->assertStringContainsString($result3, $cond->get_description(true, false, $info));
$this->assertNotEmpty($cond->get_standalone_description(false, false, $info));
$this->assertNotEquals("$nau $result1 $calc", $cond->get_standalone_description(false, false, $info));
$this->assertNotEquals("$nau $result2 $calc", $cond->get_standalone_description(false, true, $info));
$this->assertNotEquals("$nau $result1 $calc", $cond->get_standalone_description(true, false, $info));
$this->assertNotEquals("$nau $result2 $calc", $cond->get_standalone_description(true, true, $info));
}

/**
Expand All @@ -194,10 +201,9 @@ public function test_get_description(): void {
$DB->set_field('course_modules', 'availability', $str, ['id' => $page0->cmid]);
rebuild_course_cache($this->course->id, true);
$cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page1->cmid]);
$this->assertStringContainsString("4 months after completion of activity", $cond->get_description(true, false, $info));
$this->assertStringContainsString("4 months after completion of activity", $cond->get_description(true, true, $info));
$this->assertStringContainsString("4 months after completion of activity", $cond->get_description(false, false, $info));
$this->assertStringContainsString("4 months after completion of activity", $cond->get_description(false, true, $info));
$str = "(4 months after completion of activity <AVAILABILITY_CMNAME_$page1->cmid/>)";
$this->assertEquals($str, $cond->get_description(true, false, $info));
$this->assertEquals($str, $cond->get_description(true, true, $info));
$this->assertFalse($cond->completion_value_used($this->course, $page0->cmid));
$this->assertTrue($cond->completion_value_used($this->course, $page1->cmid));

Expand All @@ -215,8 +221,10 @@ public function test_get_description(): void {

$cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page0->cmid]);
$this->assertTrue($cond->update_dependency_id('course_sections', $page0->cmid, 3));
$this->assertFalse($cond->update_dependency_id('course_sections', $page0->cmid, $page0->cmid));
$this->assertFalse($cond->update_dependency_id('course_modules', $page0->cmid, $page0->cmid));
$this->assertFalse($cond->update_dependency_id('course_modules', $page0->cmid, $page1->cmid));
$this->assertFalse($cond->update_dependency_id('course_modules', $page0->cmid, 3));
$this->assertFalse($cond->update_dependency_id('course_modules', $page1->cmid, 3));
$this->assertFalse($cond->update_dependency_id('', $page1->cmid, 3));
$this->assertFalse($cond->update_dependency_id('', $page1->cmid, $page0->cmid));
Expand Down Expand Up @@ -247,6 +255,13 @@ public function test_no_enddate(): void {
$pg->create_instance(['course' => $course2]);
$modinfo1 = get_fast_modinfo($course1);
$modinfo2 = get_fast_modinfo($course2);
$info = new info_section($modinfo2->get_section_info_all()[1]);
$cond = new condition((object)['type' => 'relativedate', 'n' => 6, 'd' => 2, 's' => 2, 'm' => 1]);
$information = $cond->get_description(false, false, $info);
$strf = get_string('strftimedatetime', 'langconfig');
$str = userdate($course2->enddate - (6 * 24 * 3600), $strf);
$this->assertEquals("Until $str", $information);

$cm1 = $modinfo1->get_cm($page1->cmid);
$cm2 = $modinfo2->get_cm($page2->cmid);
$info = new info_module($cm1);
Expand All @@ -259,7 +274,6 @@ public function test_no_enddate(): void {
$this->assertFalse($cond->is_available(true, $info, false, $user->id));
$info = new info_module($cm2);
$information = $cond->get_description(true, false, $info);
$strf = get_string('strftimedatetime', 'langconfig');
$this->assertStringNotContainsString('(No course enddate)', $information);
$str = userdate($course2->enddate - (7 * 24 * 3600), $strf);
$this->assertEquals("Until $str (7 days before course end date)", $information);
Expand Down
14 changes: 10 additions & 4 deletions tests/frontend_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,26 @@ public function test_frontend(): void {
$instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'self'], '*', MUST_EXIST);
$DB->set_field('enrol', 'enrolenddate', time() + 10000, ['id' => $instance->id]);
$DB->set_field('enrol', 'enrolstartdate', time() - 100, ['id' => $instance->id]);
$page = $dg->get_plugin_generator('mod_page')->create_instance(['course' => $course, 'completion' => 1]);
$modinfo = get_fast_modinfo($course);
$cms = $modinfo->get_instances();
$cm = $cms['page'][$page->id];
$DB->set_field('course_modules', 'deletioninprogress', true, ['id' => $cm->id]);
$arr = $this->call_method([$course]);
$this->assertCount(5, $arr);
$this->assertCount(4, $arr[1]);
$this->assertCount(5, $arr[1]);
$expected = [
['field' => 1, 'display' => 'after course start date'],
['field' => 6, 'display' => 'before course start date'],
['field' => 3, 'display' => 'after user enrolment date'],
['field' => 4, 'display' => 'after enrolment method end date'],
['field' => 7, 'display' => 'after completion of activity'],
];
$this->assertEquals($expected, $arr[1]);
$this->assertTrue($arr[2]);
$this->assertCount(0, $arr[3]);
$this->assertCount(0, $arr[4]);

$this->assertCount(1, $arr[4]);
$this->assertCount(2, $arr[4][0]);
}

/**
Expand Down Expand Up @@ -216,8 +222,8 @@ public function test_behat(): void {
$class->selfenrolment_exists_in_course_ending($course->fullname, '');
$class->selfenrolment_exists_in_course_ending($course->fullname, '## today ##');
$this->expectExceptionMessage('behat_context_helper');
$class->i_should_see_relativedate('##-10 days noon##');
$class->i_make_activity_relative_date_depending_on('page1', 'page2');
$class->i_should_see_relativedate('##-10 days noon##');
}

/**
Expand Down
9 changes: 4 additions & 5 deletions tests/simple_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ public function test_save(): void {
*/
public function test_static(): void {
$this->assertCount(5, condition::options_dwm());
$expected = [0 => 'minutes', 1 => 'hours', 2 => 'days', 3 => 'weeks', 4 => 'months'];
$this->assertEquals($expected, condition::options_dwm());
$this->assertEquals($expected, condition::options_dwm(2));
$this->assertEquals(condition::options_dwm(4), condition::options_dwm(3));

$expected = [0 => 'minute', 1 => 'hour', 2 => 'day', 3 => 'week', 4 => 'month'];
$this->assertEquals($expected, condition::options_dwm(1));
$this->assertEquals('minute', condition::option_dwm(0));
Expand All @@ -133,6 +128,10 @@ public function test_static(): void {
$this->assertEquals('month', condition::option_dwm(4));
$this->assertEquals('', condition::option_dwm(5));
$this->assertEquals('', condition::option_dwm(6));
$this->assertEquals($expected, condition::options_dwm());
$expected = [0 => 'minutes', 1 => 'hours', 2 => 'days', 3 => 'weeks', 4 => 'months'];
$this->assertEquals($expected, condition::options_dwm(2));
$this->assertEquals(condition::options_dwm(4), condition::options_dwm(3));

$this->assertEquals('', condition::options_start(0));
$this->assertEquals('after course start date', condition::options_start(1));
Expand Down

0 comments on commit c55b0b8

Please sign in to comment.