Skip to content

Commit

Permalink
MDL-72991 Course: Create PHPUnit for course_modinfo and move_section_to
Browse files Browse the repository at this point in the history
Newly PHPUnit tests were created to verify the below methods
 - course_modinfo::purge_section_cache_by_id()
 - course_modinfo::purge_section_cache_by_number()
 - move_section_to()
  • Loading branch information
HuongNV13 committed Mar 17, 2022
1 parent 4bc2b24 commit 273fbac
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
45 changes: 45 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,51 @@ public function test_move_section_marker() {
$this->assertEquals(3, $course->marker);
}

/**
* Test move_section_to method with caching
*
* @covers ::move_section_to
* @return void
*/
public function test_move_section_with_section_cache(): void {
$this->resetAfterTest();
$this->setAdminUser();
$cache = cache::make('core', 'coursemodinfo');

// Generate the course and pre-requisite module.
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3], ['createsections' => true]);
// Reset course cache.
rebuild_course_cache($course->id, true);

// Build course cache.
get_fast_modinfo($course->id);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 4 section caches here.
$this->assertCount(4, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(1, $sectioncaches);
$this->assertArrayHasKey(2, $sectioncaches);
$this->assertArrayHasKey(3, $sectioncaches);

// Move section.
move_section_to($course, 2, 3);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 2 section caches left.
$this->assertCount(2, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(1, $sectioncaches);
$this->assertArrayNotHasKey(2, $sectioncaches);
$this->assertArrayNotHasKey(3, $sectioncaches);
}

public function test_course_can_delete_section() {
global $DB;
$this->resetAfterTest(true);
Expand Down
92 changes: 92 additions & 0 deletions lib/tests/modinfolib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,96 @@ public function get_section_info_by_id_provider() {
],
];
}

/**
* Test purge_section_cache_by_id method
*
* @covers \course_modinfo::purge_course_section_cache_by_id
* @return void
*/
public function test_purge_section_cache_by_id(): void {
$this->resetAfterTest();
$this->setAdminUser();
$cache = cache::make('core', 'coursemodinfo');

// Generate the course and pre-requisite section.
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3], ['createsections' => true]);
// Reset course cache.
rebuild_course_cache($course->id, true);
// Build course cache.
get_fast_modinfo($course->id);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 4 section caches here.
$this->assertCount(4, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(1, $sectioncaches);
$this->assertArrayHasKey(2, $sectioncaches);
$this->assertArrayHasKey(3, $sectioncaches);

// Purge cache for the section by id.
course_modinfo::purge_course_section_cache_by_id($course->id, $sectioncaches[1]->id);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 3 section caches left.
$this->assertCount(3, $sectioncaches);
$this->assertArrayNotHasKey(1, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(2, $sectioncaches);
$this->assertArrayHasKey(3, $sectioncaches);
// Make sure that the cacherev will be reset.
$this->assertEquals(-1, $coursemodinfo->cacherev);
}

/**
* Test purge_section_cache_by_number method
*
* @covers \course_modinfo::purge_course_section_cache_by_number
* @return void
*/
public function test_section_cache_by_number(): void {
$this->resetAfterTest();
$this->setAdminUser();
$cache = cache::make('core', 'coursemodinfo');

// Generate the course and pre-requisite section.
$course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3], ['createsections' => true]);
// Reset course cache.
rebuild_course_cache($course->id, true);
// Build course cache.
get_fast_modinfo($course->id);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 4 section caches here.
$this->assertCount(4, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(1, $sectioncaches);
$this->assertArrayHasKey(2, $sectioncaches);
$this->assertArrayHasKey(3, $sectioncaches);

// Purge cache for the section with section number is 1.
course_modinfo::purge_course_section_cache_by_number($course->id, 1);
// Get the course modinfo cache.
$coursemodinfo = $cache->get($course->id);
// Get the section cache.
$sectioncaches = $coursemodinfo->sectioncache;

// Make sure that we will have 3 section caches left.
$this->assertCount(3, $sectioncaches);
$this->assertArrayNotHasKey(1, $sectioncaches);
$this->assertArrayHasKey(0, $sectioncaches);
$this->assertArrayHasKey(2, $sectioncaches);
$this->assertArrayHasKey(3, $sectioncaches);
// Make sure that the cacherev will be reset.
$this->assertEquals(-1, $coursemodinfo->cacherev);
}
}

0 comments on commit 273fbac

Please sign in to comment.