From eb29ebefb4ce6cac77e6d3a7d936a49486b31dba Mon Sep 17 00:00:00 2001 From: Tobias Reischmann Date: Mon, 29 Apr 2019 17:24:29 +0200 Subject: [PATCH] MDL-63959 mod_feedback: Add phpunit tests for get_pages in completion --- mod/feedback/tests/completion_test.php | 138 +++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 mod/feedback/tests/completion_test.php diff --git a/mod/feedback/tests/completion_test.php b/mod/feedback/tests/completion_test.php new file mode 100644 index 00000000000..c75c2cfccbf --- /dev/null +++ b/mod/feedback/tests/completion_test.php @@ -0,0 +1,138 @@ +. +/** + * Unit tests for (some of) mod/feedback/classes/lib.php. + * + * @package mod_feedback + * @copyright 2019 Tobias Reischmann + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/mod/feedback/classes/completion.php'); + +/** + * Unit tests for (some of) mod/feedback/classes/completion.php. + * + * @copyright 2019 Tobias Reischmann + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_feedback_completion_testcase extends advanced_testcase { + + /** @var stdClass Course of this testcase. */ + private $course; + /** @var stdClass Feedback instance of this testcase. */ + private $feedback; + /** @var stdClass Course module of the feedback instance. */ + private $cm; + + /** + * Set up for every test + */ + public function setUp() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Setup test data. + $this->course = $this->getDataGenerator()->create_course(); + $this->feedback = $this->getDataGenerator()->create_module('feedback', + array('course' => $this->course->id)); + $this->cm = get_coursemodule_from_instance('feedback', $this->feedback->id); + } + + /** + * Returns the number of pages with visible elements for the current state of the feedback completion. + * @param mod_feedback_completion $completion + * @return int number of pages with at least one visible item. + */ + private function get_number_of_visible_pages(mod_feedback_completion $completion) { + $pages = $completion->get_pages(); + $result = 0; + foreach ($pages as $items) { + if (count($items) > 0) { + $result++; + } + } + return $result; + } + + /** + * Tests get_pages for transitive dependencies. + * @throws coding_exception + */ + public function test_get_pages() { + $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback'); + $itemscreated = []; + + // Create at least one page. + $itemscreated[] = $feedbackgenerator->create_item_multichoice($this->feedback, + $record = ['values' => "y\nn"]); + $itemscreated[] = $feedbackgenerator->create_item_pagebreak($this->feedback); + $itemscreated[] = $feedbackgenerator->create_item_multichoice($this->feedback, + $record = ['values' => "y\nn", 'dependitem' => $itemscreated[0]->id, 'dependvalue' => 'n']); + $itemscreated[] = $feedbackgenerator->create_item_pagebreak($this->feedback); + $itemscreated[] = $feedbackgenerator->create_item_multichoice($this->feedback, + $record = ['values' => "y\nn", 'dependitem' => $itemscreated[0]->id, 'dependvalue' => 'y']); + $itemscreated[] = $feedbackgenerator->create_item_pagebreak($this->feedback); + $itemscreated[] = $feedbackgenerator->create_item_multichoice($this->feedback, + $record = ['values' => "y\nn", 'dependitem' => $itemscreated[2]->id, 'dependvalue' => 'y']); + + // Test hiding item since transitive dependency is not met. + // Answering the first multichoice with 'y', should hide the second and therefor also the fourth. + $user1 = $this->getDataGenerator()->create_and_enrol($this->course); + $completion = new mod_feedback_completion($this->feedback, $this->cm, $this->course, + false, null, $user1->id); + + // Initially, all pages should be visible. + $this->assertEquals(4, $this->get_number_of_visible_pages($completion)); + + // Answer the first multichoice with 'y', which should exclude the second and the fourth. + $answers = ['multichoice_' . $itemscreated[0]->id => [1]]; + $completion->save_response_tmp((object) $answers); + + $this->assertEquals(2, $this->get_number_of_visible_pages($completion)); + + // Answer the third multichoice with 'n', which should exclude the last one. + $answers = ['multichoice_' . $itemscreated[4]->id => [2]]; + $completion->save_response_tmp((object) $answers); + + $this->assertEquals(2, $this->get_number_of_visible_pages($completion)); + + $completion->save_response(); + + // Test showing item since transitive dependency is met. + // Answering the first multichoice with 'n' should hide the third multichoice. + $user2 = $this->getDataGenerator()->create_and_enrol($this->course); + $completion2 = new mod_feedback_completion($this->feedback, $this->cm, $this->course, + false, null, $user2->id); + + // Initially, all pages should be visible. + $this->assertEquals(4, $this->get_number_of_visible_pages($completion2)); + + // Answer the first multichoice with 'n' should hide the third multichoice. + $answers = ['multichoice_' . $itemscreated[0]->id => [2]]; + $completion2->save_response_tmp((object) $answers); + + $this->assertEquals(3, $this->get_number_of_visible_pages($completion2)); + + // Answering the second multichoice with 'n' should hide the fourth one. + $answers = ['multichoice_' . $itemscreated[2]->id => [2]]; + $completion2->save_response_tmp((object) $answers); + + $this->assertEquals(2, $this->get_number_of_visible_pages($completion2)); + } + +}