Merge branch 'MDL-41827_master' of git://github.com/dmonllao/moodle
Conflicts: admin/tool/generator/classes/site_backend.php
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -345,7 +345,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);
|
||||
@@ -362,6 +362,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.
|
||||
@@ -382,8 +383,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);
|
||||
|
||||
@@ -504,4 +506,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,6 +149,10 @@ class tool_generator_site_backend extends tool_generator_backend {
|
||||
$options[] = '--quiet';
|
||||
}
|
||||
|
||||
if ($this->filesizelimit) {
|
||||
$options[] = '--filesizelimit="' . $this->filesizelimit . '"';
|
||||
}
|
||||
|
||||
// Extend options.
|
||||
$optionstoextend = array(
|
||||
'fixeddataset' => 'fixeddataset',
|
||||
|
||||
@@ -35,6 +35,7 @@ list($options, $unrecognized) = cli_get_params(
|
||||
'shortname' => false,
|
||||
'size' => false,
|
||||
'fixeddataset' => false,
|
||||
'filesizelimit' => false,
|
||||
'bypasscheck' => false,
|
||||
'quiet' => false
|
||||
),
|
||||
@@ -52,11 +53,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
|
||||
|
||||
@@ -76,6 +78,7 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
|
||||
$shortname = $options['shortname'];
|
||||
$sizename = $options['size'];
|
||||
$fixeddataset = $options['fixeddataset'];
|
||||
$filesizelimit = $options['filesizelimit'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
@@ -93,5 +96,5 @@ if ($error = tool_generator_course_backend::check_shortname_available($shortname
|
||||
\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();
|
||||
|
||||
@@ -34,6 +34,7 @@ list($options, $unrecognized) = cli_get_params(
|
||||
'help' => false,
|
||||
'size' => false,
|
||||
'fixeddataset' => false,
|
||||
'filesizelimit' => false,
|
||||
'bypasscheck' => false,
|
||||
'quiet' => false
|
||||
),
|
||||
@@ -57,10 +58,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
|
||||
|
||||
@@ -79,6 +81,7 @@ if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
|
||||
// Get options.
|
||||
$sizename = $options['size'];
|
||||
$fixeddataset = $options['fixeddataset'];
|
||||
$filesizelimit = $options['filesizelimit'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
@@ -91,5 +94,5 @@ try {
|
||||
\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();
|
||||
|
||||
@@ -35,7 +35,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.
|
||||
@@ -118,7 +118,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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user