From 219cae03dbed7513b59887eee78b3f5cda1a275d Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 23 Aug 2013 14:32:17 +0800 Subject: [PATCH 1/3] MDL-41399 tool_generator: Adding fixeddataset option --- admin/tool/generator/classes/backend.php | 55 +++++++++++++++---- admin/tool/generator/cli/maketestcourse.php | 5 +- .../generator/tests/maketestcourse_test.php | 2 +- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/admin/tool/generator/classes/backend.php b/admin/tool/generator/classes/backend.php index beed42112e138..813e6429c4368 100644 --- a/admin/tool/generator/classes/backend.php +++ b/admin/tool/generator/classes/backend.php @@ -89,6 +89,11 @@ class tool_generator_backend { */ private $size; + /** + * @var bool True if we want a fixed dataset or false to generate random data + */ + private $fixeddataset; + /** * @var bool True if displaying progress */ @@ -129,11 +134,12 @@ class tool_generator_backend { * * @param string $shortname Course shortname * @param int $size Size as numeric index + * @param bool $fixeddataset To use fixed or random data * @param bool $progress True if progress information should be displayed * @return int Course id * @throws coding_exception If parameters are invalid */ - public function __construct($shortname, $size, $progress = true) { + public function __construct($shortname, $size, $fixeddataset = false, $progress = true) { // Check parameter. if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) { throw new coding_exception('Invalid size'); @@ -142,6 +148,7 @@ public function __construct($shortname, $size, $progress = true) { // Set parameters. $this->shortname = $shortname; $this->size = $size; + $this->fixeddataset = $fixeddataset; $this->progress = $progress; } @@ -317,6 +324,9 @@ private function create_users() { $this->dot($number, $count); } + // Sets the pointer at the beginning to be aware of the users we use. + reset($this->userids); + $this->end_log(); } @@ -360,7 +370,7 @@ private function create_pages() { $this->log('createpages', $number, true); for ($i=0; $i<$number; $i++) { $record = array('course' => $this->course->id); - $options = array('section' => $this->get_random_section()); + $options = array('section' => $this->get_target_section()); $pagegenerator->create_instance($record, $options); $this->dot($i, $number); } @@ -445,7 +455,7 @@ private function create_big_files() { // Create resource. $record = array('course' => $this->course->id, 'name' => get_string('bigfile', 'tool_generator', $i)); - $options = array('section' => $this->get_random_section()); + $options = array('section' => $this->get_target_section()); $resource = $resourcegenerator->create_instance($record, $options); // Write file. @@ -495,13 +505,13 @@ private function create_forum() { $sofar = 0; for ($i=0; $i < $discussions; $i++) { $record = array('forum' => $forum->id, 'course' => $this->course->id, - 'userid' => $this->get_random_user()); + 'userid' => $this->get_target_user()); $discussion = $forumgenerator->create_discussion($record); $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST); $sofar++; for ($j=0; $j < $posts - 1; $j++, $sofar++) { $record = array('discussion' => $discussion->id, - 'userid' => $this->get_random_user(), 'parent' => $parentid); + 'userid' => $this->get_target_user(), 'parent' => $parentid); $forumgenerator->create_post($record); $this->dot($sofar, $totalposts); } @@ -511,21 +521,44 @@ private function create_forum() { } /** - * Gets a random section number. + * Gets a section number. + * + * Depends on $this->fixeddataset. * * @return int A section number from 1 to the number of sections */ - private function get_random_section() { - return rand(1, self::$paramsections[$this->size]); + private function get_target_section() { + + if (!$this->fixeddataset) { + $key = rand(1, self::$paramsections[$this->size]); + } else { + // Using section 1. + $key = 1; + } + + return $key; } /** - * Gets a random user id. + * Gets a user id. + * + * Depends on $this->fixeddataset. * * @return int A user id for a random created user */ - private function get_random_user() { - return $this->userids[rand(1, self::$paramusers[$this->size])]; + private function get_target_user() { + + if (!$this->fixeddataset) { + $userid = $this->userids[rand(1, self::$paramusers[$this->size])]; + } else if ($userid = current($this->userids)) { + // Moving pointer to the next user. + next($this->userids); + } else { + // Returning to the beginning if we reached the end. + $userid = reset($this->userids); + } + + return $userid; } /** diff --git a/admin/tool/generator/cli/maketestcourse.php b/admin/tool/generator/cli/maketestcourse.php index b646e2f94afc6..a0a4df8573f1d 100644 --- a/admin/tool/generator/cli/maketestcourse.php +++ b/admin/tool/generator/cli/maketestcourse.php @@ -34,6 +34,7 @@ 'help' => false, 'shortname' => false, 'size' => false, + 'fixeddataset' => false, 'bypasscheck' => false, 'quiet' => false ), @@ -53,6 +54,7 @@ Options: --shortname Shortname of course to create (required) --size Size of course to create XS, S, M, L, XL, or XXL (required) +--fixeddataset Use a fixed data set instead of randomly generated data --bypasscheck Bypasses the developer-mode check (be careful!) --quiet Do not show any output @@ -73,6 +75,7 @@ // Get options. $shortname = $options['shortname']; $sizename = $options['size']; +$fixeddataset = $options['fixeddataset']; // Check size. try { @@ -90,5 +93,5 @@ session_set_user(get_admin()); // Do backend code to generate course. -$backend = new tool_generator_backend($shortname, $size, empty($options['quiet'])); +$backend = new tool_generator_backend($shortname, $size, $fixeddataset, empty($options['quiet'])); $id = $backend->make(); diff --git a/admin/tool/generator/tests/maketestcourse_test.php b/admin/tool/generator/tests/maketestcourse_test.php index 3b2244d284b91..8b9a2607e8d5c 100644 --- a/admin/tool/generator/tests/maketestcourse_test.php +++ b/admin/tool/generator/tests/maketestcourse_test.php @@ -35,7 +35,7 @@ public function test_make_xs_course() { $this->setAdminUser(); // Create the XS course. - $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false); + $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false); $courseid = $backend->make(); // Get course details. From d9ff6296b76df09fba8ea64b8c36a47c4430c02b Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 26 Aug 2013 17:29:16 +0800 Subject: [PATCH 2/3] MDL-41399 tool_generator: Unit test for fixeddataset --- .../generator/tests/maketestcourse_test.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/admin/tool/generator/tests/maketestcourse_test.php b/admin/tool/generator/tests/maketestcourse_test.php index 8b9a2607e8d5c..4aa29a911cd29 100644 --- a/admin/tool/generator/tests/maketestcourse_test.php +++ b/admin/tool/generator/tests/maketestcourse_test.php @@ -107,4 +107,77 @@ public function test_make_xs_course() { fd.forum = ?", array($forum->instance)); $this->assertEquals(2, $posts); } + + /** + * Creates an small test course with fixed data set and checks the used sections and users. + */ + public function test_fixed_data_set() { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create the S course (more sections and activities than XS). + $backend = new tool_generator_backend('TOOL_S_COURSE_1', 1, true, false); + $courseid = $backend->make(); + + // Get course details. + $course = get_course($courseid); + $modinfo = get_fast_modinfo($course); + + // Check module instances belongs to section 1. + $instances = $modinfo->get_instances_of('page'); + $npageinstances = count($instances); + foreach ($instances as $instance) { + $this->assertEquals(1, $instance->sectionnum); + } + + // Users that started discussions are the same. + $forums = $modinfo->get_instances_of('forum'); + $nforuminstances = count($forums); + $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC'); + $lastusernumber = 0; + $discussionstarters = array(); + foreach ($discussions as $discussion) { + $usernumber = intval($discussion->lastname); + + // Checks that the users are odd numbers. + $this->assertEquals(1, $usernumber % 2); + + // Checks that the users follows an increasing order. + $this->assertGreaterThan($lastusernumber, $usernumber); + $lastusernumber = $usernumber; + $discussionstarters[$discussion->userid] = $discussion->subject; + } + + // Resetting data generators internal counters. Courses are created in individual + // processes so the counters are restarted before each course creation. + $this->resetAllData(); + $this->setAdminUser(); + + // Create another S course to check that the same data was generated. + $backend = new tool_generator_backend('TOOL_S_COURSE_2', 1, true, false); + $courseid = $backend->make(); + $course = get_course($courseid); + $modinfo = get_fast_modinfo($course); + + // Check it generates the same number of page activities. + $this->assertEquals(count($modinfo->get_instances_of('page')), $npageinstances); + + // Check it generates the same number of forum activities. + $forums = $modinfo->get_instances_of('forum'); + $this->assertEquals(count($forums), $nforuminstances); + + // Check same discussions have been started by the same users. + $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC'); + reset($discussionstarters); + foreach ($discussions as $discussion) { + $previousdiscussionsubject = current($discussionstarters); + $previoususer = key($discussionstarters); + $this->assertEquals($discussion->userid, $previoususer); + $this->assertEquals($discussion->subject, $previousdiscussionsubject); + next($discussionstarters); + } + + } } From e25be3b1dce8b939af8ba5ff4fb08a62f036772c Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 2 Sep 2013 15:55:18 +0800 Subject: [PATCH 3/3] MDL-41399 tool_generator: Removing unit test Removing dodgy database reset in the middle of a test as sequences reset is not allowed. --- .../generator/tests/maketestcourse_test.php | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/admin/tool/generator/tests/maketestcourse_test.php b/admin/tool/generator/tests/maketestcourse_test.php index 4aa29a911cd29..966b0d8e7a021 100644 --- a/admin/tool/generator/tests/maketestcourse_test.php +++ b/admin/tool/generator/tests/maketestcourse_test.php @@ -150,34 +150,5 @@ public function test_fixed_data_set() { $discussionstarters[$discussion->userid] = $discussion->subject; } - // Resetting data generators internal counters. Courses are created in individual - // processes so the counters are restarted before each course creation. - $this->resetAllData(); - $this->setAdminUser(); - - // Create another S course to check that the same data was generated. - $backend = new tool_generator_backend('TOOL_S_COURSE_2', 1, true, false); - $courseid = $backend->make(); - $course = get_course($courseid); - $modinfo = get_fast_modinfo($course); - - // Check it generates the same number of page activities. - $this->assertEquals(count($modinfo->get_instances_of('page')), $npageinstances); - - // Check it generates the same number of forum activities. - $forums = $modinfo->get_instances_of('forum'); - $this->assertEquals(count($forums), $nforuminstances); - - // Check same discussions have been started by the same users. - $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC'); - reset($discussionstarters); - foreach ($discussions as $discussion) { - $previousdiscussionsubject = current($discussionstarters); - $previoususer = key($discussionstarters); - $this->assertEquals($discussion->userid, $previoususer); - $this->assertEquals($discussion->subject, $previousdiscussionsubject); - next($discussionstarters); - } - } }