Merge branch 'MDL-41955_25' of git://github.com/dmonllao/moodle into MOODLE_25_STABLE

This commit is contained in:
Damyon Wiese
2013-10-02 08:42:34 +08:00
6 changed files with 111 additions and 22 deletions
+8 -1
View File
@@ -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
*/
@@ -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) {
@@ -94,6 +100,7 @@ abstract class tool_generator_backend {
// Set parameters.
$this->size = $size;
$this->fixeddataset = $fixeddataset;
$this->filesizelimit = $filesizelimit;
$this->progress = $progress;
}
@@ -102,15 +102,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);
}
/**
@@ -364,7 +364,7 @@ class tool_generator_course_backend extends tool_generator_backend {
// 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);
@@ -381,6 +381,7 @@ class tool_generator_course_backend extends tool_generator_backend {
* @return Random data
*/
private static function get_random_binary($length) {
$data = microtime(true);
if (strlen($data) > $length) {
// Use last digits of data.
@@ -401,8 +402,9 @@ class tool_generator_course_backend extends tool_generator_backend {
// 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);
@@ -523,4 +525,20 @@ class tool_generator_course_backend extends tool_generator_backend {
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;
}
}
@@ -63,15 +63,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);
}
/**
@@ -156,6 +157,10 @@ class tool_generator_site_backend extends tool_generator_backend {
'bypasscheck' => 'bypasscheck',
);
if ($this->filesizelimit) {
$options[] = '--filesizelimit="' . $this->filesizelimit . '"';
}
// Getting an options string.
foreach ($optionstoextend as $attribute => $option) {
if (!empty($this->{$attribute})) {
+9 -6
View File
@@ -36,6 +36,7 @@ list($options, $unrecognized) = cli_get_params(
'shortname' => false,
'size' => false,
'fixeddataset' => false,
'filesizelimit' => false,
'bypasscheck' => false,
'quiet' => false
),
@@ -53,11 +54,12 @@ Not for use on live sites; only normally works if debugging is set to DEVELOPER
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
@@ -77,6 +79,7 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
$shortname = $options['shortname'];
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
$filesizelimit = $options['filesizelimit'];
// Check size.
try {
@@ -94,5 +97,5 @@ if ($error = tool_generator_course_backend::check_shortname_available($shortname
session_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();
+8 -5
View File
@@ -35,6 +35,7 @@ list($options, $unrecognized) = cli_get_params(
'help' => false,
'size' => false,
'fixeddataset' => false,
'filesizelimit' => false,
'bypasscheck' => false,
'quiet' => false
),
@@ -58,10 +59,11 @@ Consider that, depending on the size you select, this CLI tool can really genera
$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
@@ -80,6 +82,7 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
// Get options.
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
$filesizelimit = $options['filesizelimit'];
// Check size.
try {
@@ -92,5 +95,5 @@ try {
session_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();
@@ -38,7 +38,7 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
$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.
@@ -120,7 +120,7 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
$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.
@@ -151,4 +151,57 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
}
}
/**
* 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());
}
}
}
}
}