Skip to content

Commit

Permalink
Add unittest for toggle_state
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 committed Oct 17, 2023
1 parent 63d180e commit 016ca49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
27 changes: 4 additions & 23 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,7 @@ public function delete_page($pageorid) {
* @return int
*/
public function toggle_teacherpermission_state(object $page): int {

$instance = self::get_instance_by_pageid($page->id);
$cm = self::get_cm_by_instance($instance);

if (!has_capability('mod/mootimeter:moderator', \context_module::instance($cm->id))) {
return -1;
}

$showonteacherpermission = self::get_tool_config($page->id, 'showonteacherpermission');

$helper = new \mod_mootimeter\helper();

if (empty($showonteacherpermission)) {
// The config is not set yet. Set the value to 1.
$helper->set_tool_config($page, 'showonteacherpermission', 1);
return 1;
}

// The config was already set. Toggle it.
$helper->set_tool_config($page, 'showonteacherpermission', 0);
return 0;
return $this->toggle_state($page, 'showonteacherpermission');
}

/**
Expand All @@ -522,9 +502,10 @@ public function toggle_state(object $page, string $statename): int {

$instance = self::get_instance_by_pageid($page->id);
$cm = self::get_cm_by_instance($instance);
$context = \context_module::instance($cm->id);

if (!has_capability('mod/mootimeter:moderator', \context_module::instance($cm->id))) {
return -1;
if (!has_capability('mod/mootimeter:moderator', $context)) {
throw new \required_capability_exception($context, 'mod/mootimeter:moderator', 'nopermission', 'mod_mootimeter');
}

$togglestate = self::get_tool_config($page->id, $statename);
Expand Down
37 changes: 37 additions & 0 deletions tests/helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,41 @@ public function test_set_get_tool_config() {
$helper->set_tool_config($page->id, 'question', self::TEST_QUESTION_TITLE . "2");
$this->assertEquals(self::TEST_QUESTION_TITLE . "2", \mod_mootimeter\helper::get_tool_config($page->id, 'question'));
}

/**
* Toggle state test.
* @return void
* @throws coding_exception
* @throws dml_exception
* @throws InvalidArgumentException
* @throws ExpectationFailedException
* @covers \mod_mootimeter\helper->toggle_state
* @covers \mod_mootimeter\helper->toggle_teacherpermission_state
*/
public function test_toggle_state() {
$this->resetAfterTest();

$helper = new \mod_mootimeter\helper();
$mtmgenerator = $this->getDataGenerator()->get_plugin_generator('mod_mootimeter');
$page = $mtmgenerator->create_page($this, ['instance' => $this->mootimeter->id]);

$this->setUser($this->users['teacher']);
$helper->toggle_state($page, 'teststate');
$this->assertTrue((bool) \mod_mootimeter\helper::get_tool_config($page, 'teststate'));
$helper->toggle_state($page, 'teststate');
$this->assertFalse((bool) \mod_mootimeter\helper::get_tool_config($page, 'teststate'));

$this->setUser($this->users['student']);
$this->expectException(\required_capability_exception::class);
$helper->toggle_state($page, 'teststate');
$this->assertFalse((bool) \mod_mootimeter\helper::get_tool_config($page, 'teststate'));

$this->expectException(\required_capability_exception::class);
$helper->toggle_teacherpermission_state($page);
$this->assertFalse((bool) \mod_mootimeter\helper::get_tool_config($page, 'teststate'));

$this->setUser($this->users['teacher']);
$helper->toggle_state($page, 'teststate');
$this->assertTrue((bool) \mod_mootimeter\helper::get_tool_config($page, 'teststate'));
}
}

0 comments on commit 016ca49

Please sign in to comment.