Skip to content

Commit

Permalink
tagging: check environment config length
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Sep 8, 2024
1 parent 319a9bd commit 16cc4b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion classes/local/tag/environment_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace tool_objectfs\local\tag;

use moodle_exception;

/**
* Provides current environment to file.
*
Expand Down Expand Up @@ -47,7 +49,17 @@ public static function get_description(): string {
*/
private static function get_env(): ?string {
global $CFG;
return !empty($CFG->objectfs_environment_name) ? $CFG->objectfs_environment_name : null;

if (empty($CFG->objectfs_environment_name)) {
return null;
}

// Must never be greater than 128, unlikely, but we must enforce this.
if (strlen($CFG->objectfs_environment_name) > 128) {
throw new moodle_exception('tagsource:environment:toolong', 'tool_objectfs');
}

return $CFG->objectfs_environment_name;
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_objectfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
$string['check:tagging:error'] = 'Error trying to tag object';

$string['tagsource:environment'] = 'Environment defined by $CFG->objectfs_environment_name, currently: "{$a}".';
$string['tagsource:environment:toolong'] = 'The value defined in objectfs_environment_name is too long. It must be < 128 chars';
$string['tagsource:location'] = 'Location of file, either "orphan" or "active".';

$string['task:triggerupdateobjecttags'] = 'Queue adhoc task to update object tags';
Expand Down
15 changes: 15 additions & 0 deletions tests/local/tagging_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
namespace tool_objectfs\local;

use coding_exception;
use moodle_exception;
use Throwable;
use tool_objectfs\local\manager;
use tool_objectfs\local\tag\environment_source;
use tool_objectfs\local\tag\tag_manager;
use tool_objectfs\local\tag\tag_source;
use tool_objectfs\tests\testcase;
Expand Down Expand Up @@ -412,4 +414,17 @@ public function test_get_sync_status_string_does_not_exist() {
$this->expectExceptionMessage('No status string is mapped for status: 5');
tag_manager::get_sync_status_string(5);
}

/**
* Tests the length of the defined tag source is checked correctly
*/
public function test_environment_source_too_long() {
global $CFG;
$CFG->objectfs_environment_name = 'This is a really long string. It needs to be long because it needs to be more than 128 chars for the test to trigger an exception.';
$source = new environment_source();

$this->expectException(moodle_exception::class);
$this->expectExceptionMessage(get_string('tagsource:environment:toolong', 'tool_objectfs'));
$source->get_value_for_contenthash('test');
}
}

0 comments on commit 16cc4b1

Please sign in to comment.