forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-77230-master' of https://github.com/sarjona/moodle
- Loading branch information
Showing
3 changed files
with
280 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,9 @@ | |
use externallib_advanced_testcase; | ||
use feedback_item_multichoice; | ||
use mod_feedback_external; | ||
use moodle_exception; | ||
use required_capability_exception; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
|
@@ -45,6 +48,7 @@ | |
* @copyright 2017 Juan Leyva <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @since Moodle 3.3 | ||
* @covers \mod_feedback_external | ||
*/ | ||
class external_test extends externallib_advanced_testcase { | ||
|
||
|
@@ -374,6 +378,130 @@ public function test_get_items() { | |
} | ||
} | ||
|
||
/** | ||
* Test get_items, to confirm validation is done too. | ||
* | ||
* @dataProvider items_provider | ||
* @param string $role Whether the current user should be a student or a teacher. | ||
* @param array $info Settings to create the feedback. | ||
* @param string|null $warning The warning message to display or null if warnings result is empty. | ||
*/ | ||
public function test_get_items_validation(string $role, array $info, ?string $warning): void { | ||
global $DB; | ||
|
||
// Test user with full capabilities. | ||
if ($role === 'teacher') { | ||
$this->setUser($this->teacher); | ||
} else { | ||
$this->setUser($this->student); | ||
} | ||
|
||
// Create the feedback. | ||
$data = ['course' => $this->course->id]; | ||
if (array_key_exists('closed', $info) && $info['closed']) { | ||
$data['timeopen'] = time() + DAYSECS; | ||
} | ||
$feedback = $this->getDataGenerator()->create_module('feedback', $data); | ||
|
||
$empty = true; | ||
if (!array_key_exists('empty', $info) || !$info['empty']) { | ||
$empty = false; | ||
/** @var \mod_feedback_generator $feedbackgenerator */ | ||
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback'); | ||
// Add,at least, one item to the feedback. | ||
$feedbackgenerator->create_item_label($feedback); | ||
} | ||
|
||
if (array_key_exists('complete', $info) && !$info['complete']) { | ||
$studentrole = $DB->get_record('role', array('shortname' => 'student')); | ||
$coursecontext = \context_course::instance($this->course->id); | ||
assign_capability('mod/feedback:complete', CAP_PROHIBIT, $studentrole->id, $coursecontext->id); | ||
// Empty all the caches that may be affected by this change. | ||
accesslib_clear_all_caches_for_unit_testing(); | ||
\course_modinfo::clear_instance_cache(); | ||
} | ||
|
||
$result = mod_feedback_external::get_items($feedback->id); | ||
$result = external_api::clean_returnvalue(mod_feedback_external::get_items_returns(), $result); | ||
if ($warning) { | ||
$this->assertEmpty($result['items']); | ||
$this->assertCount(1, $result['warnings']); | ||
$resultwarning = reset($result['warnings']); | ||
if ($warning == 'required_capability_exception') { | ||
$this->assertStringContainsString('error/nopermission', $resultwarning['message']); | ||
} else { | ||
$this->assertEquals($warning, $resultwarning['message']); | ||
} | ||
} else { | ||
if ($empty) { | ||
$this->assertEmpty($result['items']); | ||
} else { | ||
$this->assertCount(1, $result['items']); | ||
} | ||
$this->assertEmpty($result['warnings']); | ||
} | ||
} | ||
|
||
/** | ||
* Data provider for test_get_items_validation() and test_get_page_items_validation(). | ||
* | ||
* @return array | ||
*/ | ||
public function items_provider(): array { | ||
return [ | ||
'Valid feedback (as student)' => [ | ||
'role' => 'student', | ||
'info' => [], | ||
'warning' => null, | ||
], | ||
'Closed feedback (as student)' => [ | ||
'role' => 'student', | ||
'info' => ['closed' => true], | ||
'warning' => get_string('feedback_is_not_open', 'feedback'), | ||
], | ||
'Empty feedback (as student)' => [ | ||
'role' => 'student', | ||
'info' => ['empty' => true], | ||
'warning' => get_string('no_items_available_yet', 'feedback'), | ||
], | ||
'Closed feedback (as student)' => [ | ||
'role' => 'student', | ||
'info' => ['closed' => true], | ||
'warning' => get_string('feedback_is_not_open', 'feedback'), | ||
], | ||
'Cannot complete feedback (as student)' => [ | ||
'role' => 'student', | ||
'info' => ['complete' => false], | ||
'warning' => 'required_capability_exception', | ||
], | ||
'Valid feedback (as teacher)' => [ | ||
'role' => 'teacher', | ||
'info' => [], | ||
'warning' => null, | ||
], | ||
'Closed feedback (as teacher)' => [ | ||
'role' => 'teacher', | ||
'info' => ['closed' => true], | ||
'warning' => null, | ||
], | ||
'Empty feedback (as teacher)' => [ | ||
'role' => 'teacher', | ||
'info' => ['empty' => true], | ||
'warning' => null, | ||
], | ||
'Closed feedback (as teacher)' => [ | ||
'role' => 'teacher', | ||
'info' => ['closed' => true], | ||
'warning' => null, | ||
], | ||
'Cannot complete feedback (as teacher)' => [ | ||
'role' => 'teacher', | ||
'info' => ['complete' => false], | ||
'warning' => null, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Test launch_feedback. | ||
*/ | ||
|
@@ -454,6 +582,70 @@ public function test_get_page_items() { | |
$this->assertTrue($result['hasprevpage']); | ||
} | ||
|
||
/** | ||
* Test get_page_items, to confirm validation is done too. | ||
* | ||
* @dataProvider items_provider | ||
* @param string $role Whether the current user should be a student or a teacher. | ||
* @param array $info Settings to create the feedback. | ||
* @param string|null $warning The warning message to display or null if warnings result is empty. | ||
*/ | ||
public function test_get_page_items_validation(string $role, array $info, ?string $warning): void { | ||
global $DB; | ||
|
||
// Test user with full capabilities. | ||
if ($role === 'teacher') { | ||
$this->setUser($this->teacher); | ||
} else { | ||
$this->setUser($this->student); | ||
} | ||
|
||
// Create the feedback. | ||
$data = ['course' => $this->course->id]; | ||
if (array_key_exists('closed', $info) && $info['closed']) { | ||
$data['timeopen'] = time() + DAYSECS; | ||
} | ||
$feedback = $this->getDataGenerator()->create_module('feedback', $data); | ||
|
||
$empty = true; | ||
if (!array_key_exists('empty', $info) || !$info['empty']) { | ||
$empty = false; | ||
/** @var \mod_feedback_generator $feedbackgenerator */ | ||
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback'); | ||
// Add,at least, one item to the feedback. | ||
$feedbackgenerator->create_item_label($feedback); | ||
} | ||
|
||
if (array_key_exists('complete', $info) && !$info['complete']) { | ||
$studentrole = $DB->get_record('role', array('shortname' => 'student')); | ||
$coursecontext = \context_course::instance($this->course->id); | ||
assign_capability('mod/feedback:complete', CAP_PROHIBIT, $studentrole->id, $coursecontext->id); | ||
// Empty all the caches that may be affected by this change. | ||
accesslib_clear_all_caches_for_unit_testing(); | ||
\course_modinfo::clear_instance_cache(); | ||
} | ||
|
||
$result = mod_feedback_external::get_page_items($feedback->id, 0); | ||
$result = external_api::clean_returnvalue(mod_feedback_external::get_items_returns(), $result); | ||
if ($warning) { | ||
$this->assertEmpty($result['items']); | ||
$this->assertCount(1, $result['warnings']); | ||
$resultwarning = reset($result['warnings']); | ||
if ($warning == 'required_capability_exception') { | ||
$this->assertStringContainsString('error/nopermission', $resultwarning['message']); | ||
} else { | ||
$this->assertEquals($warning, $resultwarning['message']); | ||
} | ||
} else { | ||
if ($empty) { | ||
$this->assertEmpty($result['items']); | ||
} else { | ||
$this->assertCount(1, $result['items']); | ||
} | ||
$this->assertEmpty($result['warnings']); | ||
} | ||
} | ||
|
||
/** | ||
* Test process_page. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters