From 7f0a9a144c0726ca7cf3762d304a076ee3b643cd Mon Sep 17 00:00:00 2001 From: Nelson Moller Date: Mon, 5 Jun 2017 09:55:52 -0400 Subject: [PATCH 1/5] MM-653: Scaffolding pour un premier test. --- tests/base_test.php | 107 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/base_test.php diff --git a/tests/base_test.php b/tests/base_test.php new file mode 100644 index 0000000..74e32c7 --- /dev/null +++ b/tests/base_test.php @@ -0,0 +1,107 @@ +. + +/** + * Base class for unit tests for mod_assign. + * + * @package blocks_completion_progress + * @category phpunit + * @copyright 2017 onwards Nelson Moller {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/assign/locallib.php'); + +class mod_assign_base_testcase extends advanced_testcase { + + /** @const Default number of students to create */ + const DEFAULT_STUDENT_COUNT = 3; + /** @const Default number of teachers to create */ + const DEFAULT_TEACHER_COUNT = 1; + + /** + * Setup function - we will create a course and add an assign instance to it. + */ + protected function setUp() { + global $DB; + + $this->resetAfterTest(true); + + set_config('enablecompletion', 1); + + $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $this->teachers = array(); + for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) { + array_push($this->teachers, $this->getDataGenerator()->create_user()); + } + + $this->students = array(); + for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) { + array_push($this->students, $this->getDataGenerator()->create_user()); + } + + + $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); + foreach ($this->teachers as $i => $teacher) { + $this->getDataGenerator()->enrol_user($teacher->id, + $this->course->id, + $teacherrole->id); + } + + + $studentrole = $DB->get_record('role', array('shortname'=>'student')); + foreach ($this->students as $i => $student) { + $this->getDataGenerator()->enrol_user($student->id, + $this->course->id, + $studentrole->id); + } + } + + /** + * Convenience function to create a testable instance of an assignment. + * + * @param array $params Array of parameters to pass to the generator + * @return testable_assign Testable wrapper around the assign class. + */ + protected function create_instance($params=array()) { + $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); + $params['course'] = $this->course->id; + $instance = $generator->create_instance($params); + $cm = get_coursemodule_from_instance('assign', $instance->id); + $context = context_module::instance($cm->id); + return new assign($context, $cm, $this->course); + } + + public function test_assign_get_completion_state() { + global $DB; + $assign = $this->create_instance(array('submissiondrafts' => 0, 'completionsubmit' => 1)); + + $this->setUser($this->students[0]); + $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); + $this->assertFalse($result); + $submission = $assign->get_user_submission($this->students[0]->id, true); + $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; + $DB->update_record('assign_submission', $submission); + + $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); + + $this->assertTrue($result); + } +} \ No newline at end of file From 224266cdd831e61289f0d667b1e9c59236077d9f Mon Sep 17 00:00:00 2001 From: Nelson Moller Date: Mon, 5 Jun 2017 14:32:42 -0400 Subject: [PATCH 2/5] MM-653: A basic unit-test for the block functionning is working. --- tests/base_test.php | 80 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/tests/base_test.php b/tests/base_test.php index 74e32c7..7480e37 100644 --- a/tests/base_test.php +++ b/tests/base_test.php @@ -28,8 +28,9 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/mod/assign/locallib.php'); +require_once($CFG->dirroot.'/blocks/completion_progress/lib.php'); -class mod_assign_base_testcase extends advanced_testcase { +class block_completion_progress_base_testcase extends advanced_testcase { /** @const Default number of students to create */ const DEFAULT_STUDENT_COUNT = 3; @@ -78,7 +79,7 @@ class mod_assign_base_testcase extends advanced_testcase { * Convenience function to create a testable instance of an assignment. * * @param array $params Array of parameters to pass to the generator - * @return testable_assign Testable wrapper around the assign class. + * @return assign Assign class. */ protected function create_instance($params=array()) { $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); @@ -91,17 +92,84 @@ class mod_assign_base_testcase extends advanced_testcase { public function test_assign_get_completion_state() { global $DB; - $assign = $this->create_instance(array('submissiondrafts' => 0, 'completionsubmit' => 1)); + + // Add a block + $context = CONTEXT_COURSE::instance($this->course->id); + $block_info = [ + 'blockname' => 'completion_progress', + 'parentcontextid' => $context->id, + 'pagetypepattern' => 'course-view-*', + 'showinsubcontexts' => 0, + 'defaultweight' => 5, + 'defaultregion' => 'side-post', + 'configdata' => 'Tzo4OiJzdGRDbGFzcyI6Njp7czo3OiJvcmRlcmJ5IjtzOjExOiJvcmRlcmJ5dGltZSI7czo4OiJsb25nYmFycyI7czo3OiJzcXVlZXplIjtzOjE2OiJwcm9ncmVzc0Jhckljb25zIjtzOjE6IjEiO3M6MTQ6InNob3dwZXJjZW50YWdlIjtzOjE6IjAiO3M6MTM6InByb2dyZXNzVGl0bGUiO3M6MDoiIjtzOjE4OiJhY3Rpdml0aWVzaW5jbHVkZWQiO3M6MTg6ImFjdGl2aXR5Y29tcGxldGlvbiI7fQ==' + ]; + + $block_instance_id = $DB->insert_record('block_instances', (object) $block_info); + + $assign = $this->create_instance([ + 'submissiondrafts' => 0, + 'completionsubmit' => 1, + 'completion' => COMPLETION_TRACKING_AUTOMATIC + ]); $this->setUser($this->students[0]); - $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); + + $result = assign_get_completion_state( + $this->course, + $assign->get_course_module(), + $this->students[0]->id, + false + ); $this->assertFalse($result); + + $submissions = block_completion_progress_student_submissions($this->course->id, $this->students[0]->id); + $config = unserialize(base64_decode($block_info['configdata'])); + $activities = block_completion_progress_get_activities($this->course->id, $config); + + $completions = block_completion_progress_completions($activities, $this->students[0]->id, $this->course, + $submissions); + + $text = block_completion_progress_bar( + $activities, + $completions, + $config, + $this->students[0]->id, + $this->course, + $block_instance_id + ); + + $this->assertContains('assign', $text, '', true); + $this->assertNotContains('quiz', $text, '', true); + // The status is futureNotCompleted + $this->assertContains('background-color:#025187', $text, ''); + $submission = $assign->get_user_submission($this->students[0]->id, true); $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; $DB->update_record('assign_submission', $submission); - $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); - + $result = assign_get_completion_state( + $this->course, + $assign->get_course_module(), + $this->students[0]->id, + false + ); $this->assertTrue($result); + + $submissions = block_completion_progress_student_submissions($this->course->id, $this->students[0]->id); + $completions = block_completion_progress_completions($activities, $this->students[0]->id, $this->course, + $submissions); + + $text = block_completion_progress_bar( + $activities, + $completions, + $config, + $this->students[0]->id, + $this->course, + $block_instance_id + ); + + // The status is send but not finished + $this->assertContains('background-color:#FFCC00', $text, ''); } } \ No newline at end of file From 3c06e863fdaf92b365947ac6b26c12817e6e83ab Mon Sep 17 00:00:00 2001 From: Nelson Moller Date: Mon, 5 Jun 2017 14:43:29 -0400 Subject: [PATCH 3/5] MM-653: We take the colours values from the lang file. --- tests/base_test.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/base_test.php b/tests/base_test.php index 7480e37..3a4695b 100644 --- a/tests/base_test.php +++ b/tests/base_test.php @@ -142,7 +142,8 @@ class block_completion_progress_base_testcase extends advanced_testcase { $this->assertContains('assign', $text, '', true); $this->assertNotContains('quiz', $text, '', true); // The status is futureNotCompleted - $this->assertContains('background-color:#025187', $text, ''); + $color1 = get_string('futureNotCompleted_colour', 'block_completion_progress'); + $this->assertContains('background-color:' . $color1, $text, ''); $submission = $assign->get_user_submission($this->students[0]->id, true); $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; @@ -170,6 +171,7 @@ class block_completion_progress_base_testcase extends advanced_testcase { ); // The status is send but not finished - $this->assertContains('background-color:#FFCC00', $text, ''); + $color2 = get_string('submittednotcomplete_colour', 'block_completion_progress'); + $this->assertContains('background-color:' . $color2, $text, ''); } } \ No newline at end of file From 1c53f6847499dea6e0084b1ab99ba734cf6e3e20 Mon Sep 17 00:00:00 2001 From: Nelson Moller Date: Tue, 6 Jun 2017 09:04:52 -0400 Subject: [PATCH 4/5] MM-653: Scaffolding for the behat test. --- tests/behat/block_completion_progress.feature | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/behat/block_completion_progress.feature diff --git a/tests/behat/block_completion_progress.feature b/tests/behat/block_completion_progress.feature new file mode 100644 index 0000000..b6a6f16 --- /dev/null +++ b/tests/behat/block_completion_progress.feature @@ -0,0 +1,70 @@ +@block @block_completion_progress @mod_quiz +Feature: View the quiz being due + In order to know what quizzes are due + As a student + I can visit my dashboard + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + | teacher1 | C1 | editingteacher | + And the following "activities" exist: + | activity | course | idnumber | name | timeclose | + | quiz | C1 | Q1A | Quiz 1A No deadline | 0 | + | quiz | C1 | Q1B | Quiz 1B Past deadline | 1337 | + | quiz | C1 | Q1C | Quiz 1C Future deadline | 9000000000 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | qtype | name | questiontext | questioncategory | + | truefalse | First question | Answer the first question | Test questions | + And quiz "Quiz 1A No deadline" contains the following questions: + | question | page | + | First question | 1 | + And quiz "Quiz 1B Past deadline" contains the following questions: + | question | page | + | First question | 1 | + And quiz "Quiz 1C Future deadline" contains the following questions: + | question | page | + | First question | 1 | + And the following config values are set as admin: + | enablecompletion | 1 | + | enableavailability | 1 | + And I log in as "teacher1" + And I am on site homepage + And I follow "Course 1" + And I turn editing mode on + And I click on "Edit settings" "link" in the "Administration" "block" + And I set the following fields to these values: + | Enable completion tracking | Yes | + And I press "Save and display" + And I follow "Quiz 1A No deadline" + And I click on "Parameters" "link" in the "Administration" "block" + And I set the following fields to these values: + | Enable completion tracking | Yes | + And I press "Save and return to course" + + Scenario: Submit the quizzes + Given I log in as "student1" + And I follow "Course 1" + And I follow "Quiz 1A No deadline" + And I press "Attempt quiz now" + And I follow "Finish attempt ..." + And I press "Submit all and finish" + And I follow "Course 1" + And I follow "Quiz 1B Past deadline" + And I follow "Course 1" + And I follow "Quiz 1A No deadline" + And I press "Attempt quiz now" + And I follow "Finish attempt ..." + And I press "Submit all and finish" + And I follow "Course 1" \ No newline at end of file From 5198a2bbd0b7e63b52d25da2802edd9500de8e42 Mon Sep 17 00:00:00 2001 From: Nelson Moller Date: Tue, 6 Jun 2017 10:54:14 -0400 Subject: [PATCH 5/5] MM-653: The basic scenario is set up. --- tests/behat/block_completion_progress.feature | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/tests/behat/block_completion_progress.feature b/tests/behat/block_completion_progress.feature index b6a6f16..9473f64 100644 --- a/tests/behat/block_completion_progress.feature +++ b/tests/behat/block_completion_progress.feature @@ -1,5 +1,5 @@ @block @block_completion_progress @mod_quiz -Feature: View the quiz being due +Feature: Using block completion progress for a quiz In order to know what quizzes are due As a student I can visit my dashboard @@ -9,18 +9,21 @@ Feature: View the quiz being due | username | firstname | lastname | email | | student1 | Student | 1 | student1@example.com | | teacher1 | Teacher | 1 | teacher1@example.com | + And the following config values are set as admin: + | enablecompletion | 1 | + | enableavailability | 1 | And the following "courses" exist: - | fullname | shortname | - | Course 1 | C1 | + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | And the following "course enrolments" exist: | user | course | role | | student1 | C1 | student | | teacher1 | C1 | editingteacher | And the following "activities" exist: - | activity | course | idnumber | name | timeclose | - | quiz | C1 | Q1A | Quiz 1A No deadline | 0 | - | quiz | C1 | Q1B | Quiz 1B Past deadline | 1337 | - | quiz | C1 | Q1C | Quiz 1C Future deadline | 9000000000 | + | activity | course | idnumber | name | timeclose | enablecompletion | + | quiz | C1 | Q1A | Quiz 1A No deadline | 0 | 1 | + | quiz | C1 | Q1B | Quiz 1B Past deadline | 1337 | 1 | + | quiz | C1 | Q1C | Quiz 1C Future deadline | 9000000000 | 1 | And the following "question categories" exist: | contextlevel | reference | name | | Course | C1 | Test questions | @@ -36,35 +39,34 @@ Feature: View the quiz being due And quiz "Quiz 1C Future deadline" contains the following questions: | question | page | | First question | 1 | - And the following config values are set as admin: - | enablecompletion | 1 | - | enableavailability | 1 | - And I log in as "teacher1" + Given I log in as "teacher1" And I am on site homepage And I follow "Course 1" And I turn editing mode on - And I click on "Edit settings" "link" in the "Administration" "block" - And I set the following fields to these values: - | Enable completion tracking | Yes | - And I press "Save and display" And I follow "Quiz 1A No deadline" - And I click on "Parameters" "link" in the "Administration" "block" + And I navigate to "Edit settings" node in "Quiz administration" And I set the following fields to these values: - | Enable completion tracking | Yes | + | Completion tracking | Show activity as complete when conditions are met | + | Require view | 1 | And I press "Save and return to course" + And I add the "Completion Progress" block + And I configure the "Completion Progress" block + And I set the following fields to these values: + | Show percentage to students | Yes | + And I press "Save changes" + And I log out - Scenario: Submit the quizzes + Scenario: Basic functionning of the block Given I log in as "student1" And I follow "Course 1" - And I follow "Quiz 1A No deadline" - And I press "Attempt quiz now" - And I follow "Finish attempt ..." - And I press "Submit all and finish" - And I follow "Course 1" - And I follow "Quiz 1B Past deadline" + Then I should see "Progress: 0%" in the "Completion Progress" "block" + + Scenario: Submit the quizzes + Given I log in as "student1" And I follow "Course 1" And I follow "Quiz 1A No deadline" And I press "Attempt quiz now" And I follow "Finish attempt ..." And I press "Submit all and finish" - And I follow "Course 1" \ No newline at end of file + And I follow "C1" + Then I should see "Progress: 100%" in the "Completion Progress" "block" \ No newline at end of file