Skip to content

Commit

Permalink
Merge branch 'MDL-41827_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	admin/tool/generator/classes/site_backend.php
  • Loading branch information
stronk7 committed Sep 25, 2013
2 parents 14976b3 + c43ef6c commit 75c7c41
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 22 deletions.
9 changes: 8 additions & 1 deletion admin/tool/generator/classes/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ abstract class tool_generator_backend {
*/
protected $fixeddataset;

/**
* @var int|bool Maximum number of bytes for file.
*/
protected $filesizelimit;

/**
* @var bool True if displaying progress
*/
Expand Down Expand Up @@ -81,10 +86,11 @@ abstract class tool_generator_backend {
*
* @param int $size Size as numeric index
* @param bool $fixeddataset To use fixed or random data
* @param int|bool $filesizelimit The max number of bytes for a generated file
* @param bool $progress True if progress information should be displayed
* @throws coding_exception If parameters are invalid
*/
public function __construct($size, $fixeddataset = false, $progress = true) {
public function __construct($size, $fixeddataset = false, $filesizelimit = false, $progress = true) {

// Check parameter.
if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
Expand All @@ -94,6 +100,7 @@ public function __construct($size, $fixeddataset = false, $progress = true) {
// Set parameters.
$this->size = $size;
$this->fixeddataset = $fixeddataset;
$this->filesizelimit = $filesizelimit;
$this->progress = $progress;
}

Expand Down
30 changes: 24 additions & 6 deletions admin/tool/generator/classes/course_backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ class tool_generator_course_backend extends 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 int|bool $filesizelimit The max number of bytes for a generated file
* @param bool $progress True if progress information should be displayed
* @return int Course id
*/
public function __construct($shortname, $size, $fixeddataset = false, $progress = true) {
public function __construct($shortname, $size, $fixeddataset = false, $filesizelimit = false, $progress = true) {

// Set parameters.
$this->shortname = $shortname;

parent::__construct($size, $fixeddataset, $progress);
parent::__construct($size, $fixeddataset, $filesizelimit, $progress);
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ private function create_small_files() {

// Generate random binary data (different for each file so it
// doesn't compress unrealistically).
$data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
$data = self::get_random_binary($this->limit_filesize(self::$paramsmallfilesize[$this->size]));

$fs->create_file_from_string($filerecord, $data);
$this->dot($i, $count);
Expand All @@ -362,6 +362,7 @@ private function create_small_files() {
* @return Random data
*/
private static function get_random_binary($length) {

$data = microtime(true);
if (strlen($data) > $length) {
// Use last digits of data.
Expand All @@ -382,8 +383,9 @@ private function create_big_files() {

// Work out how many files and how many blocks to use (up to 64KB).
$count = self::$parambigfilecount[$this->size];
$blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
$blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
$filesize = $this->limit_filesize(self::$parambigfilesize[$this->size]);
$blocks = ceil($filesize / 65536);
$blocksize = floor($filesize / $blocks);

$this->log('createbigfiles', $count, true);

Expand Down Expand Up @@ -504,4 +506,20 @@ private function get_target_user() {
return $userid;
}

/**
* Restricts the binary file size if necessary
*
* @param int $length The total length
* @return int The limited length if a limit was specified.
*/
private function limit_filesize($length) {

// Limit to $this->filesizelimit.
if (is_numeric($this->filesizelimit) && $length > $this->filesizelimit) {
$length = floor($this->filesizelimit);
}

return $length;
}

}
9 changes: 7 additions & 2 deletions admin/tool/generator/classes/site_backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ class tool_generator_site_backend extends tool_generator_backend {
* @param int $size Size as numeric index
* @param bool $bypasscheck If debugging level checking was skipped.
* @param bool $fixeddataset To use fixed or random data
* @param int|bool $filesizelimit The max number of bytes for a generated file
* @param bool $progress True if progress information should be displayed
* @return int Course id
*/
public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) {
public function __construct($size, $bypasscheck, $fixeddataset = false, $filesizelimit = false, $progress = true) {

// Set parameters.
$this->bypasscheck = $bypasscheck;

parent::__construct($size, $fixeddataset, $progress);
parent::__construct($size, $fixeddataset, $filesizelimit, $progress);
}

/**
Expand Down Expand Up @@ -148,6 +149,10 @@ protected function run_create_course($shortname, $coursesize) {
$options[] = '--quiet';
}

if ($this->filesizelimit) {
$options[] = '--filesizelimit="' . $this->filesizelimit . '"';
}

// Extend options.
$optionstoextend = array(
'fixeddataset' => 'fixeddataset',
Expand Down
15 changes: 9 additions & 6 deletions admin/tool/generator/cli/maketestcourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'shortname' => false,
'size' => false,
'fixeddataset' => false,
'filesizelimit' => false,
'bypasscheck' => false,
'quiet' => false
),
Expand All @@ -52,11 +53,12 @@
level.
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
--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
--filesizelimit Limits the size of the generated files to the specified bytes
--bypasscheck Bypasses the developer-mode check (be careful!)
--quiet Do not show any output
-h, --help Print out this help
Expand All @@ -76,6 +78,7 @@
$shortname = $options['shortname'];
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
$filesizelimit = $options['filesizelimit'];

// Check size.
try {
Expand All @@ -93,5 +96,5 @@
\core\session\manager::set_user(get_admin());

// Do backend code to generate course.
$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, $filesizelimit, empty($options['quiet']));
$id = $backend->make();
13 changes: 8 additions & 5 deletions admin/tool/generator/cli/maketestsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'help' => false,
'size' => false,
'fixeddataset' => false,
'filesizelimit' => false,
'bypasscheck' => false,
'quiet' => false
),
Expand All @@ -57,10 +58,11 @@
$sitesizes
Options:
--size Size of the generated site, this value affects the number of courses and their size. Accepted values: 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
--size Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)
--fixeddataset Use a fixed data set instead of randomly generated data
--filesizelimit Limits the size of the generated files to the specified bytes
--bypasscheck Bypasses the developer-mode check (be careful!)
--quiet Do not show any output
-h, --help Print out this help
Expand All @@ -79,6 +81,7 @@
// Get options.
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
$filesizelimit = $options['filesizelimit'];

// Check size.
try {
Expand All @@ -91,5 +94,5 @@
\core\session\manager::set_user(get_admin());

// Do backend code to generate site.
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, empty($options['quiet']));
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, $filesizelimit, empty($options['quiet']));
$backend->make();
57 changes: 55 additions & 2 deletions admin/tool/generator/tests/maketestcourse_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_make_xs_course() {
$this->setAdminUser();

// Create the XS course.
$backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
$backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false, false);
$courseid = $backend->make();

// Get course details.
Expand Down Expand Up @@ -118,7 +118,7 @@ public function test_fixed_data_set() {
$this->setAdminUser();

// Create the S course (more sections and activities than XS).
$backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false);
$backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false, false);
$courseid = $backend->make();

// Get course details.
Expand Down Expand Up @@ -151,4 +151,57 @@ public function test_fixed_data_set() {
}

}

/**
* Creates a small test course specifying a maximum size and checks the generated files size is limited.
*/
public function test_filesize_limit() {

$this->resetAfterTest();
$this->setAdminUser();

// Limit.
$filesizelimit = 100;

// Create a limited XS course.
$backend = new tool_generator_course_backend('TOOL_XS_LIMITED', 0, false, $filesizelimit, false);
$courseid = $backend->make();

$course = get_course($courseid);
$modinfo = get_fast_modinfo($course);

// Check there are small files.
$fs = get_file_storage();
$resources = $modinfo->get_instances_of('resource');
foreach ($resources as $resource) {
$resourcecontext = context_module::instance($resource->id);
$files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
foreach ($files as $file) {
if ($file->get_mimetype() == 'application/octet-stream') {
$this->assertLessThanOrEqual($filesizelimit, $file->get_filesize());
}
}
}

// Create a non-limited XS course.
$backend = new tool_generator_course_backend('TOOL_XS_NOLIMITS', 0, false, false, false);
$courseid = $backend->make();

$course = get_course($courseid);
$modinfo = get_fast_modinfo($course);

// Check there are small files.
$fs = get_file_storage();
$resources = $modinfo->get_instances_of('resource');
foreach ($resources as $resource) {
$resourcecontext = context_module::instance($resource->id);
$files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
foreach ($files as $file) {
if ($file->get_mimetype() == 'application/octet-stream') {
$this->assertGreaterThan($filesizelimit, (int)$file->get_filesize());
}
}
}

}
}

0 comments on commit 75c7c41

Please sign in to comment.